-
-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathFesDatabase.cs
More file actions
437 lines (331 loc) · 11.4 KB
/
FesDatabase.cs
File metadata and controls
437 lines (331 loc) · 11.4 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
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
/*
* The contents of this file are subject to the Initial
* Developer's Public License Version 1.0 (the "License");
* you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
* https://github.com/FirebirdSQL/NETProvider/raw/master/license.txt.
*
* Software distributed under the License is distributed on
* an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
* express or implied. See the License for the specific
* language governing rights and limitations under the License.
*
* All Rights Reserved.
*/
//$Authors = Carlos Guzman Alvarez, Jiri Cincura (jiri@cincura.net)
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using FirebirdSql.Data.Client.Native.Handles;
using FirebirdSql.Data.Common;
namespace FirebirdSql.Data.Client.Native;
internal sealed class FesDatabase : DatabaseBase
{
#region Fields
private static readonly Version Version25 = new Version(2, 5);
private readonly IFbClient _fbClient;
private readonly Version _fbClientVersion;
private DatabaseHandle _handle;
private IntPtr[] _statusVector;
#endregion
#region Properties
public override bool UseUtf8ParameterBuffer => _fbClientVersion >= Version25;
public override int Handle => _handle.DangerousGetHandle().AsInt();
public override bool HasRemoteEventSupport => false;
public override bool ConnectionBroken => false;
public IFbClient FbClient => _fbClient;
public Version FbClientVersion => _fbClientVersion;
public DatabaseHandle HandlePtr => _handle;
#endregion
#region Constructors
public FesDatabase(string dllName, Charset charset, int packetSize, int blobSegmentSize, short dialect)
: base(charset, packetSize, blobSegmentSize, dialect)
{
_fbClient = FbClientFactory.Create(dllName);
_fbClientVersion = FesConnection.GetClientVersion(_fbClient);
_handle = new DatabaseHandle();
_statusVector = new IntPtr[IscCodes.ISC_STATUS_LENGTH];
}
#endregion
#region Database Methods
public override void CreateDatabase(DatabaseParameterBufferBase dpb, string database, byte[] cryptKey)
{
CheckCryptKeyForSupport(cryptKey);
var databaseBuffer = dpb.Encoding.GetBytes(database);
StatusVectorHelper.ClearStatusVector(_statusVector);
_fbClient.isc_create_database(
_statusVector,
(short)databaseBuffer.Length,
databaseBuffer,
ref _handle,
dpb.Length,
dpb.ToArray(),
0);
ProcessStatusVector(Charset.DefaultCharset);
}
public override ValueTask CreateDatabaseAsync(DatabaseParameterBufferBase dpb, string database, byte[] cryptKey, CancellationToken cancellationToken = default)
{
CheckCryptKeyForSupport(cryptKey);
var databaseBuffer = dpb.Encoding.GetBytes(database);
StatusVectorHelper.ClearStatusVector(_statusVector);
_fbClient.isc_create_database(
_statusVector,
(short)databaseBuffer.Length,
databaseBuffer,
ref _handle,
dpb.Length,
dpb.ToArray(),
0);
ProcessStatusVector(Charset.DefaultCharset);
return ValueTask.CompletedTask;
}
public override void CreateDatabaseWithTrustedAuth(DatabaseParameterBufferBase dpb, string database, byte[] cryptKey)
{
throw new NotSupportedException("Trusted Auth isn't supported on Firebird Embedded.");
}
public override ValueTask CreateDatabaseWithTrustedAuthAsync(DatabaseParameterBufferBase dpb, string database, byte[] cryptKey, CancellationToken cancellationToken = default)
{
throw new NotSupportedException("Trusted Auth isn't supported on Firebird Embedded.");
}
public override void DropDatabase()
{
StatusVectorHelper.ClearStatusVector(_statusVector);
_fbClient.isc_drop_database(_statusVector, ref _handle);
ProcessStatusVector();
_handle.Dispose();
}
public override ValueTask DropDatabaseAsync(CancellationToken cancellationToken = default)
{
StatusVectorHelper.ClearStatusVector(_statusVector);
_fbClient.isc_drop_database(_statusVector, ref _handle);
ProcessStatusVector();
_handle.Dispose();
return ValueTask.CompletedTask;
}
#endregion
#region Remote Events Methods
public override void CloseEventManager()
{
throw new NotSupportedException();
}
public override ValueTask CloseEventManagerAsync(CancellationToken cancellationToken = default)
{
throw new NotSupportedException();
}
public override void QueueEvents(RemoteEvent events)
{
throw new NotSupportedException();
}
public override ValueTask QueueEventsAsync(RemoteEvent events, CancellationToken cancellationToken = default)
{
throw new NotSupportedException();
}
public override void CancelEvents(RemoteEvent events)
{
throw new NotSupportedException();
}
public override ValueTask CancelEventsAsync(RemoteEvent events, CancellationToken cancellationToken = default)
{
throw new NotSupportedException();
}
#endregion
#region Methods
public override void Attach(DatabaseParameterBufferBase dpb, string database, byte[] cryptKey)
{
CheckCryptKeyForSupport(cryptKey);
var databaseBuffer = dpb.Encoding.GetBytes(database);
StatusVectorHelper.ClearStatusVector(_statusVector);
_fbClient.isc_attach_database(
_statusVector,
(short)databaseBuffer.Length,
databaseBuffer,
ref _handle,
dpb.Length,
dpb.ToArray());
ProcessStatusVector(Charset.DefaultCharset);
ServerVersion = GetServerVersion();
}
public override async ValueTask AttachAsync(DatabaseParameterBufferBase dpb, string database, byte[] cryptKey, CancellationToken cancellationToken = default)
{
CheckCryptKeyForSupport(cryptKey);
var databaseBuffer = dpb.Encoding.GetBytes(database);
StatusVectorHelper.ClearStatusVector(_statusVector);
_fbClient.isc_attach_database(
_statusVector,
(short)databaseBuffer.Length,
databaseBuffer,
ref _handle,
dpb.Length,
dpb.ToArray());
ProcessStatusVector(Charset.DefaultCharset);
ServerVersion = await GetServerVersionAsync(cancellationToken).ConfigureAwait(false);
}
public override void AttachWithTrustedAuth(DatabaseParameterBufferBase dpb, string database, byte[] cryptKey)
{
throw new NotSupportedException("Trusted Auth isn't supported on Firebird Embedded.");
}
public override ValueTask AttachWithTrustedAuthAsync(DatabaseParameterBufferBase dpb, string database, byte[] cryptKey, CancellationToken cancellationToken = default)
{
throw new NotSupportedException("Trusted Auth isn't supported on Firebird Embedded.");
}
public override void Detach()
{
if (TransactionCount > 0)
{
throw IscException.ForErrorCodeIntParam(IscCodes.isc_open_trans, TransactionCount);
}
if (!_handle.IsInvalid)
{
StatusVectorHelper.ClearStatusVector(_statusVector);
_fbClient.isc_detach_database(_statusVector, ref _handle);
ProcessStatusVector();
_handle.Dispose();
}
WarningMessage = null;
ServerVersion = null;
_statusVector = null;
TransactionCount = 0;
}
public override ValueTask DetachAsync(CancellationToken cancellationToken = default)
{
if (TransactionCount > 0)
{
throw IscException.ForErrorCodeIntParam(IscCodes.isc_open_trans, TransactionCount);
}
if (!_handle.IsInvalid)
{
StatusVectorHelper.ClearStatusVector(_statusVector);
_fbClient.isc_detach_database(_statusVector, ref _handle);
ProcessStatusVector();
_handle.Dispose();
}
WarningMessage = null;
ServerVersion = null;
_statusVector = null;
TransactionCount = 0;
return ValueTask.CompletedTask;
}
#endregion
#region Transaction Methods
public override TransactionBase BeginTransaction(TransactionParameterBuffer tpb)
{
var transaction = new FesTransaction(this);
transaction.BeginTransaction(tpb);
return transaction;
}
public override async ValueTask<TransactionBase> BeginTransactionAsync(TransactionParameterBuffer tpb, CancellationToken cancellationToken = default)
{
var transaction = new FesTransaction(this);
await transaction.BeginTransactionAsync(tpb, cancellationToken).ConfigureAwait(false);
return transaction;
}
#endregion
#region Cancel Methods
public override void CancelOperation(short kind)
{
var localStatusVector = new IntPtr[IscCodes.ISC_STATUS_LENGTH];
_fbClient.fb_cancel_operation(localStatusVector, ref _handle, (ushort)kind);
try
{
ProcessStatusVector(localStatusVector);
}
catch (IscException ex) when (ex.ErrorCode == IscCodes.isc_nothing_to_cancel)
{ }
}
public override ValueTask CancelOperationAsync(short kind, CancellationToken cancellationToken = default)
{
var localStatusVector = new IntPtr[IscCodes.ISC_STATUS_LENGTH];
_fbClient.fb_cancel_operation(localStatusVector, ref _handle, (ushort)kind);
try
{
ProcessStatusVector(localStatusVector);
}
catch (IscException ex) when (ex.ErrorCode == IscCodes.isc_nothing_to_cancel)
{ }
return ValueTask.CompletedTask;
}
#endregion
#region Statement Creation Methods
public override StatementBase CreateStatement()
{
return new FesStatement(this);
}
public override StatementBase CreateStatement(TransactionBase transaction)
{
return new FesStatement(this, transaction as FesTransaction);
}
#endregion
#region Parameter Buffers
public override DatabaseParameterBufferBase CreateDatabaseParameterBuffer()
{
return new DatabaseParameterBuffer1(ParameterBufferEncoding);
}
public override EventParameterBuffer CreateEventParameterBuffer()
{
return new EventParameterBuffer(Charset.Encoding);
}
public override TransactionParameterBuffer CreateTransactionParameterBuffer()
{
return new TransactionParameterBuffer(Charset.Encoding);
}
#endregion
#region Database Information Methods
public override List<object> GetDatabaseInfo(byte[] items)
{
return GetDatabaseInfo(items, IscCodes.DEFAULT_MAX_BUFFER_SIZE);
}
public override ValueTask<List<object>> GetDatabaseInfoAsync(byte[] items, CancellationToken cancellationToken = default)
{
return GetDatabaseInfoAsync(items, IscCodes.DEFAULT_MAX_BUFFER_SIZE, cancellationToken);
}
public override List<object> GetDatabaseInfo(byte[] items, int bufferLength)
{
var buffer = new byte[bufferLength];
DatabaseInfo(items, buffer, buffer.Length);
return IscHelper.ParseDatabaseInfo(buffer, Charset);
}
public override ValueTask<List<object>> GetDatabaseInfoAsync(byte[] items, int bufferLength, CancellationToken cancellationToken = default)
{
var buffer = new byte[bufferLength];
DatabaseInfo(items, buffer, buffer.Length);
return ValueTask.FromResult(IscHelper.ParseDatabaseInfo(buffer, Charset));
}
#endregion
#region Internal Methods
internal void ProcessStatusVector(IntPtr[] statusVector)
{
StatusVectorHelper.ProcessStatusVector(statusVector, Charset, WarningMessage);
}
#endregion
#region Private Methods
private void DatabaseInfo(byte[] items, byte[] buffer, int bufferLength)
{
StatusVectorHelper.ClearStatusVector(_statusVector);
_fbClient.isc_database_info(
_statusVector,
ref _handle,
(short)items.Length,
items,
(short)bufferLength,
buffer);
ProcessStatusVector();
}
private void ProcessStatusVector()
{
StatusVectorHelper.ProcessStatusVector(_statusVector, Charset, WarningMessage);
}
private void ProcessStatusVector(Charset charset)
{
StatusVectorHelper.ProcessStatusVector(_statusVector, charset, WarningMessage);
}
#endregion
#region Internal Static Methods
internal static void CheckCryptKeyForSupport(byte[] cryptKey)
{
// ICryptKeyCallbackImpl would have to be passed from C# for 'cryptKey' passing
if (cryptKey?.Length > 0)
throw new NotSupportedException("Passing Encryption Key isn't, yet, supported on Firebird Embedded.");
}
#endregion
}