This repository has been archived by the owner on Aug 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathOfflineSQLiteStore.cs
578 lines (525 loc) · 27.8 KB
/
OfflineSQLiteStore.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
// Copyright (c) Microsoft Corporation. All Rights Reserved.
// Licensed under the MIT License.
using Microsoft.Datasync.Client.Offline;
using Microsoft.Datasync.Client.Query;
using Microsoft.Datasync.Client.Serialization;
using Microsoft.Datasync.Client.SQLiteStore.Driver;
using Microsoft.Datasync.Client.SQLiteStore.Utils;
using Microsoft.Datasync.Client.Table;
using Microsoft.Datasync.Client.Utils;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Linq;
using SQLitePCL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace Microsoft.Datasync.Client.SQLiteStore
{
/// <summary>
/// An implementation of <see cref="IOfflineStore"/> using SQLite as the persistent store.
/// </summary>
public class OfflineSQLiteStore : AbstractOfflineStore, IDeltaTokenStoreProvider
{
/// <summary>
/// The mapping from the table name to the table definition. This is built using the
/// <see cref="DefineTable(string, JObject)"/> method before store initialization.
/// </summary>
private readonly Dictionary<string, TableDefinition> tableMap = new(StringComparer.OrdinalIgnoreCase);
/// <summary>
/// A lock that is used to serialize write operations to the database.
/// </summary>
private readonly DisposableLock operationLock = new();
/// <summary>
/// Creates a new instance of <see cref="OfflineSQLiteStore"/> using the provided connection string.
/// </summary>
/// <remarks>
/// <para>If the connection string starts with <c>file:</c>, then it is considered to be a URI filename and
/// should be structured as such. This allows the setting of any options (such as mode, cache, etc.)
/// if needed.</para>
/// <para>If the connection string does not start with file:, then it should be an absolute path (which starts
/// with a <c>/</c>).</para>
/// </remarks>
/// <see href="https://sqlite.org/c3ref/open.html"/>
/// <param name="connectionString">The connection string to use for persistent storage.</param>
public OfflineSQLiteStore(string connectionString)
{
Arguments.IsNotNullOrWhitespace(connectionString, nameof(connectionString));
if (!connectionString.StartsWith("file:"))
{
throw new ArgumentException("The connection string must be a Uri string valid for SQLite");
}
DbConnection = new SqliteConnection(connectionString);
}
/// <summary>
/// Creates a new instance of <see cref="OfflineSQLiteStore"/> using the provided connection string.
/// </summary>
/// <remarks>
/// <para>If the connection string starts with <c>file:</c>, then it is considered to be a URI filename and
/// should be structured as such. This allows the setting of any options (such as mode, cache, etc.)
/// if needed.</para>
/// <para>If the connection string does not start with file:, then it should be an absolute path (which starts
/// with a <c>/</c>).</para>
/// </remarks>
/// <see href="https://sqlite.org/c3ref/open.html"/>
/// <param name="connectionString">The connection string to use for persistent storage.</param>
/// <param name="logger">The logger to use for logging SQL requests.</param>
public OfflineSQLiteStore(string connectionString, ILogger logger) : this(connectionString)
{
Logger = logger;
}
/// <summary>
/// Creates a new instance of <see cref="OfflineSQLiteStore"/> using the provided SQLitePCL connection.
/// </summary>
/// <remarks>
/// This assumes you maintain all lifecycle responsibilities for the connection. The connection is not
/// opened, limits are not set, and the connection is not disposed of when the store is disposed.
/// </remarks>
/// <param name="connection">The connection.</param>
public OfflineSQLiteStore(sqlite3 connection)
{
Arguments.IsNotNull(connection, nameof(connection));
DbConnection = new SqliteConnection(connection);
}
/// <summary>
/// Creates a new instance of <see cref="OfflineSQLiteStore"/> using the provided SQLitePCL connection.
/// </summary>
/// <remarks>
/// This assumes you maintain all lifecycle responsibilities for the connection. The connection is not
/// opened, limits are not set, and the connection is not disposed of when the store is disposed.
/// </remarks>
/// <param name="connection">The connection.</param>
/// <param name="logger">The logger to use for logging SQL requests.</param>
public OfflineSQLiteStore(sqlite3 connection, ILogger logger) : this(connection)
{
Logger = logger;
}
/// <summary>
/// The database connection.
/// </summary>
internal SqliteConnection DbConnection { get; }
/// <summary>
/// The logging service
/// </summary>
public ILogger Logger { get; set; }
#region AbstractOfflineStore
/// <summary>
/// Defines the local table in the persistent store.
/// </summary>
/// <param name="tableName">The name of the local table.</param>
/// <param name="tableDefinition">The definition of the table.</param>
public override void DefineTable(string tableName, JObject tableDefinition)
{
Arguments.IsValidTableName(tableName, true, nameof(tableName));
Arguments.IsNotNull(tableDefinition, nameof(tableDefinition));
if (!tableMap.ContainsKey(tableName))
{
Logger?.LogDebug("Created table definition for table {tableName}", tableName);
tableMap.Add(tableName, new TableDefinition(tableName, tableDefinition));
}
}
/// <summary>
/// Determines if a table is defined.
/// </summary>
/// <param name="tableName">The name of the table.</param>
/// <returns>true if the table is defined.</returns>
public override bool TableIsDefined(string tableName)
=> tableMap.ContainsKey(tableName);
/// <summary>
/// Deletes items from the table where the items are identified by a query.
/// </summary>
/// <param name="query">A query description that identifies the items to be deleted.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe.</param>
/// <returns>A task that completes when the items have been deleted from the table.</returns>
public override async Task DeleteAsync(QueryDescription query, CancellationToken cancellationToken = default)
{
Arguments.IsNotNull(query, nameof(query));
await EnsureInitializedAsync(cancellationToken).ConfigureAwait(false);
_ = GetTableOrThrow(query.TableName); // Validate that the table exists.
string sql = SqlStatements.DeleteFromTable(query, out Dictionary<string, object> parameters);
using (operationLock.AcquireLock())
{
ExecuteNonQueryInternal(sql, parameters);
}
}
/// <summary>
/// Deletes items from the table where the items are identified by their ID.
/// </summary>
/// <param name="tableName">The name of the table where the items are located.</param>
/// <param name="ids">A list of IDs to delete.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe.</param>
/// <returns>A task that completes when the items have been deleted from the table.</returns>
public override async Task DeleteAsync(string tableName, IEnumerable<string> ids, CancellationToken cancellationToken = default)
{
Arguments.IsValidTableName(tableName, true, nameof(tableName));
Arguments.IsNotNull(ids, nameof(ids));
await EnsureInitializedAsync(cancellationToken).ConfigureAwait(false);
var parameters = ids.ToParameterList("id");
if (parameters.Count == 0)
{
return; // Don't execute a statement if there is nothing to execute.
}
string sql = SqlStatements.DeleteIdsFromTable(tableName, SystemProperties.JsonIdProperty, parameters.Keys);
using (operationLock.AcquireLock())
{
ExecuteNonQueryInternal(sql, parameters);
}
}
/// <summary>
/// Returns a single item by the ID of the item.
/// </summary>
/// <param name="tableName">The table name holding the item.</param>
/// <param name="id">The ID of the item.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe.</param>
/// <returns>A task that returns the item when complete.</returns>
public override async Task<JObject> GetItemAsync(string tableName, string id, CancellationToken cancellationToken = default)
{
Arguments.IsValidTableName(tableName, true, nameof(tableName));
Arguments.IsValidId(id, nameof(id));
await EnsureInitializedAsync(cancellationToken).ConfigureAwait(false);
Dictionary<string, object> parameters = new[] { id }.ToParameterList("id");
string sql = SqlStatements.GetItemById(tableName, SystemProperties.JsonIdProperty, parameters.Keys.First());
using (operationLock.AcquireLock())
{
IList<JObject> results = ExecuteQueryInternal(tableName, sql, parameters);
return results.FirstOrDefault();
}
}
/// <summary>
/// Returns items from the table.
/// </summary>
/// <param name="query">A query describing the items to be returned.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe.</param>
/// <returns>A task that returns a page of items when complete.</returns>
public override async Task<Page<JObject>> GetPageAsync(QueryDescription query, CancellationToken cancellationToken = default)
{
Arguments.IsNotNull(query, nameof(query));
await EnsureInitializedAsync(cancellationToken).ConfigureAwait(false);
string queryStmt = SqlStatements.SelectFromTable(query, out Dictionary<string, object> parameters);
using (operationLock.AcquireLock())
{
IList<JObject> rows = ExecuteQueryInternal(query.TableName, queryStmt, parameters);
Page<JObject> result = new() { Items = rows };
if (query.IncludeTotalCount)
{
string countStmt = SqlStatements.CountFromTable(query, out Dictionary<string, object> countParams);
IList<JObject> countRows = ExecuteQueryInternal(query.TableName, countStmt, countParams);
result.Count = countRows[0].Value<long>("count");
}
return result;
}
}
/// <summary>
/// Gets the list of offline tables that have been defined.
/// </summary>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe.</param>
/// <returns>A task that returns the list of tables that have been defined.</returns>
public override async Task<IList<string>> GetTablesAsync(CancellationToken cancellationToken = default)
{
await EnsureInitializedAsync(cancellationToken).ConfigureAwait(false);
return tableMap.Keys.Where(t => !t.StartsWith("__")).ToList();
}
/// <summary>
/// Creates the Delta Token Store implementation that works with this store.
/// </summary>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe.</param>
/// <returns>A task that resolves to the delta token store when complete</returns>
public Task<IDeltaTokenStore> GetDeltaTokenStoreAsync(CancellationToken cancellationToken = default)
=> Task.FromResult<IDeltaTokenStore>(new SQLiteDeltaTokenStore(this));
/// <summary>
/// Initialize the store. This is over-ridden by the store implementation to provide a point
/// where the tables can be created or updated.
/// </summary>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe.</param>
/// <returns>A task that completes when the store is initialized.</returns>
protected override Task InitializeOfflineStoreAsync(CancellationToken cancellationToken)
{
// Define the internal tables.
DefineTable(SystemTables.Configuration, SQLiteDeltaTokenStore.TableDefinition);
// Now that all the tables are defined, actually create the tables!
tableMap.Values
.Where(table => !table.IsInDatabase)
.ToList()
.ForEach(table => CreateTableFromDefinition(table));
return Task.CompletedTask;
}
/// <summary>
/// Updates or inserts the item(s) provided in the table.
/// </summary>
/// <param name="tableName">The table to be used for the operation.</param>
/// <param name="items">The item(s) to be updated or inserted.</param>
/// <param name="ignoreMissingColumns">If <c>true</c>, extra properties on the item can be ignored.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe.</param>
/// <returns>A task that completes when the item has been updated or inserted into the table.</returns>
public override async Task UpsertAsync(string tableName, IEnumerable<JObject> items, bool ignoreMissingColumns, CancellationToken cancellationToken = default)
{
Arguments.IsValidTableName(tableName, true, nameof(tableName));
Arguments.IsNotNull(items, nameof(items));
await EnsureInitializedAsync(cancellationToken).ConfigureAwait(false);
TableDefinition table = GetTableOrThrow(tableName);
var first = items.FirstOrDefault();
if (first == null)
{
return;
}
var columns = new List<ColumnDefinition>();
foreach (var prop in first.Properties())
{
if (!table.TryGetValue(prop.Name, out ColumnDefinition column) && !ignoreMissingColumns)
{
throw new InvalidOperationException($"Column '{prop.Name}' is not defined on table '{tableName}'");
}
if (column != null)
{
columns.Add(column);
}
}
if (columns.Count == 0)
{
return;
}
using (operationLock.AcquireLock())
{
ExecuteNonQueryInternal("BEGIN TRANSACTION");
try
{
BatchInsert(tableName, items, columns.Where(c => c.IsIdColumn).Take(1).ToList());
BatchUpdate(tableName, items, columns);
ExecuteNonQueryInternal("COMMIT TRANSACTION");
}
catch (SQLiteException)
{
ExecuteNonQueryInternal("ROLLBACK");
throw;
}
}
}
#endregion
/// <summary>
/// Executes a SQL query against the store. This is usedul for running arbitrary queries that are supported
/// by SQLite but not the SDK LINQ provider.
/// </summary>
/// <remarks>If doing a JOIN between two tables, then use the <see cref="ExecuteQueryAsync(JObject, string, IDictionary{string, object}, CancellationToken)"/>
/// version to define the field mapping.
/// </summary>
/// <param name="tableName">The name of the table.</param>
/// <param name="sqlStatement">The SQL statement to execute.</param>
/// <param name="parameters">A list of parameter values for referenced parameters in the SQL statement.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe.</param>
/// <returns>The list of rows returned by the query.</returns>
public async Task<IList<JObject>> ExecuteQueryAsync(string tableName, string sqlStatement, IDictionary<string, object> parameters = null, CancellationToken cancellationToken = default)
{
Arguments.IsValidTableName(tableName, nameof(tableName));
Arguments.IsNotNullOrWhitespace(sqlStatement, nameof(sqlStatement));
await EnsureInitializedAsync(cancellationToken).ConfigureAwait(false);
var tableDefinition = GetTableOrThrow(tableName);
using (operationLock.AcquireLock())
{
return ExecuteQueryInternal(tableDefinition, sqlStatement, parameters);
}
}
/// <summary>
/// Executes a SQL query against the store. This is usedul for running arbitrary queries that are supported
/// by SQLite but not the SDK LINQ provider.
/// </summary>
/// <remarks>
/// If doing a query on a single table, use <see cref="ExecuteQueryAsync(string, string, IDictionary{string, object}, CancellationToken)"/> instead.
/// </remarks>
/// <param name="definition">The definition of the result set.</param>
/// <param name="sqlStatement">The SQL statement to execute.</param>
/// <param name="parameters">A list of parameter values for referenced parameters in the SQL statement.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe.</param>
/// <returns>The list of rows returned by the query.</returns>
public async Task<IList<JObject>> ExecuteQueryAsync(JObject definition, string sqlStatement, IDictionary<string, object> parameters = null, CancellationToken cancellationToken = default)
{
Arguments.IsNotNull(definition, nameof(definition));
Arguments.IsNotNullOrWhitespace(sqlStatement, nameof(sqlStatement));
await EnsureInitializedAsync(cancellationToken).ConfigureAwait(false);
var tableDefinition = new TableDefinition("", definition);
using (operationLock.AcquireLock())
{
return ExecuteQueryInternal(tableDefinition, sqlStatement, parameters);
}
}
/// <summary>
/// Do a batch update to set the list of columns for the list of items in the given table.
/// </summary>
/// <param name="tableName">The table to update.</param>
/// <param name="items">The list of items to update (with new values).</param>
/// <param name="columns">The list of columns to update.</param>
protected void BatchInsert(string tableName, IEnumerable<JObject> items, List<ColumnDefinition> columns)
{
int batchSize = DbConnection.MaxParametersPerQuery / columns.Count;
if (batchSize == 0)
{
throw new InvalidOperationException($"The number of fields per entity in an upsert is limited to {DbConnection.MaxParametersPerQuery}");
}
foreach (var batch in items.Split(maxLength: batchSize))
{
var sql = SqlStatements.BatchInsert(tableName, columns, batch, out Dictionary<string, object> parameters);
if (sql != null)
{
ExecuteNonQueryInternal(sql, parameters);
}
}
}
/// <summary>
/// Do a batch update to set the list of columns for the list of items in the given table.
/// </summary>
/// <param name="tableName">The table to update.</param>
/// <param name="items">The list of items to update (with new values).</param>
/// <param name="columns">The list of columns to update.</param>
protected void BatchUpdate(string tableName, IEnumerable<JObject> items, List<ColumnDefinition> columns)
{
if (columns.Count <= 1)
{
// For batch updates to work, there has to be at least one column desides Id that needs to be updated.
return;
}
if (columns.Count > DbConnection.MaxParametersPerQuery)
{
throw new InvalidOperationException($"The number of fields per entity in an upsert is limited to {DbConnection.MaxParametersPerQuery}");
}
foreach (JObject item in items)
{
string sql = SqlStatements.BatchUpdate(tableName, columns, item, out Dictionary<string, object> parameters);
if (sql != null)
{
ExecuteNonQueryInternal(sql, parameters);
}
}
}
/// <summary>
/// Creates or updates the table definition in the SQLite database to the provided definition.
/// </summary>
/// <param name="tableDefinition"></param>
protected void CreateTableFromDefinition(TableDefinition tableDefinition)
{
var idColumn = tableDefinition.Columns.First(c => c.IsIdColumn);
var columnDefinitions = tableDefinition.Columns.Where(c => !c.IsIdColumn);
string createTableSql = SqlStatements.CreateTableFromColumns(tableDefinition.TableName, idColumn, columnDefinitions);
ExecuteNonQueryInternal(createTableSql);
string tableInfoSql = SqlStatements.GetTableInformation(tableDefinition.TableName);
var existingColumns = ExecuteQueryInternal(tableInfoSql).ToDictionary(c => c.Value<string>("name"), StringComparer.OrdinalIgnoreCase);
// Process changes to the table definition - column(s) added
foreach (var column in tableDefinition.Columns.Where(c => !existingColumns.ContainsKey(c.Name)))
{
string addColumnSql = SqlStatements.AddColumnToTable(tableDefinition.TableName, column);
ExecuteNonQueryInternal(addColumnSql);
}
// Process changes to the table definition - column(s) removed
foreach (var column in existingColumns.Keys.Where(c => !tableDefinition.ContainsKey(c)))
{
string dropColumnSql = SqlStatements.DropColumnFromTable(tableDefinition.TableName, column);
ExecuteNonQueryInternal(dropColumnSql);
}
// TODO: Detect changes in the SQLite definition (column types) and throw an exception if the store type does not match.
}
/// <summary>
/// Executes a SQL statement that does not produce any output.
/// </summary>
/// <param name="sqlStatement">The SQL statement to execute.</param>
/// <param name="parameters">The parameters that are referenced in the SQL statement.</param>
protected void ExecuteNonQueryInternal(string sqlStatement, IDictionary<string, object> parameters = null)
{
Arguments.IsNotNullOrWhitespace(sqlStatement, nameof(sqlStatement));
parameters ??= new Dictionary<string, object>();
LogSqlStatement(sqlStatement, parameters);
using SqliteStatement stmt = DbConnection.PrepareStatement(sqlStatement);
stmt.BindParameters(parameters);
stmt.ExecuteNonQuery();
}
/// <summary>
/// Executes a SQL query.
/// </summary>
/// <param name="sqlStatement">The SQL statement to execute.</param>
/// <param name="parameters">The parameters that are referenced in the SQL statement.</param>
/// <returns>The result of the query as a list of rows.</returns>
protected IList<JObject> ExecuteQueryInternal(string sqlStatement, Dictionary<string, object> parameters = null)
=> ExecuteQueryInternal(new TableDefinition(), sqlStatement, parameters);
/// <summary>
/// Executes a SQL query.
/// </summary>
/// <param name="tableName">The name of the table.</param>
/// <param name="sqlStatement">The SQL statement to execute.</param>
/// <param name="parameters">The parameters that are referenced in the SQL statement.</param>
/// <returns>The result of the query as a list of rows.</returns>
protected IList<JObject> ExecuteQueryInternal(string tableName, string sqlStatement, Dictionary<string, object> parameters = null)
=> ExecuteQueryInternal(GetTableOrThrow(tableName), sqlStatement, parameters);
/// <summary>
/// Executes a SQL query.
/// </summary>
/// <param name="tableDefinition">The definition of the result set.</param>
/// <param name="sqlStatement">The SQL statement to execute.</param>
/// <param name="parameters">The parameters that are referenced in the SQL statement.</param>
/// <returns>The result of the query as a list of rows.</returns>
protected IList<JObject> ExecuteQueryInternal(TableDefinition tableDefinition, string sqlStatement, IDictionary<string, object> parameters = null)
{
Arguments.IsNotNull(tableDefinition, nameof(tableDefinition));
Arguments.IsNotNullOrWhitespace(sqlStatement, nameof(sqlStatement));
parameters ??= new Dictionary<string, object>();
LogSqlStatement(sqlStatement, parameters);
var rows = new List<JObject>();
using var statement = DbConnection.PrepareStatement(sqlStatement);
statement.BindParameters(parameters);
foreach (var row in statement.ExecuteQuery())
{
var obj = new JObject();
foreach (var prop in row)
{
if (tableDefinition.TryGetValue(prop.Key, out ColumnDefinition column))
{
obj[prop.Key] = column.DeserializeValue(prop.Value);
}
else
{
obj[prop.Key] = prop.Value == null ? null : JToken.FromObject(prop.Value);
}
}
rows.Add(obj);
}
return rows;
}
/// <summary>
/// Obtains the table definition for a defined table, or throws if not available.
/// </summary>
/// <param name="tableName">The name of the table.</param>
/// <returns>The table definition</returns>
/// <exception cref="InvalidOperationException">If the table is not defined.</exception>
private TableDefinition GetTableOrThrow(string tableName)
{
if (tableMap.TryGetValue(tableName, out TableDefinition table))
{
return table;
}
throw new InvalidOperationException($"Table '{tableName}' is not defined.");
}
/// <summary>
/// Logs a SQL execution to the logging service.
/// </summary>
/// <param name="sqlStatement">The SQL Statement</param>
/// <param name="parameters">The List of parameters</param>
private void LogSqlStatement(string sqlStatement, IDictionary<string, object> parameters)
{
// Early return.
if (Logger == null) return;
Logger?.LogDebug("SQL STMT: {sqlStatement}", sqlStatement);
if (parameters.Count > 0)
{
Logger?.LogDebug("SQL PARAMS: {params}", string.Join(";", parameters.ToList().Select(x => $"{x.Key}={x.Value}")));
}
}
/// <summary>
/// Dispose of the database connection.
/// </summary>
/// <param name="disposing"></param>
protected override void Dispose(bool disposing)
{
if (disposing)
{
DbConnection.Dispose();
}
}
}
}