-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathSQLiteAsync.cs
1628 lines (1502 loc) · 55.1 KB
/
SQLiteAsync.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
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Copyright (c) 2012-2024 Krueger Systems, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
#pragma warning disable 1591 // XML Doc Comments
namespace SQLite
{
public interface ISQLiteAsyncConnection
{
string DatabasePath { get; }
int LibVersionNumber { get; }
string DateTimeStringFormat { get; }
bool StoreDateTimeAsTicks { get; }
bool StoreTimeSpanAsTicks { get; }
bool Trace { get; set; }
Action<string> Tracer { get; set; }
bool TimeExecution { get; set; }
IEnumerable<TableMapping> TableMappings { get; }
Task BackupAsync (string destinationDatabasePath, string databaseName = "main");
Task CloseAsync ();
Task<int> CreateIndexAsync (string tableName, string columnName, bool unique = false);
Task<int> CreateIndexAsync (string indexName, string tableName, string columnName, bool unique = false);
Task<int> CreateIndexAsync (string tableName, string[] columnNames, bool unique = false);
Task<int> CreateIndexAsync (string indexName, string tableName, string[] columnNames, bool unique = false);
Task<int> CreateIndexAsync<T> (Expression<Func<T, object>> property, bool unique = false);
Task<CreateTableResult> CreateTableAsync<T> (CreateFlags createFlags = CreateFlags.None) where T : new();
Task<CreateTableResult> CreateTableAsync (Type ty, CreateFlags createFlags = CreateFlags.None);
Task<CreateTablesResult> CreateTablesAsync<T, T2> (CreateFlags createFlags = CreateFlags.None)
where T : new()
where T2 : new();
Task<CreateTablesResult> CreateTablesAsync<T, T2, T3> (CreateFlags createFlags = CreateFlags.None)
where T : new()
where T2 : new()
where T3 : new();
Task<CreateTablesResult> CreateTablesAsync<T, T2, T3, T4> (CreateFlags createFlags = CreateFlags.None)
where T : new()
where T2 : new()
where T3 : new()
where T4 : new();
Task<CreateTablesResult> CreateTablesAsync<T, T2, T3, T4, T5> (CreateFlags createFlags = CreateFlags.None)
where T : new()
where T2 : new()
where T3 : new()
where T4 : new()
where T5 : new();
Task<CreateTablesResult> CreateTablesAsync (CreateFlags createFlags = CreateFlags.None, params Type[] types);
Task<IEnumerable<T>> DeferredQueryAsync<T> (string query, params object[] args) where T : new();
Task<IEnumerable<object>> DeferredQueryAsync (TableMapping map, string query, params object[] args);
Task<int> DeleteAllAsync<T> ();
Task<int> DeleteAllAsync (TableMapping map);
Task<int> DeleteAsync (object objectToDelete);
Task<int> DeleteAsync<T> (object primaryKey);
Task<int> DeleteAsync (object primaryKey, TableMapping map);
Task<int> DropTableAsync<T> () where T : new();
Task<int> DropTableAsync (TableMapping map);
Task EnableLoadExtensionAsync (bool enabled);
Task EnableWriteAheadLoggingAsync ();
Task<int> ExecuteAsync (string query, params object[] args);
Task<T> ExecuteScalarAsync<T> (string query, params object[] args);
Task<T> FindAsync<T> (object pk) where T : new();
Task<object> FindAsync (object pk, TableMapping map);
Task<T> FindAsync<T> (Expression<Func<T, bool>> predicate) where T : new();
Task<T> FindWithQueryAsync<T> (string query, params object[] args) where T : new();
Task<object> FindWithQueryAsync (TableMapping map, string query, params object[] args);
Task<T> GetAsync<T> (object pk) where T : new();
Task<object> GetAsync (object pk, TableMapping map);
Task<T> GetAsync<T> (Expression<Func<T, bool>> predicate) where T : new();
TimeSpan GetBusyTimeout ();
SQLiteConnectionWithLock GetConnection ();
Task<TableMapping> GetMappingAsync (Type type, CreateFlags createFlags = CreateFlags.None);
Task<TableMapping> GetMappingAsync<T> (CreateFlags createFlags = CreateFlags.None) where T : new();
Task<List<SQLiteConnection.ColumnInfo>> GetTableInfoAsync (string tableName);
Task<int> InsertAllAsync (IEnumerable objects, bool runInTransaction = true);
Task<int> InsertAllAsync (IEnumerable objects, string extra, bool runInTransaction = true);
Task<int> InsertAllAsync (IEnumerable objects, Type objType, bool runInTransaction = true);
Task<int> InsertAsync (object obj);
Task<int> InsertAsync (object obj, Type objType);
Task<int> InsertAsync (object obj, string extra);
Task<int> InsertAsync (object obj, string extra, Type objType);
Task<int> InsertOrReplaceAsync (object obj);
Task<int> InsertOrReplaceAsync (object obj, Type objType);
Task<List<T>> QueryAsync<T> (string query, params object[] args) where T : new();
Task<List<object>> QueryAsync (TableMapping map, string query, params object[] args);
Task<List<T>> QueryScalarsAsync<T> (string query, params object[] args);
Task ReKeyAsync (string key);
Task ReKeyAsync (byte[] key);
Task RunInTransactionAsync (Action<SQLiteConnection> action);
Task SetBusyTimeoutAsync (TimeSpan value);
AsyncTableQuery<T> Table<T> () where T : new();
Task<int> UpdateAllAsync (IEnumerable objects, bool runInTransaction = true);
Task<int> UpdateAsync (object obj);
Task<int> UpdateAsync (object obj, Type objType);
}
/// <summary>
/// A pooled asynchronous connection to a SQLite database.
/// </summary>
public partial class SQLiteAsyncConnection : ISQLiteAsyncConnection
{
readonly SQLiteConnectionString _connectionString;
/// <summary>
/// Constructs a new SQLiteAsyncConnection and opens a pooled SQLite database specified by databasePath.
/// </summary>
/// <param name="databasePath">
/// Specifies the path to the database file.
/// </param>
/// <param name="storeDateTimeAsTicks">
/// Specifies whether to store DateTime properties as ticks (true) or strings (false). You
/// absolutely do want to store them as Ticks in all new projects. The value of false is
/// only here for backwards compatibility. There is a *significant* speed advantage, with no
/// down sides, when setting storeDateTimeAsTicks = true.
/// If you use DateTimeOffset properties, it will be always stored as ticks regardingless
/// the storeDateTimeAsTicks parameter.
/// </param>
public SQLiteAsyncConnection (string databasePath, bool storeDateTimeAsTicks = true)
: this (new SQLiteConnectionString (databasePath, SQLiteOpenFlags.Create | SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.FullMutex, storeDateTimeAsTicks))
{
}
/// <summary>
/// Constructs a new SQLiteAsyncConnection and opens a pooled SQLite database specified by databasePath.
/// </summary>
/// <param name="databasePath">
/// Specifies the path to the database file.
/// </param>
/// <param name="openFlags">
/// Flags controlling how the connection should be opened.
/// Async connections should have the FullMutex flag set to provide best performance.
/// </param>
/// <param name="storeDateTimeAsTicks">
/// Specifies whether to store DateTime properties as ticks (true) or strings (false). You
/// absolutely do want to store them as Ticks in all new projects. The value of false is
/// only here for backwards compatibility. There is a *significant* speed advantage, with no
/// down sides, when setting storeDateTimeAsTicks = true.
/// If you use DateTimeOffset properties, it will be always stored as ticks regardingless
/// the storeDateTimeAsTicks parameter.
/// </param>
public SQLiteAsyncConnection (string databasePath, SQLiteOpenFlags openFlags, bool storeDateTimeAsTicks = true)
: this (new SQLiteConnectionString (databasePath, openFlags, storeDateTimeAsTicks))
{
}
/// <summary>
/// Constructs a new SQLiteAsyncConnection and opens a pooled SQLite database
/// using the given connection string.
/// </summary>
/// <param name="connectionString">
/// Details on how to find and open the database.
/// </param>
public SQLiteAsyncConnection (SQLiteConnectionString connectionString)
{
_connectionString = connectionString;
}
/// <summary>
/// Gets the database path used by this connection.
/// </summary>
public string DatabasePath => GetConnection ().DatabasePath;
/// <summary>
/// Gets the SQLite library version number. 3007014 would be v3.7.14
/// </summary>
public int LibVersionNumber => GetConnection ().LibVersionNumber;
/// <summary>
/// The format to use when storing DateTime properties as strings. Ignored if StoreDateTimeAsTicks is true.
/// </summary>
/// <value>The date time string format.</value>
public string DateTimeStringFormat => GetConnection ().DateTimeStringFormat;
/// <summary>
/// The amount of time to wait for a table to become unlocked.
/// </summary>
public TimeSpan GetBusyTimeout ()
{
return GetConnection ().BusyTimeout;
}
/// <summary>
/// Sets the amount of time to wait for a table to become unlocked.
/// </summary>
public Task SetBusyTimeoutAsync (TimeSpan value)
{
return ReadAsync<object> (conn => {
conn.BusyTimeout = value;
return null;
});
}
/// <summary>
/// Enables the write ahead logging. WAL is significantly faster in most scenarios
/// by providing better concurrency and better disk IO performance than the normal
/// journal mode. You only need to call this function once in the lifetime of the database.
/// </summary>
public Task EnableWriteAheadLoggingAsync ()
{
return WriteAsync<object> (conn => {
conn.EnableWriteAheadLogging ();
return null;
});
}
/// <summary>
/// Whether to store DateTime properties as ticks (true) or strings (false).
/// </summary>
public bool StoreDateTimeAsTicks => GetConnection ().StoreDateTimeAsTicks;
/// <summary>
/// Whether to store TimeSpan properties as ticks (true) or strings (false).
/// </summary>
public bool StoreTimeSpanAsTicks => GetConnection ().StoreTimeSpanAsTicks;
/// <summary>
/// Whether to writer queries to <see cref="Tracer"/> during execution.
/// </summary>
/// <value>The tracer.</value>
public bool Trace {
get { return GetConnection ().Trace; }
set { GetConnection ().Trace = value; }
}
/// <summary>
/// The delegate responsible for writing trace lines.
/// </summary>
/// <value>The tracer.</value>
public Action<string> Tracer {
get { return GetConnection ().Tracer; }
set { GetConnection ().Tracer = value; }
}
/// <summary>
/// Whether Trace lines should be written that show the execution time of queries.
/// </summary>
public bool TimeExecution {
get { return GetConnection ().TimeExecution; }
set { GetConnection ().TimeExecution = value; }
}
/// <summary>
/// Returns the mappings from types to tables that the connection
/// currently understands.
/// </summary>
public IEnumerable<TableMapping> TableMappings => GetConnection ().TableMappings;
/// <summary>
/// Closes all connections to all async databases.
/// You should *never* need to do this.
/// This is a blocking operation that will return when all connections
/// have been closed.
/// </summary>
public static void ResetPool ()
{
SQLiteConnectionPool.Shared.Reset ();
}
/// <summary>
/// Gets the pooled lockable connection used by this async connection.
/// You should never need to use this. This is provided only to add additional
/// functionality to SQLite-net. If you use this connection, you must use
/// the Lock method on it while using it.
/// </summary>
public SQLiteConnectionWithLock GetConnection ()
{
return SQLiteConnectionPool.Shared.GetConnection (_connectionString);
}
SQLiteConnectionWithLock GetConnectionAndTransactionLock (out object transactionLock)
{
return SQLiteConnectionPool.Shared.GetConnectionAndTransactionLock (_connectionString, out transactionLock);
}
/// <summary>
/// Closes any pooled connections used by the database.
/// </summary>
public Task CloseAsync ()
{
return Task.Factory.StartNew (() => {
SQLiteConnectionPool.Shared.CloseConnection (_connectionString);
}, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default);
}
Task<T> ReadAsync<T> (Func<SQLiteConnectionWithLock, T> read)
{
return Task.Factory.StartNew (() => {
var conn = GetConnection ();
using (conn.Lock ()) {
return read (conn);
}
}, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default);
}
Task<T> WriteAsync<T> (Func<SQLiteConnectionWithLock, T> write)
{
return Task.Factory.StartNew (() => {
var conn = GetConnection ();
using (conn.Lock ()) {
return write (conn);
}
}, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default);
}
Task<T> TransactAsync<T> (Func<SQLiteConnectionWithLock, T> transact)
{
return Task.Factory.StartNew (() => {
var conn = GetConnectionAndTransactionLock (out var transactionLock);
lock (transactionLock) {
using (conn.Lock ()) {
return transact (conn);
}
}
}, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default);
}
/// <summary>
/// Enable or disable extension loading.
/// </summary>
public Task EnableLoadExtensionAsync (bool enabled)
{
return WriteAsync<object> (conn => {
conn.EnableLoadExtension (enabled);
return null;
});
}
/// <summary>
/// Executes a "create table if not exists" on the database. It also
/// creates any specified indexes on the columns of the table. It uses
/// a schema automatically generated from the specified type. You can
/// later access this schema by calling GetMapping.
/// </summary>
/// <returns>
/// Whether the table was created or migrated.
/// </returns>
public Task<CreateTableResult> CreateTableAsync<T> (CreateFlags createFlags = CreateFlags.None)
where T : new()
{
return WriteAsync (conn => conn.CreateTable<T> (createFlags));
}
/// <summary>
/// Executes a "create table if not exists" on the database. It also
/// creates any specified indexes on the columns of the table. It uses
/// a schema automatically generated from the specified type. You can
/// later access this schema by calling GetMapping.
/// </summary>
/// <param name="ty">Type to reflect to a database table.</param>
/// <param name="createFlags">Optional flags allowing implicit PK and indexes based on naming conventions.</param>
/// <returns>
/// Whether the table was created or migrated.
/// </returns>
public Task<CreateTableResult> CreateTableAsync (Type ty, CreateFlags createFlags = CreateFlags.None)
{
return WriteAsync (conn => conn.CreateTable (ty, createFlags));
}
/// <summary>
/// Executes a "create table if not exists" on the database for each type. It also
/// creates any specified indexes on the columns of the table. It uses
/// a schema automatically generated from the specified type. You can
/// later access this schema by calling GetMapping.
/// </summary>
/// <returns>
/// Whether the table was created or migrated for each type.
/// </returns>
public Task<CreateTablesResult> CreateTablesAsync<T, T2> (CreateFlags createFlags = CreateFlags.None)
where T : new()
where T2 : new()
{
return CreateTablesAsync (createFlags, typeof (T), typeof (T2));
}
/// <summary>
/// Executes a "create table if not exists" on the database for each type. It also
/// creates any specified indexes on the columns of the table. It uses
/// a schema automatically generated from the specified type. You can
/// later access this schema by calling GetMapping.
/// </summary>
/// <returns>
/// Whether the table was created or migrated for each type.
/// </returns>
public Task<CreateTablesResult> CreateTablesAsync<T, T2, T3> (CreateFlags createFlags = CreateFlags.None)
where T : new()
where T2 : new()
where T3 : new()
{
return CreateTablesAsync (createFlags, typeof (T), typeof (T2), typeof (T3));
}
/// <summary>
/// Executes a "create table if not exists" on the database for each type. It also
/// creates any specified indexes on the columns of the table. It uses
/// a schema automatically generated from the specified type. You can
/// later access this schema by calling GetMapping.
/// </summary>
/// <returns>
/// Whether the table was created or migrated for each type.
/// </returns>
public Task<CreateTablesResult> CreateTablesAsync<T, T2, T3, T4> (CreateFlags createFlags = CreateFlags.None)
where T : new()
where T2 : new()
where T3 : new()
where T4 : new()
{
return CreateTablesAsync (createFlags, typeof (T), typeof (T2), typeof (T3), typeof (T4));
}
/// <summary>
/// Executes a "create table if not exists" on the database for each type. It also
/// creates any specified indexes on the columns of the table. It uses
/// a schema automatically generated from the specified type. You can
/// later access this schema by calling GetMapping.
/// </summary>
/// <returns>
/// Whether the table was created or migrated for each type.
/// </returns>
public Task<CreateTablesResult> CreateTablesAsync<T, T2, T3, T4, T5> (CreateFlags createFlags = CreateFlags.None)
where T : new()
where T2 : new()
where T3 : new()
where T4 : new()
where T5 : new()
{
return CreateTablesAsync (createFlags, typeof (T), typeof (T2), typeof (T3), typeof (T4), typeof (T5));
}
/// <summary>
/// Executes a "create table if not exists" on the database for each type. It also
/// creates any specified indexes on the columns of the table. It uses
/// a schema automatically generated from the specified type. You can
/// later access this schema by calling GetMapping.
/// </summary>
/// <returns>
/// Whether the table was created or migrated for each type.
/// </returns>
public Task<CreateTablesResult> CreateTablesAsync (CreateFlags createFlags = CreateFlags.None, params Type[] types)
{
return WriteAsync (conn => conn.CreateTables (createFlags, types));
}
/// <summary>
/// Executes a "drop table" on the database. This is non-recoverable.
/// </summary>
public Task<int> DropTableAsync<T> ()
where T : new()
{
return WriteAsync (conn => conn.DropTable<T> ());
}
/// <summary>
/// Executes a "drop table" on the database. This is non-recoverable.
/// </summary>
/// <param name="map">
/// The TableMapping used to identify the table.
/// </param>
public Task<int> DropTableAsync (TableMapping map)
{
return WriteAsync (conn => conn.DropTable (map));
}
/// <summary>
/// Creates an index for the specified table and column.
/// </summary>
/// <param name="tableName">Name of the database table</param>
/// <param name="columnName">Name of the column to index</param>
/// <param name="unique">Whether the index should be unique</param>
/// <returns>Zero on success.</returns>
public Task<int> CreateIndexAsync (string tableName, string columnName, bool unique = false)
{
return WriteAsync (conn => conn.CreateIndex (tableName, columnName, unique));
}
/// <summary>
/// Creates an index for the specified table and column.
/// </summary>
/// <param name="indexName">Name of the index to create</param>
/// <param name="tableName">Name of the database table</param>
/// <param name="columnName">Name of the column to index</param>
/// <param name="unique">Whether the index should be unique</param>
/// <returns>Zero on success.</returns>
public Task<int> CreateIndexAsync (string indexName, string tableName, string columnName, bool unique = false)
{
return WriteAsync (conn => conn.CreateIndex (indexName, tableName, columnName, unique));
}
/// <summary>
/// Creates an index for the specified table and columns.
/// </summary>
/// <param name="tableName">Name of the database table</param>
/// <param name="columnNames">An array of column names to index</param>
/// <param name="unique">Whether the index should be unique</param>
/// <returns>Zero on success.</returns>
public Task<int> CreateIndexAsync (string tableName, string[] columnNames, bool unique = false)
{
return WriteAsync (conn => conn.CreateIndex (tableName, columnNames, unique));
}
/// <summary>
/// Creates an index for the specified table and columns.
/// </summary>
/// <param name="indexName">Name of the index to create</param>
/// <param name="tableName">Name of the database table</param>
/// <param name="columnNames">An array of column names to index</param>
/// <param name="unique">Whether the index should be unique</param>
/// <returns>Zero on success.</returns>
public Task<int> CreateIndexAsync (string indexName, string tableName, string[] columnNames, bool unique = false)
{
return WriteAsync (conn => conn.CreateIndex (indexName, tableName, columnNames, unique));
}
/// <summary>
/// Creates an index for the specified object property.
/// e.g. CreateIndex<Client>(c => c.Name);
/// </summary>
/// <typeparam name="T">Type to reflect to a database table.</typeparam>
/// <param name="property">Property to index</param>
/// <param name="unique">Whether the index should be unique</param>
/// <returns>Zero on success.</returns>
public Task<int> CreateIndexAsync<T> (Expression<Func<T, object>> property, bool unique = false)
{
return WriteAsync (conn => conn.CreateIndex (property, unique));
}
/// <summary>
/// Inserts the given object and (and updates its
/// auto incremented primary key if it has one).
/// </summary>
/// <param name="obj">
/// The object to insert.
/// </param>
/// <returns>
/// The number of rows added to the table.
/// </returns>
public Task<int> InsertAsync (object obj)
{
return WriteAsync (conn => conn.Insert (obj));
}
/// <summary>
/// Inserts the given object (and updates its
/// auto incremented primary key if it has one).
/// The return value is the number of rows added to the table.
/// </summary>
/// <param name="obj">
/// The object to insert.
/// </param>
/// <param name="objType">
/// The type of object to insert.
/// </param>
/// <returns>
/// The number of rows added to the table.
/// </returns>
public Task<int> InsertAsync (object obj, Type objType)
{
return WriteAsync (conn => conn.Insert (obj, objType));
}
/// <summary>
/// Inserts the given object (and updates its
/// auto incremented primary key if it has one).
/// The return value is the number of rows added to the table.
/// </summary>
/// <param name="obj">
/// The object to insert.
/// </param>
/// <param name="extra">
/// Literal SQL code that gets placed into the command. INSERT {extra} INTO ...
/// </param>
/// <returns>
/// The number of rows added to the table.
/// </returns>
public Task<int> InsertAsync (object obj, string extra)
{
return WriteAsync (conn => conn.Insert (obj, extra));
}
/// <summary>
/// Inserts the given object (and updates its
/// auto incremented primary key if it has one).
/// The return value is the number of rows added to the table.
/// </summary>
/// <param name="obj">
/// The object to insert.
/// </param>
/// <param name="extra">
/// Literal SQL code that gets placed into the command. INSERT {extra} INTO ...
/// </param>
/// <param name="objType">
/// The type of object to insert.
/// </param>
/// <returns>
/// The number of rows added to the table.
/// </returns>
public Task<int> InsertAsync (object obj, string extra, Type objType)
{
return WriteAsync (conn => conn.Insert (obj, extra, objType));
}
/// <summary>
/// Inserts the given object (and updates its
/// auto incremented primary key if it has one).
/// The return value is the number of rows added to the table.
/// If a UNIQUE constraint violation occurs with
/// some pre-existing object, this function deletes
/// the old object.
/// </summary>
/// <param name="obj">
/// The object to insert.
/// </param>
/// <returns>
/// The number of rows modified.
/// </returns>
public Task<int> InsertOrReplaceAsync (object obj)
{
return WriteAsync (conn => conn.InsertOrReplace (obj));
}
/// <summary>
/// Inserts the given object (and updates its
/// auto incremented primary key if it has one).
/// The return value is the number of rows added to the table.
/// If a UNIQUE constraint violation occurs with
/// some pre-existing object, this function deletes
/// the old object.
/// </summary>
/// <param name="obj">
/// The object to insert.
/// </param>
/// <param name="objType">
/// The type of object to insert.
/// </param>
/// <returns>
/// The number of rows modified.
/// </returns>
public Task<int> InsertOrReplaceAsync (object obj, Type objType)
{
return WriteAsync (conn => conn.InsertOrReplace (obj, objType));
}
/// <summary>
/// Updates all of the columns of a table using the specified object
/// except for its primary key.
/// The object is required to have a primary key.
/// </summary>
/// <param name="obj">
/// The object to update. It must have a primary key designated using the PrimaryKeyAttribute.
/// </param>
/// <returns>
/// The number of rows updated.
/// </returns>
public Task<int> UpdateAsync (object obj)
{
return WriteAsync (conn => conn.Update (obj));
}
/// <summary>
/// Updates all of the columns of a table using the specified object
/// except for its primary key.
/// The object is required to have a primary key.
/// </summary>
/// <param name="obj">
/// The object to update. It must have a primary key designated using the PrimaryKeyAttribute.
/// </param>
/// <param name="objType">
/// The type of object to insert.
/// </param>
/// <returns>
/// The number of rows updated.
/// </returns>
public Task<int> UpdateAsync (object obj, Type objType)
{
return WriteAsync (conn => conn.Update (obj, objType));
}
/// <summary>
/// Updates all specified objects.
/// </summary>
/// <param name="objects">
/// An <see cref="IEnumerable"/> of the objects to insert.
/// </param>
/// <param name="runInTransaction">
/// A boolean indicating if the inserts should be wrapped in a transaction
/// </param>
/// <returns>
/// The number of rows modified.
/// </returns>
public Task<int> UpdateAllAsync (IEnumerable objects, bool runInTransaction = true)
{
return WriteAsync (conn => conn.UpdateAll (objects, runInTransaction));
}
/// <summary>
/// Deletes the given object from the database using its primary key.
/// </summary>
/// <param name="objectToDelete">
/// The object to delete. It must have a primary key designated using the PrimaryKeyAttribute.
/// </param>
/// <returns>
/// The number of rows deleted.
/// </returns>
public Task<int> DeleteAsync (object objectToDelete)
{
return WriteAsync (conn => conn.Delete (objectToDelete));
}
/// <summary>
/// Deletes the object with the specified primary key.
/// </summary>
/// <param name="primaryKey">
/// The primary key of the object to delete.
/// </param>
/// <returns>
/// The number of objects deleted.
/// </returns>
/// <typeparam name='T'>
/// The type of object.
/// </typeparam>
public Task<int> DeleteAsync<T> (object primaryKey)
{
return WriteAsync (conn => conn.Delete<T> (primaryKey));
}
/// <summary>
/// Deletes the object with the specified primary key.
/// </summary>
/// <param name="primaryKey">
/// The primary key of the object to delete.
/// </param>
/// <param name="map">
/// The TableMapping used to identify the table.
/// </param>
/// <returns>
/// The number of objects deleted.
/// </returns>
public Task<int> DeleteAsync (object primaryKey, TableMapping map)
{
return WriteAsync (conn => conn.Delete (primaryKey, map));
}
/// <summary>
/// Deletes all the objects from the specified table.
/// WARNING WARNING: Let me repeat. It deletes ALL the objects from the
/// specified table. Do you really want to do that?
/// </summary>
/// <returns>
/// The number of objects deleted.
/// </returns>
/// <typeparam name='T'>
/// The type of objects to delete.
/// </typeparam>
public Task<int> DeleteAllAsync<T> ()
{
return WriteAsync (conn => conn.DeleteAll<T> ());
}
/// <summary>
/// Deletes all the objects from the specified table.
/// WARNING WARNING: Let me repeat. It deletes ALL the objects from the
/// specified table. Do you really want to do that?
/// </summary>
/// <param name="map">
/// The TableMapping used to identify the table.
/// </param>
/// <returns>
/// The number of objects deleted.
/// </returns>
public Task<int> DeleteAllAsync (TableMapping map)
{
return WriteAsync (conn => conn.DeleteAll (map));
}
/// <summary>
/// Backup the entire database to the specified path.
/// </summary>
/// <param name="destinationDatabasePath">Path to backup file.</param>
/// <param name="databaseName">The name of the database to backup (usually "main").</param>
public Task BackupAsync (string destinationDatabasePath, string databaseName = "main")
{
return WriteAsync (conn => {
conn.Backup (destinationDatabasePath, databaseName);
return 0;
});
}
/// <summary>
/// Attempts to retrieve an object with the given primary key from the table
/// associated with the specified type. Use of this method requires that
/// the given type have a designated PrimaryKey (using the PrimaryKeyAttribute).
/// </summary>
/// <param name="pk">
/// The primary key.
/// </param>
/// <returns>
/// The object with the given primary key. Throws a not found exception
/// if the object is not found.
/// </returns>
public Task<T> GetAsync<T> (object pk)
where T : new()
{
return ReadAsync (conn => conn.Get<T> (pk));
}
/// <summary>
/// Attempts to retrieve an object with the given primary key from the table
/// associated with the specified type. Use of this method requires that
/// the given type have a designated PrimaryKey (using the PrimaryKeyAttribute).
/// </summary>
/// <param name="pk">
/// The primary key.
/// </param>
/// <param name="map">
/// The TableMapping used to identify the table.
/// </param>
/// <returns>
/// The object with the given primary key. Throws a not found exception
/// if the object is not found.
/// </returns>
public Task<object> GetAsync (object pk, TableMapping map)
{
return ReadAsync (conn => conn.Get (pk, map));
}
/// <summary>
/// Attempts to retrieve the first object that matches the predicate from the table
/// associated with the specified type.
/// </summary>
/// <param name="predicate">
/// A predicate for which object to find.
/// </param>
/// <returns>
/// The object that matches the given predicate. Throws a not found exception
/// if the object is not found.
/// </returns>
public Task<T> GetAsync<T> (Expression<Func<T, bool>> predicate)
where T : new()
{
return ReadAsync (conn => conn.Get<T> (predicate));
}
/// <summary>
/// Attempts to retrieve an object with the given primary key from the table
/// associated with the specified type. Use of this method requires that
/// the given type have a designated PrimaryKey (using the PrimaryKeyAttribute).
/// </summary>
/// <param name="pk">
/// The primary key.
/// </param>
/// <returns>
/// The object with the given primary key or null
/// if the object is not found.
/// </returns>
public Task<T> FindAsync<T> (object pk)
where T : new()
{
return ReadAsync (conn => conn.Find<T> (pk));
}
/// <summary>
/// Attempts to retrieve an object with the given primary key from the table
/// associated with the specified type. Use of this method requires that
/// the given type have a designated PrimaryKey (using the PrimaryKeyAttribute).
/// </summary>
/// <param name="pk">
/// The primary key.
/// </param>
/// <param name="map">
/// The TableMapping used to identify the table.
/// </param>
/// <returns>
/// The object with the given primary key or null
/// if the object is not found.
/// </returns>
public Task<object> FindAsync (object pk, TableMapping map)
{
return ReadAsync (conn => conn.Find (pk, map));
}
/// <summary>
/// Attempts to retrieve the first object that matches the predicate from the table
/// associated with the specified type.
/// </summary>
/// <param name="predicate">
/// A predicate for which object to find.
/// </param>
/// <returns>
/// The object that matches the given predicate or null
/// if the object is not found.
/// </returns>
public Task<T> FindAsync<T> (Expression<Func<T, bool>> predicate)
where T : new()
{
return ReadAsync (conn => conn.Find<T> (predicate));
}
/// <summary>
/// Attempts to retrieve the first object that matches the query from the table
/// associated with the specified type.
/// </summary>
/// <param name="query">
/// The fully escaped SQL.
/// </param>
/// <param name="args">
/// Arguments to substitute for the occurences of '?' in the query.
/// </param>
/// <returns>
/// The object that matches the given predicate or null
/// if the object is not found.
/// </returns>
public Task<T> FindWithQueryAsync<T> (string query, params object[] args)
where T : new()
{
return ReadAsync (conn => conn.FindWithQuery<T> (query, args));
}
/// <summary>
/// Attempts to retrieve the first object that matches the query from the table
/// associated with the specified type.
/// </summary>
/// <param name="map">
/// The TableMapping used to identify the table.
/// </param>
/// <param name="query">
/// The fully escaped SQL.
/// </param>
/// <param name="args">
/// Arguments to substitute for the occurences of '?' in the query.
/// </param>
/// <returns>
/// The object that matches the given predicate or null
/// if the object is not found.
/// </returns>
public Task<object> FindWithQueryAsync (TableMapping map, string query, params object[] args)
{
return ReadAsync (conn => conn.FindWithQuery (map, query, args));
}
/// <summary>
/// Retrieves the mapping that is automatically generated for the given type.
/// </summary>
/// <param name="type">
/// The type whose mapping to the database is returned.
/// </param>
/// <param name="createFlags">
/// Optional flags allowing implicit PK and indexes based on naming conventions
/// </param>
/// <returns>
/// The mapping represents the schema of the columns of the database and contains
/// methods to set and get properties of objects.
/// </returns>
public Task<TableMapping> GetMappingAsync (Type type, CreateFlags createFlags = CreateFlags.None)
{
return ReadAsync (conn => conn.GetMapping (type, createFlags));
}
/// <summary>
/// Retrieves the mapping that is automatically generated for the given type.
/// </summary>
/// <param name="createFlags">
/// Optional flags allowing implicit PK and indexes based on naming conventions
/// </param>
/// <returns>
/// The mapping represents the schema of the columns of the database and contains
/// methods to set and get properties of objects.
/// </returns>
public Task<TableMapping> GetMappingAsync<T> (CreateFlags createFlags = CreateFlags.None)
where T : new()
{
return ReadAsync (conn => conn.GetMapping<T> (createFlags));
}
/// <summary>
/// Query the built-in sqlite table_info table for a specific tables columns.