|
@@ -1544,17 +1544,17 @@ Procedure TCustomSQLQuery.ApplyRecUpdate(UpdateKind : TUpdateKind);
|
|
|
|
|
|
var FieldNamesQuoteChars : TQuoteChars;
|
|
|
|
|
|
- procedure InitialiseModifyQuery(var qry : TCustomSQLQuery; aSQL: String);
|
|
|
+ function InitialiseModifyQuery(var qry : TCustomSQLQuery): TCustomSQLQuery;
|
|
|
|
|
|
begin
|
|
|
- qry := TCustomSQLQuery.Create(nil);
|
|
|
- with qry do
|
|
|
- begin
|
|
|
- ParseSQL := False;
|
|
|
- DataBase := Self.DataBase;
|
|
|
- Transaction := Self.Transaction;
|
|
|
- SQL.text := aSQL;
|
|
|
- end;
|
|
|
+ if not assigned(qry) then
|
|
|
+ begin
|
|
|
+ qry := TCustomSQLQuery.Create(nil);
|
|
|
+ qry.ParseSQL := False;
|
|
|
+ qry.DataBase := Self.DataBase;
|
|
|
+ qry.Transaction := Self.Transaction;
|
|
|
+ end;
|
|
|
+ Result:=qry;
|
|
|
end;
|
|
|
|
|
|
procedure UpdateWherePart(var sql_where : string;x : integer);
|
|
@@ -1562,8 +1562,11 @@ var FieldNamesQuoteChars : TQuoteChars;
|
|
|
begin
|
|
|
if (pfInKey in Fields[x].ProviderFlags) or
|
|
|
((FUpdateMode = upWhereAll) and (pfInWhere in Fields[x].ProviderFlags)) or
|
|
|
- ((FUpdateMode = UpWhereChanged) and (pfInWhere in Fields[x].ProviderFlags) and (fields[x].value <> fields[x].oldvalue)) then
|
|
|
- sql_where := sql_where + '(' + FieldNamesQuoteChars[0] + fields[x].FieldName + FieldNamesQuoteChars[1] + '= :"' + 'OLD_' + fields[x].FieldName + '") and ';
|
|
|
+ ((FUpdateMode = UpWhereChanged) and (pfInWhere in Fields[x].ProviderFlags) and (Fields[x].Value <> Fields[x].OldValue)) then
|
|
|
+ if Fields[x].OldValue = NULL then
|
|
|
+ sql_where := sql_where + FieldNamesQuoteChars[0] + Fields[x].FieldName + FieldNamesQuoteChars[1] + ' is null and '
|
|
|
+ else
|
|
|
+ sql_where := sql_where + '(' + FieldNamesQuoteChars[0] + Fields[x].FieldName + FieldNamesQuoteChars[1] + '= :"' + 'OLD_' + Fields[x].FieldName + '") and ';
|
|
|
end;
|
|
|
|
|
|
function ModifyRecQuery : string;
|
|
@@ -1632,6 +1635,7 @@ var FieldNamesQuoteChars : TQuoteChars;
|
|
|
end;
|
|
|
|
|
|
var qry : TCustomSQLQuery;
|
|
|
+ s : string;
|
|
|
x : integer;
|
|
|
Fld : TField;
|
|
|
|
|
@@ -1639,37 +1643,25 @@ begin
|
|
|
FieldNamesQuoteChars := TSQLConnection(DataBase).FieldNameQuoteChars;
|
|
|
|
|
|
case UpdateKind of
|
|
|
- ukModify : begin
|
|
|
- if not assigned(FUpdateQry) then
|
|
|
- begin
|
|
|
- if (trim(FUpdateSQL.Text)<> '') then
|
|
|
- InitialiseModifyQuery(FUpdateQry,FUpdateSQL.Text)
|
|
|
- else
|
|
|
- InitialiseModifyQuery(FUpdateQry,ModifyRecQuery);
|
|
|
- end;
|
|
|
- qry := FUpdateQry;
|
|
|
- end;
|
|
|
ukInsert : begin
|
|
|
- if not assigned(FInsertQry) then
|
|
|
- begin
|
|
|
- if (trim(FInsertSQL.Text)<> '') then
|
|
|
- InitialiseModifyQuery(FInsertQry,FInsertSQL.Text)
|
|
|
- else
|
|
|
- InitialiseModifyQuery(FInsertQry,InsertRecQuery);
|
|
|
- end;
|
|
|
- qry := FInsertQry;
|
|
|
+ s := trim(FInsertSQL.Text);
|
|
|
+ if s = '' then s := InsertRecQuery;
|
|
|
+ qry := InitialiseModifyQuery(FInsertQry);
|
|
|
+ end;
|
|
|
+ ukModify : begin
|
|
|
+ s := trim(FUpdateSQL.Text);
|
|
|
+ if (s='') and (not assigned(FUpdateQry) or (UpdateMode<>upWhereKeyOnly)) then //first time or dynamic where part
|
|
|
+ s := ModifyRecQuery;
|
|
|
+ qry := InitialiseModifyQuery(FUpdateQry);
|
|
|
end;
|
|
|
ukDelete : begin
|
|
|
- if not assigned(FDeleteQry) then
|
|
|
- begin
|
|
|
- if (trim(FDeleteSQL.Text)<> '') then
|
|
|
- InitialiseModifyQuery(FDeleteQry,FDeleteSQL.Text)
|
|
|
- else
|
|
|
- InitialiseModifyQuery(FDeleteQry,DeleteRecQuery);
|
|
|
- end;
|
|
|
- qry := FDeleteQry;
|
|
|
+ s := trim(FDeleteSQL.Text);
|
|
|
+ if (s='') and (not assigned(FDeleteQry) or (UpdateMode<>upWhereKeyOnly)) then
|
|
|
+ s := DeleteRecQuery;
|
|
|
+ qry := InitialiseModifyQuery(FDeleteQry);
|
|
|
end;
|
|
|
end;
|
|
|
+ if (qry.SQL.Text<>s) and (s<>'') then qry.SQL.Text:=s; //assign only when changed, to avoid UnPrepare/Prepare
|
|
|
assert(qry.sql.Text<>'');
|
|
|
with qry do
|
|
|
begin
|