Selaa lähdekoodia

* Fixed prepare for select statements

git-svn-id: trunk@11511 -
michael 17 vuotta sitten
vanhempi
commit
d39c352d92

+ 1 - 2
packages/fcl-db/src/sqldb/interbase/ibconnection.pp

@@ -533,7 +533,6 @@ begin
 
 
     if isc_dsql_prepare(@Status[0], @tr, @Statement, 0, @Buf[1], Dialect, nil) <> 0 then
     if isc_dsql_prepare(@Status[0], @tr, @Statement, 0, @Buf[1], Dialect, nil) <> 0 then
       CheckError('PrepareStatement', Status);
       CheckError('PrepareStatement', Status);
-    FPrepared := True;
     if assigned(AParams) and (AParams.count > 0) then
     if assigned(AParams) and (AParams.count > 0) then
       begin
       begin
       AllocSQLDA(in_SQLDA,Length(ParamBinding));
       AllocSQLDA(in_SQLDA,Length(ParamBinding));
@@ -556,7 +555,6 @@ begin
       AllocSQLDA(in_SQLDA,0);
       AllocSQLDA(in_SQLDA,0);
     if FStatementType = stselect then
     if FStatementType = stselect then
       begin
       begin
-      FPrepared := False;
       if isc_dsql_describe(@Status[0], @Statement, 1, SQLDA) <> 0 then
       if isc_dsql_describe(@Status[0], @Statement, 1, SQLDA) <> 0 then
         CheckError('PrepareSelect', Status);
         CheckError('PrepareSelect', Status);
       if SQLDA^.SQLD > SQLDA^.SQLN then
       if SQLDA^.SQLD > SQLDA^.SQLN then
@@ -576,6 +574,7 @@ begin
         end;
         end;
       {$R+}
       {$R+}
       end;
       end;
+    FPrepared := True;
     end;
     end;
 end;
 end;
 
 

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

@@ -212,7 +212,7 @@ type
     FUpdateQry,
     FUpdateQry,
     FDeleteQry,
     FDeleteQry,
     FInsertQry           : TCustomSQLQuery;
     FInsertQry           : TCustomSQLQuery;
-
+    FOpenDidPrepare : Boolean;
     procedure FreeFldBuffers;
     procedure FreeFldBuffers;
     function GetServerIndexDefs: TServerIndexDefs;
     function GetServerIndexDefs: TServerIndexDefs;
     function GetStatementType : TStatementType;
     function GetStatementType : TStatementType;
@@ -993,7 +993,11 @@ procedure TCustomSQLQuery.InternalClose;
 begin
 begin
   if StatementType = stSelect then FreeFldBuffers;
   if StatementType = stSelect then FreeFldBuffers;
 // Database and FCursor could be nil, for example if the database is not assigned, and .open is called
 // Database and FCursor could be nil, for example if the database is not assigned, and .open is called
-  if (not IsPrepared) and (assigned(database)) and (assigned(FCursor)) then TSQLConnection(database).UnPrepareStatement(FCursor);
+  if (FOpenDidPrepare) and (assigned(database)) and (assigned(FCursor)) then
+    begin
+    FOpenDidPrepare:=False;
+    TSQLConnection(database).UnPrepareStatement(FCursor);
+    end;
   if DefaultFields then
   if DefaultFields then
     DestroyFields;
     DestroyFields;
   FIsEOF := False;
   FIsEOF := False;
@@ -1176,7 +1180,9 @@ var tel, fieldc : integer;
     IndexFields : TStrings;
     IndexFields : TStrings;
 begin
 begin
   try
   try
-    Prepare;
+    FOpenDidPrepare:=Not Prepared;
+    If FOpenDidPrepare then
+      Prepare;
     if FCursor.FStatementType in [stSelect] then
     if FCursor.FStatementType in [stSelect] then
       begin
       begin
       Execute;
       Execute;
@@ -1230,14 +1236,21 @@ end;
 // public part
 // public part
 
 
 procedure TCustomSQLQuery.ExecSQL;
 procedure TCustomSQLQuery.ExecSQL;
+
+Var
+  ExecDidPrepare : Boolean;
+
 begin
 begin
   try
   try
-    Prepare;
+    ExecDidPrepare:=Not IsPrepared;
+    If ExecDidPrepare then
+      Prepare;
     Execute;
     Execute;
   finally
   finally
     // FCursor has to be assigned, or else the prepare went wrong before PrepareStatment was
     // FCursor has to be assigned, or else the prepare went wrong before PrepareStatment was
     // called, so UnPrepareStatement shoudn't be called either
     // called, so UnPrepareStatement shoudn't be called either
-    if (not IsPrepared) and (assigned(database)) and (assigned(FCursor)) then TSQLConnection(database).UnPrepareStatement(Fcursor);
+    if (ExecDidPrepare) and (assigned(database)) and (assigned(FCursor)) then
+      TSQLConnection(database).UnPrepareStatement(Fcursor);
   end;
   end;
 end;
 end;