2
0
Эх сурвалжийг харах

fcl-db: postgresql: cosmetic

git-svn-id: trunk@29479 -
lacak 10 жил өмнө
parent
commit
aa7dfdff96

+ 83 - 77
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;
+{ TPQTrans }
 
 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,22 +475,50 @@ begin
   result := true;
 end;
 
-function TPQConnection.StartImplicitTransaction(trans : TSQLHandle; AParams : string) : boolean;
+procedure TPQConnection.RollBackRetaining(trans : TSQLHandle);
 var
+  res : PPGresult;
   tr  : TPQTrans;
-  i   : Integer;
-  t : TPQTranConnection;
-  L : TList;
 begin
-  result:=false;
   tr := trans as TPQTrans;
+  res := PQexec(tr.PGConn, 'ROLLBACK');
+  CheckResultError(res,tr.PGConn,SErrRollbackFailed);
 
+  PQclear(res);
+  res := PQexec(tr.PGConn, 'BEGIN');
+  CheckResultError(res,tr.PGConn,sErrTransactionFailed);
+
+  PQclear(res);
+end;
+
+procedure TPQConnection.CommitRetaining(trans : TSQLHandle);
+var
+  res : PPGresult;
+  tr  : TPQTrans;
+begin
+  tr := trans as TPQTrans;
+  res := PQexec(tr.PGConn, 'COMMIT');
+  CheckResultError(res,tr.PGConn,SErrCommitFailed);
+
+  PQclear(res);
+  res := PQexec(tr.PGConn, 'BEGIN');
+  CheckResultError(res,tr.PGConn,sErrTransactionFailed);
+
+  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;
+  T:=Nil;
   L:=FConnectionPool.LockList;
   try
-    while (I<L.Count) do
+    while (i<L.Count) do
       begin
       T:=TPQTranConnection(L[i]);
       if (T.FPGConn=nil) or not T.FTranActive then
@@ -496,55 +534,24 @@ begin
   finally
     FConnectionPool.UnLockList;
   end;
+
   if (T=Nil) then
     begin
     T:=TPQTranConnection.Create;
     T.FTranActive:=True;
     AddConnection(T);
     end;
-  if (T.FPGConn<>nil) then
-    tr.PGConn:=T.FPGConn
-  else
+
+  if (T.FPGConn=nil) then
     begin
-    tr.PGConn := PQconnectdb(pchar(FConnectString));
-    T.FPGConn:=tr.PGConn;
-    CheckConnectionStatus(tr.PGConn);
+    T.FPGConn := PQconnectdb(pchar(FConnectString));
+    CheckConnectionStatus(T.FPGConn);
     if CharSet <> '' then
-      PQsetClientEncoding(tr.PGConn, pchar(CharSet));
+      PQsetClientEncoding(T.FPGConn, pchar(CharSet));
     end;
-  result := true;
-end;
 
-procedure TPQConnection.RollBackRetaining(trans : TSQLHandle);
-var
-  res : PPGresult;
-  tr  : TPQTrans;
-begin
-  tr := trans as TPQTrans;
-  res := PQexec(tr.PGConn, 'ROLLBACK');
-  CheckResultError(res,tr.PGConn,SErrRollbackFailed);
-
-  PQclear(res);
-  res := PQexec(tr.PGConn, 'BEGIN');
-  CheckResultError(res,tr.PGConn,sErrTransactionFailed);
-
-  PQclear(res);
-end;
-
-procedure TPQConnection.CommitRetaining(trans : TSQLHandle);
-var
-  res : PPGresult;
-  tr  : TPQTrans;
-begin
-  tr := trans as TPQTrans;
-  res := PQexec(tr.PGConn, 'COMMIT');
-  CheckResultError(res,tr.PGConn,SErrCommitFailed);
-
-  PQclear(res);
-  res := PQexec(tr.PGConn, 'BEGIN');
-  CheckResultError(res,tr.PGConn,sErrTransactionFailed);
-
-  PQclear(res);
+  TPQTrans(trans).PGConn := T.FPGConn;
+  Result := true;
 end;
 
 function TPQConnection.StartDBTransaction(trans: TSQLHandle;
@@ -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;