-
Notifications
You must be signed in to change notification settings - Fork 58
/
FIBQuery.pas
6348 lines (5803 loc) · 166 KB
/
FIBQuery.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
{***************************************************************}
{ FIBPlus - component library for direct access to Firebird and }
{ InterBase databases }
{ }
{ FIBPlus is based in part on the product }
{ Free IB Components, written by Gregory H. Deatz for }
{ Hoagland, Longo, Moran, Dunst & Doukas Company. }
{ mailto:gdeatz@hlmdd.com }
{ }
{ Copyright (c) 1998-2013 Devrace Ltd. }
{ Written by Serge Buzadzhy (buzz@devrace.com) }
{ }
{ ------------------------------------------------------------- }
{ FIBPlus home page: http://www.fibplus.com/ }
{ FIBPlus support : http://www.devrace.com/support/ }
{ ------------------------------------------------------------- }
{ }
{ Please see the file License.txt for full license information }
{***************************************************************}
unit FIBQuery;
interface
{$I FIBPlus.inc}
uses
SysUtils, Classes, ibase,IB_Intf, IB_Externals,FIBPlatforms,
DB, fib, FIBDatabase, StdFuncs,IB_ErrorCodes,SqlTxtRtns,pFIBProps,
pFIBInterfaces {, FIBXMLDataSetReader}
{$IFDEF SUPPORT_ARRAY_FIELD}, pFIBArray {$ENDIF}
{$IFDEF D6+},FMTBcd, Variants{$ENDIF} ;
type
(*Stream Events*)
TCallBackBlobReadWrite= procedure(BlobSize:integer; BytesProcessing:integer; var Stop:boolean) of object;
TFIBQuery = class;
TFIBXSQLDA = class;
TFIBXSQLVAR = class;
TExtDescribeSQLVar =
record
sql_relation_alias: array[0..LENGTH_METANAMES-1] of AnsiChar;
end;
TTypeSetToParam =(tspNull,tspIsNullable,tspScale, tspValue,tspSqlVar);
(* TFIBXSQLVAR *)
TFIBXSQLVAR = class(TObject)
private
function GetAsBoolean: boolean;
procedure SetAsBoolean(const Value: boolean);
protected
FIndex: Integer;
FModified: Boolean;
FName: string;
FQuery: TFIBQuery;
FVariantFalse,
FVariantTrue: Variant;
FXSQLVAR: PXSQLVAR; // Point to the PXSQLVAR in the owner object
FParent : TFIBXSQLDA;
// Added variables
FIsMacro :boolean;
FQuoted :boolean;
FOldValue:Variant; // Value Param from last ExecQuery
FDefMacroValue :string;
FSrvSQLType :integer;
FSrvSQLSubType :integer;
FSrvSQLLen :Smallint;
FSrvSQLScale :Smallint;
FInWhereClause :boolean;
FCanForceIsNull:boolean;
FInitialized :boolean;
FBeginPosInText:integer;
FEndPosInText :integer;
FIsDefferedSetting:boolean;
FStreamValue :TMemoryStream;
FWideTempValue :WideString;
FParDataIsPrepared:boolean;
{$IFDEF SUPPORT_ARRAY_FIELD}
vFIBArray:TpFIBArray;
{$ENDIF}
function GetAsInt64: Int64;
function GetAsCurrency: Currency;
{$IFNDEF NO_USE_COMP}
function GetAsComp: Comp;
{$ENDIF}
function GetAsDateTime: TDateTime;
function GetAsTimeStamp: TTimeStamp;
function GetAsDouble: Double;
function GetAsFloat: Double;
function GetAsSingle: Float;
function GetAsLong: Long;
function GetAsPointer: Pointer;
function GetAsQuad: TISC_QUAD;
function GetAsShort: Short;
function GetAsString: string;
function GetAsAnsiString: Ansistring;
function GetAsVariant: Variant;
function GetAsExtended: Extended;
function GetAsXSQLVAR: PXSQLVAR;
function GetIsNull: Boolean;
function GetIsNullable: Boolean;
function GetSize: Integer;
function GetSQLType: Integer;
function GetServerSQLType:Integer;
function GetSQLSubtype: Short;
function GetServerSQLSubType:Integer;
function GetServerSQLSize:Integer;
function GetServerSQLScale:Integer;
// Array Support
{$IFDEF SUPPORT_ARRAY_FIELD}
procedure CheckArrayType;
function GetDimensionCount:Integer;
function GetDimension(Index: Integer): TISC_ARRAY_BOUND;
function GetSliceSize:integer;
function GetElementType:TFieldType;
function GetArraySize:integer;
{$ENDIF}
//
procedure SetValue(aSQLType,aSize:integer;ValueType:TTypeSetToParam;const aValue;ws:PWideString=nil);
procedure SetAsCurrency(aValue: Currency);
{$IFNDEF NO_USE_COMP}
procedure SetAsComp(aValue: comp); //patchInt64A
{$ENDIF}
procedure SetAsInt64(aValue: Int64);
procedure SetAsDateTime(aValue: TDateTime);
procedure SetAsTime(aValue: TDateTime);
procedure SetAsDate(aValue: TDateTime);
procedure SetAsTimeStamp(aValue: TTimeStamp);
procedure SetAsDouble(aValue: Double);
procedure SetAsFloat(aValue: Double);
procedure SetAsSingle(aValue: Float);
procedure SetAsExtended(aValue: Extended);
procedure SetAsLong(aValue: Long);
procedure SetAsQuad(aValue: TISC_QUAD);
procedure SetAsShort(aValue: Short);
procedure InternalSetAsString(aValue:Pointer; IsWide:boolean; AdjustDeffered:boolean=False);
procedure SetAsString(const aValue: string);
procedure SetAsWideString(const aValue: WideString);
procedure SetAsAnsiString(const aValue: Ansistring);
procedure SetAsStrData(Len:Integer;const aValue);
function GetAsWideString:WideString;
procedure SetAsVariant(Value: Variant);
procedure SetAsXSQLVAR(aValue: PXSQLVAR);
procedure SetIsNull(aValue: Boolean);
procedure SetIsNullable(aValue: Boolean);
function GetScale: integer;
procedure SetScale(Value: integer);
function GetAsBcd: TBcd;
procedure SetAsBcd(Value: TBcd);
function GetAsGUID: TGUID;
procedure SetAsGuid(aValue: TGUID);
procedure SetSQLLen(A:SmallInt);
public
constructor Create(AParent: TFIBXSQLDA);
destructor Destroy; override;
procedure Assign(Source: TFIBXSQLVAR);
// procedure SetSQLLen(A:SmallInt);
function IsNumericType(SQLType:Integer):boolean;
function IsRealType(SQLType:Integer):boolean;
function IsDateTimeType(SQLType:Integer):boolean;
procedure LoadFromFile(const FileName: string); overload;
procedure LoadFromFile(const FileName: string;cb: TCallBackBlobReadWrite); overload;
procedure LoadFromStream(Stream: TStream);
procedure SaveToFile(const FileName: string;cb: TCallBackBlobReadWrite=nil);
procedure SaveToFileStream(const FileName: string);
procedure SaveToStream(Stream: TStream);
procedure Clear;
function IsParam:boolean;
function IsBlob:boolean;
function SqlName:string;
function AliasName:string;
function RelationName:string;
function CharacterSet :string;
// Array Support
{$IFDEF SUPPORT_ARRAY_FIELD}
function IsArray:boolean;
function GetArrayElement(Indexes: array of Integer):Variant;
function GetArrayValues:Variant;
procedure SetArrayValue(Value:Variant);
{$ENDIF}
function IsDefMacroValue :boolean;
procedure SetDefMacroValue;
//
property AsCurrency: Currency read GetAsCurrency write SetAsCurrency;
{$IFNDEF NO_USE_COMP}
property AsComp: comp read GetAsComp write SetAsComp;
{$ENDIF}
property AsExtended: Extended read GetAsExtended write SetAsExtended;
property AsInt64: Int64 read GetAsInt64 write SetAsInt64;
property AsBcd: TBcd read GetAsBcd write SetAsBcd;
property AsGuid:TGUID read GetAsGUID write SetAsGUID ;
property AsDateTime: TDateTime read GetAsDateTime write SetAsDateTime;
property AsDate: TDateTime read GetAsDateTime write SetAsDate;
property AsTime: TDateTime read GetAsDateTime write SetAsTime;
property AsTimeStamp:TTimeStamp read GetAsTimeStamp write SetAsTimeStamp;
property AsDouble: Double read GetAsDouble write SetAsDouble;
property AsFloat: Double read GetAsFloat write SetAsFloat;
property AsSingle: Float read GetAsSingle write SetAsSingle;
property AsInteger: Integer read GetAsLong write SetAsLong;
property AsLong: Long read GetAsLong write SetAsLong;
property AsPointer: Pointer read GetAsPointer ;
property AsQuad: TISC_QUAD read GetAsQuad write SetAsQuad;
property AsShort: Short read GetAsShort write SetAsShort;
property AsString: string read GetAsString write SetAsString;
property AsWideString: WideString read GetAsWideString write SetAsWideString;
property AsAnsiString: AnsiString read GetAsAnsiString write SetAsAnsiString;
property AsVariant: Variant read GetAsVariant write SetAsVariant;
property AsXSQLVAR: PXSQLVAR read GetAsXSQLVAR write SetAsXSQLVAR;
property AsBoolean: boolean read GetAsBoolean write SetAsBoolean;
property Data: PXSQLVAR read FXSQLVAR write FXSQLVAR;
property IsNull: Boolean read GetIsNull write SetIsNull;
property IsNullable: Boolean read GetIsNullable write SetIsNullable;
property Scale: Integer read GetScale write SetScale;
property Index: Integer read FIndex;
property Modified: Boolean read FModified write FModified;
property Name: string read FName;
property Size: Integer read GetSize;
property ServerSize: Integer read GetServerSQLSize;
property SQLType: Integer read GetSQLType;
property ServerSQLType: Integer read GetServerSQLType;
property SQLSubtype:Short read GetSQLSubtype;
property ServerSQLSubType: Integer read GetServerSQLSubType;
property Value: Variant read GetAsVariant write SetAsVariant;
property OldValue: Variant read FOldValue;
property VariantFalse: Variant read FVariantFalse write FVariantFalse;
property VariantTrue: Variant read FVariantTrue write FVariantTrue;
// Added properties
property IsMacro:boolean read FIsMacro write FIsMacro;
property Quoted :boolean read FQuoted write FQuoted;
property DefMacroValue:string read FDefMacroValue write FDefMacroValue;
property InWhereClause:boolean read FInWhereClause;
property BeginPosInText:integer read FBeginPosInText;
property EndPosInText :integer read FEndPosInText;
// Array Support
{$IFDEF SUPPORT_ARRAY_FIELD}
property FIBArray:TpFIBArray read vFIBArray;
property DimensionCount:integer read GetDimensionCount;
property Dimension[Index: Integer]: TISC_ARRAY_BOUND read GetDimension;
property ElementType:TFieldType read GetElementType;
property ArraySize:Integer read GetArraySize;
{$ENDIF}
end;
TFIBXSQLVARArray = array[0..0] of TFIBXSQLVAR;
PFIBXSQLVARArray = ^TFIBXSQLVARArray;
(* TFIBXSQLVAR *)
TFIBXSQLDA = class(TObject)
private
FEquelNames: TStringList;
FCachedNames:TStringList;
FCount: Integer;
FHasDefferedSettings:Boolean;
procedure AdjustDefferedSettings;
protected
FNames : TStringList;
FQuery: TFIBQuery;
FSize: Integer;
FXSQLDA: PXSQLDA;
FXSQLVARs: PFIBXSQLVARArray; // array of FIBXQLVARs
FIsParams: boolean;
function GetModified: Boolean;
function GetNames: string;
function GetRecordSize: Integer;
function GetXSQLDA: PXSQLDA;
function GetXSQLVAR(Idx: Integer): TFIBXSQLVAR;
function GetXSQLVARByName(const Idx: string): TFIBXSQLVAR;
procedure Initialize;
procedure SetCount(Value: Integer);
procedure AddName(const FieldName: string; Idx: Integer; aQuoted:boolean);
procedure SetUnModifiedToVars;
public
constructor Create(aIsParams:boolean);
destructor Destroy; override;
procedure ClearValues;
function FindParam(const aParamName: string): TFIBXSQLVAR;
function ParamByName(const aParamName: string): TFIBXSQLVAR;
procedure AssignValues(SourceSQLDA:TFIBXSQLDA);
property Query: TFIBQuery read FQuery;
property AsXSQLDA: PXSQLDA read GetXSQLDA;
property ByName[const Idx: string]: TFIBXSQLVAR read GetXSQLVARByName;
property Count: Integer read FCount write SetCount;
property Modified: Boolean read GetModified;
property Names: string read GetNames;
property RecordSize: Integer read GetRecordSize;
property Vars[Idx: Integer]: TFIBXSQLVAR read GetXSQLVAR; default;
end;
(* TFIBBatch - basis for batch input and batch output objects. *)
TBatchState = (bsNotPrepared,bsFileReady,bsInProcess,bsInError);
TFIBBatch = class(TObject)
protected
FFilename: string;
FColumns: TFIBXSQLDA;
FParams : TFIBXSQLDA;
FState : TBatchState;
FVersion: integer;
FCharset:Ansistring;
public
constructor Create;
procedure ReadyStream; virtual; abstract;
property Columns: TFIBXSQLDA read FColumns;
property Filename: string read FFilename write FFilename;
property Params: TFIBXSQLDA read FParams;
property State : TBatchState read FState ;
end;
(* TFIBBatchInputStream - see FIBMiscellaneous for good examples. *)
TFIBBatchInputStream = class(TFIBBatch)
public
function ReadParameters: Boolean; virtual; abstract;
end;
TFIBBatchInputStreamClass = class of TFIBBatchInputStream;
(* TFIBBatchOutputStream - see FIBMiscellaneous for good examples. *)
TFIBBatchOutputStream = class(TFIBBatch)
protected
public
function WriteColumns: Boolean; virtual; abstract;
end;
TFIBBatchOutputStreamClass = class of TFIBBatchOutputStream;
(* TFIBQuery *)
TFIBSQLTypes = (SQLUnknown, SQLSelect, SQLInsert,
SQLUpdate, SQLDelete, SQLDDL,
SQLGetSegment, SQLPutSegment,
SQLExecProcedure, SQLStartTransaction,
SQLCommit, SQLRollback,
SQLSelectForUpdate, SQLSetGenerator,SQLSavePointOperation
);
TOnSQLFetch =procedure (RecordNumber:integer; var StopFetching:boolean
) of object;
TBatchOperation =(boInput,boOutput,boOutputToQuery);
TBatchAction =(baContinue,baStop,baSkip);
TBatchErrorAction =(beFail, beAbort, beRetry,beIgnore);
TOnBatching =
procedure(BatchOperation:TBatchOperation;RecNumber:integer;var BatchAction :TBatchAction) of object;
TOnBatchError = procedure(E:EFIBError;var BatchErrorAction:TBatchErrorAction) of object;
{
TOnBatchXMLFile =
procedure (Reader:TXMLDataSetFileReader; const CurRec:TRecordDesc; const RecordNo:integer; var Stop:boolean) of object;
}
TAllRowsAffected =
record
Updates: integer;
Deletes: integer;
Selects: integer;
Inserts: integer;
end;
TQueryRunStateValues=(qrsInPrepare,qrsInExecute,qrsInClose);
TQueryRunState = set of TQueryRunStateValues;
TFIBQuery = class(TComponent,ISQLObject,IFIBQuery)
private
FOnBatching:TOnBatching;
FDoParamCheck:boolean;
FParser: TSQLParser;
FTransactionEnding:TNotifyEvent;
FTransactionEnded :TNotifyEvent;
FBeforeExecute :TNotifyEvent;
FAfterExecute :TNotifyEvent;
FAfterFirstFetch :TNotifyEvent;
{$IFDEF CSMonitor}
FCSMonitorSupport: TCSMonitorSupport;
procedure SetMonitorSupport(Value:TCSMonitorSupport);
function GetCSMonText: string;
{$ENDIF}
function GetSQLKind: TSQLKind;
protected
FBase: TFIBBase;
FBOF, // At BOF?
FEof, // At EOF
FGoToFirstRecordOnExecute, // Automatically position record on first record after executing
FOpen, // Is a cursor open?
FPrepared: Boolean; // Has the query been prepared?
FRecordCount: Integer; // How many records have been read so far?
FHandle: TISC_STMT_HANDLE; // Once prepared, this accesses the SQL Query
FOnSQLChanging: TNotifyEvent; // Call this when the SQL is changing.
FSQL: TStrings; // SQL Query (by user)
FParamCheck: Boolean; // Check for parameters? (just like TQuery)
FProcessedSQL: string; // SQL Query (pre-processed for param labels)
FPreparedSQL :AnsiString;
FSQLParams, // Any parameters to the query.
FSQLRecord: TFIBXSQLDA; // The current record
FSQLType: TFIBSQLTypes; // Select, update, delete, insert, create, alter, etc...
FUserSQLParams:TFIBXSQLDA;
FProcExecuted:boolean;
FOnSQLFetch:TOnSQLFetch;
FMacroChar :Char;
vUserParamsCreated:boolean;
FCountLockSQL:integer;
FModifyTable:string;
FOptions:TpFIBQueryOptions;
vDiffParams:boolean;
FOnlySrvParams :TStringList;
FCallTime :Cardinal;
FHaveMacros:boolean;
FNeedForceIsNull:boolean;
FMacroChanged:boolean;
FSQLTextChangeCount:integer;
FHaveStreamParams:boolean;
FQueryRunState:TQueryRunState;
FCodePageApplied:boolean;
FAutoCloseOnTransactionEnd:boolean;
vFetched:boolean;
{$DEFINE FIB_INTERFACE}
{$I FIBQueryPT.inc}
{$UNDEF FIB_INTERFACE}
procedure SaveStreamedParams(toParams:TFIBXSQLDA);
procedure ClearStreamedParams;
procedure SetParamCheck(Value:boolean);
function GetModifyTable:string;
procedure DatabaseDisconnecting(Sender: TObject);
function GetDatabase: TFIBDatabase;
function GetDBHandle: PISC_DB_HANDLE;
function GetEOF: Boolean;
function GetFields(const Idx: Integer): TFIBXSQLVAR;
function GetFieldIndex(const FieldName: string): Integer;
function GetPlan: string;
function GetRecordCount: Integer;
function GetRowsAffected: Integer;
function GetAllRowsAffected: TAllRowsAffected ;
function GetSQLParams: TFIBXSQLDA;
function GetTransaction: TFIBTransaction;
function GetTRHandle: PISC_TR_HANDLE;
procedure SetDatabase(Value: TFIBDatabase); virtual;
procedure SetSQL(Value: TStrings);
procedure SetMacroChar(Value:Char);
procedure SetTransaction(Value: TFIBTransaction);
procedure SQLChanging(Sender: TObject);
procedure SQLChange(Sender: TObject);
procedure DoTransactionEnding(Sender: TObject);
// Added procedures
procedure SaveRestoreValues(SQLDA:TFIBXSQLDA;IsSave:boolean);
function GetWhereClause(Index:Integer):string;
procedure SetWhereClause(Index:Integer;const WhereClauseTxt:string);
function GetOrderString:string;
procedure SetOrderString(const OrderTxt:string);
function GetGroupByString:string;
procedure SetGroupByString(const GroupByTxt:string);
function GetFieldsClause:string;
procedure SetFieldsClause(const NewFields:string);
procedure PrepareUserParamsTypes;
procedure StartStatisticExec(const stText:string);
procedure EndStatisticExec(const stText:string);
procedure DoStatisticPrepare(const stText:string);
function ParamsNotExist(const SQLText:string):boolean;
procedure PreprocessSQL(const sSQL:String;IsUserSQL:boolean);
procedure DoBeforeExecute;
procedure DoAfterExecute;
procedure DoAfterFirstFetch;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Loaded; override;
property Handle: TISC_STMT_HANDLE read FHandle;
property QueryRunState:TQueryRunState read FQueryRunState;
private
FExtSQLDA:array of TExtDescribeSQLVar;
procedure FillExtDescribeSQLVars;
procedure ConvertSQLTextToCodePage;
public
function TableAliasForField(FieldIndex:integer):string; overload;
function TableAliasForFieldByName(const aFieldName:string):string;
//{$IFNDEF BCB}
function TableAliasForField(const aFieldName:string):string; overload;
// {$ENDIF}
private
FOnBatchError :TOnBatchError ;
FCursorName :string;
// FOnApplyXMLFile:TOnBatchXMLFile;
// FOnApplyXMLError:TOnBatchError;
// procedure ReadXmlFile(Reader:TXMLDataSetFileReader; const CurRec:TRecordDesc; const RecordNo:integer; var Stop:boolean);
public
function BatchInput(InputObject: TFIBBatchInputStream) :boolean;
function BatchOutput(OutputObject: TFIBBatchOutputStream):boolean;
procedure BatchInputRawFile(const FileName:Ansistring);
procedure BatchOutputRawFile(const FileName:Ansistring;Version:integer=3);
procedure BatchToQuery(ToQuery:TFIBQuery;Mappings:TStrings);
// procedure BatchXmlFile(const aFileName:string);
public
function Call(ErrCode: ISC_STATUS; RaiseError: Boolean): ISC_STATUS;
procedure CheckClosed(const OpName:Ansistring);// raise error if query is not closed.
procedure CheckOpen(const OpName:Ansistring); // raise error if query is not open.
procedure CheckValidStatement; // raise error if statement is invalid.
procedure Close; // close the query.
function Current: TFIBXSQLDA;
procedure ExecQuery; virtual; // ExecQuery the query.
procedure ExecuteImmediate;
// procedure CancelQuery; // Only For IB
{$IFDEF SUPPORT_IB2007}
procedure ExecuteAsBatch; overload;
procedure ExecuteAsBatch(const SQLs:array of Ansistring); overload;
{$ENDIF}
procedure FreeHandle;
function Next: TFIBXSQLDA;
procedure Prepare; // Prepare the query.
function FieldByName(const FieldName: string): TFIBXSQLVAR;
function FindField(const FieldName: string): TFIBXSQLVAR;
function FN(const FieldName: string): TFIBXSQLVAR;
function FieldByOrigin(const TableName,FieldName:string):TFIBXSQLVAR; overload;
function SQLFieldName(const aFieldName:string):string;
{$IFDEF SUPPORT_ARRAY_FIELD}
procedure PrepareArrayFields;
procedure PrepareArraySqlVar(
SqlVar:TFIBXSQLVAR;const RelName,aSQLName:string; IsField:boolean
);
{$ENDIF}
procedure SetParamValues(const ParamValues: array of Variant); overload;
procedure SetParamValues(const ParamNames: string;ParamValues: array of Variant); overload;
procedure ExecWP(const ParamValues: array of Variant); overload;
procedure ExecWP(const ParamNames: string;ParamValues: array of Variant); overload;
// Exec Query with ParamValues
procedure ExecWPS(const ParamSources: array of ISQLObject); overload;
procedure ExecWPS(ParamSource:ISQLObject; AllRecords:boolean=True); overload;
procedure BeginModifySQLText;
procedure EndModifySQLText;
function CountModifySQLText:integer;
function GetMainWhereIndex:integer;
function GetMainWhereClause :string;
procedure SetMainWhereClause (const Value:string);
function IsProc :boolean;
function ParamByName(const ParamName:string): TFIBXSQLVAR;
function FindParam (const aParamName: string): TFIBXSQLVAR;
procedure ApplyMacro;
procedure RestoreMacroDefaultValues;
function FieldCount:integer;
function SQLDescribeInfo(InfoRequest:array of AnsiChar):PXSQLDA;
property Bof: Boolean read FBOF;
property DBHandle: PISC_DB_HANDLE read GetDBHandle;
property Eof: Boolean read GetEOF;
property FldByName[const FieldName: string]: TFIBXSQLVAR read FieldByName; default;
property Fields[const Idx: Integer]: TFIBXSQLVAR read GetFields;
property FieldIndex[const FieldName: string]: Integer read GetFieldIndex;
property Open: Boolean read FOpen;
property Params: TFIBXSQLDA read GetSQLParams;
property Plan: string read GetPlan;
property Prepared: Boolean read FPrepared;
property RecordCount: Integer read GetRecordCount;
property RowsAffected: Integer read GetRowsAffected;
property AllRowsAffected: TAllRowsAffected read GetAllRowsAffected;
property SQLType: TFIBSQLTypes read FSQLType;
property TRHandle: PISC_TR_HANDLE read GetTRHandle;
property ProcExecuted:boolean read FProcExecuted write FProcExecuted;
property OnSQLFetch:TOnSQLFetch read FOnSQLFetch write FOnSQLFetch; // for internal use
property OnlySrvParams:TStringList read FOnlySrvParams;
protected
FConditions:TConditions;
procedure AddCondition(const Name,Condition: string; Enabled: boolean);
procedure SetConditions(Value:TConditions);
published
property Conditions: TConditions read FConditions write SetConditions;
public
{ISQLObject}
function ParamCount:integer;
function ParamName(ParamIndex:integer):string;
function FieldName(FieldIndex:integer):string;
function FieldsCount:integer;
function FieldExist(const FieldName:string; var FieldIndex:integer):boolean;
function ParamExist(const ParamName:string; var ParamIndex:integer):boolean;
function FieldValue(const FieldName:string;Old:boolean):variant; overload;
function FieldValue(const FieldIndex:integer;Old:boolean):variant; overload;
function ParamValue(const ParamName:string):variant; overload;
function ParamValue(const ParamIndex:integer):variant; overload;
function DefMacroValue(const MacroName:string):string;
procedure SetParamValue(const ParamIndex:integer; aValue:Variant);
function IEof:boolean;
procedure INext;
{End ISQLObject}
function ReadySQLText(ForChangeExecSQL:boolean=True):string;
property SQLTextChangeCount:integer read FSQLTextChangeCount;
private
procedure SetPlanClause(const Value:string);
function GetPlanClause:string;
public
procedure AssignProperties(Source: TFIBQuery);
function WhereClausesCount:integer;
property WhereClause[Index:integer]:string read GetWhereClause write SetWhereClause;
property MainWhereClause:string read GetMainWhereClause write SetMainWhereClause;
property IndexMainWhere:integer read GetMainWhereIndex;
property CursorName :string read FCursorName write FCursorName;
property OrderClause:string read GetOrderString write SetOrderString;
property GroupByClause:string read GetGroupByString write SetGroupByString;
property FieldsClause:string read GetFieldsClause write SetFieldsClause;
property PlanClause:string read GetPlanClause write SetPlanClause;
property ModifyTable:string read GetModifyTable;
property CallTime :Cardinal read FCallTime;
property MacroChanged:boolean read FMacroChanged;
property SQLKind:TSQLKind read GetSQLKind;
property BeforeExecute:TNotifyEvent read FBeforeExecute write FBeforeExecute;
property AfterExecute :TNotifyEvent read FAfterExecute write FAfterExecute;
published
property Transaction: TFIBTransaction read GetTransaction write SetTransaction;
property Database: TFIBDatabase read GetDatabase write SetDatabase;
property GoToFirstRecordOnExecute: Boolean read FGoToFirstRecordOnExecute
write FGoToFirstRecordOnExecute
default True;
property ParamCheck: Boolean read FParamCheck write SetParamCheck default True;
property SQL: TStrings read FSQL write SetSQL;
property OnSQLChanging: TNotifyEvent read FOnSQLChanging write FOnSQLChanging;
property Options : TpFIBQueryOptions read FOptions write FOptions stored False;
property OnBatching : TOnBatching read FOnBatching write FOnBatching ;
property OnBatchError :TOnBatchError read FOnBatchError write FOnBatchError;
// property OnBatchXMLFile:TOnBatchXMLFile read FOnApplyXMLFile write FOnApplyXMLFile;
// property OnBacthXMLError:TOnBatchError read FOnApplyXMLError write FOnApplyXMLError;
property TransactionEnding:TNotifyEvent read FTransactionEnding write FTransactionEnding;
property TransactionEnded :TNotifyEvent read FTransactionEnded write FTransactionEnded;
property AfterFirstFetch:TNotifyEvent read FAfterFirstFetch write FAfterFirstFetch;
{$IFDEF CSMonitor}
property CSMonitorSupport: TCSMonitorSupport read FCSMonitorSupport write SetMonitorSupport;
{$ENDIF}
end;
procedure BlobToStream (ModelVar:TFIBXSQLVAR; BlobID:TISC_QUAD;Stream: TStream);
const
ExecProcPrefix ='EXECUTE ';
//Statistic consts
scPrepareCount ='PrepareCount' ;
scExecuteCount ='ExecuteCount' ;
scSumTimeExecute ='SumTimeExecute';
scAvgTimeExecute ='AvgTimeExecute';
scMaxTimeExecute ='MaxTimeExecute';
scLastTimeExecute='LastTimeExecute';
scLastQuery ='LastQueryName';
fibGUID_NULL: TGUID = '{00000000-0000-0000-0000-000000000000}';
chUnicodeFSS=3;
{$IFDEF SUPPORT_KOI8_CHARSET}
chFBKOI8R=63;
chFBKOI8U=64;
CodePageKOI8R=20866;
CodePageKOI8RU=21866;
{$ENDIF}
var DisableEncodingSQLText:boolean;
TraceString:string;
implementation
uses
FIBMiscellaneous, StrUtil,
IBBlobFilter, FIBConsts,FIBCloneComponents
//Added uses
{$IFNDEF NO_MONITOR}
,FIBSQLMonitor
{$ENDIF}
{$IFDEF CSMonitor}
,FIBDataSet,pFIBDataSet
{$ENDIF}
;
const
cPlanMaxLength=16384;
(* TFIBXSQLVAR *)
constructor TFIBXSQLVAR.Create(AParent: TFIBXSQLDA);
begin
FParent := AParent;
FVariantFalse := 0;
FVariantTrue := 1;
FModified:=False;
FIsMacro :=False;
FQuoted :=False;
FOldValue :=Unassigned;
FInWhereClause:=False;
FInitialized :=False;
FCanForceIsNull:=False;
{$IFDEF SUPPORT_ARRAY_FIELD}
vFIBArray :=nil;
{$ENDIF}
end;
destructor TFIBXSQLVAR.Destroy; //override;
begin
{$IFDEF SUPPORT_ARRAY_FIELD}
if Assigned(vFIBArray) then vFIBArray.Free;
{$ENDIF}
inherited Destroy;
FreeAndNil(FStreamValue);
end;
{$WARNINGS OFF}
procedure TFIBXSQLVAR.Assign(Source: TFIBXSQLVAR);
var
szBuff: PAnsiChar;
s_bhandle, d_bhandle: TISC_BLOB_HANDLE;
bSourceBlob, bDestBlob: Boolean;
iSegs, iMaxSeg, iSize: Long;
iBlobType: Short;
SP:TFIBXSQLVAR;
DestSQLType,SrcSQLType:integer;
begin
if IsMacro then
begin
AsString:=Source.AsString;
Exit;
end;
szBuff := nil;
SrcSQLType :=Source.FXSQLVAR^.sqltype and (not 1);
DestSQLType:=FXSQLVAR^.sqltype and (not 1) ;
bSourceBlob:=SrcSQLType=SQL_BLOB;
bDestBlob :=True;
s_bhandle :=nil;
d_bhandle :=nil;
try
if (Source.IsNull) then
begin
IsNull := True;
Exit;
end
else
if (DestSQLType = SQL_ARRAY) or (SrcSQLType = SQL_ARRAY) then Exit;
// arrays not supported.
if (DestSQLType <> SQL_BLOB) and not bSourceBlob then
begin
AsXSQLVAR := Source.AsXSQLVAR;
Exit;
end
else
if (SrcSQLType <> SQL_BLOB) then
begin
szBuff := nil;
FIBAlloc(szBuff, 0, Source.FXSQLVAR^.sqllen);
Move(Source.FXSQLVAR^.sqldata[0], szBuff[0], Source.FXSQLVAR^.sqllen);
iSize := Source.FXSQLVAR^.sqllen;
end
else
if (DestSQLType <> SQL_BLOB) then
begin
if FParent=FQuery.FUserSQLParams then
begin
if not FQuery.Prepared then FQuery.Prepare;
SP:=FQuery.FSQLParams.FindParam(Name);
bDestBlob := not (
(SP=nil) or (SP.FXSQLVAR^.sqltype and (not 1) <> SQL_BLOB)
);
if bDestBlob then AsQuad:=SP.AsQuad;
end
else
bDestBlob := False;
end;
if bSourceBlob then
begin
// read the blob
Source.FQuery.Call(
Source.FQuery.Database.ClientLibrary.isc_open_blob2(StatusVector, Source.FQuery.DBHandle,
Source.FQuery.TRHandle, @s_bhandle, PISC_QUAD(Source.FXSQLVAR.sqldata),
0, nil), True
);
with Source.FQuery,Source.FQuery.Database do
try
GetBlobInfo(ClientLibrary,@s_bhandle,iSegs, iMaxSeg, iSize,iBlobType);
szBuff := nil;
FIBAlloc(szBuff, 0, iSize);
ReadBlob(ClientLibrary,@s_bhandle, szBuff, iSize);
if (not bDestBlob) // avoid
or (FXSQLVAR^.sqlsubtype<>Source.FXSQLVAR^.sqlsubtype)
then
IBFilterBuffer(Database,szBuff, iSize, Source.FXSQLVAR^.sqlsubtype, False); // ivan_ra
finally
Source.FQuery.Call(
ClientLibrary.isc_close_blob(StatusVector, @s_bhandle), True
);
end;
end;
if bDestBlob then
begin
// write the blob
FQuery.Call(FQuery.Database.ClientLibrary.isc_create_blob2(StatusVector, FQuery.DBHandle,
FQuery.TRHandle, @d_bhandle, PISC_QUAD(FXSQLVAR.sqldata),
0, nil), True);
try
if (not bSourceBlob) // avoid conversation
or (FXSQLVAR^.sqlsubtype<>Source.FXSQLVAR^.sqlsubtype)
then
IBFilterBuffer(FQuery.Database,szBuff, iSize, Source.FXSQLVAR^.sqlsubtype, True); // ivan_ra
WriteBlob(FQuery.Database.ClientLibrary,@d_bhandle, szBuff, iSize);
IsNull := False;
finally
FQuery.Call(FQuery.Database.ClientLibrary.isc_close_blob(StatusVector, @d_bhandle), True);
end;
end
else
begin
// just copy the buffer
FXSQLVAR.sqltype := SQL_TEXT;
FXSQLVAR.sqllen := iSize;
FIBAlloc(FXSQLVAR.sqldata, iSize, iSize);
Move(szBuff[0], FXSQLVAR^.sqldata[0], iSize);
end;
finally
FIBAlloc(szBuff, 0, 0);
end;
end;
procedure TFIBXSQLVAR.SetSQLLen(A:SmallInt);
begin
FXSQLVAR^.sqllen:=A
end;
function TFIBXSQLVAR.GetAsInt64: Int64;
begin
Result := 0;
if not IsNull then
case FXSQLVAR^.sqltype and (not 1) of
SQL_TEXT, SQL_VARYING:
begin
try
Result := StrToInt64(AsWideString);
except
on E: Exception do FIBError(feInvalidDataConversion, [nil]);
end;
end;
SQL_SHORT:
Result := PShort(FXSQLVAR^.sqldata)^ div Trunc(E10[-FXSQLVAR^.sqlscale]);
SQL_LONG:
Result := PLong(FXSQLVAR^.sqldata)^ div Trunc(E10[-FXSQLVAR^.sqlscale]);
SQL_INT64:
Result := PInt64(FXSQLVAR^.sqldata)^ div Trunc(E10[-FXSQLVAR^.sqlscale]);
SQL_DOUBLE, SQL_FLOAT, SQL_D_FLOAT:
Result := Trunc(AsDouble);
SQL_NULL: Result:=0;
else
FIBError(feInvalidDataConversion, [nil]);
end;
end;
function TFIBXSQLVAR.GetAsCurrency: Currency;
begin
if IsNull then
Result:=0
else
if (FQuery.Database.SQLDialect < 3)
or (FXSQLVAR^.sqltype and (not 1)<>SQL_INT64)
then
Result := GetAsDouble
else
Result := PInt64(FXSQLVAR^.sqldata)^*E10[FXSQLVAR^.sqlscale];
end;
{$IFNDEF NO_USE_COMP}
function TFIBXSQLVAR.GetAsComp: Comp;
begin
InitFPU;
Result := 0;
if not IsNull then
if (FXSQLVAR^.sqltype and (not 1))<>SQL_INT64 then
Result:= AsDouble
else
Result := PInt64(FXSQLVAR^.sqldata)^ *E10[FXSQLVAR^.sqlscale];
end;
{$ENDIF}
const
IBBuffDateDelta=678576;
function TFIBXSQLVAR.GetAsTimeStamp: TTimeStamp;
begin
if IsNull then
begin
Result.Time :=0;
Result.Date :=0;
end
else
case FXSQLVAR^.sqltype and (not 1) of
SQL_TEXT, SQL_VARYING:
try
Result := DateTimeToTimeStamp(StrToDate(AsWideString));
except
on E: EConvertError do FIBError(feInvalidDataConversion, [nil]);
end;
SQL_TYPE_TIME:
begin
Result.Date := 0;
Result.Time := PISC_TIME(FXSQLVAR^.sqldata)^ div 10
end;
SQL_TYPE_DATE:
begin
Result.Date := PISC_DATE(FXSQLVAR^.sqldata)^ + IBBuffDateDelta;
Result.Time := 0
end;
SQL_TIMESTAMP:
with PISC_QUAD(FXSQLVAR^.sqldata)^ do
begin
Result.Date := gds_quad_high + IBBuffDateDelta;
Result.Time := gds_quad_low div 10
end;
else
FIBError(feInvalidDataConversion, [nil]);
end;
end;
function TFIBXSQLVAR.GetAsDateTime: TDateTime;
const
MSecsPerDay10 = MSecsPerDay * 10;
begin
Result := 0;
if not IsNull then
case FXSQLVAR^.sqltype and (not 1) of
SQL_TEXT, SQL_VARYING:
try
Result := StrToDate(AsWideString);
except
on E: EConvertError do FIBError(feInvalidDataConversion, [nil]);
end;
SQL_TYPE_TIME:
begin
Result :=PISC_TIME(FXSQLVAR^.sqldata)^/MSecsPerDay10;
end;
SQL_TYPE_DATE:
begin
Result:=PISC_DATE(FXSQLVAR^.sqldata)^-IBDateDelta;
end;
SQL_TIMESTAMP:
begin
{ Result:=
PISC_QUAD(FXSQLVAR^.sqldata)^.gds_quad_high-IBDateDelta+
PISC_QUAD(FXSQLVAR^.sqldata)^.gds_quad_low/MSecsPerDay10;}
Result:=
HookTimeStampToDateTime(AsTimeStamp);
end;
else
FIBError(feInvalidDataConversion, [nil]);
end;
end;
function TFIBXSQLVAR.GetAsDouble: Double;
begin
Result := 0;
if not IsNull then
case FXSQLVAR^.sqltype and (not 1) of
SQL_TEXT, SQL_VARYING:
begin