/
SQLiteConnection.cs
347 lines (298 loc) · 9.59 KB
/
SQLiteConnection.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading;
using System.Diagnostics.CodeAnalysis;
using SQLibre.Core;
using static SQLibre.Core.Raw;
using static SQLibre.Core.Raw.NativeMethods;
using static SQLibre.SQLiteException;
using System.Runtime.Loader;
using System.Windows.Input;
using System.Data;
namespace SQLibre
{
/// <summary>
/// ToDo:
/// 1. Command.Reader property
/// 2. Test Dispose() for all common objects in any variant using them
/// 3. Reader.NextResult call sqlite3.enable_sqlite3_next_stmt(true)
/// 4. Import common featuries from SQLitePCL.Ugly
/// </summary>
public sealed class SQLiteConnection : IDbConnection
{
internal const string main_database_name = Core.DbOpenOptions.MainDatabaseName;
internal const string memorydb_connection_string = $"DataSource={DbOpenOptions.MemoryDb}";
private static readonly object _lock = new object();
private IntPtr _handle;
private SQLiteConnectionOptions _connectionOptions;
private readonly List<WeakReference<SQLiteCommand>> _commands = new();
public string DateTimeSqliteDefaultFormat => _connectionOptions.DateTimeStringFormat;
public bool StoreDateTimeAsTicks => _connectionOptions.StoreDateTimeAsTicks;
public bool StoreTimeSpanAsTicks => _connectionOptions.StoreTimeSpanAsTicks;
public System.Globalization.DateTimeStyles DateTimeStyle => _connectionOptions.DateTimeStyle;
public int HashCode => _connectionOptions.GetHashCode();
public string DatabasePath => _connectionOptions.DatabasePath;
public DbHandle Handle => _handle;
public ConnectionState State { get; internal set; } = ConnectionState.Closed;
public bool UsingAutoCommit { get => _connectionOptions.UsingAutoCommit; set => _connectionOptions.UsingAutoCommit = value; }
internal SQLiteConnectionOptions ConnectionOptions => _connectionOptions;
internal SQLiteTransaction? Transaction { get; set; }
private string _connsectionString;
public string ConnectionString
{
get => _connsectionString;
#pragma warning disable CS8767 // Nullability of reference types in type of parameter doesn't match implicitly implemented member (possibly because of nullability attributes).
set
#pragma warning restore CS8767 // Nullability of reference types in type of parameter doesn't match implicitly implemented member (possibly because of nullability attributes).
{
if (State != ConnectionState.Closed)
throw new InvalidOperationException("Connection already opened");
_connsectionString = value ?? DbOpenOptions.MemoryDb;
_connectionOptions = new SQLiteConnectionOptions(_connsectionString);
}
}
public int ConnectionTimeout => _connectionOptions.CommandTimeout;
public string Database => Path.GetFileNameWithoutExtension(_connectionOptions.DatabasePath);
public static void Config(int configOptions, int value)
=> CheckOK(sqlite3_config(configOptions, value));
public static void Config(int configOptions)
=> CheckOK(sqlite3_config(configOptions));
public SQLiteConnection()
{
}
public SQLiteConnection(string connectionString) : this(new SQLiteConnectionOptions(connectionString))
{
_connsectionString = connectionString;
}
public SQLiteConnection(SQLiteConnectionOptions connectionOptions)
{
if (string.IsNullOrEmpty(_connsectionString))
_connsectionString = connectionOptions.DatabasePath;
_connectionOptions = connectionOptions;
Open();
}
public void EnableWriteAhead() => Execute("PRAGMA journal_mode=WAL"u8);
public IDbTransaction BeginTransaction()
=>BeginTransaction(IsolationLevel.Serializable);
public IDbTransaction BeginTransaction(IsolationLevel isolationLevel)
{
CheckOpenState(nameof(BeginTransaction));
if (Transaction != null)
throw new InvalidOperationException("transaction already active");
Transaction = new SQLiteTransaction(this, isolationLevel);
return Transaction;
}
public void Commit()
{
CheckOpenState(nameof(Commit));
if (Transaction == null)
throw new InvalidOperationException("transaction does not exist");
Transaction.Commit();
}
public void Rollback()
{
CheckOpenState(nameof(Rollback));
if (Transaction == null)
throw new InvalidOperationException("transaction does not exist");
Transaction.Commit();
}
public static void DropDb(SQLiteConnectionOptions options)
{
if (File.Exists(options.DatabasePath))
{
using (var cn = new SQLiteConnection(options))
SQLiteConnectionPool.Remove(cn.Handle, true);
File.Delete(options.DatabasePath);
}
}
public static unsafe void CreateDb(SQLiteConnectionOptions options, SQLiteEncoding encoding = SQLiteEncoding.Utf8, SQLiteJournalMode journalMode = SQLiteJournalMode.WAL)
{
if (File.Exists(options.DatabasePath))
return;
IntPtr db = IntPtr.Zero;
try
{
var rc = NativeMethods.sqlite3_open_v2((Utf8z)options.DatabasePath, out db, (int)options.OpenFlags, (Utf8z)options.VfsName);
SQLiteException.CheckOK(db, rc);
ExecuteInternal(db, (Utf8z)$"PRAGMA journal_mode={journalMode};");
ExecuteInternal(db, (Utf8z)$"PRAGMA encoding={encoding};");
}
catch
{
throw;
}
finally
{
if (db != IntPtr.Zero)
{
var rc = sqlite3_close_v2(db);
SQLiteException.CheckOK(db, rc);
}
}
}
public int Execute(string commandText)
{
CheckOpenState(nameof(Execute));
return ExecuteInternal(_handle, (Utf8z)commandText);
}
public int Execute(ReadOnlySpan<byte> commandText)
{
CheckOpenState(nameof(Execute));
return ExecuteInternal(_handle, commandText);
}
internal static unsafe int ExecuteInternal(DbHandle db, ReadOnlySpan<byte> statement)
{
var rc = sqlite3_exec(db, (Utf8z)statement, IntPtr.Zero, IntPtr.Zero, out var p_errMsg);
if (p_errMsg != IntPtr.Zero)
{
var error = new Utf8z(p_errMsg).ToString();
sqlite3_free(p_errMsg);
throw new SQLiteException(rc, error);
}
return rc;
}
public int Execute(string commandText, params object[] parameters)
{
CheckOpenState(nameof(Execute));
using (SQLiteCommand cmd = CreateCommand(commandText))
{
int i = 0;
string pName = string.Empty;
foreach (var p in parameters)
{
if (i % 2 == 0)
pName = (string)(p ?? throw new ArgumentNullException(nameof(parameters)));
else
cmd.Bind(pName, p);
i++;
}
return cmd.Execute();
}
}
public T? ExecuteScalar<T>(string commandText, params object[] parameters)
{
CheckOpenState(nameof(ExecuteScalar));
using (SQLiteCommand cmd = CreateCommand(commandText))
{
int i = 0;
string pName = string.Empty;
foreach (var p in parameters)
{
if (i % 2 == 0)
pName = (string)(p ?? throw new ArgumentNullException(nameof(parameters)));
else
cmd.Bind(pName, p);
i++;
}
using (var r = cmd.ExecuteReader())
{
if (r.Read())
return (T?)r.GetValue(0);
return default;
}
}
}
public SQLiteReader ExecuteReader(string commandText, params object[] parameters)
{
CheckOpenState(nameof(ExecuteReader));
SQLiteCommand cmd = CreateCommand(commandText);
int i = 0;
string pName = string.Empty;
foreach (var p in parameters)
{
if (i % 2 == 0)
pName = (string)(p ?? throw new ArgumentNullException(nameof(parameters)));
else
cmd.Bind(pName, p);
i++;
}
return cmd.ExecuteReader();
}
public SQLiteCommand CreateCommand(string statement) => CreateCommand((ReadOnlySpan<byte>)(Utf8z)statement);
public SQLiteCommand CreateCommand(ReadOnlySpan<byte> statement)
{
var cmd = new SQLiteCommand(this, statement);
AddCommand(cmd);
return cmd;
}
public long LastInsertRowId()
=> sqlite3_last_insert_rowid(Handle);
public void Close() => Dispose();
public void Dispose()
{
Transaction?.Dispose();
ClearCommandsCollection();
State = ConnectionState.Closed;
SQLiteConnectionPool.Remove(Handle, false);
GC.SuppressFinalize(this);
}
private void ClearCommandsCollection()
{
Monitor.Enter(_lock);
try
{
for (var i = _commands.Count - 1; i >= 0; i--)
{
var reference = _commands[i];
if (reference.TryGetTarget(out var command))
command.Dispose();
else
_commands.RemoveAt(i);
}
}
catch { throw; }
finally { Monitor.Exit(_lock); }
}
internal void AddCommand(SQLiteCommand command)
{
Monitor.Enter(_lock);
try
{
_commands.Add(new WeakReference<SQLiteCommand>(command));
}
catch { throw; }
finally { Monitor.Exit(_lock); }
}
internal void RemoveCommand(SQLiteCommand command)
{
Monitor.Enter(_lock);
try
{
for (var i = _commands.Count - 1; i >= 0; i--)
{
if (_commands[i].TryGetTarget(out var item)
&& item == command)
{
_commands.RemoveAt(i);
}
}
}
catch { throw; }
finally { Monitor.Exit(_lock); }
}
public void ChangeDatabase(string databaseName) => throw new NotSupportedException();
public void Open()
{
if (State == ConnectionState.Open)
return;
if (State != ConnectionState.Closed)
throw new InvalidOperationException("Connection already opened");
_handle = SQLiteConnectionPool.GetConnection(_connectionOptions.OpenOptions, 100000,
db => ExecuteInternal(db, (Utf8z)$"PRAGMA journal_mode={_connectionOptions.JournalMode}"));
State = ConnectionState.Open;
}
private void CheckOpenState(string source)
{
if (State != ConnectionState.Open)
throw new InvalidOperationException($"Method {source} required opened connection");
}
IDbCommand IDbConnection.CreateCommand()
{
throw new NotImplementedException();
}
}
}