Parcourir la source

fcl-db: sqldb:
- if there is already opened FUpdateQry, which is used for refreshing field values, we must first close it and later open again to get new values.
- if there is automatically generated UpdateSQL, then we must always call ConstructUpdateSQL to correctly get ReturningClause
(probably there is better way how to handle SQL statements with returning clause)

git-svn-id: trunk@30798 -

lacak il y a 10 ans
Parent
commit
8b41206ffd
1 fichiers modifiés avec 22 ajouts et 18 suppressions
  1. 22 18
      packages/fcl-db/src/sqldb/sqldb.pp

+ 22 - 18
packages/fcl-db/src/sqldb/sqldb.pp

@@ -1663,7 +1663,7 @@ var x          : integer;
 begin
   sql_fields := '';
   sql_values := '';
-  returning_fields :='';
+  returning_fields := '';
   for x := 0 to Query.Fields.Count -1 do
     begin
     F:=Query.Fields[x];
@@ -1673,7 +1673,7 @@ begin
       sql_values := sql_values + ':"' + F.FieldName + '",';
       end;
     if ReturningClause and (pfRefreshOnInsert in F.ProviderFlags) then
-      returning_fields :=returning_fields+FieldNameQuoteChars[0] + F.FieldName + FieldNameQuoteChars[1] + ',';
+      returning_fields := returning_fields + FieldNameQuoteChars[0] + F.FieldName + FieldNameQuoteChars[1] + ',';
     end;
   if length(sql_fields) = 0 then
     DatabaseErrorFmt(sNoUpdateFields,['insert'],self);
@@ -1686,7 +1686,7 @@ begin
     if ReturningClause then
       begin
       setlength(returning_fields,length(returning_fields)-1);
-      result:=Result+' returning '+returning_fields;
+      Result := Result + ' returning ' + returning_fields;
       end;
     end;
 end;
@@ -1704,7 +1704,7 @@ var x : integer;
 begin
   sql_set := '';
   sql_where := '';
-  returning_fields :='';
+  returning_fields := '';
   for x := 0 to Query.Fields.Count -1 do
     begin
     F:=Query.Fields[x];
@@ -1712,7 +1712,7 @@ begin
     if (pfInUpdate in F.ProviderFlags) and (not F.ReadOnly) then
       sql_set := sql_set +FieldNameQuoteChars[0] + F.FieldName + FieldNameQuoteChars[1] +'=:"' + F.FieldName + '",';
     if ReturningClause and (pfRefreshOnUpdate in F.ProviderFlags) then
-      returning_fields :=returning_fields+FieldNameQuoteChars[0] + F.FieldName + FieldNameQuoteChars[1] + ',';
+      returning_fields := returning_fields + FieldNameQuoteChars[0] + F.FieldName + FieldNameQuoteChars[1] + ',';
     end;
   if length(sql_set) = 0 then DatabaseErrorFmt(sNoUpdateFields,['update'],self);
   setlength(sql_set,length(sql_set)-1);
@@ -1724,7 +1724,7 @@ begin
     if ReturningClause then
       begin
       setlength(returning_fields,length(returning_fields)-1);
-      result:=Result+' returning '+returning_fields;
+      Result := Result + ' returning ' + returning_fields;
       end;
     end;
 end;
@@ -1807,27 +1807,29 @@ begin
   ReturningClause:=(sqSupportReturning in ConnOptions) and not (sqoRefreshUsingSelect in Query.Options) and (Query.RefreshSQL.Count=0);
   case UpdateKind of
     ukInsert : begin
-               s := trim(Query.FInsertSQL.Text);
+               s := Trim(Query.FInsertSQL.Text);
                if s = '' then
-                 s := ConstructInsertSQL(Query,ReturningClause)
+                 s := ConstructInsertSQL(Query, ReturningClause)
                else
                  ReturningClause := False;
-               qry := InitialiseUpdateStatement(Query,Query.FInsertQry);
+               qry := InitialiseUpdateStatement(Query, Query.FInsertQry);
                end;
     ukModify : begin
-               s := trim(Query.FUpdateSQL.Text);
-               if (s='') and (not assigned(Query.FUpdateQry) or (Query.UpdateMode<>upWhereKeyOnly)) then //first time or dynamic where part
-                 s := ConstructUpdateSQL(Query,ReturningClause)
+               s := Trim(Query.FUpdateSQL.Text);
+               if s = '' then begin
+                 //if not assigned(Query.FUpdateQry) or (Query.UpdateMode<>upWhereKeyOnly) then // first time or dynamic where part
+                   s := ConstructUpdateSQL(Query, ReturningClause);
+               end
                else
                  ReturningClause := False;
-               qry := InitialiseUpdateStatement(Query,Query.FUpdateQry);
+               qry := InitialiseUpdateStatement(Query, Query.FUpdateQry);
                end;
     ukDelete : begin
-               s := trim(Query.FDeleteSQL.Text);
+               s := Trim(Query.FDeleteSQL.Text);
                if (s='') and (not assigned(Query.FDeleteQry) or (Query.UpdateMode<>upWhereKeyOnly)) then
                  s := ConstructDeleteSQL(Query);
-               ReturningClause:=False;
-               qry := InitialiseUpdateStatement(Query,Query.FDeleteQry);
+               ReturningClause := False;
+               qry := InitialiseUpdateStatement(Query, Query.FDeleteQry);
                end;
   end;
   if (s<>'') and (qry.SQL.Text<>s) then
@@ -1844,13 +1846,15 @@ begin
     ApplyFieldUpdate(Query.Cursor,P as TSQLDBParam,Fld,B);
     end;
   if ReturningClause then
+    begin
+    Qry.Close;
     Qry.Open
+    end
   else
     Qry.Execute;
   if (scoApplyUpdatesChecksRowsAffected in Options) and (Qry.RowsAffected<>1) then
     begin
-    if ReturningClause then
-      Qry.Close;
+    Qry.Close;
     DatabaseErrorFmt(SErrFailedToUpdateRecord, [Qry.RowsAffected], Query);
     end;
   if ReturningClause then