-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathSqliteConnection.cs
940 lines (813 loc) · 37.6 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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using Microsoft.Data.Sqlite.Properties;
using SQLitePCL;
using static SQLitePCL.raw;
namespace Microsoft.Data.Sqlite
{
/// <summary>
/// Represents a connection to a SQLite database.
/// </summary>
/// <seealso href="https://docs.microsoft.com/dotnet/standard/data/sqlite/connection-strings">Connection Strings</seealso>
/// <seealso href="https://docs.microsoft.com/dotnet/standard/data/sqlite/async">Async Limitations</seealso>
public partial class SqliteConnection : DbConnection
{
internal const string MainDatabaseName = "main";
private const int SQLITE_WIN32_DATA_DIRECTORY_TYPE = 1;
private const int SQLITE_WIN32_TEMP_DIRECTORY_TYPE = 2;
private readonly List<WeakReference<SqliteCommand>> _commands = new();
private Dictionary<string, (object? state, strdelegate_collation? collation)>? _collations;
private Dictionary<(string name, int arity), (int flags, object? state, delegate_function_scalar? func)>? _functions;
private Dictionary<(string name, int arity), (int flags, object? state, delegate_function_aggregate_step? func_step,
delegate_function_aggregate_final? func_final)>? _aggregates;
private HashSet<(string file, string? proc)>? _extensions;
private string _connectionString;
private ConnectionState _state;
private SqliteConnectionInternal? _innerConnection;
private bool _extensionsEnabled;
private int? _defaultTimeout;
private static readonly StateChangeEventArgs _fromClosedToOpenEventArgs = new StateChangeEventArgs(ConnectionState.Closed, ConnectionState.Open);
private static readonly StateChangeEventArgs _fromOpenToClosedEventArgs = new StateChangeEventArgs(ConnectionState.Open, ConnectionState.Closed);
static SqliteConnection()
{
Type.GetType("SQLitePCL.Batteries_V2, SQLitePCLRaw.batteries_v2")
?.GetRuntimeMethod("Init", Type.EmptyTypes)
?.Invoke(null, null);
try
{
var currentAppData = Type.GetType("Windows.Storage.ApplicationData, Windows, ContentType=WindowsRuntime")
?? Type.GetType("Windows.Storage.ApplicationData, Microsoft.Windows.SDK.NET")
?.GetRuntimeProperty("Current")?.GetValue(null);
var localFolder = currentAppData?.GetType()
.GetRuntimeProperty("LocalFolder")?.GetValue(currentAppData);
var localFolderPath = (string?)localFolder?.GetType()
.GetRuntimeProperty("Path")?.GetValue(localFolder);
if (localFolderPath != null)
{
var rc = sqlite3_win32_set_directory(SQLITE_WIN32_DATA_DIRECTORY_TYPE, localFolderPath);
Debug.Assert(rc == SQLITE_OK);
}
var tempFolder = currentAppData?.GetType()
.GetRuntimeProperty("TemporaryFolder")?.GetValue(currentAppData);
var tempFolderPath = (string?)tempFolder?.GetType()
.GetRuntimeProperty("Path")?.GetValue(tempFolder);
if (tempFolderPath != null)
{
var rc = sqlite3_win32_set_directory(SQLITE_WIN32_TEMP_DIRECTORY_TYPE, tempFolderPath);
Debug.Assert(rc == SQLITE_OK);
}
}
catch
{
// Ignore "The process has no package identity."
}
}
/// <summary>
/// Initializes a new instance of the <see cref="SqliteConnection" /> class.
/// </summary>
public SqliteConnection()
: this(null)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="SqliteConnection" /> class.
/// </summary>
/// <param name="connectionString">The string used to open the connection.</param>
/// <seealso href="https://docs.microsoft.com/dotnet/standard/data/sqlite/connection-strings">Connection Strings</seealso>
/// <seealso cref="SqliteConnectionStringBuilder" />
public SqliteConnection(string? connectionString)
=> ConnectionString = connectionString;
/// <summary>
/// Gets a handle to underlying database connection.
/// </summary>
/// <value>A handle to underlying database connection.</value>
/// <seealso href="https://docs.microsoft.com/dotnet/standard/data/sqlite/interop">Interoperability</seealso>
public virtual sqlite3? Handle
=> _innerConnection?.Handle;
/// <summary>
/// Gets or sets a string used to open the connection.
/// </summary>
/// <value>A string used to open the connection.</value>
/// <seealso href="https://docs.microsoft.com/dotnet/standard/data/sqlite/connection-strings">Connection Strings</seealso>
/// <seealso cref="SqliteConnectionStringBuilder" />
[AllowNull]
public override string ConnectionString
{
get => _connectionString;
[MemberNotNull(nameof(_connectionString), nameof(PoolGroup))]
set
{
if (State != ConnectionState.Closed)
{
throw new InvalidOperationException(Resources.ConnectionStringRequiresClosedConnection);
}
_connectionString = value ?? string.Empty;
PoolGroup = SqliteConnectionFactory.Instance.GetPoolGroup(_connectionString);
}
}
internal SqliteConnectionPoolGroup PoolGroup { get; set; }
internal SqliteConnectionStringBuilder ConnectionOptions
=> PoolGroup.ConnectionOptions;
/// <summary>
/// Gets the name of the current database. Always 'main'.
/// </summary>
/// <value>The name of the current database.</value>
public override string Database
=> MainDatabaseName;
/// <summary>
/// Gets the path to the database file. Will be absolute for open connections.
/// </summary>
/// <value>The path to the database file.</value>
public override string DataSource
{
get
{
string? dataSource = null;
if (State == ConnectionState.Open)
{
dataSource = sqlite3_db_filename(Handle, MainDatabaseName).utf8_to_string();
}
return dataSource ?? ConnectionOptions.DataSource;
}
}
/// <summary>
/// Gets or sets the default <see cref="SqliteCommand.CommandTimeout" /> value for commands created using
/// this connection. This is also used for internal commands in methods like
/// <see cref="BeginTransaction()" />.
/// </summary>
/// <value>The default <see cref="SqliteCommand.CommandTimeout" /> value.</value>
/// <seealso href="https://docs.microsoft.com/dotnet/standard/data/sqlite/database-errors">Database Errors</seealso>
public virtual int DefaultTimeout
{
get => _defaultTimeout ?? ConnectionOptions.DefaultTimeout;
set => _defaultTimeout = value;
}
/// <summary>
/// Gets the version of SQLite used by the connection.
/// </summary>
/// <value>The version of SQLite used by the connection.</value>
public override string ServerVersion
=> sqlite3_libversion().utf8_to_string();
/// <summary>
/// Gets the current state of the connection.
/// </summary>
/// <value>The current state of the connection.</value>
public override ConnectionState State
=> _state;
/// <summary>
/// Gets the <see cref="DbProviderFactory" /> for this connection.
/// </summary>
/// <value>The <see cref="DbProviderFactory" />.</value>
protected override DbProviderFactory DbProviderFactory
=> SqliteFactory.Instance;
/// <summary>
/// Gets or sets the transaction currently being used by the connection, or null if none.
/// </summary>
/// <value>The transaction currently being used by the connection.</value>
protected internal virtual SqliteTransaction? Transaction { get; set; }
/// <summary>
/// Empties the connection pool.
/// </summary>
/// <remarks>Any open connections will not be returned the the pool when closed.</remarks>
public static void ClearAllPools()
=> SqliteConnectionFactory.Instance.ClearPools();
/// <summary>
/// Empties the connection pool associated with the connection.
/// </summary>
/// <param name="connection">The connection.</param>
/// <remarks>Any open connections will not be returned the the pool when closed.</remarks>
public static void ClearPool(SqliteConnection connection)
=> connection.PoolGroup.Clear();
/// <summary>
/// Opens a connection to the database using the value of <see cref="ConnectionString" />. If
/// <c>Mode=ReadWriteCreate</c> is used (the default) the file is created, if it doesn't already exist.
/// </summary>
/// <exception cref="SqliteException">A SQLite error occurs while opening the connection.</exception>
public override void Open()
{
if (State == ConnectionState.Open)
{
return;
}
_innerConnection = SqliteConnectionFactory.Instance.GetConnection(this);
int rc;
_state = ConnectionState.Open;
try
{
if (ConnectionOptions.ForeignKeys.HasValue)
{
this.ExecuteNonQuery(
"PRAGMA foreign_keys = " + (ConnectionOptions.ForeignKeys.Value ? "1" : "0") + ";");
}
if (ConnectionOptions.RecursiveTriggers)
{
this.ExecuteNonQuery("PRAGMA recursive_triggers = 1;");
}
if (_collations != null)
{
foreach (var item in _collations)
{
rc = sqlite3_create_collation(Handle, item.Key, item.Value.state, item.Value.collation);
SqliteException.ThrowExceptionForRC(rc, Handle);
}
}
if (_functions != null)
{
foreach (var item in _functions)
{
rc = sqlite3_create_function(Handle, item.Key.name, item.Key.arity, item.Value.state, item.Value.func);
SqliteException.ThrowExceptionForRC(rc, Handle);
}
}
if (_aggregates != null)
{
foreach (var item in _aggregates)
{
rc = sqlite3_create_function(
Handle, item.Key.name, item.Key.arity, item.Value.state, item.Value.func_step, item.Value.func_final);
SqliteException.ThrowExceptionForRC(rc, Handle);
}
}
if (_extensions != null
&& _extensions.Count != 0)
{
rc = sqlite3_db_config(Handle, SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION, 1, out _);
SqliteException.ThrowExceptionForRC(rc, Handle);
foreach (var item in _extensions)
{
LoadExtensionCore(item.file, item.proc);
}
}
if (_extensionsEnabled)
{
rc = sqlite3_enable_load_extension(Handle, _extensionsEnabled ? 1 : 0);
SqliteException.ThrowExceptionForRC(rc, Handle);
}
}
catch
{
_innerConnection.Close();
_innerConnection = null;
_state = ConnectionState.Closed;
throw;
}
OnStateChange(_fromClosedToOpenEventArgs);
}
/// <summary>
/// Closes the connection to the database. Open transactions are rolled back.
/// </summary>
public override void Close()
{
if (State != ConnectionState.Open)
{
return;
}
Transaction?.Dispose();
for (var i = _commands.Count - 1; i >= 0; i--)
{
var reference = _commands[i];
if (reference.TryGetTarget(out var command))
{
// NB: Calls RemoveCommand()
command.Dispose();
}
else
{
_commands.RemoveAt(i);
}
}
Debug.Assert(_commands.Count == 0);
_innerConnection!.Close();
_innerConnection = null;
_state = ConnectionState.Closed;
OnStateChange(_fromOpenToClosedEventArgs);
}
internal void Deactivate()
{
int rc;
if (_collations != null)
{
foreach (var item in _collations.Keys)
{
rc = sqlite3_create_collation(Handle, item, null, null);
SqliteException.ThrowExceptionForRC(rc, Handle);
}
}
if (_functions != null)
{
foreach (var (name, arity) in _functions.Keys)
{
rc = sqlite3_create_function(Handle, name, arity, null, null);
SqliteException.ThrowExceptionForRC(rc, Handle);
}
}
if (_aggregates != null)
{
foreach (var (name, arity) in _aggregates.Keys)
{
rc = sqlite3_create_function(
Handle, name, arity, null, null, null);
SqliteException.ThrowExceptionForRC(rc, Handle);
}
}
// TODO: Unload extensions (currently not supported by SQLite)
if (_extensionsEnabled)
{
rc = sqlite3_enable_load_extension(Handle, 0);
SqliteException.ThrowExceptionForRC(rc, Handle);
}
}
/// <summary>
/// Releases any resources used by the connection and closes it.
/// </summary>
/// <param name="disposing">
/// <see langword="true" /> to release managed and unmanaged resources;
/// <see langword="false" /> to release only unmanaged resources.
/// </param>
protected override void Dispose(bool disposing)
{
if (disposing)
{
Close();
}
base.Dispose(disposing);
}
/// <summary>
/// Creates a new command associated with the connection.
/// </summary>
/// <returns>The new command.</returns>
/// <remarks>
/// The command's <see cref="SqliteCommand.Transaction" /> property will also be set to the current
/// transaction.
/// </remarks>
public new virtual SqliteCommand CreateCommand()
=> new()
{
Connection = this,
CommandTimeout = DefaultTimeout,
Transaction = Transaction
};
/// <summary>
/// Creates a new command associated with the connection.
/// </summary>
/// <returns>The new command.</returns>
protected override DbCommand CreateDbCommand()
=> CreateCommand();
internal void AddCommand(SqliteCommand command)
=> _commands.Add(new WeakReference<SqliteCommand>(command));
internal void RemoveCommand(SqliteCommand command)
{
for (var i = _commands.Count - 1; i >= 0; i--)
{
if (_commands[i].TryGetTarget(out var item)
&& item == command)
{
_commands.RemoveAt(i);
}
}
}
/// <summary>
/// Create custom collation.
/// </summary>
/// <param name="name">Name of the collation.</param>
/// <param name="comparison">Method that compares two strings.</param>
/// <seealso href="https://docs.microsoft.com/dotnet/standard/data/sqlite/collation">Collation</seealso>
public virtual void CreateCollation(string name, Comparison<string>? comparison)
=> CreateCollation(
name, null, comparison != null ? (_, s1, s2) => comparison(s1, s2) : (Func<object?, string, string, int>?)null);
/// <summary>
/// Create custom collation.
/// </summary>
/// <typeparam name="T">The type of the state object.</typeparam>
/// <param name="name">Name of the collation.</param>
/// <param name="state">State object passed to each invocation of the collation.</param>
/// <param name="comparison">Method that compares two strings, using additional state.</param>
/// <seealso href="https://docs.microsoft.com/dotnet/standard/data/sqlite/collation">Collation</seealso>
public virtual void CreateCollation<T>(string name, T state, Func<T, string, string, int>? comparison)
{
if (string.IsNullOrEmpty(name))
{
throw new ArgumentNullException(nameof(name));
}
var collation = comparison != null ? (v, s1, s2) => comparison((T)v, s1, s2) : (strdelegate_collation?)null;
if (State == ConnectionState.Open)
{
var rc = sqlite3_create_collation(Handle, name, state, collation);
SqliteException.ThrowExceptionForRC(rc, Handle);
}
_collations ??= new Dictionary<string, (object?, strdelegate_collation?)>(StringComparer.OrdinalIgnoreCase);
_collations[name] = (state, collation);
}
/// <summary>
/// Begins a transaction on the connection.
/// </summary>
/// <returns>The transaction.</returns>
/// <exception cref="SqliteException">A SQLite error occurs during execution.</exception>
/// <seealso href="https://docs.microsoft.com/dotnet/standard/data/sqlite/transactions">Transactions</seealso>
/// <seealso href="https://docs.microsoft.com/dotnet/standard/data/sqlite/database-errors">Database Errors</seealso>
public new virtual SqliteTransaction BeginTransaction()
=> BeginTransaction(IsolationLevel.Unspecified);
/// <summary>
/// Begins a transaction on the connection.
/// </summary>
/// <param name="deferred">
/// <see langword="true" /> to defer the creation of the transaction.
/// This also causes transactions to upgrade from read transactions to write transactions as needed by their commands.
/// </param>
/// <returns>The transaction.</returns>
/// <remarks>
/// Warning, commands inside a deferred transaction can fail if they cause the
/// transaction to be upgraded from a read transaction to a write transaction
/// but the database is locked. The application will need to retry the entire
/// transaction when this happens.
/// </remarks>
/// <exception cref="SqliteException">A SQLite error occurs during execution.</exception>
/// <seealso href="https://docs.microsoft.com/dotnet/standard/data/sqlite/transactions">Transactions</seealso>
/// <seealso href="https://docs.microsoft.com/dotnet/standard/data/sqlite/database-errors">Database Errors</seealso>
public virtual SqliteTransaction BeginTransaction(bool deferred)
=> BeginTransaction(IsolationLevel.Unspecified, deferred);
/// <summary>
/// Begins a transaction on the connection.
/// </summary>
/// <param name="isolationLevel">The isolation level of the transaction.</param>
/// <returns>The transaction.</returns>
protected override DbTransaction BeginDbTransaction(IsolationLevel isolationLevel)
=> BeginTransaction(isolationLevel);
/// <summary>
/// Begins a transaction on the connection.
/// </summary>
/// <param name="isolationLevel">The isolation level of the transaction.</param>
/// <returns>The transaction.</returns>
/// <exception cref="SqliteException">A SQLite error occurs during execution.</exception>
/// <seealso href="https://docs.microsoft.com/dotnet/standard/data/sqlite/transactions">Transactions</seealso>
/// <seealso href="https://docs.microsoft.com/dotnet/standard/data/sqlite/database-errors">Database Errors</seealso>
public new virtual SqliteTransaction BeginTransaction(IsolationLevel isolationLevel)
=> BeginTransaction(isolationLevel, deferred: isolationLevel == IsolationLevel.ReadUncommitted);
/// <summary>
/// Begins a transaction on the connection.
/// </summary>
/// <param name="isolationLevel">The isolation level of the transaction.</param>
/// <param name="deferred">
/// <see langword="true" /> to defer the creation of the transaction.
/// This also causes transactions to upgrade from read transactions to write transactions as needed by their commands.
/// </param>
/// <returns>The transaction.</returns>
/// <remarks>
/// Warning, commands inside a deferred transaction can fail if they cause the
/// transaction to be upgraded from a read transaction to a write transaction
/// but the database is locked. The application will need to retry the entire
/// transaction when this happens.
/// </remarks>
/// <exception cref="SqliteException">A SQLite error occurs during execution.</exception>
/// <seealso href="https://docs.microsoft.com/dotnet/standard/data/sqlite/transactions">Transactions</seealso>
/// <seealso href="https://docs.microsoft.com/dotnet/standard/data/sqlite/database-errors">Database Errors</seealso>
public virtual SqliteTransaction BeginTransaction(IsolationLevel isolationLevel, bool deferred)
{
if (State != ConnectionState.Open)
{
throw new InvalidOperationException(Resources.CallRequiresOpenConnection(nameof(BeginTransaction)));
}
if (Transaction != null)
{
throw new InvalidOperationException(Resources.ParallelTransactionsNotSupported);
}
return Transaction = new SqliteTransaction(this, isolationLevel, deferred);
}
/// <summary>
/// Changes the current database. Not supported.
/// </summary>
/// <param name="databaseName">The name of the database to use.</param>
/// <exception cref="NotSupportedException">Always.</exception>
public override void ChangeDatabase(string databaseName)
=> throw new NotSupportedException();
/// <summary>
/// Enables extension loading on the connection.
/// </summary>
/// <param name="enable"><see langword="true" /> to enable; <see langword="false" /> to disable.</param>
/// <seealso href="https://docs.microsoft.com/dotnet/standard/data/sqlite/extensions">Extensions</seealso>
public virtual void EnableExtensions(bool enable = true)
{
if (State == ConnectionState.Open)
{
var rc = sqlite3_enable_load_extension(Handle, enable ? 1 : 0);
SqliteException.ThrowExceptionForRC(rc, Handle);
}
_extensionsEnabled = enable;
}
/// <summary>
/// Loads a SQLite extension library.
/// </summary>
/// <param name="file">The shared library containing the extension.</param>
/// <param name="proc">The entry point. If null, the default entry point is used.</param>
/// <seealso href="https://docs.microsoft.com/dotnet/standard/data/sqlite/extensions">Extensions</seealso>
public virtual void LoadExtension(string file, string? proc = null)
{
if (State == ConnectionState.Open)
{
int rc;
if (!_extensionsEnabled)
{
rc = sqlite3_db_config(Handle, SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION, 1, out _);
SqliteException.ThrowExceptionForRC(rc, Handle);
}
LoadExtensionCore(file, proc);
}
_extensions ??= new HashSet<(string, string?)>();
_extensions.Add((file, proc));
}
private void LoadExtensionCore(string file, string? proc)
{
var rc = sqlite3_load_extension(Handle, utf8z.FromString(file), utf8z.FromString(proc), out var errmsg);
if (rc != SQLITE_OK)
{
throw new SqliteException(Resources.SqliteNativeError(rc, errmsg.utf8_to_string()), rc, rc);
}
}
/// <summary>
/// Backup of the connected database.
/// </summary>
/// <param name="destination">The destination of the backup.</param>
/// <seealso href="https://docs.microsoft.com/dotnet/standard/data/sqlite/backup">Online Backup</seealso>
public virtual void BackupDatabase(SqliteConnection destination)
=> BackupDatabase(destination, MainDatabaseName, MainDatabaseName);
/// <summary>
/// Backup of the connected database.
/// </summary>
/// <param name="destination">The destination of the backup.</param>
/// <param name="destinationName">The name of the destination database.</param>
/// <param name="sourceName">The name of the source database.</param>
/// <seealso href="https://docs.microsoft.com/dotnet/standard/data/sqlite/backup">Online Backup</seealso>
public virtual void BackupDatabase(SqliteConnection destination, string destinationName, string sourceName)
{
if (State != ConnectionState.Open)
{
throw new InvalidOperationException(Resources.CallRequiresOpenConnection(nameof(BackupDatabase)));
}
if (destination == null)
{
throw new ArgumentNullException(nameof(destination));
}
var close = false;
if (destination.State != ConnectionState.Open)
{
destination.Open();
close = true;
}
try
{
using var backup = sqlite3_backup_init(destination.Handle, destinationName, Handle, sourceName);
int rc;
if (backup.IsInvalid)
{
rc = sqlite3_errcode(destination.Handle);
SqliteException.ThrowExceptionForRC(rc, destination.Handle);
}
rc = sqlite3_backup_step(backup, -1);
SqliteException.ThrowExceptionForRC(rc, destination.Handle);
}
finally
{
if (close)
{
destination.Close();
}
}
}
/// <summary>
/// Returns schema information for the data source of this conneciton.
/// </summary>
/// <returns>Schema information.</returns>
public override DataTable GetSchema()
=> GetSchema(DbMetaDataCollectionNames.MetaDataCollections);
/// <summary>
/// Returns schema information for the data source of this conneciton.
/// </summary>
/// <param name="collectionName">The name of the schema.</param>
/// <returns>Schema information.</returns>
public override DataTable GetSchema(string collectionName)
=> GetSchema(collectionName, Array.Empty<string>());
/// <summary>
/// Returns schema information for the data source of this conneciton.
/// </summary>
/// <param name="collectionName">The name of the schema.</param>
/// <param name="restrictionValues">The restrictions.</param>
/// <returns>Schema information.</returns>
public override DataTable GetSchema(string collectionName, string?[] restrictionValues)
{
if (restrictionValues is not null && restrictionValues.Length != 0)
{
throw new ArgumentException(Resources.TooManyRestrictions(collectionName));
}
if (string.Equals(collectionName, DbMetaDataCollectionNames.MetaDataCollections, StringComparison.OrdinalIgnoreCase))
{
return new DataTable(DbMetaDataCollectionNames.MetaDataCollections)
{
Columns =
{
{ DbMetaDataColumnNames.CollectionName },
{ DbMetaDataColumnNames.NumberOfRestrictions, typeof(int) },
{ DbMetaDataColumnNames.NumberOfIdentifierParts, typeof(int) }
},
Rows =
{
new object[] { DbMetaDataCollectionNames.MetaDataCollections, 0, 0 },
new object[] { DbMetaDataCollectionNames.ReservedWords, 0, 0 }
}
};
}
else if (string.Equals(collectionName, DbMetaDataCollectionNames.ReservedWords, StringComparison.OrdinalIgnoreCase))
{
var dataTable = new DataTable(DbMetaDataCollectionNames.ReservedWords)
{
Columns =
{
{ DbMetaDataColumnNames.ReservedWord }
}
};
int rc;
string keyword;
var count = sqlite3_keyword_count();
for (var i = 0; i < count; i++)
{
rc = sqlite3_keyword_name(i, out keyword);
SqliteException.ThrowExceptionForRC(rc, null);
dataTable.Rows.Add(new object[] { keyword });
}
return dataTable;
}
throw new ArgumentException(Resources.UnknownCollection(collectionName));
}
private void CreateFunctionCore<TState, TResult>(
string name,
int arity,
TState state,
Func<TState, SqliteValueReader, TResult>? function,
bool isDeterministic)
{
if (name == null)
{
throw new ArgumentNullException(nameof(name));
}
delegate_function_scalar? func = null;
if (function != null)
{
func = (ctx, user_data, args) =>
{
// TODO: Avoid allocation when niladic
var values = new SqliteParameterReader(name, args);
try
{
// TODO: Avoid closure by passing function via user_data
var result = function((TState)user_data, values);
new SqliteResultBinder(ctx, result).Bind();
}
catch (Exception ex)
{
sqlite3_result_error(ctx, ex.Message);
if (ex is SqliteException sqlEx)
{
// NB: This must be called after sqlite3_result_error()
sqlite3_result_error_code(ctx, sqlEx.SqliteErrorCode);
}
}
};
}
var flags = isDeterministic ? SQLITE_DETERMINISTIC : 0;
if (State == ConnectionState.Open)
{
var rc = sqlite3_create_function(
Handle,
name,
arity,
flags,
state,
func);
SqliteException.ThrowExceptionForRC(rc, Handle);
}
_functions ??= new Dictionary<(string, int), (int, object?, delegate_function_scalar?)>(FunctionsKeyComparer.Instance);
_functions[(name, arity)] = (flags, state, func);
}
private void CreateAggregateCore<TAccumulate, TResult>(
string name,
int arity,
TAccumulate seed,
Func<TAccumulate, SqliteValueReader, TAccumulate>? func,
Func<TAccumulate, TResult>? resultSelector,
bool isDeterministic)
{
if (name == null)
{
throw new ArgumentNullException(nameof(name));
}
delegate_function_aggregate_step? func_step = null;
if (func != null)
{
func_step = (ctx, user_data, args) =>
{
var context = (AggregateContext<TAccumulate>)user_data;
if (context.Exception != null)
{
return;
}
// TODO: Avoid allocation when niladic
var reader = new SqliteParameterReader(name, args);
try
{
// TODO: Avoid closure by passing func via user_data
// NB: No need to set ctx.state since we just mutate the instance
context.Accumulate = func(context.Accumulate, reader);
}
catch (Exception ex)
{
context.Exception = ex;
}
};
}
delegate_function_aggregate_final? func_final = null;
if (resultSelector != null)
{
func_final = (ctx, user_data) =>
{
var context = (AggregateContext<TAccumulate>)user_data;
if (context.Exception == null)
{
try
{
// TODO: Avoid closure by passing resultSelector via user_data
var result = resultSelector(context.Accumulate);
new SqliteResultBinder(ctx, result).Bind();
}
catch (Exception ex)
{
context.Exception = ex;
}
}
if (context.Exception != null)
{
sqlite3_result_error(ctx, context.Exception.Message);
if (context.Exception is SqliteException sqlEx)
{
// NB: This must be called after sqlite3_result_error()
sqlite3_result_error_code(ctx, sqlEx.SqliteErrorCode);
}
}
};
}
var flags = isDeterministic ? SQLITE_DETERMINISTIC : 0;
var state = new AggregateContext<TAccumulate>(seed);
if (State == ConnectionState.Open)
{
var rc = sqlite3_create_function(
Handle,
name,
arity,
flags,
state,
func_step,
func_final);
SqliteException.ThrowExceptionForRC(rc, Handle);
}
_aggregates ??=
new Dictionary<(string, int), (int, object?, delegate_function_aggregate_step?, delegate_function_aggregate_final?)>(
FunctionsKeyComparer.Instance);
_aggregates[(name, arity)] = (flags, state, func_step, func_final);
}
private static Func<TState, SqliteValueReader, TResult>? IfNotNull<TState, TResult>(
object? x,
Func<TState, SqliteValueReader, TResult> value)
=> x != null ? value : null;
private static object?[] GetValues(SqliteValueReader reader)
{
var values = new object?[reader.FieldCount];
reader.GetValues(values);
return values;
}
private sealed class AggregateContext<T>
{
public AggregateContext(T seed)
=> Accumulate = seed;
public T Accumulate { get; set; }
public Exception? Exception { get; set; }
}
private sealed class FunctionsKeyComparer : IEqualityComparer<(string name, int arity)>
{
public static readonly FunctionsKeyComparer Instance = new();
public bool Equals((string name, int arity) x, (string name, int arity) y)
=> StringComparer.OrdinalIgnoreCase.Equals(x.name, y.name)
&& x.arity == y.arity;
public int GetHashCode((string name, int arity) obj)
{
var nameHashCode = StringComparer.OrdinalIgnoreCase.GetHashCode(obj.name);
var arityHashCode = obj.arity.GetHashCode();
return ((int)(((uint)nameHashCode << 5) | ((uint)nameHashCode >> 27)) + nameHashCode) ^ arityHashCode;
}
}
}
}