Repository navigation
Expand file tree
/
Copy pathSQLite.TableCopyAdapter.Reliability.cs
More file actions
68 lines (61 loc) · 3.86 KB
/
Copy pathSQLite.TableCopyAdapter.Reliability.cs
File metadata and controls
68 lines (61 loc) · 3.86 KB
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
using System.Data;
using System.Data.Common;
using DBAClientX.DataMovement;
using Microsoft.Data.Sqlite;
namespace DBAClientX;
public sealed partial class SQLiteTableCopyAdapter
{
/// <inheritdoc />
public override bool SupportsAtomicCheckpoints => true;
/// <inheritdoc />
protected override DbConnection CreateCheckpointConnection() => CreateConnection();
/// <inheritdoc />
protected override Task OpenCheckpointConnectionAsync(DbConnection connection, CancellationToken cancellationToken)
=> OpenConnectionAsync((SqliteConnection)connection, cancellationToken);
/// <inheritdoc />
protected override async Task<string> ResolveCheckpointTableIdentityAsync(DbConnection connection, DbTransaction? transaction, DbaTableCopyDefinition definition, CancellationToken cancellationToken)
{
string[] segments = DbaIdentifierPath.SplitSegments(definition.DestinationName).Select(DbaIdentifierPath.UnquoteSegment).ToArray();
if (segments.Length is < 1 or > 2) throw new ArgumentException("SQLite checkpoint destinations require a table name with an optional schema.", nameof(definition));
string schema = segments.Length == 2 ? segments[0] : "main";
using DbCommand command = connection.CreateCommand();
command.Transaction = transaction;
command.CommandTimeout = CommandTimeout;
command.CommandText = $"SELECT name FROM {QuotePath(schema)}.sqlite_master WHERE type='table' AND name=@name COLLATE NOCASE";
command.Parameters.Add(new SqliteParameter("@name", segments[segments.Length - 1]));
object? name = await command.ExecuteScalarAsync(cancellationToken).ConfigureAwait(false);
if (name is not string actualName) throw new InvalidOperationException($"Checkpoint destination '{definition.DestinationName}' does not exist.");
return DbaIdentifierPath.NormalizeSqliteIdentifier(schema) + ":" + actualName;
}
/// <inheritdoc />
protected override async Task<DataTable> ExecuteBoundedPageCoreAsync(string query, IReadOnlyDictionary<string, object?> parameters, long? maxBytes, CancellationToken cancellationToken)
{
using SqliteConnection? owned = _readConnection == null
? CreateConnection()
: null;
SqliteConnection connection = _readConnection ?? owned!;
if (owned != null)
{
await OpenConnectionAsync(connection, cancellationToken).ConfigureAwait(false);
}
using SqliteCommand command = connection.CreateCommand();
command.Transaction = _readTransaction;
command.CommandText = query;
command.CommandTimeout = CommandTimeout;
foreach (var parameter in parameters) command.Parameters.AddWithValue(parameter.Key, parameter.Value ?? DBNull.Value);
// SqliteCommand.Cancel does nothing; interrupting the connection stops a page query that is running.
return await RunInterruptibleAsync(connection, async () =>
{
using SqliteDataReader reader = await command.ExecuteReaderAsync(cancellationToken).ConfigureAwait(false);
var fields = new SqliteCopyFieldReader(reader);
return await DbaTableCopyPageReader.ReadAsync(reader, maxBytes, fields.GetPayloadBytes, fields.ReadValue, cancellationToken).ConfigureAwait(false);
}, cancellationToken).ConfigureAwait(false);
}
/// <inheritdoc />
protected override async Task WriteTransactionalPageAsync(DbConnection connection, DbTransaction transaction, DbaTableCopyDefinition definition, DataTable page, DbaTableCopyOptions options, CancellationToken cancellationToken)
{
using var sqlite = CreateClient();
await sqlite.WriteBulkRowsAsync((SqliteConnection)connection, (SqliteTransaction)transaction, page,
NormalizeSQLiteBulkDestinationTableName(definition.DestinationName), options.BatchSize, cancellationToken).ConfigureAwait(false);
}
}