diff --git a/packages/fcl-db/src/sqldb/interbase/ibconnection.pp b/packages/fcl-db/src/sqldb/interbase/ibconnection.pp index 0eaa3366be..3ac0dbf75f 100644 --- a/packages/fcl-db/src/sqldb/interbase/ibconnection.pp +++ b/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 CheckError('PrepareStatement', Status); - FPrepared := True; if assigned(AParams) and (AParams.count > 0) then begin AllocSQLDA(in_SQLDA,Length(ParamBinding)); @@ -556,7 +555,6 @@ begin AllocSQLDA(in_SQLDA,0); if FStatementType = stselect then begin - FPrepared := False; if isc_dsql_describe(@Status[0], @Statement, 1, SQLDA) <> 0 then CheckError('PrepareSelect', Status); if SQLDA^.SQLD > SQLDA^.SQLN then @@ -576,6 +574,7 @@ begin end; {$R+} end; + FPrepared := True; end; end; diff --git a/packages/fcl-db/src/sqldb/sqldb.pp b/packages/fcl-db/src/sqldb/sqldb.pp index 54da2cda05..5bf86befba 100644 --- a/packages/fcl-db/src/sqldb/sqldb.pp +++ b/packages/fcl-db/src/sqldb/sqldb.pp @@ -212,7 +212,7 @@ type FUpdateQry, FDeleteQry, FInsertQry : TCustomSQLQuery; - + FOpenDidPrepare : Boolean; procedure FreeFldBuffers; function GetServerIndexDefs: TServerIndexDefs; function GetStatementType : TStatementType; @@ -993,7 +993,11 @@ procedure TCustomSQLQuery.InternalClose; begin if StatementType = stSelect then FreeFldBuffers; // 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 DestroyFields; FIsEOF := False; @@ -1176,7 +1180,9 @@ var tel, fieldc : integer; IndexFields : TStrings; begin try - Prepare; + FOpenDidPrepare:=Not Prepared; + If FOpenDidPrepare then + Prepare; if FCursor.FStatementType in [stSelect] then begin Execute; @@ -1230,14 +1236,21 @@ end; // public part procedure TCustomSQLQuery.ExecSQL; + +Var + ExecDidPrepare : Boolean; + begin try - Prepare; + ExecDidPrepare:=Not IsPrepared; + If ExecDidPrepare then + Prepare; Execute; finally // FCursor has to be assigned, or else the prepare went wrong before PrepareStatment was // 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;