Browse Source

* Patch from Laco to fix assigning transaction/database when database/transaction does not match

git-svn-id: trunk@30483 -
michael 10 years ago
parent
commit
c1935de563
1 changed files with 32 additions and 33 deletions
  1. 32 33
      packages/fcl-db/src/sqldb/sqldb.pp

+ 32 - 33
packages/fcl-db/src/sqldb/sqldb.pp

@@ -861,12 +861,27 @@ begin
     begin
     FDatabase.FreeNotification(Self);
     FDatabase.RegisterStatement(Self);
-    if (Transaction=nil) and (Assigned(FDatabase.Transaction)) then
-      transaction := FDatabase.Transaction;
+    if not Assigned(Transaction) or (Transaction.DataBase <> FDatabase) then
+      Transaction := FDatabase.Transaction;
     OnChangeSQL(Self);
     end;
 end;
 
+procedure TCustomSQLStatement.SetTransaction(AValue: TSQLTransaction);
+begin
+  if FTransaction=AValue then Exit;
+  UnPrepare;
+  if Assigned(FTransaction) then
+    FTransaction.RemoveFreeNotification(Self);
+  FTransaction:=AValue;
+  if Assigned(FTransaction) then
+    begin
+    FTransaction.FreeNotification(Self);
+    if Database <> FTransaction.DataBase then
+      Database := Transaction.Database as TSQLConnection;
+    end;
+end;
+
 procedure TCustomSQLStatement.SetDataSource(AValue: TDataSource);
 
 begin
@@ -894,21 +909,6 @@ begin
   FSQL.Assign(AValue);
 end;
 
-procedure TCustomSQLStatement.SetTransaction(AValue: TSQLTransaction);
-begin
-  if FTransaction=AValue then Exit;
-  UnPrepare;
-  if Assigned(FTransaction) then
-    FTransaction.RemoveFreeNotification(Self);
-  FTransaction:=AValue;
-  if Assigned(FTransaction) then
-    begin
-    FTransaction.FreeNotification(Self);
-    If (Database=Nil) then
-      Database:=Transaction.Database as TSQLConnection;
-    end;
-end;
-
 Procedure TCustomSQLStatement.DoExecute;
 begin
   FRowsAffected:=-1;
@@ -2293,31 +2293,30 @@ end;
 procedure TCustomSQLQuery.SetTransaction(Value: TDBTransaction);
 
 begin
+  if Transaction = Value then Exit;
   UnPrepare;
   inherited;
   If Assigned(FStatement) then
-    FStatement.Transaction:=TSQLTransaction(Value);
-  If (Transaction<>Nil) and (Database=Nil) then
-    Database:=SQLTransaction.Database;
+    FStatement.Transaction := TSQLTransaction(Value);
+  If Assigned(Transaction) and (SQLTransaction.DataBase<>Database) then
+    Database := Transaction.Database;
 end;
 
 procedure TCustomSQLQuery.SetDatabase(Value : TDatabase);
 
-var db : tsqlconnection;
+var DB : TSQLConnection;
 
 begin
-  if (Database <> Value) then
-    begin
-    if assigned(value) and not (Value is TSQLConnection) then
-      DatabaseErrorFmt(SErrNotASQLConnection,[value.Name],self);
-    UnPrepare;
-    db := TSQLConnection(Value);
-    If Assigned(FStatement) then
-      FStatement.Database:=DB;
-    inherited setdatabase(value);
-    if assigned(value) and (Transaction = nil) and (Assigned(db.Transaction)) then
-      transaction := Db.Transaction;
-    end;
+  if Database = Value then Exit;
+  if Assigned(Value) and not (Value is TSQLConnection) then
+    DatabaseErrorFmt(SErrNotASQLConnection, [Value.Name], Self);
+  UnPrepare;
+  DB := TSQLConnection(Value);
+  If Assigned(FStatement) then
+    FStatement.Database := DB;
+  inherited;
+  if Assigned(DB) and Assigned(DB.Transaction) and (not Assigned(Transaction) or (Transaction.DataBase<>Value)) then
+    Transaction := DB.Transaction;
 end;
 
 function TCustomSQLQuery.IsPrepared: Boolean;