forked from vrcx-team/VRCX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSQLite.cs
151 lines (139 loc) · 4.6 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using CefSharp;
using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.IO;
using System.Threading;
namespace VRCX
{
public class SQLite
{
public static readonly SQLite Instance;
private readonly ReaderWriterLockSlim m_ConnectionLock;
private readonly SQLiteConnection m_Connection;
static SQLite()
{
Instance = new SQLite();
}
public SQLite()
{
m_ConnectionLock = new ReaderWriterLockSlim();
var dataSource = Path.Combine(Program.BaseDirectory, "VRCX.sqlite3");
m_Connection = new SQLiteConnection($"Data Source=\"{dataSource}\";Version=3;PRAGMA locking_mode=NORMAL;PRAGMA busy_timeout=5000", true);
}
internal void Init()
{
m_Connection.Open();
}
internal void Exit()
{
m_Connection.Close();
m_Connection.Dispose();
}
public void Execute(IJavascriptCallback callback, string sql, IDictionary<string, object> args = null)
{
try
{
m_ConnectionLock.EnterReadLock();
try
{
using (var command = new SQLiteCommand(sql, m_Connection))
{
if (args != null)
{
foreach (var arg in args)
{
command.Parameters.Add(new SQLiteParameter(arg.Key, arg.Value));
}
}
using (var reader = command.ExecuteReader())
{
while (reader.Read() == true)
{
var values = new object[reader.FieldCount];
reader.GetValues(values);
if (callback.CanExecute == true)
{
callback.ExecuteAsync(null, values);
}
}
}
}
if (callback.CanExecute == true)
{
callback.ExecuteAsync(null, null);
}
}
finally
{
m_ConnectionLock.ExitReadLock();
}
}
catch (Exception e)
{
if (callback.CanExecute == true)
{
callback.ExecuteAsync(e.Message, null);
}
}
callback.Dispose();
}
public void Execute(Action<object[]> callback, string sql, IDictionary<string, object> args = null)
{
m_ConnectionLock.EnterReadLock();
try
{
using (var command = new SQLiteCommand(sql, m_Connection))
{
if (args != null)
{
foreach (var arg in args)
{
command.Parameters.Add(new SQLiteParameter(arg.Key, arg.Value));
}
}
using (var reader = command.ExecuteReader())
{
while (reader.Read() == true)
{
var values = new object[reader.FieldCount];
reader.GetValues(values);
callback(values);
}
}
}
}
catch
{
}
finally
{
m_ConnectionLock.ExitReadLock();
}
}
public int ExecuteNonQuery(string sql, IDictionary<string, object> args = null)
{
int result = -1;
m_ConnectionLock.EnterWriteLock();
try
{
using (var command = new SQLiteCommand(sql, m_Connection))
{
if (args != null)
{
foreach (var arg in args)
{
command.Parameters.Add(new SQLiteParameter(arg.Key, arg.Value));
}
}
result = command.ExecuteNonQuery();
}
}
finally
{
m_ConnectionLock.ExitWriteLock();
}
return result;
}
}
}