-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathJdbcCommand.cs
More file actions
304 lines (245 loc) · 9.21 KB
/
JdbcCommand.cs
File metadata and controls
304 lines (245 loc) · 9.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
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
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Grpc.Core;
using JDBC.NET.Data.Exceptions;
using JDBC.NET.Data.Utilities;
using JDBC.NET.Proto;
namespace JDBC.NET.Data
{
public class JdbcCommand : DbCommand
{
#region Fields
private bool _isDisposed;
private JdbcDataReader _dataReader;
private JdbcTransaction _dbTransaction;
#endregion
#region Properties
public int FetchSize { get; set; }
public override string CommandText { get; set; }
public override int CommandTimeout { get; set; }
public override CommandType CommandType { get; set; }
public override UpdateRowSource UpdatedRowSource
{
get => throw new NotSupportedException();
set => throw new NotSupportedException();
}
protected override DbConnection DbConnection { get; set; }
protected override DbParameterCollection DbParameterCollection => Parameters;
public new JdbcParameterCollection Parameters { get; } = new();
protected override DbTransaction DbTransaction
{
get => _dbTransaction;
set
{
if (value is not JdbcTransaction jdbcTransaction)
throw new InvalidOperationException();
if (Connection is not JdbcConnection jdbcConnection)
throw new InvalidOperationException();
if (jdbcConnection.CurrentTransaction != jdbcTransaction)
throw new InvalidDataException("The transaction associated with this command is not the connection's active transaction.");
_dbTransaction = jdbcTransaction;
}
}
public override bool DesignTimeVisible
{
get => false;
set => throw new NotSupportedException();
}
private bool IsPrepared { get; set; }
private bool IsStatementCreated => StatementId is not null;
private string StatementId { get; set; }
#endregion
#region Constructor
internal JdbcCommand(JdbcConnection connection)
{
Connection = connection;
FetchSize = connection.ConnectionStringBuilder.FetchSize;
}
#endregion
#region Public Methods
public override void Prepare()
{
IsPrepared = true;
}
public override int ExecuteNonQuery()
{
try
{
return ExecuteNonQueryAsync().Result;
}
catch (AggregateException e) when (e.InnerExceptions.Count == 1)
{
throw e.InnerExceptions[0];
}
}
public override async Task<int> ExecuteNonQueryAsync(CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
cancellationToken.Register(Cancel);
await using var dbDataReader = await ExecuteDbDataReaderAsync(CommandBehavior.Default, cancellationToken).ConfigureAwait(false);
while (await dbDataReader.NextResultAsync(cancellationToken).ConfigureAwait(false))
{
}
return dbDataReader.RecordsAffected;
}
public override object ExecuteScalar()
{
try
{
return ExecuteScalarAsync(CancellationToken.None).Result;
}
catch (AggregateException e) when (e.InnerExceptions.Count == 1)
{
throw e.InnerExceptions[0];
}
}
public override async Task<object> ExecuteScalarAsync(CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
cancellationToken.Register(Cancel);
object result = null;
await using var dbDataReader = await ExecuteDbDataReaderAsync(CommandBehavior.Default, cancellationToken).ConfigureAwait(false);
if (await dbDataReader.ReadAsync(cancellationToken).ConfigureAwait(false) && dbDataReader.FieldCount > 0)
{
result = dbDataReader.GetValue(0);
}
return result;
}
protected override DbParameter CreateDbParameter()
{
return new JdbcParameter();
}
protected override DbDataReader ExecuteDbDataReader(CommandBehavior behavior)
{
try
{
return ExecuteDbDataReaderAsync(behavior, CancellationToken.None).Result;
}
catch (AggregateException e) when (e.InnerExceptions.Count == 1)
{
throw e.InnerExceptions[0];
}
}
protected override async Task<DbDataReader> ExecuteDbDataReaderAsync(CommandBehavior behavior, CancellationToken cancellationToken)
{
if (Connection is not JdbcConnection jdbcConnection)
throw new InvalidOperationException();
if (_dataReader?.IsClosed == false)
throw new InvalidOperationException("The previously executed DataReader has not been closed yet.");
CreateStatement();
var response = await jdbcConnection.Bridge.Statement.executeStatementAsync(
new ExecuteStatementRequest
{
StatementId = StatementId,
FetchSize = FetchSize,
Sql = IsPrepared ? string.Empty : CommandText
},
cancellationToken: cancellationToken
);
_dataReader = new JdbcDataReader(this, response);
return _dataReader;
}
public override void Cancel()
{
if (Connection is not JdbcConnection jdbcConnection)
throw new InvalidOperationException();
try
{
jdbcConnection.Bridge.Statement.cancelStatement(new CancelStatementRequest
{
StatementId = StatementId
});
}
catch (RpcException ex)
{
throw new JdbcException(ex);
}
}
#endregion
#region Private Methods
private void CreateStatement()
{
if (IsPrepared)
{
CreatePreparedStatement();
return;
}
if (Parameters.Count > 0)
{
CreatePreparedStatement();
return;
}
CreateRawStatement();
}
private void CreatePreparedStatement()
{
if (Connection is not JdbcConnection jdbcConnection)
throw new InvalidOperationException();
CloseStatement();
List<JdbcParameter> orderedParameters = Parameters
.OfType<JdbcParameter>()
.OrderBy(x => CommandText.IndexOf(x.ParameterName, StringComparison.Ordinal))
.ToList();
var response = jdbcConnection.Bridge.Statement.prepareStatement(new PrepareStatementRequest
{
ConnectionId = jdbcConnection.ConnectionId,
Sql = orderedParameters.Aggregate(CommandText, (x, parameter) => x.Replace(parameter.ParameterName, "?"))
});
StatementId = response.StatementId;
for (var i = 0; i < orderedParameters.Count; i++)
{
var parameter = orderedParameters[i];
jdbcConnection.Bridge.Statement.setParameter(new SetParameterRequest
{
StatementId = StatementId,
Index = i + 1,
Value = parameter.Value.ToString(),
Type = ParameterTypeUtility.Convert(parameter.DbType)
});
}
}
private void CreateRawStatement()
{
if (Connection is not JdbcConnection jdbcConnection)
throw new InvalidOperationException();
CloseStatement();
var response = jdbcConnection.Bridge.Statement.createStatement(new CreateStatementRequest
{
ConnectionId = jdbcConnection.ConnectionId,
});
StatementId = response.StatementId;
}
private void CloseStatement()
{
if (!IsStatementCreated)
return;
if (Connection is not JdbcConnection jdbcConnection)
throw new InvalidOperationException();
if (Connection.State is not ConnectionState.Closed)
{
jdbcConnection.Bridge.Statement.closeStatement(new CloseStatementRequest
{
StatementId = StatementId
});
}
StatementId = null;
}
#endregion
#region IDisposable
protected override void Dispose(bool disposing)
{
if (_isDisposed)
return;
CloseStatement();
_isDisposed = true;
base.Dispose(disposing);
}
#endregion
}
}