-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathSQLite.cs
107 lines (86 loc) · 2.82 KB
/
SQLite.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Mono.Data.Sqlite;
using System.Diagnostics;
namespace OpenSMO
{
public class Sql
{
public static string Filename;
public static int Version;
public static bool Compress;
public static bool UseCommit;
public static bool ReportErrors = true;
private static SqliteConnection conn;
private static SqliteCommand cmd;
public static void Connect()
{
UseCommit = bool.Parse(MainClass.Instance.ServerConfig.Get("Database_UseCommit"));
conn = new SqliteConnection("Data Source=" + Filename + ";Version=" + Version.ToString() + ";New=False;Compress=" + Compress.ToString() + ";Journal Mode=Off;UTF8Encoding=True;");
try { conn.Open(); } catch (Exception ex) {
MainClass.AddLog("Couldn't open SQLite database: " + ex.Message, true);
}
if (UseCommit)
Query("BEGIN TRANSACTION");
}
public static bool Connected
{
get { return conn != null; }
}
public static string AddSlashes(string str)
{
return str.Replace("'", "''");
}
public static void Close()
{
conn.Close();
}
private static int commitTimer;
public static void Update()
{
if (UseCommit) {
if (++commitTimer >= MainClass.Instance.FPS * int.Parse(MainClass.Instance.ServerConfig.Get("Database_CommitTime"))) {
commitTimer = 0;
Query("COMMIT TRANSACTION");
Query("BEGIN TRANSACTION");
}
}
}
public static Hashtable[] Query(string qry)
{
Stopwatch sw = new Stopwatch();
sw.Start();
SqliteDataReader reader = null;
cmd = conn.CreateCommand();
cmd.CommandText = qry;
try { reader = cmd.ExecuteReader(); } catch (Exception ex) {
if (ReportErrors) {
MainClass.AddLog("Query error: '" + ex.Message + "'", true);
MainClass.AddLog("Query was: '" + qry + "'", true);
}
return null;
}
List<Hashtable> ret = new List<Hashtable>();
while (reader.Read()) {
Hashtable row = new Hashtable();
for (int i = 0; i < reader.FieldCount; i++) {
if (reader[i].GetType() == typeof(Int64)) {
if ((long)reader[i] > int.MaxValue)
row[reader.GetName(i)] = (long)reader[i];
else
row[reader.GetName(i)] = (int)(long)reader[i];
} else
row[reader.GetName(i)] = reader[i];
}
ret.Add(row);
}
sw.Stop();
if (sw.ElapsedMilliseconds >= 1000)
MainClass.AddLog("SQL Query took very long: " + sw.ElapsedMilliseconds + "ms", true);
return ret.ToArray();
}
}
}