forked from synopse/mORMot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SynDBFirebird.pas
1074 lines (962 loc) · 41.2 KB
/
SynDBFirebird.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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/// Firebird 2.5+ direct access classes to be used with our SynDB architecture
// - this unit is a part of the freeware Synopse mORMot framework,
// licensed under a MPL/GPL/LGPL tri-license; version 1.18
unit SynDBFirebird;
{
This file is part of Synopse mORMot framework.
Synopse mORMot framework. Copyright (C) 2016 Arnaud Bouchez
Synopse Informatique - http://synopse.info
*** BEGIN LICENSE BLOCK *****
Version: MPL 1.1/GPL 2.0/LGPL 2.1
The contents of this file are subject to the Mozilla Public License Version
1.1 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL
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.
The Original Code is Synopse mORMot framework.
The Initial Developer of the Original Code is Arnaud Bouchez.
Portions created by the Initial Developer are Copyright (C) 2016
the Initial Developer. All Rights Reserved.
Contributor(s):
Alternatively, the contents of this file may be used under the terms of
either the GNU General Public License Version 2 or later (the "GPL"), or
the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
in which case the provisions of the GPL or the LGPL are applicable instead
of those above. If you wish to allow use of your version of this file only
under the terms of either the GPL or the LGPL, and not to allow others to
use your version of this file under the terms of the MPL, indicate your
decision by deleting the provisions above and replace them with the notice
and other provisions required by the GPL or the LGPL. If you do not delete
the provisions above, a recipient may use your version of this file under
the terms of any one of the MPL, the GPL or the LGPL.
***** END LICENSE BLOCK *****
Version 1.18
- first public release, corresponding to mORMot Framework 1.18
TODO:
- share a global database connection for embedded mode, instead of
setting ForceOnlyOneSharedConnection=true
}
{$I Synopse.inc} // define HASINLINE USETYPEINFO CPU32 CPU64 OWNNORMTOUPPER
interface
uses
Windows,
SysUtils,
{$ifndef DELPHI5OROLDER}
Variants,
{$endif}
Classes,
Contnrs,
SynCommons,
SynLog,
SynDB;
{ -------------- TSQLDBFirebird* classes and types implementing a Firebird connection }
type
/// generic Exception type, generated for Firebird connection
EFirebirdException = class(ESQLDBException);
/// implements properties shared by the Firebird library
// - will first search for a "Firebird Embedded Server" library in the
// executable folder or its 'Firebird\' sub-folder
// - if not found, the Firebird client library will be searched in the path,
// or, in default, from the Windows registry settings
// - use TSQLDBFirebirdEmbeddedConnectionProperties or
// TSQLDBFirebirdConnectionClientProperties to force either of the two modes
// - we focus on Firebird 2.5.0 and up (released on October 2010), since it
// was the first multi-thread-capable and thread-safe client implementation
TSQLDBFirebirdConnectionProperties = class(TSQLDBConnectionPropertiesThreadSafe)
protected
fDefaultPageSize: Integer;
/// opaque protected reference to the corresponding TFirebirdLib instance
fFirebirdInstance: TObject;
// this overridden method will force dFirebird kind of DBMS
procedure SetInternalProperties; override;
public
/// initialize the connection properties
// - will raise an exception if no Firebird library is available
constructor Create(const aServerName, aDatabaseName, aUserID, aPassWord: RawUTF8); override;
/// create a new connection
// - call this method if the shared MainConnection is not enough (e.g. for
// multi-thread access)
// - the caller is responsible of freeing this instance
// - this overridden method will create an TSQLDBFirebirdConnection instance
function NewConnection: TSQLDBConnection; override;
/// the page size on disk to be used when creating a new database file
// - only used in embedded mode, if no database file exists
// - default value is 4096, but you may set 8192 or 16384 for better performance
property DefaultPageSize: Integer read fDefaultPageSize write fDefaultPageSize;
end;
/// implements properties shared by the Firebird embedded engine
// - will search for a "Firebird Embedded Server" library in the executable
// folder or its 'Firebird\' sub-folder
// - if not found, will raise an EFirebirdException
TSQLDBFirebirdEmbeddedConnectionProperties = class(TSQLDBFirebirdConnectionProperties)
public
/// initialize the embedded connection properties
// - will raise an exception if no embedded Firebird library is available
constructor Create(const aServerName, aDatabaseName, aUserID, aPassWord: RawUTF8); override;
end;
/// implements a direct connection to the Firebird client library
// - the Firebird client libray will be searched in the path, or, in default,
// from the Windows registry settings
// - if not found, will raise an EFirebirdException
TSQLDBFirebirdConnectionClientProperties = class(TSQLDBFirebirdConnectionProperties)
public
/// initialize the embedded connection properties
// - will raise an exception if no embedded Firebird library is available
constructor Create(const aServerName, aDatabaseName, aUserID, aPassWord: RawUTF8); override;
end;
TSQLDBFirebirdStatus = array[0..19] of PtrInt;
/// implements a direct connection to the Firebird library
TSQLDBFirebirdConnection = class(TSQLDBConnectionThreadSafe)
protected
fFirebirdProperties: TSQLDBFirebirdConnectionProperties;
fEmbedded: boolean;
fBuf: PByteArray;
fBufLen: integer;
fStatus: TSQLDBFirebirdStatus;
fDatabase: pointer;
fTransaction: pointer;
// increased on each StartTransaction call
fTransactionID: cardinal;
/// opaque protected reference to the corresponding TFirebirdLib instance
fFirebirdInstance: TObject;
procedure BufAddStr(dpb: byte; const Value: RawUTF8; DoNotStoreIfBlank: boolean=true);
procedure BufAddI8(dpb: byte; Value: byte=0);
procedure BufAddI32(dpb: byte; Value: cardinal);
procedure BufClear;
public
/// connect to a specified Firebird database
constructor Create(aProperties: TSQLDBConnectionProperties); override;
/// release memory and connection
destructor Destroy; override;
/// connect to the Firebird library, i.e. create the DB instance
// - should raise an Exception on error
procedure Connect; override;
/// stop connection to the Firebird library, i.e. release the DB instance
// - should raise an Exception on error
procedure Disconnect; override;
/// return TRUE if Connect has been already successfully called
function IsConnected: boolean; override;
/// initialize a new SQL query statement for the given connection
// - the caller should free the instance after use
function NewStatement: TSQLDBStatement; override;
/// begin a Transaction for this connection
// - current implementation do not support nested transaction with those
// methods: exception will be raised in such case
procedure StartTransaction; override;
/// commit changes of a Transaction for this connection
// - StartTransaction method must have been called before
procedure Commit; override;
/// discard changes of a Transaction for this connection
// - StartTransaction method must have been called before
procedure Rollback; override;
published
/// TRUE if the Embedded Firebird Library is used for this connection
property Embedded: boolean read fEmbedded;
end;
TSQLDBFirebirdInternalStatement = record
Transaction: pointer;
TransactionID: cardinal;
Statement: pointer;
end;
/// implements a statement using a Firebird connection
TSQLDBFirebirdStatement = class(TSQLDBStatementWithParamsAndColumns)
protected
fMain: TSQLDBFirebirdInternalStatement;
fAutoCommit: TSQLDBFirebirdInternalStatement;
fCurrent: ^TSQLDBFirebirdInternalStatement;
fInput: TByteDynArray;
fOutput: TByteDynArray;
fStatus: TSQLDBFirebirdStatus;
function ReleaseMainStatementIfAny: boolean;
public
{{ create a Firebird statement instance, from an existing Firebird connection
- the Execute method can be called once per TSQLDBFirebirdStatement instance,
but you can use the Prepare once followed by several ExecutePrepared methods
- if the supplied connection is not of TOleDBConnection type, will raise
an exception }
constructor Create(aConnection: TSQLDBConnection); override;
{{ release all associated memory and Firebird handles }
destructor Destroy; override;
{{ Prepare an UTF-8 encoded SQL statement
- parameters marked as ? will be bound later, before ExecutePrepared call
- if ExpectResults is TRUE, then Step() and Column*() methods are available
to retrieve the data rows
- raise an EFirebirdException or ESQLDBException on any error }
procedure Prepare(const aSQL: RawUTF8; ExpectResults: Boolean=false); overload; override;
{{ Execute a prepared SQL statement
- parameters marked as ? should have been already bound with Bind*() functions
- raise an EFirebirdException or ESQLDBException on any error }
procedure ExecutePrepared; override;
{/ Reset the previous prepared statement
- this overridden implementation will reset all bindings and the cursor state
- raise an EFirebirdException on any error }
procedure Reset; override;
{/ After a statement has been prepared via Prepare() + ExecutePrepared() or
Execute(), this method must be called one or more times to evaluate it
- you shall call this method before calling any Column*() methods
- return TRUE on success, with data ready to be retrieved by Column*()
- return FALSE if no more row is available (e.g. if the SQL statement
is not a SELECT but an UPDATE or INSERT command)
- access the first or next row of data from the SQL Statement result:
if SeekFirst is TRUE, will put the cursor on the first row of results,
otherwise, it will fetch one row of data, to be called within a loop
- raise an EFirebirdException or ESQLDBException exception on any error }
function Step(SeekFirst: boolean=false): boolean; override;
{{ returns TRUE if the column contains NULL }
function ColumnNull(Col: integer): boolean; override;
{{ return a Column integer value of the current Row, first Col is 0 }
function ColumnInt(Col: integer): Int64; override;
{{ return a Column floating point value of the current Row, first Col is 0 }
function ColumnDouble(Col: integer): double; override;
{{ return a Column floating point value of the current Row, first Col is 0 }
function ColumnDateTime(Col: integer): TDateTime; override;
{{ return a Column currency value of the current Row, first Col is 0
- should retrieve directly the 64 bit Currency content, to avoid
any rounding/conversion error from floating-point types }
function ColumnCurrency(Col: integer): currency; override;
{{ return a Column UTF-8 encoded text value of the current Row, first Col is 0 }
function ColumnUTF8(Col: integer): RawUTF8; override;
{{ return a Column as a blob value of the current Row, first Col is 0
- ColumnBlob() will return the binary content of the field is was not ftBlob,
e.g. a 8 bytes RawByteString for a vtInt64/vtDouble/vtDate/vtCurrency,
or a direct mapping of the RawUnicode }
function ColumnBlob(Col: integer): RawByteString; override;
{{ append all columns values of the current Row to a JSON stream
- will use WR.Expand to guess the expected output format
- fast overridden implementation with no temporary variable
- BLOB field value is saved as Base64, in the '"\uFFF0base64encodedbinary"
format and contains true BLOB data }
procedure ColumnsToJSON(WR: TJSONWriter); override;
/// returns the number of rows updated by the execution of this statement
function UpdateCount: integer; override;
end;
const
FBLIBNAME: array[boolean] of TFileName =
{$ifdef MSWINDOWS}
('gds32.dll','fbembed.dll');
{$endif}
{$ifdef LINUX}
('libfbclient.so','libfbembed.so');
{$endif}
/// check from the file beginning if sounds like a valid Firebird database file
// - no official signature is available, so it will be a "best-guess"
function IsFirebirdFile(const FileName: TFileName): boolean;
implementation
function IsFirebirdFile(const FileName: TFileName): boolean;
var F: THandle;
Header: packed record
pag_type, pag_flags: byte;
pag_checksum: word;
pag_generation, pag_scn, reserved: cardinal;
hdr_page_size, hdr_ods_version: word;
end; // cf. http://www.firebirdsql.org/manual/fbint-standard-header.html
begin
F := FileOpen(FileName,fmOpenRead or fmShareDenyNone);
if F=INVALID_HANDLE_VALUE then
result := false else begin
result := (FileRead(F,Header,sizeof(Header))=SizeOf(Header)) and
(Header.pag_type=1) and (Header.pag_checksum=12345) and
(Header.hdr_page_size and $3ff=0) and (Header.hdr_ods_version and $8000<>0);
FileClose(F);
end;
end;
{ -------------- Firebird library interfaces, constants and types }
const
METADATALENGTH = 68;
MAX_DPB_SIZE = 1024;
{ supports SQL delimited identifier, SQLDATE/DATE, TIME,
TIMESTAMP, CURRENT_DATE, CURRENT_TIME, CURRENT_TIMESTAMP, and
64-bit exact numeric type }
SQL_DIALECT = 3;
// fits XSQLDA/XSQLVAR layout
SQLDA_CURRENT_VERSION = 1;
DSQL_close = 1;
DSQL_drop = 2;
type
PIscTrHandle = PPointer;
ISCStatus = PtrInt;
PISCStatus = ^ISCStatus;
PPISCStatus = ^PISCStatus;
PGDSQuad = ^GDS_Quad;
PISCQuad = ^GDS_Quad;
GDS_QUAD = record
gds_quad_high: integer;
gds_quad_low: cardinal;
end;
PXSQLVar = ^XSQLVar;
XSQLVAR = object
// datatype of field
sqltype: Smallint;
// scale factor
sqlscale: Smallint;
// datatype subtype
sqlsubtype: Smallint;
// length of data area
sqllen: Smallint;
// address of data
sqldata: PAnsiChar;
// address of indicator variable
sqlind: PSmallintArray;
// length of sqlname field
sqlname_length: Smallint;
// name of field, name length + space for #0
sqlname: array[0..METADATALENGTH-1] of AnsiChar;
// length of relation name
relname_length: Smallint;
// field's relation name + space for #0
relname: array[0..METADATALENGTH-1] of AnsiChar;
// length of owner name
ownname_length: Smallint;
// relation's owner name + space for #0
ownname: array[0..METADATALENGTH-1] of AnsiChar;
// length of alias name
aliasname_length: Smallint;
// relation's alias name + space for #0
aliasname: array[0..METADATALENGTH-1] of AnsiChar;
// wrapper to return the column name from sqlname[] content
function ColumnName: RawUTF8;
end;
PXSQLDA = ^XSQLDA;
XSQLDA = record
// version of this XSQLDA = SQLDA_CURRENT_VERSION = 1
version: Smallint;
// XSQLDA name field -> RESERVED
sqldaid: array[0..7] of AnsiChar;
// length in bytes of SQLDA -> RESERVED
sqldabc: Integer;
// number of fields allocated
sqln: Smallint;
// actual number of fields
sqld: Smallint;
// fields content
sqlvar: array[0..0] of XSQLVar;
end;
// used by isc_start_multiple()
PISCTEB = ^TISCTEB;
TISCTEB = record
Handle: pointer;
Len: Integer;
Address: PAnsiChar;
end;
// Database parameter block stuff
TFirebirdDatabaseParameterBlock = (dpb_cdd_pathname=1,
dpb_allocation, dpb_journal, dpb_page_size, dpb_num_buffers,
dpb_buffer_length, dpb_debug, dpb_garbage_collect, dpb_verify, dpb_sweep,
dpb_enable_journal, dpb_disable_journal, dpb_dbkey_scope, dpb_number_of_users,
dpb_trace, dpb_no_garbage_collect, dpb_damaged, dpb_license, dpb_sys_user_name,
dpb_encrypt_key, dpb_activate_shadow, dpb_sweep_interval, dpb_delete_shadow,
dpb_force_write, dpb_begin_log, dpb_quit_log, dpb_no_reserve, dpb_user_name,
dpb_password, dpb_password_enc, dpb_sys_user_name_enc, dpb_interp, dpb_online_dump,
dpb_old_file_size, dpb_old_num_files, dpb_old_file, dpb_old_start_page,
dpb_old_start_seqno, dpb_old_start_file, dpb_drop_walfile, dpb_old_dump_id,
dpb_wal_backup_dir, dpb_wal_chkptlen, dpb_wal_numbufs, dpb_wal_bufsize,
dpb_wal_grp_cmt_wait, dpb_lc_messages, dpb_lc_ctype, dpb_cache_manager,
dpb_shutdown, dpb_online, dpb_shutdown_delay, dpb_reserved,
dpb_overwrite, dpb_sec_attach, dpb_disable_wal, dpb_connect_timeout,
dpb_dummy_packet_interval, dpb_gbak_attach, dpb_sql_role_name,
dpb_set_page_buffers, dpb_working_directory, dpb_sql_dialect,
dpb_set_db_readonly, dpb_set_db_sql_dialect, dpb_gfix_attach, dpb_gstat_attach);
/// direct access to the Firebird library
TFirebirdLib = class(TSQLDBLib)
protected
fLibName: TFileName;
fEmbedded: boolean;
public
isc_attach_database: function(var user_status: TSQLDBFirebirdStatus; file_length: Smallint;
file_name: PAnsiChar; var handle: pointer; dpb_length: Smallint; dpb: PByteArray): ISCStatus;
{$ifdef MSWINDOWS} stdcall {$else} cdecl {$endif};
isc_detach_database: function(var user_status: TSQLDBFirebirdStatus; var handle: pointer): ISCStatus;
{$ifdef MSWINDOWS} stdcall {$else} cdecl {$endif};
isc_create_database: function(var user_status: TSQLDBFirebirdStatus; file_length: Smallint;
file_name: PAnsiChar; var handle: pointer; dpb_length: Smallint; dpb: PAnsiChar;
db_type: Smallint): ISCStatus;
{$ifdef MSWINDOWS} stdcall {$else} cdecl {$endif};
isc_sqlcode: function (var user_status: TSQLDBFirebirdStatus): integer;
{$ifdef MSWINDOWS} stdcall {$else} cdecl {$endif};
isc_sql_interprete: procedure(sqlcode: SmallInt; buf: PAnsiChar; buflen: SmallInt);
{$ifdef MSWINDOWS} stdcall {$else} cdecl {$endif};
fb_interpret: function(buf: PAnsiChar; bufsize: integer; status_vector: PPISCStatus): ISCStatus;
{$ifdef MSWINDOWS} stdcall {$else} cdecl {$endif};
isc_dsql_execute_immediate: function(var user_status: TSQLDBFirebirdStatus; var db_handle: pointer;
tra_handle: PIscTrHandle; buflen: Word; buf: PAnsiChar; dialect: Word=SQL_DIALECT;
sqlda: PXSQLDA=nil): ISCStatus;
{$ifdef MSWINDOWS} stdcall {$else} cdecl {$endif};
isc_start_multiple: function(var user_status: TSQLDBFirebirdStatus; var tra_handle: pointer;
count: Smallint; vector: PISCTEB): ISCStatus;
{$ifdef MSWINDOWS} stdcall {$else} cdecl {$endif};
isc_rollback_transaction: function(var user_status: TSQLDBFirebirdStatus; var tra_handle: pointer): ISCStatus;
{$ifdef MSWINDOWS} stdcall {$else} cdecl {$endif};
isc_commit_transaction: function(var user_status: TSQLDBFirebirdStatus; var tra_handle: pointer): ISCStatus;
{$ifdef MSWINDOWS} stdcall {$else} cdecl {$endif};
isc_commit_retaining: function(var user_status: TSQLDBFirebirdStatus; var tra_handle: pointer): ISCStatus;
{$ifdef MSWINDOWS} stdcall {$else} cdecl {$endif};
isc_dsql_allocate_statement: function(var user_status: TSQLDBFirebirdStatus; var db_handle: pointer;
var stmt_handle: pointer): ISCStatus;
{$ifdef MSWINDOWS} stdcall {$else} cdecl {$endif};
isc_dsql_free_statement: function(var user_status: TSQLDBFirebirdStatus; var stmt_handle: pointer;
option: Word): ISCStatus;
{$ifdef MSWINDOWS} stdcall {$else} cdecl {$endif};
isc_dsql_execute: function(var user_status: TSQLDBFirebirdStatus; var tra_handle: pointer;
var stmt_handle: pointer; dialect: Word; sqlda: PXSQLDA): ISCStatus;
{$ifdef MSWINDOWS} stdcall {$else} cdecl {$endif};
isc_dsql_describe: function(var user_status: TSQLDBFirebirdStatus; var stmt_handle: pointer;
dialect: Word; sqlda: PXSQLDA): ISCStatus;
{$ifdef MSWINDOWS} stdcall {$else} cdecl {$endif};
isc_dsql_prepare: function(var user_status: TSQLDBFirebirdStatus; var tra_handle: pointer;
var stmt_handle: pointer; buflen: Word; buf: PAnsiChar; dialect: Word;
sqlda: PXSQLDA): ISCStatus;
{$ifdef MSWINDOWS} stdcall {$else} cdecl {$endif};
isc_dsql_sql_info: function(var user_status: TSQLDBFirebirdStatus; var stmt_handle: pointer;
itemlsength: Smallint; items: PAnsiChar; buflen: Smallint; buf: PAnsiChar): ISCStatus;
{$ifdef MSWINDOWS} stdcall {$else} cdecl {$endif};
isc_create_blob2: function(var user_status: TSQLDBFirebirdStatus; var db_handle: pointer;
var tra_handle: pointer; var blob_handle: pointer; blob_id: PISCQuad;
bpb_length: Smallint; bpb: PAnsiChar): ISCStatus;
{$ifdef MSWINDOWS} stdcall {$else} cdecl {$endif};
isc_put_segment: function(var user_status: TSQLDBFirebirdStatus; var blob_handle: pointer;
buffer_length: Word; buffer: PAnsiChar): ISCStatus;
{$ifdef MSWINDOWS} stdcall {$else} cdecl {$endif};
isc_close_blob: function(var user_status: TSQLDBFirebirdStatus; var blob_handle: pointer): ISCStatus;
{$ifdef MSWINDOWS} stdcall {$else} cdecl {$endif};
isc_dsql_set_cursor_name: function(var user_status: TSQLDBFirebirdStatus; var stmt_handle: pointer;
cursorname: PAnsiChar; type_: Word=0): ISCStatus;
{$ifdef MSWINDOWS} stdcall {$else} cdecl {$endif};
public
/// load the Firebird library
// - and retrieve all SQL*() addresses for FIREBIRD_ENTRIES[] items
constructor Create(aEmbedded: boolean);
/// raise an EFirebirdException on error
procedure Check(Status: ISCStatus; var PrivateStatus: TSQLDBFirebirdStatus);
/// wrapper around isc_start_multiple() function
procedure StartTransaction(var PrivateStatus: TSQLDBFirebirdStatus;
var Database, Transaction: pointer);
/// the loaded library name
property LibName: TFileName read fLibName;
/// if the library is the "Firebird Embedded Server" version
property Embedded: boolean read fEmbedded;
end;
const
isc_dpb_version1 = 1;
FIREBIRD_ENTRIES: array[0..20] of PChar =
('isc_attach_database','isc_detach_database','isc_create_database',
'isc_sqlcode','isc_sql_interprete','fb_interpret',
'isc_dsql_execute_immediate',
'isc_start_multiple','isc_rollback_transaction','isc_commit_transaction',
'isc_commit_retaining',
'isc_dsql_allocate_statement','isc_dsql_free_statement',
'isc_dsql_execute','isc_dsql_describe','isc_dsql_prepare',
'isc_dsql_sql_info','isc_create_blob2','isc_put_segment','isc_close_blob',
'isc_dsql_set_cursor_name');
{ TSQLDBFirebirdConnection }
procedure TSQLDBFirebirdConnection.Connect;
var Log: ISynLog;
ServerName, SQL: RawByteString;
begin
Log := SynDBLog.Enter;
Disconnect;
if fProperties.ServerName='' then
raise EFirebirdException.Create('ServerName missing');
ServerName := CurrentAnsiConvert.UTF8ToAnsi(fProperties.ServerName);
with TFirebirdLib(fFirebirdInstance) do begin
if fEmbedded and not FileExists(
{$ifdef UNICODE}UTF8ToString(fProperties.ServerName){$else}ServerName{$endif}) then begin
SQL := fProperties.SQLCreateDatabase(fProperties.ServerName,
TSQLDBFirebirdConnectionProperties(fProperties).DefaultPageSize);
Log.Log(sllDB,'No DB file found -> % using %',
[SQL,fLibName],fProperties);
Check(isc_dsql_execute_immediate(fStatus,fDatabase,nil,0,pointer(SQL)),fStatus);
exit;
end;
Log.Log(sllDB,'Connect to "%" as "%" using %',
[fProperties.ServerName,fProperties.UserID,fLibName],fProperties);
BufClear;
BufAddStr(ord(dpb_user_name),fProperties.UserID);
BufAddStr(ord(dpb_password),fProperties.PassWord);
Check(isc_attach_database(fStatus,length(ServerName),pointer(ServerName),
fDatabase,fBufLen,fBuf),fStatus);
inherited Connect; // notify any re-connection
end;
end;
constructor TSQLDBFirebirdConnection.Create(aProperties: TSQLDBConnectionProperties);
begin
if not aProperties.InheritsFrom(TSQLDBFirebirdConnectionProperties) then
raise EFirebirdException.Create('Invalid TSQLDBFirebirdConnection.Create');
fFirebirdProperties := TSQLDBFirebirdConnectionProperties(aProperties);
fFirebirdInstance := fFirebirdProperties.fFirebirdInstance;
fEmbedded := TFirebirdLib(fFirebirdProperties.fFirebirdInstance).Embedded;
GetMem(fBuf,MAX_DPB_SIZE);
inherited;
end;
destructor TSQLDBFirebirdConnection.Destroy;
begin
inherited Destroy;
FreeMem(fBuf);
end;
procedure TSQLDBFirebirdConnection.Disconnect;
begin
try
inherited Disconnect; // flush any cached statement
finally
if fFirebirdInstance=nil then
raise EFirebirdException.Create('No lib');
if fDatabase<>nil then
with TFirebirdLib(fFirebirdInstance) do begin
if TransactionCount>0 then
Rollback;
Check(isc_detach_database(fStatus,fDatabase),fStatus);
end;
end;
end;
function TSQLDBFirebirdConnection.IsConnected: boolean;
begin
result := fDatabase<>nil;
end;
function TSQLDBFirebirdConnection.NewStatement: TSQLDBStatement;
begin
result := TSQLDBFirebirdStatement.Create(self);
end;
procedure TSQLDBFirebirdConnection.Commit;
begin
inherited Commit;
if fTransaction=nil then
raise EFirebirdException.Create('Commit');
with TFirebirdLib(fFirebirdInstance) do
try
Check(isc_commit_transaction(fStatus,fTransaction),fStatus);
except
inc(fTransactionCount); // the transaction is still active
raise;
end;
end;
procedure TSQLDBFirebirdConnection.Rollback;
begin
inherited RollBack;
if fTransaction=nil then
raise EFirebirdException.Create('Rollback');
with TFirebirdLib(fFirebirdInstance) do
Check(isc_rollback_transaction(fStatus,fTransaction),fStatus);
end;
procedure TSQLDBFirebirdConnection.StartTransaction;
begin
if TransactionCount>0 then
raise EFirebirdException.Create('TSQLDBFirebirdConnection do not provide nested transactions');
inherited StartTransaction;
if fTransaction<>nil then
raise EFirebirdException.Create('StartTransaction');
inc(fTransactionID);
TFirebirdLib(fFirebirdInstance).StartTransaction(fStatus,fDatabase,fTransaction);
end;
procedure TSQLDBFirebirdConnection.BufClear;
begin
fBuf[0] := isc_dpb_version1;
fBufLen := 1;
end;
procedure TSQLDBFirebirdConnection.BufAddStr(dpb: byte; const Value: RawUTF8;
DoNotStoreIfBlank: boolean);
var L: integer;
begin
L := length(Value);
if (L=0) and DoNotStoreIfBlank then
exit;
if L>255 then
raise EFirebirdException.Create('BufAddStr size>255');
if fBufLen+L>=MAX_DPB_SIZE-5 then
raise EFirebirdException.Create('BufAdd overflow');
fBuf[fBufLen] := dpb;
inc(fBufLen);
fBuf[fBufLen] := L;
inc(fBufLen);
move(pointer(Value)^,fBuf[fBufLen],L);
inc(fBufLen,L);
end;
procedure TSQLDBFirebirdConnection.BufAddI32(dpb: byte; Value: cardinal);
begin
fBuf[fBufLen] := dpb;
inc(fBufLen);
case Value of
0..255: begin
fBuf[fBufLen] := 1;
inc(fBufLen);
fBuf[fBufLen] := Value;
inc(fBufLen);
end;
256..65535: begin
fBuf[fBufLen] := 2;
inc(fBufLen);
PWord(@fBuf[fBufLen])^ := Value;
inc(fBufLen,2);
end;
else begin
fBuf[fBufLen] := 4;
inc(fBufLen);
PCardinal(@fBuf[fBufLen])^ := Value;
inc(fBufLen,4);
end;
end;
end;
procedure TSQLDBFirebirdConnection.BufAddI8(dpb, Value: byte);
begin
fBuf[fBufLen] := dpb;
inc(fBufLen);
fBuf[fBufLen] := Value;
inc(fBufLen);
end;
{ TSQLDBFirebirdStatement }
function TSQLDBFirebirdStatement.ColumnBlob(Col: integer): RawByteString;
begin
end;
function TSQLDBFirebirdStatement.ColumnUTF8(Col: integer): RawUTF8;
begin
end;
function TSQLDBFirebirdStatement.ColumnCurrency(Col: integer): currency;
begin
end;
function TSQLDBFirebirdStatement.ColumnDateTime(Col: integer): TDateTime;
begin
end;
function TSQLDBFirebirdStatement.ColumnDouble(Col: integer): double;
begin
end;
function TSQLDBFirebirdStatement.ColumnInt(Col: integer): Int64;
begin
end;
function TSQLDBFirebirdStatement.ColumnNull(Col: integer): boolean;
begin
end;
procedure TSQLDBFirebirdStatement.ColumnsToJSON(WR: TJSONWriter);
var col, METHODTOBEWRITTEN: integer;
begin
if (CurrentRow<=0) then
raise EFirebirdException.Create('TSQLDBFirebirdStatement.ColumnsToJSON() with no prior Step');
if WR.Expand then
WR.Add('{');
for col := 0 to fColumnCount-1 do // fast direct conversion from OleDB buffer
with fColumns[col] do begin
if WR.Expand then
WR.AddFieldName(ColumnName); // add '"ColumnName":'
// ....
WR.Add(',');
end;
WR.CancelLastComma; // cancel last ','
if WR.Expand then
WR.Add('}');
end;
constructor TSQLDBFirebirdStatement.Create(aConnection: TSQLDBConnection);
begin
if not aConnection.InheritsFrom(TSQLDBFirebirdConnection) then
raise EFirebirdException.CreateFmt('%s.Create expects a TSQLDBFirebirdConnection',[ClassName]);
inherited Create(aConnection);
end;
destructor TSQLDBFirebirdStatement.Destroy;
begin
try
with TFirebirdLib(TSQLDBFirebirdConnection(fConnection).fFirebirdInstance) do begin
if fMain.Statement<>nil then
Check(isc_dsql_free_statement(fStatus,fMain.Statement,DSQL_drop),fStatus);
if fAutoCommit.Statement<>nil then
Check(isc_dsql_free_statement(fStatus,fAutoCommit.Statement,DSQL_drop),fStatus);
if fAutoCommit.Transaction<>nil then
Check(isc_rollback_transaction(fStatus,fAutoCommit.Transaction),fStatus);
end;
finally
inherited Destroy;
end;
end;
function TSQLDBFirebirdStatement.UpdateCount: integer;
begin
end;
procedure TSQLDBFirebirdStatement.Prepare(const aSQL: RawUTF8; ExpectResults: Boolean);
var Log: ISynLog;
Conn: TSQLDBFirebirdConnection;
begin
Log := SynDBLog.Enter(self);
if (fColumnCount>0) or (fAutoCommit.Statement<>nil) or (fCurrent<>nil) then
raise EFirebirdException.CreateFmt('%s.Prepare should be called only once',[ClassName]);
// 1. process SQL
inherited Prepare(aSQL,ExpectResults); // set fSQL + Connect if necessary
// 2. prepare statement and bind result columns (if any)
Conn := TSQLDBFirebirdConnection(fConnection);
with TFirebirdLib(Conn.fFirebirdInstance) do
try
// always prepare auto-commit transaction and associated statement
StartTransaction(fStatus,Conn.fDatabase,fAutoCommit.Transaction);
Check(isc_dsql_allocate_statement(fStatus,Conn.fDatabase,fAutoCommit.Statement),fStatus);
Check(isc_dsql_prepare(fStatus,fAutoCommit.Transaction,fAutoCommit.Statement,length(fSQL),
pointer(fSQL),SQL_DIALECT,nil),fStatus);
// execution within a main transaction will be prepared on purpose
except
on E: Exception do begin
Log.Log(sllError,E);
raise;
end;
end;
end;
function TSQLDBFirebirdStatement.ReleaseMainStatementIfAny: boolean;
var MainConn: TSQLDBFirebirdConnection;
begin
if (fAutoCommit.Statement=nil) or (fAutoCommit.Transaction=nil) then
raise EFirebirdException.Create('Prepare should be called before ExecutePrepared/Reset');
MainConn := TSQLDBFirebirdConnection(fConnection);
with TFirebirdLib(MainConn.fFirebirdInstance) do
if (fMain.Transaction<>nil) and (MainConn.fTransactionID<>fMain.TransactionID) then
try
Check(isc_dsql_free_statement(fStatus,fMain.Statement,DSQL_drop),fStatus);
Check(isc_rollback_transaction(fStatus,fMain.Transaction),fStatus);
result := true;
finally // force nil: connection may be lost
fMain.Statement := nil;
fMain.Transaction := nil;
end else
result := false;
end;
// allocate a TByteDynArray to contain
function XSQLDAAllocate(var Buf: TByteDynArray; number: integer): PXSQLDA;
begin
SetLength(Buf,sizeof(XSQLDA)+(number-1)*sizeof(XSQLVar));
result := pointer(Buf);
result.version := SQLDA_CURRENT_VERSION;
result.sqln := number;
result.sqld := number;
end;
procedure TSQLDBFirebirdStatement.ExecutePrepared;
var MainConn: TSQLDBFirebirdConnection;
i: integer;
begin
inherited ExecutePrepared; // set fConnection.fLastAccessTicks
MainConn := TSQLDBFirebirdConnection(fConnection);
with TFirebirdLib(MainConn.fFirebirdInstance) do begin
// 1. release any deprecated main transaction/statement
ReleaseMainStatementIfAny;
// 2. decide which transaction/statement is to be used
if MainConn.fTransaction<>nil then begin
// main transaction is active -> prepare a statement for it (if needed)
if fMain.Transaction=nil then begin
fMain.Transaction := MainConn.fTransaction;
fMain.TransactionID := MainConn.fTransactionID;
assert(fMain.Statement=nil);
Check(isc_dsql_allocate_statement(fStatus,MainConn.fDatabase,fMain.Statement),fStatus);
Check(isc_dsql_prepare(fStatus,fMain.Transaction,fMain.Statement,length(fSQL),
pointer(fSQL),SQL_DIALECT,nil),fStatus);
end;
fCurrent := @fMain;
end else
// we are in auto-commit mode
fCurrent := @fAutoCommit;
// 3. bind parameters
// 4. execute the statement
if fExpectResults then
// (re)open result cursor for queries
Check(isc_dsql_set_cursor_name(fStatus,fCurrent.Statement,'SynDB'),fStatus);
Check(isc_dsql_execute(fStatus,fCurrent.Transaction,fCurrent.Statement,SQL_DIALECT,pointer(fInput)),fStatus);
if (fCurrent=@fAutoCommit) and not fExpectResults then
// emulate AUTOCOMMIT mode
Check(isc_commit_retaining(fStatus,fAutoCommit.Transaction),fStatus);
// 5. prepare results
if fOutput=nil then begin // allocate fOutput buffer (reuse the same)
Check(isc_dsql_describe(fStatus,fCurrent.Statement,SQL_DIALECT,XSQLDAAllocate(fOutput,32)),fStatus);
with PXSQLDA(fOutput)^ do
if sqld>sqln then
Check(isc_dsql_describe(fStatus,fCurrent.Statement,SQL_DIALECT,XSQLDAAllocate(fOutput,sqld)),fStatus);
with PXSQLDA(fOutput)^ do begin
for i := 0 to sqld-1 do
with PSQLDBColumnProperty(fColumn.AddAndMakeUniqueName(sqlvar[i].ColumnName))^ do begin
ColumnValueDBType := sqlvar[i].sqltype;
end;
end;
end;
end;
end;
function TSQLDBFirebirdStatement.Step(SeekFirst: boolean): boolean;
begin
result := false;
end;
procedure TSQLDBFirebirdStatement.Reset;
begin
ReleaseMainStatementIfAny; // global transaction context may have changed
inherited Reset;
with TFirebirdLib(TSQLDBFirebirdConnection(fConnection).fFirebirdInstance) do begin
if fExpectResults and (fCurrent<>nil) then
// release opened cursor for queries
Check(isc_dsql_free_statement(fStatus,fCurrent.Statement,DSQL_close),fStatus);
end;
end;
{ TFirebirdLib }
procedure TFirebirdLib.Check(Status: ISCStatus; var PrivateStatus: TSQLDBFirebirdStatus);
procedure RaiseEFirebirdException;
var Code: integer;
sql,tmp: array[byte] of AnsiChar;
Vec: pointer;
msg: string;
begin
Code := isc_sqlcode(PrivateStatus);
isc_sql_interprete(Code,sql,sizeof(sql));
Vec := @PrivateStatus;
while fb_interpret(tmp,sizeof(tmp),@Vec)>0 do begin
msg := msg+' '+SysUtils.Trim(string(tmp));
if msg[length(msg)]<>'.' then
msg := msg+'.';
end;
raise EFirebirdException.CreateFmt('Firebird Error %x:%s [ SQLCODE=%d (%s) ]',
[Status,msg,Code,sql]);
end;
const
CLASS_MASK = $F0000000; // Defines the code as warning, error, info, or other
CLASS_ERROR = 0; // Code represents an error
CLASS_WARNING = 1; // Code represents a warning
CLASS_INFO = 2; // Code represents an information msg
begin
if (Status<>0) and ((Status and CLASS_MASK) shr 30=CLASS_ERROR) then
RaiseEFirebirdException;
end;
constructor TFirebirdLib.Create(aEmbedded: boolean);
var P: PPointer;
i: integer;
{$ifdef MSWINDOWS}
Key: HKEY;
Size: Cardinal;
HR: Integer;
CurrentDir: TFileName;
{$endif}
begin
if aEmbedded then begin
fLibName := ExeVersion.ProgramFilePath;
if FileExists(LibName+FBLIBNAME[true]) then
fLibName := LibName+FBLIBNAME[true] else
fLibName := LibName+'Firebird\'+FBLIBNAME[true];
if FileExists(fLibName) then begin
{$ifdef MSWINDOWS}
CurrentDir := GetCurrentDir;
SetCurrentDir(ExtractFilePath(fLibName));
{$endif}
fHandle := SafeLoadLibrary(LibName);
{$ifdef MSWINDOWS}
SetCurrentDir(CurrentDir);
{$endif}
if fHandle<>0 then
fEmbedded := true;
end;
end else begin
fLibName := FBLIBNAME[false];
fHandle := SafeLoadLibrary(fLibName);
{$ifdef MSWINDOWS}
if fHandle=0 then begin // not found in path -> search from registry
HR := RegOpenKeyEx(HKEY_LOCAL_MACHINE,
'SOFTWARE\Firebird Project\Firebird Server\Instances',0,KEY_READ,Key);
if HR=ERROR_SUCCESS then begin
HR := RegQueryValueEx(Key,'DefaultInstance',nil,nil,nil,@Size);
if HR=ERROR_SUCCESS then begin
SetLength(fLibName,Size div SizeOf(Char));
HR := RegQueryValueEx(Key,'DefaultInstance',nil,nil,pointer(fLibName),@Size);
if HR=ERROR_SUCCESS then begin
fLibName := IncludeTrailingPathDelimiter(SysUtils.Trim(fLibName));
if FileExists(LibName+FBLIBNAME[false]) then
fLibName := LibName+FBLIBNAME[false] else
fLibName := LibName+('bin\'+FBLIBNAME[false]);
fHandle := SafeLoadLibrary(LibName);
end;
end;
RegCloseKey(Key);
end;
end;
{$endif}
end;
if fHandle=0 then
raise EFirebirdException.CreateFmt('Unable to find Firebird Client Interface %s',
[FBLIBNAME[aEmbedded]]);
SynDBLog.Add.Log(sllDebug,'Loading %',StringToUTF8(LibName));
P := @@isc_attach_database;
for i := 0 to High(FIREBIRD_ENTRIES) do begin
P^ := GetProcAddress(fHandle,FIREBIRD_ENTRIES[i]);
if P^=nil then begin
FreeLibrary(fHandle);
fHandle := 0;
raise EFirebirdException.CreateFmt('Invalid %s: missing %s',[LibName,Firebird_ENTRIES[i]]);
end;
inc(P);
end;
end;
procedure TFirebirdLib.StartTransaction(var PrivateStatus: TSQLDBFirebirdStatus;
var Database, Transaction: pointer);
var Vector: TISCTEB;
begin
Vector.Handle := @Database;
Vector.Len := 0;
Vector.Address := nil;
Check(isc_start_multiple(PrivateStatus,Transaction,1,@Vector),PrivateStatus);
end;
{ TSQLDBFirebirdConnectionProperties }
var
// define two GlobalFirebird[Embedded] libraries instances
GlobalFirebird: array[boolean] of TFirebirdLib;
function Firebird(Embedded: boolean): TFirebirdLib;
const LIBNOTAVAILABLE: TFirebirdLib = pointer(-1);
begin
if GlobalFirebird[Embedded]=nil then
try
result := TFirebirdLib.Create(Embedded);
GarbageCollector.Add(result);
GlobalFirebird[Embedded] := result;