27 |
|
{ IBX For Lazarus (Firebird Express) } |
28 |
|
{ Contributor: Tony Whyman, MWA Software http://www.mwasoftware.co.uk } |
29 |
|
{ Portions created by MWA Software are copyright McCallum Whyman } |
30 |
< |
{ Associates Ltd 2011 } |
30 |
> |
{ Associates Ltd 2011 - 2018 } |
31 |
|
{ } |
32 |
|
{************************************************************************} |
33 |
|
|
43 |
|
{$ELSE} |
44 |
|
unix, |
45 |
|
{$ENDIF} |
46 |
< |
SysUtils, Classes, Db, |
47 |
< |
IBHeader, IB, IBCustomDataSet, IBSQL, IBSQLParser; |
46 |
> |
SysUtils, Classes, Db, IB, IBCustomDataSet, IBSQL; |
47 |
|
|
48 |
|
type |
49 |
|
|
55 |
|
FPrepared: Boolean; |
56 |
|
FParams: TParams; |
57 |
|
FText: string; |
59 |
– |
FRowsAffected: Integer; |
60 |
– |
FCheckRowsAffected: Boolean; |
58 |
|
FSQLUpdating: boolean; |
59 |
+ |
FInQueryChanged: boolean; |
60 |
|
function GetRowsAffected: Integer; |
61 |
|
procedure PrepareSQL; |
62 |
|
procedure QueryChanged(Sender: TObject); |
68 |
|
procedure SetPrepared(Value: Boolean); |
69 |
|
procedure SetPrepare(Value: Boolean); |
70 |
|
procedure WriteParamData(Writer: TWriter); |
71 |
< |
function GetStmtHandle: TISC_STMT_HANDLE; |
71 |
> |
function GetStmtHandle: IStatement; |
72 |
> |
procedure UpdateSQL; |
73 |
|
|
74 |
|
protected |
75 |
|
{ IProviderSupport } |
79 |
|
procedure PSSetCommandText(const CommandText: string); override; |
80 |
|
procedure PSSetParams(AParams: TParams); override; *) |
81 |
|
|
83 |
– |
function CreateParser: TSelectSQLParser; override; |
82 |
|
procedure DefineProperties(Filer: TFiler); override; |
83 |
|
procedure InitFieldDefs; override; |
84 |
|
procedure InternalOpen; override; |
86 |
|
function GetParamsCount: Word; |
87 |
|
function GenerateQueryForLiveUpdate : Boolean; |
88 |
|
procedure SetFiltered(Value: Boolean); override; |
89 |
+ |
procedure SQLChanged(Sender: TObject); override; |
90 |
+ |
procedure SQLChanging(Sender: TObject); override; |
91 |
|
|
92 |
|
public |
93 |
|
constructor Create(AOwner: TComponent); override; |
99 |
|
function ParamByName(const Value: string): TParam; |
100 |
|
procedure Prepare; |
101 |
|
procedure UnPrepare; |
102 |
+ |
procedure ResetParser; override; |
103 |
|
property Prepared: Boolean read FPrepared write SetPrepare; |
104 |
|
property ParamCount: Word read GetParamsCount; |
105 |
< |
property StmtHandle: TISC_STMT_HANDLE read GetStmtHandle; |
105 |
> |
property StmtHandle: IStatement read GetStmtHandle; |
106 |
|
property StatementType; |
107 |
|
property Text: string read FText; |
108 |
|
property RowsAffected: Integer read GetRowsAffected; |
134 |
|
property TransactionFree; |
135 |
|
property OnFilterRecord; |
136 |
|
property OnValidatePost; |
137 |
+ |
property OnDeleteReturning; |
138 |
|
end; |
139 |
|
|
140 |
|
implementation |
141 |
|
|
142 |
+ |
uses FBMessages; |
143 |
+ |
|
144 |
|
{ TIBQuery } |
145 |
|
|
146 |
|
constructor TIBQuery.Create(AOwner: TComponent); |
150 |
|
TStringList(SQL).OnChange := QueryChanged; |
151 |
|
FParams := TParams.Create(Self); |
152 |
|
ParamCheck := True; |
149 |
– |
FRowsAffected := -1; |
153 |
|
end; |
154 |
|
|
155 |
|
destructor TIBQuery.Destroy; |
202 |
|
SetPrepared(False); |
203 |
|
end; |
204 |
|
|
205 |
+ |
procedure TIBQuery.ResetParser; |
206 |
+ |
begin |
207 |
+ |
inherited ResetParser; |
208 |
+ |
UpdateSQL; |
209 |
+ |
end; |
210 |
+ |
|
211 |
|
procedure TIBQuery.SetQuery(Value: TStrings); |
212 |
|
begin |
213 |
|
if SQL.Text <> Value.Text then |
223 |
|
end; |
224 |
|
|
225 |
|
procedure TIBQuery.QueryChanged(Sender: TObject); |
217 |
– |
var |
218 |
– |
List: TParams; |
226 |
|
begin |
227 |
< |
if not (csReading in ComponentState) then |
228 |
< |
begin |
229 |
< |
Disconnect; |
230 |
< |
if HasParser and not FSQLUpdating then |
224 |
< |
begin |
225 |
< |
FSQLUpdating := true; |
226 |
< |
try |
227 |
< |
SQL.Text := Parser.SQLText; |
228 |
< |
finally |
229 |
< |
FSQLUpdating := false |
230 |
< |
end; |
231 |
< |
end; |
232 |
< |
if ParamCheck or (csDesigning in ComponentState) then |
227 |
> |
if FInQueryChanged then Exit; |
228 |
> |
FInQueryChanged := true; |
229 |
> |
try |
230 |
> |
if not (csReading in ComponentState) then |
231 |
|
begin |
232 |
< |
List := TParams.Create(Self); |
233 |
< |
try |
234 |
< |
FText := List.ParseSQL(SQL.Text, True); |
235 |
< |
List.AssignValues(FParams); |
236 |
< |
FParams.Clear; |
237 |
< |
FParams.Assign(List); |
240 |
< |
finally |
241 |
< |
List.Free; |
242 |
< |
end; |
232 |
> |
Disconnect; |
233 |
> |
if csDesigning in ComponentState then |
234 |
> |
FText := FParams.ParseSQL(SQL.Text, true) |
235 |
> |
else |
236 |
> |
FText := SQL.Text; |
237 |
> |
DataEvent(dePropertyChange, 0); |
238 |
|
end else |
239 |
< |
FText := SQL.Text; |
240 |
< |
DataEvent(dePropertyChange, 0); |
241 |
< |
end else |
242 |
< |
FText := FParams.ParseSQL(SQL.Text, true); |
243 |
< |
SelectSQL.Assign(SQL); |
239 |
> |
FText := FParams.ParseSQL(SQL.Text, true); |
240 |
> |
|
241 |
> |
if not FSQLUpdating then |
242 |
> |
begin |
243 |
> |
Prepared := false; |
244 |
> |
SelectSQL.Assign(SQL); |
245 |
> |
end; |
246 |
> |
finally |
247 |
> |
FInQueryChanged := false; |
248 |
> |
end; |
249 |
|
end; |
250 |
|
|
251 |
|
procedure TIBQuery.SetParamsList(Value: TParams); |
294 |
|
begin |
295 |
|
if Value then |
296 |
|
begin |
297 |
– |
FRowsAffected := -1; |
298 |
– |
FCheckRowsAffected := True; |
297 |
|
if Length(Text) > 1 then PrepareSQL |
298 |
|
else IBError(ibxeEmptySQLStatement, [nil]); |
299 |
|
end |
300 |
|
else |
301 |
|
begin |
304 |
– |
if FCheckRowsAffected then |
305 |
– |
FRowsAffected := RowsAffected; |
302 |
|
InternalUnPrepare; |
303 |
+ |
FParams.Clear; |
304 |
|
end; |
305 |
|
FPrepared := Value; |
306 |
|
end; |
350 |
|
|
351 |
|
function TIBQuery.ParamByName(const Value: string): TParam; |
352 |
|
begin |
353 |
+ |
if not Prepared then |
354 |
+ |
Prepare; |
355 |
|
Result := FParams.ParamByName(Value); |
356 |
|
end; |
357 |
|
|
371 |
|
begin |
372 |
|
CheckInActive; |
373 |
|
if SQL.Count <= 0 then |
375 |
– |
begin |
376 |
– |
FCheckRowsAffected := False; |
374 |
|
IBError(ibxeEmptySQLStatement, [nil]); |
375 |
< |
end; |
375 |
> |
|
376 |
|
ActivateConnection(); |
377 |
|
DidActivate := ActivateTransaction; |
378 |
|
try |
383 |
|
finally |
384 |
|
if DidActivate then |
385 |
|
DeactivateTransaction; |
389 |
– |
FCheckRowsAffected := True; |
386 |
|
end; |
387 |
|
end; |
388 |
|
|
391 |
|
var |
392 |
|
i : integer; |
393 |
|
Buffer: Pointer; |
394 |
< |
SQLParam: TIBXSQLVAR; |
394 |
> |
SQLParam: ISQLParam; |
395 |
|
|
396 |
|
begin |
397 |
|
for I := 0 to FParams.Count - 1 do |
442 |
|
end; |
443 |
|
|
444 |
|
procedure TIBQuery.PrepareSQL; |
445 |
+ |
var List: TParams; |
446 |
|
begin |
447 |
|
QSelect.GenerateParamNames := GenerateParamNames; |
448 |
|
InternalPrepare; |
449 |
+ |
UpdateSQL; |
450 |
+ |
if ParamCheck then |
451 |
+ |
begin |
452 |
+ |
List := TParams.Create(Self); |
453 |
+ |
try |
454 |
+ |
FText := List.ParseSQL(SQL.Text, True); |
455 |
+ |
List.AssignValues(FParams); |
456 |
+ |
FParams.Clear; |
457 |
+ |
FParams.Assign(List); |
458 |
+ |
finally |
459 |
+ |
List.Free; |
460 |
+ |
end; |
461 |
+ |
end; |
462 |
|
end; |
463 |
|
|
464 |
|
|
494 |
|
AddFieldToList(Params[i].Name, Self, DetailFields); |
495 |
|
end; |
496 |
|
|
497 |
< |
function TIBQuery.GetStmtHandle: TISC_STMT_HANDLE; |
497 |
> |
function TIBQuery.GetStmtHandle: IStatement; |
498 |
|
begin |
499 |
|
Result := SelectStmtHandle; |
500 |
|
end; |
501 |
|
|
502 |
< |
function TIBQuery.CreateParser: TSelectSQLParser; |
502 |
> |
procedure TIBQuery.UpdateSQL; |
503 |
|
begin |
504 |
< |
Result := inherited CreateParser; |
505 |
< |
Result.OnSQLChanging := QueryChanged; |
504 |
> |
if not FSQLUpdating and not FInQueryChanged and (SQL.Text <> SelectSQL.Text) then |
505 |
> |
begin |
506 |
> |
FSQLUpdating := true; |
507 |
> |
try |
508 |
> |
SQL.Text := SelectSQL.Text; |
509 |
> |
finally |
510 |
> |
FSQLUpdating := false |
511 |
> |
end; |
512 |
> |
end; |
513 |
|
end; |
514 |
|
|
515 |
|
function TIBQuery.GenerateQueryForLiveUpdate : Boolean; |
532 |
|
inherited SetFiltered(value); |
533 |
|
end; |
534 |
|
|
535 |
+ |
procedure TIBQuery.SQLChanged(Sender: TObject); |
536 |
+ |
begin |
537 |
+ |
inherited SQLChanged(Sender); |
538 |
+ |
UpdateSQL; |
539 |
+ |
end; |
540 |
+ |
|
541 |
+ |
procedure TIBQuery.SQLChanging(Sender: TObject); |
542 |
+ |
begin |
543 |
+ |
inherited SQLChanging(Sender); |
544 |
+ |
Prepared := false; |
545 |
+ |
end; |
546 |
+ |
|
547 |
|
{ TIBQuery IProviderSupport } |
548 |
|
(* |
549 |
|
function TIBQuery.PSGetParams: TParams; |