-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLite.Session.cs
More file actions
139 lines (128 loc) · 4.21 KB
/
Copy pathSQLite.Session.cs
File metadata and controls
139 lines (128 loc) · 4.21 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
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
using System;
using System.Collections.Generic;
using System.Data;
using Microsoft.Data.Sqlite;
namespace DBAClientX;
public partial class SQLite
{
/// <summary>
/// Opens a managed SQLite session that reuses one connection for a sequence of commands.
/// </summary>
/// <param name="database">Absolute or relative path of the SQLite database file.</param>
/// <returns>A session that owns the underlying provider connection.</returns>
/// <remarks>
/// Use this for workflows that require connection-local state such as attached databases,
/// temporary tables, or explicit transaction blocks without exposing provider objects.
/// </remarks>
public virtual SQLiteSession OpenSession(string database)
{
var connectionString = BuildOperationalConnectionString(database);
SqliteConnection? connection = null;
try
{
(connection, _, _) = ResolveConnection(connectionString, useTransaction: false);
return new SQLiteSession(this, connection);
}
catch
{
connection?.Dispose();
throw;
}
}
internal int ExecuteSessionNonQuery(
SqliteConnection connection,
SqliteTransaction? transaction,
string query,
IDictionary<string, object?>? parameters)
{
ValidateCommandText(query);
try
{
return ExecuteNonQuery(connection, transaction, query, parameters);
}
catch (SqliteException ex)
{
throw new DbaQueryExecutionException("Failed to execute non-query.", query, ex);
}
}
internal object? ExecuteSessionScalar(
SqliteConnection connection,
SqliteTransaction? transaction,
string query,
IDictionary<string, object?>? parameters)
{
ValidateCommandText(query);
try
{
return ExecuteScalar(connection, transaction, query, parameters);
}
catch (SqliteException ex)
{
throw new DbaQueryExecutionException("Failed to execute scalar query.", query, ex);
}
}
internal IReadOnlyList<T> ExecuteSessionQueryAsList<T>(
SqliteConnection connection,
SqliteTransaction? transaction,
string query,
Func<IDataRecord, T> map,
IDictionary<string, object?>? parameters,
Action<IDataRecord>? initialize)
{
ValidateCommandText(query);
if (map == null)
{
throw new ArgumentNullException(nameof(map));
}
try
{
return ExecuteWithRetry(() =>
{
using var command = connection.CreateCommand();
command.Transaction = transaction;
command.CommandText = query;
AddParameters(command, parameters);
var commandTimeout = CommandTimeout;
if (commandTimeout > 0)
{
command.CommandTimeout = commandTimeout;
}
using var reader = command.ExecuteReader(CommandBehavior.Default);
initialize?.Invoke(reader);
List<T> results = new();
while (reader.Read())
{
results.Add(map(reader));
}
UpdateOutputParameters(command, parameters);
return (IReadOnlyList<T>)results;
});
}
catch (SqliteException ex)
{
throw new DbaQueryExecutionException("Failed to execute mapped query.", query, ex);
}
}
internal TResult ExecuteSessionTransaction<TResult>(
SqliteConnection connection,
Func<SQLiteSession, TResult> operation)
{
if (operation == null)
{
throw new ArgumentNullException(nameof(operation));
}
using SqliteTransaction transaction = connection.BeginTransaction();
SQLiteSession transactionSession = new(this, connection, transaction);
try
{
TResult result = operation(transactionSession);
transaction.Commit();
return result;
}
catch
{
transaction.Rollback();
throw;
}
}
}