Skip to content

Commit 732064e

Browse files
committed
Simplify SelectForUpdate command handling
1 parent 2689e7f commit 732064e

1 file changed

Lines changed: 15 additions & 155 deletions

File tree

‎src/System/Data/SQLite/SQLiteDbConnection.Adapters.cs‎

Lines changed: 15 additions & 155 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,6 @@ public DataTable Select(SQLiteCommand command, CancellationToken cancellationTok
150150
private DataTable Select(SQLiteCommand command, bool disposeSelect, CancellationToken cancellationToken)
151151
{
152152
ArgumentNullException.ThrowIfNull(command);
153-
cancellationToken.ThrowIfCancellationRequested();
154153

155154
using var adapter = disposeSelect ? CreateAdapter() : CreateAdapter(command);
156155
if (disposeSelect)
@@ -176,7 +175,7 @@ private DataTable Select(SQLiteCommand command, bool disposeSelect, Cancellation
176175
#region SelectForUpdate
177176

178177
/// <summary>
179-
/// Selects data from the specified table and creates an adapter that can update the returned data.
178+
/// Selects data from the specified table and returns the data adapter used to populate it.
180179
/// </summary>
181180
/// <param name="tableName">The name of the table.</param>
182181
/// <param name="fields">The fields to select.</param>
@@ -189,29 +188,11 @@ private DataTable Select(SQLiteCommand command, bool disposeSelect, Cancellation
189188
{
190189
cancellationToken.ThrowIfCancellationRequested();
191190

192-
return SelectForUpdate(CreateCommandSelect(tableName, fields), generateCommands: true, disposeSelect: true, cancellationToken);
193-
}
194-
195-
/// <summary>
196-
/// Selects data from the specified table and creates an adapter that can update the returned data.
197-
/// </summary>
198-
/// <param name="tableName">The name of the table.</param>
199-
/// <param name="fields">The fields to select.</param>
200-
/// <param name="generateCommands"><see langword="true"/> to generate insert, update, and delete commands; otherwise, <see langword="false"/>.</param>
201-
/// <param name="cancellationToken">A token to observe while waiting to acquire the connection lock.</param>
202-
/// <returns>
203-
/// The configured data adapter and selected data table.
204-
/// The caller is responsible for disposing the returned adapter.
205-
/// </returns>
206-
public (SQLiteDataAdapter Adapter, DataTable Table) SelectForUpdate(string tableName, IEnumerable<string> fields, bool generateCommands, CancellationToken cancellationToken = default)
207-
{
208-
cancellationToken.ThrowIfCancellationRequested();
209-
210-
return SelectForUpdate(CreateCommandSelect(tableName, fields), generateCommands, disposeSelect: true, cancellationToken);
191+
return SelectForUpdate(CreateCommandSelect(tableName, fields), disposeSelect: true, cancellationToken);
211192
}
212193

213194
/// <summary>
214-
/// Selects data from the specified table and creates an adapter that can update the returned data.
195+
/// Selects data from the specified table and returns the data adapter used to populate it.
215196
/// </summary>
216197
/// <param name="tableName">The name of the table.</param>
217198
/// <param name="fields">The fields to select.</param>
@@ -228,11 +209,11 @@ private DataTable Select(SQLiteCommand command, bool disposeSelect, Cancellation
228209
public (SQLiteDataAdapter Adapter, DataTable Table) SelectForUpdate(string tableName, IEnumerable<string> fields, string? expr, params SQLiteParameter[] parameters)
229210
#endif
230211
{
231-
return SelectForUpdate(CreateCommandSelect(tableName, fields, expr, parameters), generateCommands: true, disposeSelect: true, CancellationToken.None);
212+
return SelectForUpdate(CreateCommandSelect(tableName, fields, expr, parameters), disposeSelect: true, CancellationToken.None);
232213
}
233214

234215
/// <summary>
235-
/// Selects data from the specified table and creates an adapter that can update the returned data.
216+
/// Selects data from the specified table and returns the data adapter used to populate it.
236217
/// </summary>
237218
/// <param name="tableName">The name of the table.</param>
238219
/// <param name="fields">The fields to select.</param>
@@ -252,58 +233,11 @@ private DataTable Select(SQLiteCommand command, bool disposeSelect, Cancellation
252233
{
253234
cancellationToken.ThrowIfCancellationRequested();
254235

255-
return SelectForUpdate(CreateCommandSelect(tableName, fields, expr, parameters), generateCommands: true, disposeSelect: true, cancellationToken);
236+
return SelectForUpdate(CreateCommandSelect(tableName, fields, expr, parameters), disposeSelect: true, cancellationToken);
256237
}
257238

258239
/// <summary>
259-
/// Selects data from the specified table and creates an adapter that can update the returned data.
260-
/// </summary>
261-
/// <param name="tableName">The name of the table.</param>
262-
/// <param name="fields">The fields to select.</param>
263-
/// <param name="expr">An optional SQL expression appended to the command.</param>
264-
/// <param name="generateCommands"><see langword="true"/> to generate insert, update, and delete commands; otherwise, <see langword="false"/>.</param>
265-
/// <param name="parameters">The parameters to add to the command.</param>
266-
/// <returns>
267-
/// The configured data adapter and selected data table.
268-
/// The caller is responsible for disposing the returned adapter.
269-
/// </returns>
270-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
271-
#if NETSTANDARD2_1_OR_GREATER || NET5_0_OR_GREATER
272-
public (SQLiteDataAdapter Adapter, DataTable Table) SelectForUpdate(string tableName, IEnumerable<string> fields, string? expr, bool generateCommands, params ReadOnlySpan<SQLiteParameter> parameters)
273-
#else
274-
public (SQLiteDataAdapter Adapter, DataTable Table) SelectForUpdate(string tableName, IEnumerable<string> fields, string? expr, bool generateCommands, params SQLiteParameter[] parameters)
275-
#endif
276-
{
277-
return SelectForUpdate(CreateCommandSelect(tableName, fields, expr, parameters), generateCommands, disposeSelect: true, CancellationToken.None);
278-
}
279-
280-
/// <summary>
281-
/// Selects data from the specified table and creates an adapter that can update the returned data.
282-
/// </summary>
283-
/// <param name="tableName">The name of the table.</param>
284-
/// <param name="fields">The fields to select.</param>
285-
/// <param name="expr">An optional SQL expression appended to the command.</param>
286-
/// <param name="generateCommands"><see langword="true"/> to generate insert, update, and delete commands; otherwise, <see langword="false"/>.</param>
287-
/// <param name="cancellationToken">A token to observe while waiting to acquire the connection lock.</param>
288-
/// <param name="parameters">The parameters to add to the command.</param>
289-
/// <returns>
290-
/// The configured data adapter and selected data table.
291-
/// The caller is responsible for disposing the returned adapter.
292-
/// </returns>
293-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
294-
#if NETSTANDARD2_1_OR_GREATER || NET5_0_OR_GREATER
295-
public (SQLiteDataAdapter Adapter, DataTable Table) SelectForUpdate(string tableName, IEnumerable<string> fields, string? expr, bool generateCommands, CancellationToken cancellationToken, params ReadOnlySpan<SQLiteParameter> parameters)
296-
#else
297-
public (SQLiteDataAdapter Adapter, DataTable Table) SelectForUpdate(string tableName, IEnumerable<string> fields, string? expr, bool generateCommands, CancellationToken cancellationToken, params SQLiteParameter[] parameters)
298-
#endif
299-
{
300-
cancellationToken.ThrowIfCancellationRequested();
301-
302-
return SelectForUpdate(CreateCommandSelect(tableName, fields, expr, parameters), generateCommands, disposeSelect: true, cancellationToken);
303-
}
304-
305-
/// <summary>
306-
/// Executes the specified query and creates an adapter that can update the returned data.
240+
/// Executes the specified query and returns the data adapter used to populate the selected data.
307241
/// </summary>
308242
/// <param name="sql">The SQL statement to execute.</param>
309243
/// <param name="parameters">The parameters to add to the command.</param>
@@ -318,11 +252,11 @@ private DataTable Select(SQLiteCommand command, bool disposeSelect, Cancellation
318252
public (SQLiteDataAdapter Adapter, DataTable Table) SelectForUpdate(string sql, params SQLiteParameter[] parameters)
319253
#endif
320254
{
321-
return SelectForUpdate(CreateCommand(sql, parameters), generateCommands: true, disposeSelect: true, CancellationToken.None);
255+
return SelectForUpdate(CreateCommand(sql, parameters), disposeSelect: true, CancellationToken.None);
322256
}
323257

324258
/// <summary>
325-
/// Executes the specified query and creates an adapter that can update the returned data.
259+
/// Executes the specified query and returns the data adapter used to populate the selected data.
326260
/// </summary>
327261
/// <param name="sql">The SQL statement to execute.</param>
328262
/// <param name="cancellationToken">A token to observe while waiting to acquire the connection lock.</param>
@@ -340,54 +274,11 @@ private DataTable Select(SQLiteCommand command, bool disposeSelect, Cancellation
340274
{
341275
cancellationToken.ThrowIfCancellationRequested();
342276

343-
return SelectForUpdate(CreateCommand(sql, parameters), generateCommands: true, disposeSelect: true, cancellationToken);
344-
}
345-
346-
/// <summary>
347-
/// Executes the specified query and creates an adapter that can update the returned data.
348-
/// </summary>
349-
/// <param name="sql">The SQL statement to execute.</param>
350-
/// <param name="generateCommands"><see langword="true"/> to generate insert, update, and delete commands; otherwise, <see langword="false"/>.</param>
351-
/// <param name="parameters">The parameters to add to the command.</param>
352-
/// <returns>
353-
/// The configured data adapter and selected data table.
354-
/// The caller is responsible for disposing the returned adapter.
355-
/// </returns>
356-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
357-
#if NETSTANDARD2_1_OR_GREATER || NET5_0_OR_GREATER
358-
public (SQLiteDataAdapter Adapter, DataTable Table) SelectForUpdate(string sql, bool generateCommands, params ReadOnlySpan<SQLiteParameter> parameters)
359-
#else
360-
public (SQLiteDataAdapter Adapter, DataTable Table) SelectForUpdate(string sql, bool generateCommands, params SQLiteParameter[] parameters)
361-
#endif
362-
{
363-
return SelectForUpdate(CreateCommand(sql, parameters), generateCommands, disposeSelect: true, CancellationToken.None);
364-
}
365-
366-
/// <summary>
367-
/// Executes the specified query and creates an adapter that can update the returned data.
368-
/// </summary>
369-
/// <param name="sql">The SQL statement to execute.</param>
370-
/// <param name="generateCommands"><see langword="true"/> to generate insert, update, and delete commands; otherwise, <see langword="false"/>.</param>
371-
/// <param name="cancellationToken">A token to observe while waiting to acquire the connection lock.</param>
372-
/// <param name="parameters">The parameters to add to the command.</param>
373-
/// <returns>
374-
/// The configured data adapter and selected data table.
375-
/// The caller is responsible for disposing the returned adapter.
376-
/// </returns>
377-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
378-
#if NETSTANDARD2_1_OR_GREATER || NET5_0_OR_GREATER
379-
public (SQLiteDataAdapter Adapter, DataTable Table) SelectForUpdate(string sql, bool generateCommands, CancellationToken cancellationToken, params ReadOnlySpan<SQLiteParameter> parameters)
380-
#else
381-
public (SQLiteDataAdapter Adapter, DataTable Table) SelectForUpdate(string sql, bool generateCommands, CancellationToken cancellationToken, params SQLiteParameter[] parameters)
382-
#endif
383-
{
384-
cancellationToken.ThrowIfCancellationRequested();
385-
386-
return SelectForUpdate(CreateCommand(sql, parameters), generateCommands, disposeSelect: true, cancellationToken);
277+
return SelectForUpdate(CreateCommand(sql, parameters), disposeSelect: true, cancellationToken);
387278
}
388279

389280
/// <summary>
390-
/// Executes the specified command and creates an adapter that can update the returned data.
281+
/// Executes the specified command and returns the data adapter used to populate the selected data.
391282
/// </summary>
392283
/// <param name="command">
393284
/// The SQLite command to execute. The caller retains ownership of the command.
@@ -399,33 +290,14 @@ private DataTable Select(SQLiteCommand command, bool disposeSelect, Cancellation
399290
/// Disposing the adapter does not dispose <paramref name="command"/>.
400291
/// </returns>
401292
[MethodImpl(MethodImplOptions.AggressiveInlining)]
402-
public (SQLiteDataAdapter Adapter, DataTable Table) SelectForUpdate(SQLiteCommand command, CancellationToken cancellationToken)
293+
public (SQLiteDataAdapter Adapter, DataTable Table) SelectForUpdate(SQLiteCommand command, CancellationToken cancellationToken = default)
403294
{
404-
return SelectForUpdate(command, generateCommands: true, disposeSelect: false, cancellationToken);
295+
return SelectForUpdate(command, disposeSelect: false, cancellationToken);
405296
}
406297

407-
/// <summary>
408-
/// Executes the specified command and creates an adapter that can update the returned data.
409-
/// </summary>
410-
/// <param name="command">
411-
/// The SQLite command to execute. The caller retains ownership of the command.
412-
/// </param>
413-
/// <param name="generateCommands"><see langword="true"/> to generate insert, update, and delete commands; otherwise, <see langword="false"/>.</param>
414-
/// <param name="cancellationToken">A token to observe while waiting to acquire the connection lock.</param>
415-
/// <returns>
416-
/// The configured data adapter and selected data table.
417-
/// The caller is responsible for disposing the returned adapter.
418-
/// Disposing the adapter does not dispose <paramref name="command"/>.
419-
/// </returns>
420-
public (SQLiteDataAdapter Adapter, DataTable Table) SelectForUpdate(SQLiteCommand command, bool generateCommands = true, CancellationToken cancellationToken = default)
421-
{
422-
return SelectForUpdate(command, generateCommands, disposeSelect: false, cancellationToken);
423-
}
424-
425-
private (SQLiteDataAdapter Adapter, DataTable Table) SelectForUpdate(SQLiteCommand command, bool generateCommands, bool disposeSelect, CancellationToken cancellationToken)
298+
private (SQLiteDataAdapter Adapter, DataTable Table) SelectForUpdate(SQLiteCommand command, bool disposeSelect, CancellationToken cancellationToken)
426299
{
427300
ArgumentNullException.ThrowIfNull(command);
428-
cancellationToken.ThrowIfCancellationRequested();
429301

430302
var adapter = disposeSelect ? CreateAdapter() : CreateAdapter(command);
431303
if (disposeSelect)
@@ -437,18 +309,6 @@ private DataTable Select(SQLiteCommand command, bool disposeSelect, Cancellation
437309
{
438310
var table = new DataTable();
439311
Fill(adapter, table, cancellationToken);
440-
if (generateCommands)
441-
{
442-
var cmdBuilder = new SQLiteCommandBuilder(adapter);
443-
#if DEBUG_
444-
var insertCommand = cmdBuilder.GetInsertCommand();
445-
var updateCommand = cmdBuilder.GetUpdateCommand();
446-
var deleteCommand = cmdBuilder.GetDeleteCommand();
447-
insertCommand?.Dispose();
448-
updateCommand?.Dispose();
449-
deleteCommand?.Dispose();
450-
#endif
451-
}
452312
return (adapter, table);
453313
}
454314
catch (Exception ex)
@@ -500,4 +360,4 @@ public int Update(SQLiteDataAdapter adapter, DataTable table, CancellationToken
500360
}
501361

502362
#endregion
503-
}
363+
}

0 commit comments

Comments
 (0)