1
1
//
2
- // Copyright (c) 2009-2021 Krueger Systems, Inc.
2
+ // Copyright (c) 2009-2024 Krueger Systems, Inc.
3
3
//
4
4
// Permission is hereby granted, free of charge, to any person obtaining a copy
5
5
// of this software and associated documentation files (the "Software"), to deal
@@ -114,6 +114,7 @@ public static NotNullConstraintViolationException New (SQLiteException exception
114
114
public enum SQLiteOpenFlags
115
115
{
116
116
ReadOnly = 1 , ReadWrite = 2 , Create = 4 ,
117
+ Uri = 0x40 , Memory = 0x80 ,
117
118
NoMutex = 0x8000 , FullMutex = 0x10000 ,
118
119
SharedCache = 0x20000 , PrivateCache = 0x40000 ,
119
120
ProtectionComplete = 0x00100000 ,
@@ -159,11 +160,109 @@ public enum CreateFlags
159
160
FullTextSearch4 = 0x200
160
161
}
161
162
163
+ public interface ISQLiteConnection : IDisposable
164
+ {
165
+ Sqlite3DatabaseHandle Handle { get ; }
166
+ string DatabasePath { get ; }
167
+ int LibVersionNumber { get ; }
168
+ bool TimeExecution { get ; set ; }
169
+ bool Trace { get ; set ; }
170
+ Action < string > Tracer { get ; set ; }
171
+ bool StoreDateTimeAsTicks { get ; }
172
+ bool StoreTimeSpanAsTicks { get ; }
173
+ string DateTimeStringFormat { get ; }
174
+ TimeSpan BusyTimeout { get ; set ; }
175
+ IEnumerable < TableMapping > TableMappings { get ; }
176
+ bool IsInTransaction { get ; }
177
+
178
+ event EventHandler < NotifyTableChangedEventArgs > TableChanged ;
179
+
180
+ void Backup ( string destinationDatabasePath , string databaseName = "main" ) ;
181
+ void BeginTransaction ( ) ;
182
+ void Close ( ) ;
183
+ void Commit ( ) ;
184
+ SQLiteCommand CreateCommand ( string cmdText , params object [ ] ps ) ;
185
+ SQLiteCommand CreateCommand ( string cmdText , Dictionary < string , object > args ) ;
186
+ int CreateIndex ( string indexName , string tableName , string [ ] columnNames , bool unique = false ) ;
187
+ int CreateIndex ( string indexName , string tableName , string columnName , bool unique = false ) ;
188
+ int CreateIndex ( string tableName , string columnName , bool unique = false ) ;
189
+ int CreateIndex ( string tableName , string [ ] columnNames , bool unique = false ) ;
190
+ int CreateIndex < T > ( Expression < Func < T , object > > property , bool unique = false ) ;
191
+ CreateTableResult CreateTable < T > ( CreateFlags createFlags = CreateFlags . None ) ;
192
+ CreateTableResult CreateTable ( Type ty , CreateFlags createFlags = CreateFlags . None ) ;
193
+ CreateTablesResult CreateTables < T , T2 > ( CreateFlags createFlags = CreateFlags . None )
194
+ where T : new ( )
195
+ where T2 : new ( ) ;
196
+ CreateTablesResult CreateTables < T , T2 , T3 > ( CreateFlags createFlags = CreateFlags . None )
197
+ where T : new ( )
198
+ where T2 : new ( )
199
+ where T3 : new ( ) ;
200
+ CreateTablesResult CreateTables < T , T2 , T3 , T4 > ( CreateFlags createFlags = CreateFlags . None )
201
+ where T : new ( )
202
+ where T2 : new ( )
203
+ where T3 : new ( )
204
+ where T4 : new ( ) ;
205
+ CreateTablesResult CreateTables < T , T2 , T3 , T4 , T5 > ( CreateFlags createFlags = CreateFlags . None )
206
+ where T : new ( )
207
+ where T2 : new ( )
208
+ where T3 : new ( )
209
+ where T4 : new ( )
210
+ where T5 : new ( ) ;
211
+ CreateTablesResult CreateTables ( CreateFlags createFlags = CreateFlags . None , params Type [ ] types ) ;
212
+ IEnumerable < T > DeferredQuery < T > ( string query , params object [ ] args ) where T : new ( ) ;
213
+ IEnumerable < object > DeferredQuery ( TableMapping map , string query , params object [ ] args ) ;
214
+ int Delete ( object objectToDelete ) ;
215
+ int Delete < T > ( object primaryKey ) ;
216
+ int Delete ( object primaryKey , TableMapping map ) ;
217
+ int DeleteAll < T > ( ) ;
218
+ int DeleteAll ( TableMapping map ) ;
219
+ int DropTable < T > ( ) ;
220
+ int DropTable ( TableMapping map ) ;
221
+ void EnableLoadExtension ( bool enabled ) ;
222
+ void EnableWriteAheadLogging ( ) ;
223
+ int Execute ( string query , params object [ ] args ) ;
224
+ T ExecuteScalar < T > ( string query , params object [ ] args ) ;
225
+ T Find < T > ( object pk ) where T : new ( ) ;
226
+ object Find ( object pk , TableMapping map ) ;
227
+ T Find < T > ( Expression < Func < T , bool > > predicate ) where T : new ( ) ;
228
+ T FindWithQuery < T > ( string query , params object [ ] args ) where T : new ( ) ;
229
+ object FindWithQuery ( TableMapping map , string query , params object [ ] args ) ;
230
+ T Get < T > ( object pk ) where T : new ( ) ;
231
+ object Get ( object pk , TableMapping map ) ;
232
+ T Get < T > ( Expression < Func < T , bool > > predicate ) where T : new ( ) ;
233
+ TableMapping GetMapping ( Type type , CreateFlags createFlags = CreateFlags . None ) ;
234
+ TableMapping GetMapping < T > ( CreateFlags createFlags = CreateFlags . None ) ;
235
+ List < SQLiteConnection . ColumnInfo > GetTableInfo ( string tableName ) ;
236
+ int Insert ( object obj ) ;
237
+ int Insert ( object obj , Type objType ) ;
238
+ int Insert ( object obj , string extra ) ;
239
+ int Insert ( object obj , string extra , Type objType ) ;
240
+ int InsertAll ( IEnumerable objects , bool runInTransaction = true ) ;
241
+ int InsertAll ( IEnumerable objects , string extra , bool runInTransaction = true ) ;
242
+ int InsertAll ( IEnumerable objects , Type objType , bool runInTransaction = true ) ;
243
+ int InsertOrReplace ( object obj ) ;
244
+ int InsertOrReplace ( object obj , Type objType ) ;
245
+ List < T > Query < T > ( string query , params object [ ] args ) where T : new ( ) ;
246
+ List < object > Query ( TableMapping map , string query , params object [ ] args ) ;
247
+ List < T > QueryScalars < T > ( string query , params object [ ] args ) ;
248
+ void ReKey ( string key ) ;
249
+ void ReKey ( byte [ ] key ) ;
250
+ void Release ( string savepoint ) ;
251
+ void Rollback ( ) ;
252
+ void RollbackTo ( string savepoint ) ;
253
+ void RunInTransaction ( Action action ) ;
254
+ string SaveTransactionPoint ( ) ;
255
+ TableQuery < T > Table < T > ( ) where T : new ( ) ;
256
+ int Update ( object obj ) ;
257
+ int Update ( object obj , Type objType ) ;
258
+ int UpdateAll ( IEnumerable objects , bool runInTransaction = true ) ;
259
+ }
260
+
162
261
/// <summary>
163
262
/// An open connection to a SQLite database.
164
263
/// </summary>
165
264
[ Preserve ( AllMembers = true ) ]
166
- public partial class SQLiteConnection : IDisposable
265
+ public partial class SQLiteConnection : ISQLiteConnection
167
266
{
168
267
private bool _open ;
169
268
private TimeSpan _busyTimeout ;
@@ -364,7 +463,7 @@ public static string Quote (string unsafeString)
364
463
/// if your database is encrypted.
365
464
/// This only has an effect if you are using the SQLCipher nuget package.
366
465
/// </summary>
367
- /// <param name="key">Ecryption key plain text that is converted to the real encryption key using PBKDF2 key derivation</param>
466
+ /// <param name="key">Encryption key plain text that is converted to the real encryption key using PBKDF2 key derivation</param>
368
467
void SetKey ( string key )
369
468
{
370
469
if ( key == null )
@@ -379,7 +478,7 @@ void SetKey (string key)
379
478
/// if your database is encrypted.
380
479
/// This only has an effect if you are using the SQLCipher nuget package.
381
480
/// </summary>
382
- /// <param name="key">256-bit (32 byte) ecryption key data</param>
481
+ /// <param name="key">256-bit (32 byte) encryption key data</param>
383
482
void SetKey ( byte [ ] key )
384
483
{
385
484
if ( key == null )
@@ -390,6 +489,32 @@ void SetKey (byte[] key)
390
489
ExecuteScalar < string > ( "pragma key = \" x'" + s + "'\" " ) ;
391
490
}
392
491
492
+ /// <summary>
493
+ /// Change the encryption key for a SQLCipher database with "pragma rekey = ...".
494
+ /// </summary>
495
+ /// <param name="key">Encryption key plain text that is converted to the real encryption key using PBKDF2 key derivation</param>
496
+ public void ReKey ( string key )
497
+ {
498
+ if ( key == null )
499
+ throw new ArgumentNullException ( nameof ( key ) ) ;
500
+ var q = Quote ( key ) ;
501
+ ExecuteScalar < string > ( "pragma rekey = " + q ) ;
502
+ }
503
+
504
+ /// <summary>
505
+ /// Change the encryption key for a SQLCipher database.
506
+ /// </summary>
507
+ /// <param name="key">256-bit (32 byte) or 384-bit (48 bytes) encryption key data</param>
508
+ public void ReKey ( byte [ ] key )
509
+ {
510
+ if ( key == null )
511
+ throw new ArgumentNullException ( nameof ( key ) ) ;
512
+ if ( key . Length != 32 && key . Length != 48 )
513
+ throw new ArgumentException ( "Key must be 32 bytes (256-bit) or 48 bytes (384-bit)" , nameof ( key ) ) ;
514
+ var s = String . Join ( "" , key . Select ( x => x . ToString ( "X2" ) ) ) ;
515
+ ExecuteScalar < string > ( "pragma rekey = \" x'" + s + "'\" " ) ;
516
+ }
517
+
393
518
/// <summary>
394
519
/// Enable or disable extension loading.
395
520
/// </summary>
@@ -1894,7 +2019,7 @@ public int Update (object obj, Type objType)
1894
2019
}
1895
2020
ps . Add ( pk . GetValue ( obj ) ) ;
1896
2021
var q = string . Format ( "update \" {0}\" set {1} where \" {2}\" = ? " , map . TableName , string . Join ( "," , ( from c in cols
1897
- select "\" " + c . Name + "\" = ? " ) . ToArray ( ) ) , pk . Name ) ;
2022
+ select "\" " + c . Name + "\" = ? " ) . ToArray ( ) ) , pk . Name ) ;
1898
2023
1899
2024
try {
1900
2025
rowsAffected = Execute ( q , ps . ToArray ( ) ) ;
@@ -1905,7 +2030,7 @@ public int Update (object obj, Type objType)
1905
2030
throw NotNullConstraintViolationException . New ( ex , map , obj ) ;
1906
2031
}
1907
2032
1908
- throw ex ;
2033
+ throw ;
1909
2034
}
1910
2035
1911
2036
if ( rowsAffected > 0 )
@@ -2349,7 +2474,7 @@ public class AutoIncrementAttribute : Attribute
2349
2474
{
2350
2475
}
2351
2476
2352
- [ AttributeUsage ( AttributeTargets . Property ) ]
2477
+ [ AttributeUsage ( AttributeTargets . Property , AllowMultiple = true ) ]
2353
2478
public class IndexedAttribute : Attribute
2354
2479
{
2355
2480
public string Name { get ; set ; }
@@ -2786,7 +2911,7 @@ public static string SqlDecl (TableMapping.Column p, bool storeDateTimeAsTicks,
2786
2911
public static string SqlType ( TableMapping . Column p , bool storeDateTimeAsTicks , bool storeTimeSpanAsTicks )
2787
2912
{
2788
2913
var clrType = p . ColumnType ;
2789
- if ( clrType == typeof ( Boolean ) || clrType == typeof ( Byte ) || clrType == typeof ( UInt16 ) || clrType == typeof ( SByte ) || clrType == typeof ( Int16 ) || clrType == typeof ( Int32 ) || clrType == typeof ( UInt32 ) || clrType == typeof ( Int64 ) ) {
2914
+ if ( clrType == typeof ( Boolean ) || clrType == typeof ( Byte ) || clrType == typeof ( UInt16 ) || clrType == typeof ( SByte ) || clrType == typeof ( Int16 ) || clrType == typeof ( Int32 ) || clrType == typeof ( UInt32 ) || clrType == typeof ( Int64 ) || clrType == typeof ( UInt64 ) ) {
2790
2915
return "integer" ;
2791
2916
}
2792
2917
else if ( clrType == typeof ( Single ) || clrType == typeof ( Double ) || clrType == typeof ( Decimal ) ) {
@@ -3185,7 +3310,7 @@ internal static void BindParameter (Sqlite3Statement stmt, int index, object val
3185
3310
else if ( value is Boolean ) {
3186
3311
SQLite3 . BindInt ( stmt , index , ( bool ) value ? 1 : 0 ) ;
3187
3312
}
3188
- else if ( value is UInt32 || value is Int64 ) {
3313
+ else if ( value is UInt32 || value is Int64 || value is UInt64 ) {
3189
3314
SQLite3 . BindInt64 ( stmt , index , Convert . ToInt64 ( value ) ) ;
3190
3315
}
3191
3316
else if ( value is Single || value is Double || value is Decimal ) {
@@ -3319,6 +3444,9 @@ object ReadCol (Sqlite3Statement stmt, int index, SQLite3.ColType type, Type clr
3319
3444
else if ( clrType == typeof ( Int64 ) ) {
3320
3445
return SQLite3 . ColumnInt64 ( stmt , index ) ;
3321
3446
}
3447
+ else if ( clrType == typeof ( UInt64 ) ) {
3448
+ return ( ulong ) SQLite3 . ColumnInt64 ( stmt , index ) ;
3449
+ }
3322
3450
else if ( clrType == typeof ( UInt32 ) ) {
3323
3451
return ( uint ) SQLite3 . ColumnInt64 ( stmt , index ) ;
3324
3452
}
@@ -3463,6 +3591,12 @@ internal static Action<object, Sqlite3Statement, int> GetFastSetter<T> (SQLiteCo
3463
3591
return SQLite3 . ColumnInt64 ( stmt , index ) ;
3464
3592
} ) ;
3465
3593
}
3594
+ else if ( clrType == typeof ( UInt64 ) )
3595
+ {
3596
+ fastSetter = CreateNullableTypedSetterDelegate < T , UInt64 > ( column , ( stmt , index ) => {
3597
+ return ( ulong ) SQLite3 . ColumnInt64 ( stmt , index ) ;
3598
+ } ) ;
3599
+ }
3466
3600
else if ( clrType == typeof ( UInt32 ) ) {
3467
3601
fastSetter = CreateNullableTypedSetterDelegate < T , UInt32 > ( column , ( stmt , index ) => {
3468
3602
return ( uint ) SQLite3 . ColumnInt64 ( stmt , index ) ;
0 commit comments