This repository has been archived by the owner on Nov 1, 2018. It is now read-only.
/
SqliteCommand.cs
441 lines (386 loc) · 18.6 KB
/
SqliteCommand.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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Data.Sqlite.Properties;
using SQLitePCL;
namespace Microsoft.Data.Sqlite
{
/// <summary>
/// Represents a SQL statement to be executed against a SQLite database.
/// </summary>
public class SqliteCommand : DbCommand
{
private readonly Lazy<SqliteParameterCollection> _parameters = new Lazy<SqliteParameterCollection>(
() => new SqliteParameterCollection());
/// <summary>
/// Initializes a new instance of the <see cref="SqliteCommand" /> class.
/// </summary>
public SqliteCommand()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="SqliteCommand" /> class.
/// </summary>
/// <param name="commandText">The SQL to execute against the database.</param>
public SqliteCommand(string commandText)
=> CommandText = commandText;
/// <summary>
/// Initializes a new instance of the <see cref="SqliteCommand" /> class.
/// </summary>
/// <param name="commandText">The SQL to execute against the database.</param>
/// <param name="connection">The connection used by the command.</param>
public SqliteCommand(string commandText, SqliteConnection connection)
: this(commandText)
=> Connection = connection;
/// <summary>
/// Initializes a new instance of the <see cref="SqliteCommand" /> class.
/// </summary>
/// <param name="commandText">The SQL to execute against the database.</param>
/// <param name="connection">The connection used by the command.</param>
/// <param name="transaction">The transaction within which the command executes.</param>
public SqliteCommand(string commandText, SqliteConnection connection, SqliteTransaction transaction)
: this(commandText, connection)
=> Transaction = transaction;
/// <summary>
/// Gets or sets a value indicating how <see cref="CommandText" /> is interpreted. Only
/// <see cref="CommandType.Text" /> is supported.
/// </summary>
/// <value>A value indicating how <see cref="CommandText" /> is interpreted.</value>
public override CommandType CommandType
{
get => CommandType.Text;
set
{
if (value != CommandType.Text)
{
throw new ArgumentException(Resources.InvalidCommandType(value));
}
}
}
/// <summary>
/// Gets or sets the SQL to execute against the database.
/// </summary>
/// <value>The SQL to execute against the database.</value>
public override string CommandText { get; set; }
/// <summary>
/// Gets or sets the connection used by the command.
/// </summary>
/// <value>The connection used by the command.</value>
public new virtual SqliteConnection Connection { get; set; }
/// <summary>
/// Gets or sets the connection used by the command. Must be a <see cref="SqliteConnection" />.
/// </summary>
/// <value>The connection used by the command.</value>
protected override DbConnection DbConnection
{
get => Connection;
set => Connection = (SqliteConnection)value;
}
/// <summary>
/// Gets or sets the transaction within which the command executes.
/// </summary>
/// <value>The transaction within which the command executes.</value>
public new virtual SqliteTransaction Transaction { get; set; }
/// <summary>
/// Gets or sets the transaction within which the command executes. Must be a <see cref="SqliteTransaction" />.
/// </summary>
/// <value>The transaction within which the command executes.</value>
protected override DbTransaction DbTransaction
{
get => Transaction;
set => Transaction = (SqliteTransaction)value;
}
/// <summary>
/// Gets the collection of parameters used by the command.
/// </summary>
/// <value>The collection of parameters used by the command.</value>
public new virtual SqliteParameterCollection Parameters
=> _parameters.Value;
/// <summary>
/// Gets the collection of parameters used by the command.
/// </summary>
/// <value>The collection of parameters used by the command.</value>
protected override DbParameterCollection DbParameterCollection
=> Parameters;
/// <summary>
/// Gets or sets the number of seconds to wait before terminating the attempt to execute the command. Defaults to 30.
/// </summary>
/// <value>The number of seconds to wait before terminating the attempt to execute the command.</value>
/// <remarks>
/// The timeout is used when the command is waiting to obtain a lock on the table.
/// </remarks>
public override int CommandTimeout { get; set; } = 30;
/// <summary>
/// Gets or sets a value indicating whether the command should be visible in an interface control.
/// </summary>
/// <value>A value indicating whether the command should be visible in an interface control.</value>
public override bool DesignTimeVisible { get; set; }
/// <summary>
/// Gets or sets a value indicating how the results are applied to the row being updated.
/// </summary>
/// <value>A value indicating how the results are applied to the row being updated.</value>
public override UpdateRowSource UpdatedRowSource { get; set; }
/// <summary>
/// Creates a new parameter.
/// </summary>
/// <returns>The new parameter.</returns>
public new virtual SqliteParameter CreateParameter()
=> new SqliteParameter();
/// <summary>
/// Creates a new parameter.
/// </summary>
/// <returns>The new parameter.</returns>
protected override DbParameter CreateDbParameter()
=> CreateParameter();
/// <summary>
/// Creates a prepared version of the command on the database. This has no effect.
/// </summary>
public override void Prepare()
{
}
/// <summary>
/// Executes the <see cref="CommandText" /> against the database and returns a data reader.
/// </summary>
/// <returns>The data reader.</returns>
/// <exception cref="SqliteException">A SQLite error occurs during execution.</exception>
public new virtual SqliteDataReader ExecuteReader()
=> ExecuteReader(CommandBehavior.Default);
/// <summary>
/// Executes the <see cref="CommandText" /> against the database and returns a data reader.
/// </summary>
/// <param name="behavior">
/// A description of the results of the query and its effect on the database.
/// <para>Only <see cref="CommandBehavior.Default" />, <see cref="CommandBehavior.SequentialAccess" />,
/// <see cref="CommandBehavior.SingleResult" />, <see cref="CommandBehavior.SingleRow" />, and
/// <see cref="CommandBehavior.CloseConnection" /> are supported.</para>
/// </param>
/// <returns>The data reader.</returns>
/// <exception cref="SqliteException">A SQLite error occurs during execution.</exception>
public new virtual SqliteDataReader ExecuteReader(CommandBehavior behavior)
{
if ((behavior & ~(CommandBehavior.Default | CommandBehavior.SequentialAccess | CommandBehavior.SingleResult
| CommandBehavior.SingleRow | CommandBehavior.CloseConnection)) != 0)
{
throw new ArgumentException(Resources.InvalidCommandBehavior(behavior));
}
if (Connection?.State != ConnectionState.Open)
{
throw new InvalidOperationException(Resources.CallRequiresOpenConnection(nameof(ExecuteReader)));
}
if (string.IsNullOrEmpty(CommandText))
{
throw new InvalidOperationException(Resources.CallRequiresSetCommandText(nameof(ExecuteReader)));
}
if (Transaction != Connection.Transaction)
{
throw new InvalidOperationException(
Transaction == null
? Resources.TransactionRequired
: Resources.TransactionConnectionMismatch);
}
// This is not a guarantee. SQLITE_BUSY can still be thrown before the command timeout.
// This sets a timeout handler but this can be cleared by concurrent commands.
raw.sqlite3_busy_timeout(Connection.Handle, CommandTimeout * 1000);
var hasChanges = false;
var changes = 0;
var stmts = new Queue<(sqlite3_stmt, bool)>();
var tail = CommandText;
do
{
var rc = raw.sqlite3_prepare_v2(
Connection.Handle,
tail,
out var stmt,
out tail);
SqliteException.ThrowExceptionForRC(rc, Connection.Handle);
// Statement was empty, white space, or a comment
if (stmt.ptr == IntPtr.Zero)
{
if (!string.IsNullOrEmpty(tail))
{
continue;
}
break;
}
var boundParams = 0;
if (_parameters.IsValueCreated)
{
boundParams = _parameters.Value.Bind(stmt);
}
var expectedParams = raw.sqlite3_bind_parameter_count(stmt);
if (expectedParams != boundParams)
{
var unboundParams = new List<string>();
for (var i = 1; i <= expectedParams; i++)
{
var name = raw.sqlite3_bind_parameter_name(stmt, i);
if (_parameters.IsValueCreated
||
!_parameters.Value.Cast<SqliteParameter>().Any(p => p.ParameterName == name))
{
unboundParams.Add(name);
}
}
throw new InvalidOperationException(Resources.MissingParameters(string.Join(", ", unboundParams)));
}
try
{
var timer = Stopwatch.StartNew();
while (raw.SQLITE_LOCKED == (rc = raw.sqlite3_step(stmt)) || rc == raw.SQLITE_BUSY)
{
if (timer.ElapsedMilliseconds >= CommandTimeout * 1000)
{
break;
}
raw.sqlite3_reset(stmt);
#if NET451
// TODO: Consider having an async path that uses Task.Delay()
Thread.Sleep(150);
#endif
}
SqliteException.ThrowExceptionForRC(rc, Connection.Handle);
}
catch
{
stmt.Dispose();
throw;
}
// NB: This is only a heuristic to separate SELECT statements from INSERT/UPDATE/DELETE statements. It
// will result in unexpected corner cases, but it's the best we can do without re-parsing SQL
if (raw.sqlite3_stmt_readonly(stmt) != 0)
{
stmts.Enqueue((stmt, rc != raw.SQLITE_DONE));
}
else
{
hasChanges = true;
changes += raw.sqlite3_changes(Connection.Handle);
stmt.Dispose();
}
}
while (!string.IsNullOrEmpty(tail));
var closeConnection = (behavior & CommandBehavior.CloseConnection) != 0;
return new SqliteDataReader(Connection, stmts, hasChanges ? changes : -1, closeConnection);
}
/// <summary>
/// Executes the <see cref="CommandText" /> against the database and returns a data reader.
/// </summary>
/// <param name="behavior">A description of query's results and its effect on the database.</param>
/// <returns>The data reader.</returns>
protected override DbDataReader ExecuteDbDataReader(CommandBehavior behavior)
=> ExecuteReader(behavior);
/// <summary>
/// Executes the <see cref="CommandText" /> asynchronously against the database and returns a data reader.
/// </summary>
/// <returns>A task representing the asynchronous operation.</returns>
/// <remarks>
/// SQLite does not support asynchronous execution. Use write-ahead logging instead.
/// </remarks>
/// <seealso href="http://sqlite.org/wal.html">Write-Ahead Logging</seealso>
public new virtual Task<SqliteDataReader> ExecuteReaderAsync()
=> ExecuteReaderAsync(CommandBehavior.Default, CancellationToken.None);
/// <summary>
/// Executes the <see cref="CommandText" /> asynchronously against the database and returns a data reader.
/// </summary>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <returns>A task representing the asynchronous operation.</returns>
/// <remarks>
/// SQLite does not support asynchronous execution. Use write-ahead logging instead.
/// </remarks>
/// <seealso href="http://sqlite.org/wal.html">Write-Ahead Logging</seealso>
public new virtual Task<SqliteDataReader> ExecuteReaderAsync(CancellationToken cancellationToken)
=> ExecuteReaderAsync(CommandBehavior.Default, cancellationToken);
/// <summary>
/// Executes the <see cref="CommandText" /> asynchronously against the database and returns a data reader.
/// </summary>
/// <param name="behavior">A description of query's results and its effect on the database.</param>
/// <returns>A task representing the asynchronous operation.</returns>
/// <remarks>
/// SQLite does not support asynchronous execution. Use write-ahead logging instead.
/// </remarks>
/// <seealso href="http://sqlite.org/wal.html">Write-Ahead Logging</seealso>
public new virtual Task<SqliteDataReader> ExecuteReaderAsync(CommandBehavior behavior)
=> ExecuteReaderAsync(behavior, CancellationToken.None);
/// <summary>
/// Executes the <see cref="CommandText" /> asynchronously against the database and returns a data reader.
/// </summary>
/// <param name="behavior">A description of query's results and its effect on the database.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <returns>A task representing the asynchronous operation.</returns>
/// <remarks>
/// SQLite does not support asynchronous execution. Use write-ahead logging instead.
/// </remarks>
/// <seealso href="http://sqlite.org/wal.html">Write-Ahead Logging</seealso>
public new virtual Task<SqliteDataReader> ExecuteReaderAsync(
CommandBehavior behavior,
CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
return Task.FromResult(ExecuteReader(behavior));
}
/// <summary>
/// Executes the <see cref="CommandText" /> asynchronously against the database and returns a data reader.
/// </summary>
/// <param name="behavior">A description of query's results and its effect on the database.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <returns>A task representing the asynchronous operation.</returns>
protected override async Task<DbDataReader> ExecuteDbDataReaderAsync(
CommandBehavior behavior,
CancellationToken cancellationToken)
=> await ExecuteReaderAsync(behavior, cancellationToken);
/// <summary>
/// Executes the <see cref="CommandText" /> against the database.
/// </summary>
/// <returns>The number of rows inserted, updated, or deleted. -1 for SELECT statements.</returns>
/// <exception cref="SqliteException">A SQLite error occurs during execution.</exception>
public override int ExecuteNonQuery()
{
if (Connection?.State != ConnectionState.Open)
{
throw new InvalidOperationException(Resources.CallRequiresOpenConnection(nameof(ExecuteNonQuery)));
}
if (CommandText == null)
{
throw new InvalidOperationException(Resources.CallRequiresSetCommandText(nameof(ExecuteNonQuery)));
}
var reader = ExecuteReader();
reader.Dispose();
return reader.RecordsAffected;
}
/// <summary>
/// Executes the <see cref="CommandText" /> against the database and returns the result.
/// </summary>
/// <returns>The first column of the first row of the results, or null if no results.</returns>
/// <exception cref="SqliteException">A SQLite error occurs during execution.</exception>
public override object ExecuteScalar()
{
if (Connection?.State != ConnectionState.Open)
{
throw new InvalidOperationException(Resources.CallRequiresOpenConnection(nameof(ExecuteScalar)));
}
if (CommandText == null)
{
throw new InvalidOperationException(Resources.CallRequiresSetCommandText(nameof(ExecuteScalar)));
}
using (var reader = ExecuteReader())
{
return reader.Read()
? reader.GetValue(0)
: null;
}
}
/// <summary>
/// Attempts to cancel the execution of the command. Does nothing.
/// </summary>
public override void Cancel()
{
}
}
}