-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMiniREST.SQL.Base.pas
584 lines (505 loc) · 17.3 KB
/
MiniREST.SQL.Base.pas
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
{$IFDEF FPC}
{$mode DELPHI}
{$IFEND}
unit MiniREST.SQL.Base;
interface
uses MiniREST.SQL.Intf, MiniREST.SQL.Common, SyncObjs, {$IFNDEF FPC}Generics.Collections,
{$ELSE}fgl,{$IFEND} SysUtils, Classes;
type
TMiniRESTSQLConnectionBase = class;
{ TMiniRESTSQLConnectionFactoryBase }
TMiniRESTSQLConnectionFactoryBase = class abstract(TInterfacedObject, IMiniRESTSQLConnectionFactory)
strict private
FConnectionsToNotifyFree: TList;
private
FDatabaseType: TMiniRESTSQLDatabaseType;
FOnOpenQueryException: TMiniRESTOnOpenQueryException;
protected
FConnectionFactoryEventLogger: IMiniRESTSQLConnectionFactoryEventLogger;
{$IFNDEF FPC}
FSemaphore: TLightweightSemaphore;
FQueue: TQueue<IMiniRESTSQLConnection>;
{$ELSE}
FConnectionGetEvent: PRTLEvent;
FConnectionReleaseEvent: PRTLEvent;
FQueue: TFPGInterfacedObjectList<IMiniRESTSQLConnection>;
//FQueuePosition: Integer;
FAvailableConnections: Integer;
{$IFEND}
FCriticalSection: TCriticalSection;
FConnectionCounter: Integer;
FConnectionsCount: Integer;
FSingletonConnection: IMiniRESTSQLConnection;
procedure AddConnectionToNotifyFree(AConnection: IMiniRESTSQLConnection);
procedure RemoveConnectionToNotifyFree(AConnection: IMiniRESTSQLConnection);
procedure ReleaseConnection(AConnection: IMiniRESTSQLConnection); virtual;
function InternalGetconnection: IMiniRESTSQLConnection; virtual; abstract;
procedure LogConnectionPoolEvent(const AMessage: string);
function GetOnOpenQueryException: TMiniRESTOnOpenQueryException;
public
constructor Create(const AConnectionCount: Integer); overload;
constructor Create(AParams: IMiniRESTSQLConnectionFactoryParams); overload;
destructor Destroy; override;
function GetConnection: IMiniRESTSQLConnection; overload;
function GetObject: TObject;
function GetConnectionsCount: Integer; virtual; abstract;
function GetQueueCount: Integer; virtual; abstract;
function GetConnection(const AIdentifier: string): IMiniRESTSQLConnection; overload;
function GetSingletonConnection: IMiniRESTSQLConnection;
procedure InvalidateConnections;
function GetDatabaseType: TMiniRESTSQLDatabaseType;
end;
{ TMiniRESTSQLConnectionBase }
TMiniRESTSQLConnectionBase = class abstract(TInterfacedObject, IMiniRESTSQLConnection)
strict private
FOwner: TObject;
FConnectionID: Integer;
FValid: Boolean;
FDatabaseType: TMiniRESTSQLDatabaseType;
protected
FName: string;
FEstaNoPool: Boolean;
procedure SetValid(const AValid: Boolean);
function _Release: Integer; {$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
function GetObject: TObject; virtual; abstract;
procedure SetOwner(AOwner: Pointer);
public
constructor Create(AOwner: IMiniRESTSQLConnectionFactory); overload;
constructor Create(AParams: IMiniRESTSQLConnectionParams); overload;
procedure StartTransaction; virtual; abstract;
procedure Commit; virtual; abstract;
procedure Rollback; virtual; abstract;
procedure Connect; virtual; abstract;
function GetQuery: IMiniRESTSQLQuery; overload; virtual; abstract;
function GetQuery(const ASQL: string): IMiniRESTSQLQuery; overload; virtual; abstract;
function GetQuery(const ASQL: string; AParams : array of IMiniRESTSQLParam): IMiniRESTSQLQuery; overload; virtual; abstract;
function GetName: string;
function SetName(const AName: string): IMiniRESTSQLConnection;
function Execute(const ACommand: string; AParams: array of IMiniRESTSQLParam): Integer; virtual; abstract;
function GetDatabaseInfo: IMiniRESTSQLDatabaseInfo; virtual; abstract;
function InTransaction: Boolean; virtual; abstract;
function GetConnectionID: Integer;
function IsValid: Boolean;
procedure Invalidate; virtual; abstract;
function GetDatabaseType: TMiniRESTSQLDatabaseType;
end;
TMiniRESTSQLPrimaryKeyInfo = class(TInterfacedObject, IMiniRESTSQLPrimaryKeyInfo)
private
FFields: TArray<string>;
FName: string;
public
function GetFields: TArray<System.string>;
procedure SetFields(const AFields: TArray<System.string>);
function GetName: string;
procedure SetName(const AName: string);
end;
TMiniRESTSQLForeignKeyInfo = class(TInterfacedObject, IMiniRESTSQLForeignKeyInfo)
private
FFKFields: TArray<string>;
FFKTableName: string;
FFields: TArray<string>;
FName: string;
public
function GetFKFields: TArray<System.string>;
procedure SetFKFields(const AFields: TArray<System.string>);
function GetFKTableName: string;
procedure SetFKTableName(const AName: string);
function GetFields: TArray<System.string>;
procedure SetFields(const AFields: TArray<System.string>);
function GetName: string;
procedure SetName(const AName: string);
end;
TMiniRESTSQLColumnInfo = class(TInterfacedObject, IMiniRESTSQLColumnInfo)
private
FName: string;
public
constructor Create(const AName: string);
function GetName: string;
end;
TMiniRESTSQLConnectionParams = class(TInterfacedObject, IMiniRESTSQLConnectionParams)
private
FConnectionFactory: IMiniRESTSQLConnectionFactory;
FConnectionID: Integer;
public
//class function New: IMiniRESTSQLConnectionParams;
function GetConnectionFactory: IMiniRESTSQLConnectionFactory;
procedure SetConnectionFactory(AConnectionFactory: IMiniRESTSQLConnectionFactory);
function GetConnectionID: Integer;
procedure SetConnectionID(const AID: Integer);
end;
TMiniRESTSQLConnectionFactoryParams = class(TInterfacedObject, IMiniRESTSQLConnectionFactoryParams)
private
FConnectionCount: Integer;
FConnectionFactoryEventLogger: IMiniRESTSQLConnectionFactoryEventLogger;
FCharSet: string;
FDatabaseType: TMiniRESTSQLDatabaseType;
FOnOpenQueryException: TMiniRESTOnOpenQueryException;
public
function GetConnectionsCount: Integer;
procedure SetConnectionsCount(const ACount: Integer);
function GetObject: TObject;
function GetConnectionFactoryEventLogger: IMiniRESTSQLConnectionFactoryEventLogger;
procedure SetConnectionFactoryEventLogger(ALogger: IMiniRESTSQLConnectionFactoryEventLogger);
function GetCharSet: string;
procedure SetCharSet(const ACharSet: string);
function GetDatabaseType: TMiniRESTSQLDatabaseType;
procedure SetDatabseType(const ADatabaseType: TMiniRESTSQLDatabaseType);
function GetOnOpenQueryException: TMiniRESTOnOpenQueryException;
procedure SetOnOpenQueryException(AValue: TMiniRESTOnOpenQueryException);
end;
implementation
var
gConnectionIDCounter: Integer;
{ TMiniRESTSQLConnectionFactoryBase }
constructor TMiniRESTSQLConnectionFactoryBase.Create(const AConnectionCount: Integer);
var
LParams: IMiniRESTSQLConnectionFactoryParams;
begin
LParams := TMiniRESTSQLConnectionFactoryParams.Create;
LParams.SetConnectionsCount(AConnectionCount);
Create(LParams);
end;
destructor TMiniRESTSQLConnectionFactoryBase.Destroy;
var
I: Integer;
LItemToNotifyFree: TMiniRESTSQLConnectionBase;
begin
for I := 0 to (FConnectionsToNotifyFree.Count - 1) do
begin
LItemToNotifyFree := TMiniRESTSQLConnectionBase(FConnectionsToNotifyFree.Items[I]);
LItemToNotifyFree.SetOwner(nil);
end;
{$IFNDEF FPC}
FSemaphore.Free;
FQueue.Free;
{$ELSE}
RTLeventdestroy(FConnectionReleaseEvent);
RTLeventdestroy(FConnectionGetEvent);
FQueue.Free;
{$ENDIF}
FCriticalSection.Free;
FConnectionsToNotifyFree.Free;
inherited;
end;
function TMiniRESTSQLConnectionFactoryBase.GetConnection: IMiniRESTSQLConnection;
begin
Result := GetConnection('');
end;
function TMiniRESTSQLConnectionFactoryBase.GetObject: TObject;
begin
Result := Self;
end;
procedure TMiniRESTSQLConnectionFactoryBase.ReleaseConnection(
AConnection: IMiniRESTSQLConnection);
begin
{$IFNDEF FPC}
FCriticalSection.Enter;
try
FQueue.Enqueue(AConnection);
FSemaphore.Release(1);
TMiniRESTSQLConnectionBase(AConnection).FEstaNoPool := True;
FConnectionsToNotifyFree.Remove(Pointer(AConnection));
finally
FCriticalSection.Leave;
end;
{$IFEND}
end;
{ TMiniRESTSQLConnectionBase }
constructor TMiniRESTSQLConnectionBase.Create(
AOwner: IMiniRESTSQLConnectionFactory);
var
LID: Integer;
LParams: IMiniRESTSQLConnectionParams;
begin
LID := InterLockedIncrement(gConnectionIDCounter);
LParams := TMiniRESTSQLConnectionParams.Create;
LParams.SetConnectionFactory(AOwner);
LParams.SetConnectionID(LID);
Create(LParams);
end;
function TMiniRESTSQLConnectionBase.GetName: string;
begin
Result := FName;
end;
function TMiniRESTSQLConnectionBase.SetName(
const AName: string): IMiniRESTSQLConnection;
begin
FName := AName;
Result := Self;
end;
procedure TMiniRESTSQLConnectionBase.SetOwner(AOwner: Pointer);
begin
FOwner := AOwner;
end;
function TMiniRESTSQLConnectionBase._Release: Integer; {$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
begin
if (FRefCount = 1) and (FOwner <> nil) and (not FEstaNoPool) then
TMiniRESTSQLConnectionFactoryBase(FOwner).ReleaseConnection(Self);
Result := inherited _Release;
end;
{ TMiniRESTSQLPrimaryKeyInfo }
function TMiniRESTSQLPrimaryKeyInfo.GetFields: TArray<System.string>;
begin
Result := FFields;
end;
function TMiniRESTSQLPrimaryKeyInfo.GetName: string;
begin
Result := FName;
end;
procedure TMiniRESTSQLPrimaryKeyInfo.SetFields(
const AFields: TArray<System.string>);
begin
FFields := AFields;
end;
procedure TMiniRESTSQLPrimaryKeyInfo.SetName(const AName: string);
begin
FName := AName;
end;
{ TMiniRESTSQLForeignKeyInfo }
function TMiniRESTSQLForeignKeyInfo.GetFields: TArray<System.string>;
begin
Result := FFields;
end;
function TMiniRESTSQLForeignKeyInfo.GetFKFields: TArray<System.string>;
begin
Result := FFKFields;
end;
function TMiniRESTSQLForeignKeyInfo.GetFKTableName: string;
begin
Result := FFKTableName;
end;
function TMiniRESTSQLForeignKeyInfo.GetName: string;
begin
Result := FName;
end;
procedure TMiniRESTSQLForeignKeyInfo.SetFields(
const AFields: TArray<System.string>);
begin
FFields := AFields;
end;
procedure TMiniRESTSQLForeignKeyInfo.SetFKFields(
const AFields: TArray<System.string>);
begin
FFKFields := AFields;
end;
procedure TMiniRESTSQLForeignKeyInfo.SetFKTableName(const AName: string);
begin
FFKTableName := AName;
end;
procedure TMiniRESTSQLForeignKeyInfo.SetName(const AName: string);
begin
FName := AName;
end;
{ TMiniRESTSQLColumnInfo }
constructor TMiniRESTSQLColumnInfo.Create(const AName: string);
begin
FName := AName;
end;
function TMiniRESTSQLColumnInfo.GetName: string;
begin
Result := FName;
end;
procedure TMiniRESTSQLConnectionFactoryBase.AddConnectionToNotifyFree(AConnection: IMiniRESTSQLConnection);
begin
FConnectionsToNotifyFree.Add(AConnection.GetObject);
end;
procedure TMiniRESTSQLConnectionFactoryBase.RemoveConnectionToNotifyFree(AConnection: IMiniRESTSQLConnection);
begin
FConnectionsToNotifyFree.Remove(AConnection.GetObject);
end;
procedure TMiniRESTSQLConnectionFactoryBase.LogConnectionPoolEvent(const AMessage: string);
begin
if FConnectionFactoryEventLogger = nil then
Exit;
FConnectionFactoryEventLogger.LogPoolEvent(AMessage);
end;
function TMiniRESTSQLConnectionFactoryBase.GetConnection(const AIdentifier: string): IMiniRESTSQLConnection;
var
LConnection: IMiniRESTSQLConnection;
begin
{$IFNDEF FPC}
FSemaphore.Acquire;
FCriticalSection.Enter;
try
if FQueue.Count = 0 then
begin
LConnection := InternalGetconnection.SetName('Connection' + IntToStr(FConnectionCounter));
Inc(FConnectionCounter);
Result := LConnection;
TMiniRESTSQLConnectionBase(Result).SetValid(True);
end
else
begin
Result := FQueue.Dequeue;
end;
TMiniRESTSQLConnectionBase(Result).FEstaNoPool := False;
finally
FSemaphore.Release(1);
FCriticalSection.Leave;
end;
{$IFEND}
{$IFDEF FPC}
if InterlockedDecrement(FAvailableConnections) < 0 then
begin
RTLeventResetEvent(FConnectionReleaseEvent);
RTLeventWaitFor(FConnectionReleaseEvent);
end;
RTLeventWaitFor(FConnectionGetEvent);
try
if (FConnectionCounter < FConnectionsCount) then
begin
LConnection := InternalGetconnection.SetName('Connection' + IntToStr(FConnectionCounter) + ' ' + AIdentifier);
Inc(FConnectionCounter);
Result := LConnection;
TMiniRESTSQLConnectionBase(Result.GetObject).SetValid(True);
end
else
begin
LConnection := FQueue.Last;
Result := FQueue.Extract(LConnection);
end;
TMiniRESTSQLConnectionBase(Result.GetObject).FEstaNoPool := False;
LogConnectionPoolEvent(Format('GET CONNECTION %d - %s', [Result.GetConnectionID, Result.GetName]));
finally
RTLeventSetEvent(FConnectionGetEvent);
end;
{$IFEND}
end;
function TMiniRESTSQLConnectionBase.GetConnectionID: Integer;
begin
Result := FConnectionID;
end;
procedure TMiniRESTSQLConnectionParams.SetConnectionFactory(AConnectionFactory: IMiniRESTSQLConnectionFactory);
begin
FConnectionFactory := AConnectionFactory;
end;
function TMiniRESTSQLConnectionParams.GetConnectionFactory: IMiniRESTSQLConnectionFactory;
begin
Result := FConnectionFactory;
end;
function TMiniRESTSQLConnectionParams.GetConnectionID: Integer;
begin
Result := FConnectionID;
end;
procedure TMiniRESTSQLConnectionParams.SetConnectionID(const AID: Integer);
begin
FConnectionID := AID;
end;
constructor TMiniRESTSQLConnectionBase.Create(AParams: IMiniRESTSQLConnectionParams);
begin
FOwner := nil;
FOwner := AParams.GetConnectionFactory.GetObject;
FConnectionID := AParams.GetConnectionID;
FDatabaseType := AParams.GetConnectionFactory.GetDatabaseType;
TMiniRESTSQLConnectionFactoryBase(FOwner).AddConnectionToNotifyFree(Self);
end;
function TMiniRESTSQLConnectionFactoryParams.GetConnectionsCount: Integer;
begin
Result := FConnectionCount;
end;
procedure TMiniRESTSQLConnectionFactoryParams.SetConnectionsCount(const ACount: Integer);
begin
FConnectionCount := ACount;
end;
// class function TMiniRESTSQLConnectionFactoryParams.New: IMiniRESTSQLConnectionFactoryParams;
// begin
// Result := Create;
// end;
constructor TMiniRESTSQLConnectionFactoryBase.Create(AParams: IMiniRESTSQLConnectionFactoryParams);
begin
FConnectionsCount := AParams.GetConnectionsCount;
{$IFNDEF FPC}
FSemaphore := TLightweightSemaphore.Create(FConnectionsCount, FConnectionsCount);
FQueue := TQueue<IMiniRESTSQLConnection>.Create;
{$ELSE}
FAvailableConnections := FConnectionsCount;
FQueue := TFPGInterfacedObjectList<IMiniRESTSQLConnection>.Create;
FConnectionGetEvent := RTLEventCreate;
FConnectionReleaseEvent := RTLEventCreate;
RTLeventSetEvent(FConnectionGetEvent);
RTLeventSetEvent(FConnectionReleaseEvent);
{$ENDIF}
FCriticalSection := TCriticalSection.Create;
FConnectionsToNotifyFree := TList.Create;
FConnectionFactoryEventLogger := AParams.GetConnectionFactoryEventLogger;
FDatabaseType := AParams.GetDatabaseType;
FOnOpenQueryException := AParams.GetOnOpenQueryException;
end;
function TMiniRESTSQLConnectionFactoryParams.GetObject: TObject;
begin
Result := Self;
end;
function TMiniRESTSQLConnectionFactoryParams.GetConnectionFactoryEventLogger: IMiniRESTSQLConnectionFactoryEventLogger;
begin
Result := FConnectionFactoryEventLogger;
end;
procedure TMiniRESTSQLConnectionFactoryParams.SetConnectionFactoryEventLogger(ALogger: IMiniRESTSQLConnectionFactoryEventLogger);
begin
FConnectionFactoryEventLogger := ALogger;
end;
function TMiniRESTSQLConnectionFactoryBase.GetSingletonConnection: IMiniRESTSQLConnection;
begin
if not Assigned(FSingletonConnection) then
FSingletonConnection := GetConnection('Singleton Connetion');
Result := FSingletonConnection;
end;
procedure TMiniRESTSQLConnectionFactoryBase.InvalidateConnections;
var
I: Integer;
LConnectionObj: TMiniRESTSQLConnectionBase;
LConnection: IMiniRESTSQLConnection;
begin
for I := 0 to (FConnectionsToNotifyFree.Count - 1) do
begin
LConnectionObj := FConnectionsToNotifyFree.Items[I];
if LConnectionObj.GetInterface(IMiniRESTSQLConnection, LConnection) then
LConnection.Invalidate;
end;
end;
function TMiniRESTSQLConnectionBase.IsValid: Boolean;
begin
Result := FValid;
end;
procedure TMiniRESTSQLConnectionBase.SetValid(const AValid: Boolean);
begin
FValid := AValid;
end;
function TMiniRESTSQLConnectionFactoryParams.GetCharSet: string;
begin
Result := FCharSet;
end;
procedure TMiniRESTSQLConnectionFactoryParams.SetCharSet(const ACharSet: string);
begin
FCharSet := ACharSet;
end;
function TMiniRESTSQLConnectionBase.GetDatabaseType: TMiniRESTSQLDatabaseType;
begin
Result := FDatabaseType;
end;
function TMiniRESTSQLConnectionFactoryBase.GetDatabaseType: TMiniRESTSQLDatabaseType;
begin
Result := FDatabaseType;
end;
function TMiniRESTSQLConnectionFactoryParams.GetDatabaseType: TMiniRESTSQLDatabaseType;
begin
Result := FDatabaseType;
end;
procedure TMiniRESTSQLConnectionFactoryParams.SetDatabseType(const ADatabaseType: TMiniRESTSQLDatabaseType);
begin
FDatabaseType := ADatabaseType;
end;
function TMiniRESTSQLConnectionFactoryParams.GetOnOpenQueryException: TMiniRESTOnOpenQueryException;
begin
Result := FOnOpenQueryException;
end;
procedure TMiniRESTSQLConnectionFactoryParams.SetOnOpenQueryException(AValue: TMiniRESTOnOpenQueryException);
begin
FOnOpenQueryException := AValue;
end;
function TMiniRESTSQLConnectionFactoryBase.GetOnOpenQueryException: TMiniRESTOnOpenQueryException;
begin
Result := FOnOpenQueryException;
end;
initialization
gConnectionIDCounter := 0;
end.