diff --git a/packages/fcl-db/src/sqldb/postgres/pqconnection.pp b/packages/fcl-db/src/sqldb/postgres/pqconnection.pp index d31ebbe238..8d0321c34e 100644 --- a/packages/fcl-db/src/sqldb/postgres/pqconnection.pp +++ b/packages/fcl-db/src/sqldb/postgres/pqconnection.pp @@ -21,10 +21,10 @@ type TPQTrans = Class(TSQLHandle) protected - PGConn : PPGConn; - FList : TThreadList; - Procedure RegisterCursor(S : TPQCursor); - Procedure UnRegisterCursor(S : TPQCursor); + PGConn : PPGConn; + FList : TThreadList; + Procedure RegisterCursor(Cursor : TPQCursor); + Procedure UnRegisterCursor(Cursor : TPQCursor); Public Constructor Create; Destructor Destroy; override; @@ -60,6 +60,8 @@ type Destructor Destroy; override; end; + { EPQDatabaseError } + EPQDatabaseError = class(EDatabaseError) public SEVERITY:string; @@ -70,6 +72,8 @@ type STATEMENT_POSITION:string; end; + { TPQTranConnection } + TPQTranConnection = class protected FPGConn : PPGConn; @@ -125,7 +129,7 @@ type function RowsAffected(cursor: TSQLCursor): TRowsCount; override; public constructor Create(AOwner : TComponent); override; - destructor destroy; override; + destructor Destroy; override; function GetConnectionInfo(InfoType:TConnInfoType): string; override; procedure CreateDB; override; procedure DropDB; override; @@ -189,23 +193,12 @@ const Oid_Bool = 16; oid_numeric = 1700; Oid_uuid = 2950; + { TPQTrans } -procedure TPQTrans.RegisterCursor(S: TPQCursor); -begin - FList.Add(S); - S.tr:=Self; -end; - -procedure TPQTrans.UnRegisterCursor(S: TPQCursor); -begin - S.tr:=Nil; - FList.Remove(S); -end; - constructor TPQTrans.Create; begin - Flist:=TThreadList.Create; + FList:=TThreadList.Create; FList.Duplicates:=dupIgnore; end; @@ -216,19 +209,39 @@ Var I : integer; begin - L:=Flist.LockList; + L:=FList.LockList; try For I:=0 to L.Count-1 do TPQCursor(L[i]).tr:=Nil; finally - Flist.UnlockList; + FList.UnlockList; end; FreeAndNil(FList); inherited Destroy; end; +procedure TPQTrans.RegisterCursor(Cursor: TPQCursor); +begin + FList.Add(Cursor); + Cursor.tr:=Self; +end; + +procedure TPQTrans.UnRegisterCursor(Cursor: TPQCursor); +begin + Cursor.tr:=Nil; + FList.Remove(Cursor); +end; + + { TPQCursor } +destructor TPQCursor.Destroy; +begin + if Assigned(tr) then + tr.UnRegisterCursor(Self); + inherited Destroy; +end; + function TPQCursor.GetFieldBinding(F: TFieldDef): PFieldBinding; Var @@ -252,13 +265,8 @@ begin end; end; -destructor TPQCursor.Destroy; -begin - if Assigned(tr) then - Tr.UnRegisterCursor(Self); - inherited Destroy; -end; +{ TPQConnection } constructor TPQConnection.Create(AOwner : TComponent); @@ -270,7 +278,7 @@ begin FConnectionPool:=TThreadlist.Create; end; -destructor TPQConnection.destroy; +destructor TPQConnection.Destroy; begin // We must disconnect here. If it is done in inherited, then connection pool is gone. Connected:=False; @@ -432,6 +440,7 @@ var begin result := false; tr := trans as TPQTrans; + // unprepare statements associated with given transaction L:=tr.FList.LockList; try For I:=0 to L.Count-1 do @@ -441,8 +450,9 @@ begin end; L.Clear; finally - tr.flist.UnlockList; + tr.FList.UnlockList; end; + res := PQexec(tr.PGConn, 'ROLLBACK'); CheckResultError(res,tr.PGConn,SErrRollbackFailed); PQclear(res); @@ -465,56 +475,6 @@ begin result := true; end; -function TPQConnection.StartImplicitTransaction(trans : TSQLHandle; AParams : string) : boolean; -var - tr : TPQTrans; - i : Integer; - t : TPQTranConnection; - L : TList; -begin - result:=false; - tr := trans as TPQTrans; - - //find an unused connection in the pool - i:=0; - t:=Nil; - L:=FConnectionPool.LockList; - try - while (Inil) then - tr.PGConn:=T.FPGConn - else - begin - tr.PGConn := PQconnectdb(pchar(FConnectString)); - T.FPGConn:=tr.PGConn; - CheckConnectionStatus(tr.PGConn); - if CharSet <> '' then - PQsetClientEncoding(tr.PGConn, pchar(CharSet)); - end; - result := true; -end; - procedure TPQConnection.RollBackRetaining(trans : TSQLHandle); var res : PPGresult; @@ -547,6 +507,53 @@ begin PQclear(res); end; +function TPQConnection.StartImplicitTransaction(trans : TSQLHandle; AParams : string) : boolean; +var + i : Integer; + T : TPQTranConnection; + L : TList; +begin + //find an unused connection in the pool + i:=0; + T:=Nil; + L:=FConnectionPool.LockList; + try + while (i '' then + PQsetClientEncoding(T.FPGConn, pchar(CharSet)); + end; + + TPQTrans(trans).PGConn := T.FPGConn; + Result := true; +end; + function TPQConnection.StartDBTransaction(trans: TSQLHandle; AParams: string): boolean; @@ -866,7 +873,6 @@ var i : integer; P : TParam; PQ : TSQLDBParam; - r : PPGresult; begin with (cursor as TPQCursor) do @@ -1029,7 +1035,7 @@ begin end else begin - // Registercursor sets tr + // RegisterCursor sets tr TPQTrans(aTransaction.Handle).RegisterCursor(Cursor as TPQCursor); if Assigned(AParams) and (AParams.Count > 0) then @@ -1119,7 +1125,7 @@ begin end else if ErrorOnUnknownType then - DatabaseError('unhandled field type :'+FB^.TypeName,Self); + DatabaseError('Unhandled field type :'+FB^.TypeName,Self); end; end; end;