24 |
|
{ Corporation. All Rights Reserved. } |
25 |
|
{ Contributor(s): Jeff Overcash } |
26 |
|
{ } |
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 } |
31 |
+ |
{ } |
32 |
|
{************************************************************************} |
33 |
|
|
34 |
|
unit IBUpdateSQL; |
37 |
|
|
38 |
|
interface |
39 |
|
|
40 |
< |
uses SysUtils, Classes, DB, IB, IBCustomDataSet, IBQuery; |
40 |
> |
uses SysUtils, Classes, DB, IB, IBCustomDataSet, IBSQL; |
41 |
|
|
42 |
|
type |
43 |
|
{ TIBUpdateSQL } |
45 |
|
TIBUpdateSQL = class(TIBDataSetUpdateObject) |
46 |
|
private |
47 |
|
FDataSet: TIBCustomDataSet; |
48 |
< |
FQueries: array[TUpdateKind] of TIBQuery; |
48 |
> |
FQueries: array[TUpdateKind] of TIBSQL; |
49 |
|
FSQLText: array[TUpdateKind] of TStrings; |
50 |
< |
function GetQuery(UpdateKind: TUpdateKind): TIBQuery; |
50 |
> |
function GetQuery(UpdateKind: TUpdateKind): TIBSQL; |
51 |
|
function GetSQLIndex(Index: Integer): TStrings; |
52 |
|
procedure SetSQL(UpdateKind: TUpdateKind; Value: TStrings); |
53 |
|
procedure SetSQLIndex(Index: Integer; Value: TStrings); |
54 |
|
protected |
55 |
+ |
procedure InternalPrepare(UpdateKind: TUpdateKind); |
56 |
|
function GetSQL(UpdateKind: TUpdateKind): TStrings; override; |
57 |
|
function GetDataSet: TIBCustomDataSet; override; |
58 |
|
procedure SetDataSet(ADataSet: TIBCustomDataSet); override; |
59 |
|
procedure SQLChanged(Sender: TObject); |
60 |
+ |
procedure Apply(UpdateKind: TUpdateKind; buff: PChar); override; |
61 |
+ |
procedure ExecSQL(UpdateKind: TUpdateKind; buff: PChar); |
62 |
|
public |
63 |
|
constructor Create(AOwner: TComponent); override; |
64 |
|
destructor Destroy; override; |
57 |
– |
procedure Apply(UpdateKind: TUpdateKind); override; |
58 |
– |
procedure ExecSQL(UpdateKind: TUpdateKind); |
59 |
– |
procedure SetParams(UpdateKind: TUpdateKind); |
65 |
|
property DataSet; |
66 |
< |
property Query[UpdateKind: TUpdateKind]: TIBQuery read GetQuery; |
66 |
> |
property Query[UpdateKind: TUpdateKind]: TIBSQL read GetQuery; |
67 |
|
property SQL[UpdateKind: TUpdateKind]: TStrings read GetSQL write SetSQL; |
68 |
|
published |
69 |
|
property ModifySQL: TStrings index 0 read GetSQLIndex write SetSQLIndex; |
100 |
|
inherited Destroy; |
101 |
|
end; |
102 |
|
|
103 |
< |
procedure TIBUpdateSQL.ExecSQL(UpdateKind: TUpdateKind); |
103 |
> |
procedure TIBUpdateSQL.ExecSQL(UpdateKind: TUpdateKind; buff: PChar); |
104 |
|
begin |
105 |
+ |
InternalPrepare(UpdateKind); |
106 |
|
with Query[UpdateKind] do |
107 |
|
begin |
108 |
< |
Prepare; |
109 |
< |
ExecSQL; |
110 |
< |
if RowsAffected <> 1 then IBError(ibxeUpdateFailed, [nil]); |
108 |
> |
ExecQuery; |
109 |
> |
// if RowsAffected <> 1 then IBError(ibxeUpdateFailed, [nil]); |
110 |
> |
// Commented out in release 1.2 |
111 |
> |
if FieldCount > 0 then {Has RETURNING Clause} |
112 |
> |
UpdateRecordFromQuery(Current,Buff); |
113 |
|
end; |
114 |
|
end; |
115 |
|
|
116 |
< |
function TIBUpdateSQL.GetQuery(UpdateKind: TUpdateKind): TIBQuery; |
116 |
> |
function TIBUpdateSQL.GetQuery(UpdateKind: TUpdateKind): TIBSQL; |
117 |
|
begin |
118 |
|
if not Assigned(FQueries[UpdateKind]) then |
119 |
|
begin |
120 |
< |
FQueries[UpdateKind] := TIBQuery.Create(Self); |
120 |
> |
FQueries[UpdateKind] := TIBSQL.Create(Self); |
121 |
|
FQueries[UpdateKind].SQL.Assign(FSQLText[UpdateKind]); |
122 |
|
if (FDataSet is TIBCustomDataSet) then |
123 |
|
begin |
158 |
|
SetSQL(TUpdateKind(Index), Value); |
159 |
|
end; |
160 |
|
|
161 |
+ |
procedure TIBUpdateSQL.InternalPrepare(UpdateKind: TUpdateKind); |
162 |
+ |
begin |
163 |
+ |
with Query[UpdateKind] do |
164 |
+ |
begin |
165 |
+ |
with Transaction do |
166 |
+ |
if not InTransaction then StartTransaction; |
167 |
+ |
if not Prepared then Prepare; |
168 |
+ |
end; |
169 |
+ |
end; |
170 |
+ |
|
171 |
|
procedure TIBUpdateSQL.SQLChanged(Sender: TObject); |
172 |
|
var |
173 |
|
UpdateKind: TUpdateKind; |
176 |
|
if Sender = FSQLText[UpdateKind] then |
177 |
|
begin |
178 |
|
if Assigned(FQueries[UpdateKind]) then |
161 |
– |
begin |
162 |
– |
FQueries[UpdateKind].Params.Clear; |
179 |
|
FQueries[UpdateKind].SQL.Assign(FSQLText[UpdateKind]); |
164 |
– |
end; |
180 |
|
Break; |
181 |
|
end; |
182 |
|
end; |
183 |
|
|
184 |
< |
procedure TIBUpdateSQL.SetParams(UpdateKind: TUpdateKind); |
170 |
< |
var |
171 |
< |
I: Integer; |
172 |
< |
Old: Boolean; |
173 |
< |
Param: TParam; |
174 |
< |
PName: string; |
175 |
< |
Field: TField; |
176 |
< |
Value: Variant; |
184 |
> |
procedure TIBUpdateSQL.Apply(UpdateKind: TUpdateKind; buff: PChar); |
185 |
|
begin |
186 |
|
if not Assigned(FDataSet) then Exit; |
187 |
< |
with Query[UpdateKind] do |
188 |
< |
begin |
189 |
< |
for I := 0 to Params.Count - 1 do |
182 |
< |
begin |
183 |
< |
Param := Params[I]; |
184 |
< |
PName := Param.Name; |
185 |
< |
Old := CompareText(Copy(PName, 1, 4), 'OLD_') = 0; {do not localize} |
186 |
< |
if Old then |
187 |
< |
System.Delete(PName, 1, 4); |
188 |
< |
Field := FDataSet.FindField(PName); |
189 |
< |
if not Assigned(Field) then |
190 |
< |
Continue; |
191 |
< |
if Old then |
192 |
< |
Param.AssignFieldValue(Field, Field.OldValue) else |
193 |
< |
begin |
194 |
< |
Value := Field.NewValue; |
195 |
< |
if VarIsEmpty(Value) then |
196 |
< |
Value := Field.OldValue; |
197 |
< |
Param.AssignFieldValue(Field, Value); |
198 |
< |
end; |
199 |
< |
end; |
200 |
< |
end; |
201 |
< |
end; |
202 |
< |
|
203 |
< |
procedure TIBUpdateSQL.Apply(UpdateKind: TUpdateKind); |
204 |
< |
begin |
205 |
< |
SetParams(UpdateKind); |
206 |
< |
ExecSQL(UpdateKind); |
187 |
> |
InternalPrepare(UpdateKind); |
188 |
> |
InternalSetParams(Query[UpdateKind].Params,buff); |
189 |
> |
ExecSQL(UpdateKind,buff); |
190 |
|
end; |
191 |
|
|
192 |
|
end. |