11//
2- // Copyright (c) 2009-2021 Krueger Systems, Inc.
2+ // Copyright (c) 2009-2024 Krueger Systems, Inc.
33//
44// Permission is hereby granted, free of charge, to any person obtaining a copy
55// of this software and associated documentation files (the "Software"), to deal
@@ -114,6 +114,7 @@ public static NotNullConstraintViolationException New (SQLiteException exception
114114 public enum SQLiteOpenFlags
115115 {
116116 ReadOnly = 1 , ReadWrite = 2 , Create = 4 ,
117+ Uri = 0x40 , Memory = 0x80 ,
117118 NoMutex = 0x8000 , FullMutex = 0x10000 ,
118119 SharedCache = 0x20000 , PrivateCache = 0x40000 ,
119120 ProtectionComplete = 0x00100000 ,
@@ -159,11 +160,109 @@ public enum CreateFlags
159160 FullTextSearch4 = 0x200
160161 }
161162
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+
162261 /// <summary>
163262 /// An open connection to a SQLite database.
164263 /// </summary>
165264 [ Preserve ( AllMembers = true ) ]
166- public partial class SQLiteConnection : IDisposable
265+ public partial class SQLiteConnection : ISQLiteConnection
167266 {
168267 private bool _open ;
169268 private TimeSpan _busyTimeout ;
@@ -364,7 +463,7 @@ public static string Quote (string unsafeString)
364463 /// if your database is encrypted.
365464 /// This only has an effect if you are using the SQLCipher nuget package.
366465 /// </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>
368467 void SetKey ( string key )
369468 {
370469 if ( key == null )
@@ -379,7 +478,7 @@ void SetKey (string key)
379478 /// if your database is encrypted.
380479 /// This only has an effect if you are using the SQLCipher nuget package.
381480 /// </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>
383482 void SetKey ( byte [ ] key )
384483 {
385484 if ( key == null )
@@ -390,6 +489,32 @@ void SetKey (byte[] key)
390489 ExecuteScalar < string > ( "pragma key = \" x'" + s + "'\" " ) ;
391490 }
392491
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+
393518 /// <summary>
394519 /// Enable or disable extension loading.
395520 /// </summary>
@@ -1894,7 +2019,7 @@ public int Update (object obj, Type objType)
18942019 }
18952020 ps . Add ( pk . GetValue ( obj ) ) ;
18962021 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 ) ;
18982023
18992024 try {
19002025 rowsAffected = Execute ( q , ps . ToArray ( ) ) ;
@@ -1905,7 +2030,7 @@ public int Update (object obj, Type objType)
19052030 throw NotNullConstraintViolationException . New ( ex , map , obj ) ;
19062031 }
19072032
1908- throw ex ;
2033+ throw ;
19092034 }
19102035
19112036 if ( rowsAffected > 0 )
@@ -2349,7 +2474,7 @@ public class AutoIncrementAttribute : Attribute
23492474 {
23502475 }
23512476
2352- [ AttributeUsage ( AttributeTargets . Property ) ]
2477+ [ AttributeUsage ( AttributeTargets . Property , AllowMultiple = true ) ]
23532478 public class IndexedAttribute : Attribute
23542479 {
23552480 public string Name { get ; set ; }
@@ -2786,7 +2911,7 @@ public static string SqlDecl (TableMapping.Column p, bool storeDateTimeAsTicks,
27862911 public static string SqlType ( TableMapping . Column p , bool storeDateTimeAsTicks , bool storeTimeSpanAsTicks )
27872912 {
27882913 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 ) ) {
27902915 return "integer" ;
27912916 }
27922917 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
31853310 else if ( value is Boolean ) {
31863311 SQLite3 . BindInt ( stmt , index , ( bool ) value ? 1 : 0 ) ;
31873312 }
3188- else if ( value is UInt32 || value is Int64 ) {
3313+ else if ( value is UInt32 || value is Int64 || value is UInt64 ) {
31893314 SQLite3 . BindInt64 ( stmt , index , Convert . ToInt64 ( value ) ) ;
31903315 }
31913316 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
33193444 else if ( clrType == typeof ( Int64 ) ) {
33203445 return SQLite3 . ColumnInt64 ( stmt , index ) ;
33213446 }
3447+ else if ( clrType == typeof ( UInt64 ) ) {
3448+ return ( ulong ) SQLite3 . ColumnInt64 ( stmt , index ) ;
3449+ }
33223450 else if ( clrType == typeof ( UInt32 ) ) {
33233451 return ( uint ) SQLite3 . ColumnInt64 ( stmt , index ) ;
33243452 }
@@ -3463,6 +3591,12 @@ internal static Action<object, Sqlite3Statement, int> GetFastSetter<T> (SQLiteCo
34633591 return SQLite3 . ColumnInt64 ( stmt , index ) ;
34643592 } ) ;
34653593 }
3594+ else if ( clrType == typeof ( UInt64 ) )
3595+ {
3596+ fastSetter = CreateNullableTypedSetterDelegate < T , UInt64 > ( column , ( stmt , index ) => {
3597+ return ( ulong ) SQLite3 . ColumnInt64 ( stmt , index ) ;
3598+ } ) ;
3599+ }
34663600 else if ( clrType == typeof ( UInt32 ) ) {
34673601 fastSetter = CreateNullableTypedSetterDelegate < T , UInt32 > ( column , ( stmt , index ) => {
34683602 return ( uint ) SQLite3 . ColumnInt64 ( stmt , index ) ;
0 commit comments