Browse Source

* Some cosmetic changes, added SQLConnection/SQLTransaction to avoid typecasts everywhere

git-svn-id: trunk@26767 -
michael 11 years ago
parent
commit
b9eb560dde
1 changed files with 73 additions and 42 deletions
  1. 73 42
      packages/fcl-db/src/sqldb/sqldb.pp

+ 73 - 42
packages/fcl-db/src/sqldb/sqldb.pp

@@ -169,7 +169,6 @@ type
     destructor Destroy; override;
     destructor Destroy; override;
     procedure StartTransaction; override;
     procedure StartTransaction; override;
     procedure EndTransaction; override;
     procedure EndTransaction; override;
-    property ConnOptions: TConnOptions read FConnOptions;
     procedure ExecuteDirect(SQL : String); overload; virtual;
     procedure ExecuteDirect(SQL : String); overload; virtual;
     procedure ExecuteDirect(SQL : String; ATransaction : TSQLTransaction); overload; virtual;
     procedure ExecuteDirect(SQL : String; ATransaction : TSQLTransaction); overload; virtual;
     procedure GetTableNames(List : TStrings; SystemTables : Boolean = false); virtual;
     procedure GetTableNames(List : TStrings; SystemTables : Boolean = false); virtual;
@@ -180,6 +179,7 @@ type
     function GetStatementInfo(const ASQL: string; Full: Boolean; ASchema : TSchemaType): TSQLStatementInfo; virtual;
     function GetStatementInfo(const ASQL: string; Full: Boolean; ASchema : TSchemaType): TSQLStatementInfo; virtual;
     procedure CreateDB; virtual;
     procedure CreateDB; virtual;
     procedure DropDB; virtual;
     procedure DropDB; virtual;
+    property ConnOptions: TConnOptions read FConnOptions;
   published
   published
     property Password : string read FPassword write FPassword;
     property Password : string read FPassword write FPassword;
     property Transaction : TSQLTransaction read FTransaction write SetTransaction;
     property Transaction : TSQLTransaction read FTransaction write SetTransaction;
@@ -207,22 +207,25 @@ type
     FTrans               : TSQLHandle;
     FTrans               : TSQLHandle;
     FAction              : TCommitRollbackAction;
     FAction              : TCommitRollbackAction;
     FParams              : TStringList;
     FParams              : TStringList;
+    function GetSQLConnection: TSQLConnection;
     procedure SetParams(const AValue: TStringList);
     procedure SetParams(const AValue: TStringList);
+    procedure SetSQLConnection(AValue: TSQLConnection);
   protected
   protected
     function GetHandle : Pointer; virtual;
     function GetHandle : Pointer; virtual;
     Procedure SetDatabase (Value : TDatabase); override;
     Procedure SetDatabase (Value : TDatabase); override;
     Function LogEvent(EventType : TDBEventType) : Boolean;
     Function LogEvent(EventType : TDBEventType) : Boolean;
     Procedure Log(EventType : TDBEventType; Const Msg : String); virtual;
     Procedure Log(EventType : TDBEventType; Const Msg : String); virtual;
   public
   public
+    constructor Create(AOwner : TComponent); override;
+    destructor Destroy; override;
     procedure Commit; virtual;
     procedure Commit; virtual;
     procedure CommitRetaining; virtual;
     procedure CommitRetaining; virtual;
     procedure Rollback; virtual;
     procedure Rollback; virtual;
     procedure RollbackRetaining; virtual;
     procedure RollbackRetaining; virtual;
     procedure StartTransaction; override;
     procedure StartTransaction; override;
-    constructor Create(AOwner : TComponent); override;
-    destructor Destroy; override;
-    property Handle: Pointer read GetHandle;
     procedure EndTransaction; override;
     procedure EndTransaction; override;
+    property Handle: Pointer read GetHandle;
+    Property SQLConnection : TSQLConnection Read GetSQLConnection Write SetSQLConnection;
   published
   published
     property Action : TCommitRollbackAction read FAction write FAction Default caRollBack;
     property Action : TCommitRollbackAction read FAction write FAction Default caRollBack;
     property Database;
     property Database;
@@ -302,9 +305,7 @@ type
 
 
   TCustomSQLQuery = class (TCustomBufDataset)
   TCustomSQLQuery = class (TCustomBufDataset)
   private
   private
-    // FCursor              : TSQLCursor;
     FSchemaType: TSchemaType;
     FSchemaType: TSchemaType;
-//    FSQL: TStringlist;
     FUpdateable          : boolean;
     FUpdateable          : boolean;
     FTableName           : string;
     FTableName           : string;
     FStatement           : TCustomSQLStatement;
     FStatement           : TCustomSQLStatement;
@@ -318,10 +319,6 @@ type
     FSQLBuf              : String;
     FSQLBuf              : String;
     FWhereStartPos       : integer;
     FWhereStartPos       : integer;
     FWhereStopPos        : integer;
     FWhereStopPos        : integer;
-    // FParseSQL            : boolean;
-//    FMasterLink          : TMasterParamsDatalink;
-//    FSchemaInfo          : TSchemaInfo;
-
     FServerFilterText    : string;
     FServerFilterText    : string;
     FServerFiltered      : Boolean;
     FServerFiltered      : Boolean;
 
 
@@ -340,8 +337,12 @@ type
     function GetParseSQL: Boolean;
     function GetParseSQL: Boolean;
     function GetServerIndexDefs: TServerIndexDefs;
     function GetServerIndexDefs: TServerIndexDefs;
     function GetSQL: TStringlist;
     function GetSQL: TStringlist;
+    function GetSQLConnection: TSQLConnection;
+    function GetSQLTransaction: TSQLTransaction;
     function GetStatementType : TStatementType;
     function GetStatementType : TStatementType;
     procedure SetParamCheck(AValue: Boolean);
     procedure SetParamCheck(AValue: Boolean);
+    procedure SetSQLConnection(AValue: TSQLConnection);
+    procedure SetSQLTransaction(AValue: TSQLTransaction);
     procedure SetUpdateSQL(const AValue: TStringlist);
     procedure SetUpdateSQL(const AValue: TStringlist);
     procedure SetDeleteSQL(const AValue: TStringlist);
     procedure SetDeleteSQL(const AValue: TStringlist);
     procedure SetInsertSQL(const AValue: TStringlist);
     procedure SetInsertSQL(const AValue: TStringlist);
@@ -350,10 +351,8 @@ type
     procedure SetSQL(const AValue: TStringlist);
     procedure SetSQL(const AValue: TStringlist);
     procedure SetUsePrimaryKeyAsKey(AValue : Boolean);
     procedure SetUsePrimaryKeyAsKey(AValue : Boolean);
     procedure SetUpdateMode(AValue : TUpdateMode);
     procedure SetUpdateMode(AValue : TUpdateMode);
-//    procedure OnChangeSQL(Sender : TObject);
     procedure OnChangeModifySQL(Sender : TObject);
     procedure OnChangeModifySQL(Sender : TObject);
     procedure Execute;
     procedure Execute;
-//    Function SQLParser(const ASQL : string) : TStatementType;
     procedure ApplyFilter;
     procedure ApplyFilter;
     Function AddFilter(SQLstr : string) : string;
     Function AddFilter(SQLstr : string) : string;
   protected
   protected
@@ -387,15 +386,17 @@ type
     Function LogEvent(EventType : TDBEventType) : Boolean;
     Function LogEvent(EventType : TDBEventType) : Boolean;
     Procedure Log(EventType : TDBEventType; Const Msg : String); virtual;
     Procedure Log(EventType : TDBEventType; Const Msg : String); virtual;
   public
   public
+    constructor Create(AOwner : TComponent); override;
+    destructor Destroy; override;
     procedure Prepare; virtual;
     procedure Prepare; virtual;
     procedure UnPrepare; virtual;
     procedure UnPrepare; virtual;
     procedure ExecSQL; virtual;
     procedure ExecSQL; virtual;
-    constructor Create(AOwner : TComponent); override;
-    destructor Destroy; override;
     procedure SetSchemaInfo( ASchemaType : TSchemaType; ASchemaObjectName, ASchemaPattern : string); virtual;
     procedure SetSchemaInfo( ASchemaType : TSchemaType; ASchemaObjectName, ASchemaPattern : string); virtual;
-    property Prepared : boolean read IsPrepared;
     function RowsAffected: TRowsCount; virtual;
     function RowsAffected: TRowsCount; virtual;
     function ParamByName(Const AParamName : String) : TParam;
     function ParamByName(Const AParamName : String) : TParam;
+    Property Prepared : boolean read IsPrepared;
+    Property SQLConnection : TSQLConnection Read GetSQLConnection Write SetSQLConnection;
+    Property SQLTransaction: TSQLTransaction Read GetSQLTransaction Write SetSQLTransaction;
   protected
   protected
 
 
     // redeclared data set properties
     // redeclared data set properties
@@ -1297,9 +1298,19 @@ begin
   FParams.Assign(AValue);
   FParams.Assign(AValue);
 end;
 end;
 
 
-function TSQLTransaction.GetHandle: pointer;
+function TSQLTransaction.GetSQLConnection: TSQLConnection;
+begin
+  Result:=Database as TSQLConnection;
+end;
+
+procedure TSQLTransaction.SetSQLConnection(AValue: TSQLConnection);
 begin
 begin
-  Result := TSQLConnection(Database).GetTransactionHandle(FTrans);
+  Database:=AValue;
+end;
+
+function TSQLTransaction.GetHandle: Pointer;
+begin
+  Result := SQLConnection.GetTransactionHandle(FTrans);
 end;
 end;
 
 
 procedure TSQLTransaction.Commit;
 procedure TSQLTransaction.Commit;
@@ -1309,7 +1320,7 @@ begin
     closedatasets;
     closedatasets;
     If LogEvent(detCommit) then
     If LogEvent(detCommit) then
       Log(detCommit,SCommitting);
       Log(detCommit,SCommitting);
-    if TSQLConnection(Database).commit(FTrans) then
+    if SQLConnection.commit(FTrans) then
       begin
       begin
       closeTrans;
       closeTrans;
       FreeAndNil(FTrans);
       FreeAndNil(FTrans);
@@ -1323,7 +1334,7 @@ begin
     begin
     begin
     If LogEvent(detCommit) then
     If LogEvent(detCommit) then
       Log(detCommit,SCommitRetaining);
       Log(detCommit,SCommitRetaining);
-    TSQLConnection(Database).commitRetaining(FTrans);
+    SQLConnection.commitRetaining(FTrans);
     end;
     end;
 end;
 end;
 
 
@@ -1334,7 +1345,7 @@ begin
     closedatasets;
     closedatasets;
     If LogEvent(detRollback) then
     If LogEvent(detRollback) then
       Log(detRollback,SRollingBack);
       Log(detRollback,SRollingBack);
-    if TSQLConnection(Database).RollBack(FTrans) then
+    if SQLConnection.RollBack(FTrans) then
       begin
       begin
       CloseTrans;
       CloseTrans;
       FreeAndNil(FTrans);
       FreeAndNil(FTrans);
@@ -1348,7 +1359,7 @@ begin
     begin
     begin
     If LogEvent(detRollback) then
     If LogEvent(detRollback) then
       Log(detRollback,SRollBackRetaining);
       Log(detRollback,SRollBackRetaining);
-    TSQLConnection(Database).RollBackRetaining(FTrans);
+    SQLConnection.RollBackRetaining(FTrans);
     end;
     end;
 end;
 end;
 
 
@@ -1360,7 +1371,7 @@ begin
   if Active then
   if Active then
     DatabaseError(SErrTransAlreadyActive);
     DatabaseError(SErrTransAlreadyActive);
 
 
-  db := TSQLConnection(Database);
+  db := SQLConnection;
 
 
   if Db = nil then
   if Db = nil then
     DatabaseError(SErrDatabasenAssigned);
     DatabaseError(SErrDatabasenAssigned);
@@ -1387,7 +1398,7 @@ begin
   inherited Destroy;
   inherited Destroy;
 end;
 end;
 
 
-Procedure TSQLTransaction.SetDatabase(Value : TDatabase);
+procedure TSQLTransaction.SetDatabase(Value: TDatabase);
 
 
 begin
 begin
   If Value<>Database then
   If Value<>Database then
@@ -1396,18 +1407,18 @@ begin
       DatabaseErrorFmt(SErrNotASQLConnection,[value.Name],self);
       DatabaseErrorFmt(SErrNotASQLConnection,[value.Name],self);
     CheckInactive;
     CheckInactive;
     If Assigned(Database) then
     If Assigned(Database) then
-      with TSQLConnection(DataBase) do
+      with SQLConnection do
         if Transaction = self then Transaction := nil;
         if Transaction = self then Transaction := nil;
     inherited SetDatabase(Value);
     inherited SetDatabase(Value);
     If Assigned(Database) and not (csLoading in ComponentState) then
     If Assigned(Database) and not (csLoading in ComponentState) then
-      If (TSQLConnection(DataBase).Transaction=Nil) then
-        TSQLConnection(DataBase).Transaction:=Self;
+      If (SQLConnection.Transaction=Nil) then
+        SQLConnection.Transaction:=Self;
     end;
     end;
 end;
 end;
 
 
 function TSQLTransaction.LogEvent(EventType: TDBEventType): Boolean;
 function TSQLTransaction.LogEvent(EventType: TDBEventType): Boolean;
 begin
 begin
-  Result:=Assigned(Database) and TSQLConnection(Database).LogEvent(EventType);
+  Result:=Assigned(Database) and SQLConnection.LogEvent(EventType);
 end;
 end;
 
 
 procedure TSQLTransaction.Log(EventType: TDBEventType; const Msg: String);
 procedure TSQLTransaction.Log(EventType: TDBEventType; const Msg: String);
@@ -1422,7 +1433,7 @@ begin
       M:=Name+' : '+Msg
       M:=Name+' : '+Msg
     else
     else
       M:=Msg;
       M:=Msg;
-    TSQLConnection(Database).Log(EventType,M);
+    SQLConnection.Log(EventType,M);
     end;
     end;
 end;
 end;
 
 
@@ -1438,7 +1449,7 @@ begin
   if (FSQL <> nil) and ParamCheck then
   if (FSQL <> nil) and ParamCheck then
     begin
     begin
     if assigned(DataBase) then
     if assigned(DataBase) then
-      ConnOptions := TSQLConnection(DataBase).ConnOptions
+      ConnOptions := SQLConnection.ConnOptions
     else
     else
       ConnOptions := [sqEscapeRepeat,sqEscapeSlash];
       ConnOptions := [sqEscapeRepeat,sqEscapeSlash];
     //preserve existing param. values
     //preserve existing param. values
@@ -1474,7 +1485,7 @@ begin
   If Assigned(FStatement) then
   If Assigned(FStatement) then
     FStatement.Transaction:=TSQLTransaction(Value);
     FStatement.Transaction:=TSQLTransaction(Value);
   If (Transaction<>Nil) and (Database=Nil) then
   If (Transaction<>Nil) and (Database=Nil) then
-    Database:=TSQLTransaction(Transaction).Database;
+    Database:=SQLTransaction.Database;
 end;
 end;
 
 
 procedure TCustomSQLQuery.SetDatabase(Value : TDatabase);
 procedure TCustomSQLQuery.SetDatabase(Value : TDatabase);
@@ -1493,7 +1504,7 @@ begin
     inherited setdatabase(value);
     inherited setdatabase(value);
 (*
 (*
      FStatement.Database:=Db,
      FStatement.Database:=Db,
-    if assigned(FCursor) then TSQLConnection(DataBase).DeAllocateCursorHandle(FCursor);
+    if assigned(FCursor) then SQLConnection.DeAllocateCursorHandle(FCursor);
 *)
 *)
     if assigned(value) and (Transaction = nil) and (Assigned(db.Transaction)) then
     if assigned(value) and (Transaction = nil) and (Assigned(db.Transaction)) then
       transaction := Db.Transaction;
       transaction := Db.Transaction;
@@ -1595,7 +1606,7 @@ end;
 procedure TCustomSQLQuery.FreeFldBuffers;
 procedure TCustomSQLQuery.FreeFldBuffers;
 begin
 begin
   if assigned(Cursor) then
   if assigned(Cursor) then
-     TSQLConnection(Database).FreeFldBuffers(Cursor);
+     SQLConnection.FreeFldBuffers(Cursor);
 end;
 end;
 
 
 function TCustomSQLQuery.GetParamCheck: Boolean;
 function TCustomSQLQuery.GetParamCheck: Boolean;
@@ -1623,6 +1634,16 @@ begin
   Result:=TStringList(Fstatement.SQL);
   Result:=TStringList(Fstatement.SQL);
 end;
 end;
 
 
+function TCustomSQLQuery.GetSQLConnection: TSQLConnection;
+begin
+  Result:=Database as TSQLConnection;
+end;
+
+function TCustomSQLQuery.GetSQLTransaction: TSQLTransaction;
+begin
+  Result:=Transaction as TSQLTransaction;
+end;
+
 function TCustomSQLQuery.Fetch : boolean;
 function TCustomSQLQuery.Fetch : boolean;
 begin
 begin
   if Not Assigned(Cursor) then
   if Not Assigned(Cursor) then
@@ -1631,7 +1652,7 @@ begin
     Exit;
     Exit;
   If LogEvent(detFetch) then
   If LogEvent(detFetch) then
     Log(detFetch,FSQLBuf);
     Log(detFetch,FSQLBuf);
-  if not FIsEof then FIsEOF := not TSQLConnection(Database).Fetch(Cursor);
+  if not FIsEof then FIsEOF := not SQLConnection.Fetch(Cursor);
   Result := not FIsEOF;
   Result := not FIsEOF;
 end;
 end;
 
 
@@ -1652,13 +1673,13 @@ end;
 
 
 function TCustomSQLQuery.LoadField(FieldDef : TFieldDef;buffer : pointer; out CreateBlob : boolean) : boolean;
 function TCustomSQLQuery.LoadField(FieldDef : TFieldDef;buffer : pointer; out CreateBlob : boolean) : boolean;
 begin
 begin
-  result := TSQLConnection(Database).LoadField(Cursor,FieldDef,buffer, Createblob)
+  result := SQLConnection.LoadField(Cursor,FieldDef,buffer, Createblob)
 end;
 end;
 
 
 procedure TCustomSQLQuery.LoadBlobIntoBuffer(FieldDef: TFieldDef;
 procedure TCustomSQLQuery.LoadBlobIntoBuffer(FieldDef: TFieldDef;
   ABlobBuf: PBufBlobField);
   ABlobBuf: PBufBlobField);
 begin
 begin
-  TSQLConnection(DataBase).LoadBlobIntoBuffer(FieldDef, ABlobBuf, Cursor,(Transaction as TSQLTransaction));
+  SQLConnection.LoadBlobIntoBuffer(FieldDef, ABlobBuf, Cursor,SQLTransaction);
 end;
 end;
 
 
 procedure TCustomSQLQuery.InternalAddRecord(Buffer: Pointer; AAppend: Boolean);
 procedure TCustomSQLQuery.InternalAddRecord(Buffer: Pointer; AAppend: Boolean);
@@ -1696,7 +1717,7 @@ begin
   try
   try
     FieldDefs.Clear;
     FieldDefs.Clear;
     if not Assigned(Database) then DatabaseError(SErrDatabasenAssigned);
     if not Assigned(Database) then DatabaseError(SErrDatabasenAssigned);
-    TSQLConnection(Database).AddFieldDefs(Cursor,FieldDefs);
+    SQLConnection.AddFieldDefs(Cursor,FieldDefs);
   finally
   finally
     FLoadingFieldDefs := False;
     FLoadingFieldDefs := False;
     if Assigned(Cursor) then Cursor.FInitFieldDef := false;
     if Assigned(Cursor) then Cursor.FInitFieldDef := false;
@@ -1980,7 +2001,7 @@ begin
     // Cursor has to be assigned, or else the prepare went wrong before PrepareStatment was
     // Cursor 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
     // Don't deallocate cursor; f.e. RowsAffected is requested later
     // Don't deallocate cursor; f.e. RowsAffected is requested later
-    if not Prepared and (assigned(Database)) and (assigned(Cursor)) then TSQLConnection(Database).UnPrepareStatement(Cursor);
+    if not Prepared and (assigned(Database)) and (assigned(Cursor)) then SQLConnection.UnPrepareStatement(Cursor);
   end;
   end;
 end;
 end;
 
 
@@ -2145,7 +2166,7 @@ procedure TCustomSQLQuery.UpdateServerIndexDefs;
 begin
 begin
   FServerIndexDefs.Clear;
   FServerIndexDefs.Clear;
   if assigned(DataBase) and (FTableName<>'') then
   if assigned(DataBase) and (FTableName<>'') then
-    TSQLConnection(DataBase).UpdateIndexDefs(ServerIndexDefs,FTableName);
+    SQLConnection.UpdateIndexDefs(ServerIndexDefs,FTableName);
 end;
 end;
 
 
 procedure TCustomSQLQuery.ApplyRecUpdate(UpdateKind: TUpdateKind);
 procedure TCustomSQLQuery.ApplyRecUpdate(UpdateKind: TUpdateKind);
@@ -2248,7 +2269,7 @@ var qry : TCustomSQLQuery;
     Fld : TField;
     Fld : TField;
 
 
 begin
 begin
-  FieldNamesQuoteChars := TSQLConnection(DataBase).FieldNameQuoteChars;
+  FieldNamesQuoteChars := SQLConnection.FieldNameQuoteChars;
 
 
   case UpdateKind of
   case UpdateKind of
     ukInsert : begin
     ukInsert : begin
@@ -2318,13 +2339,13 @@ begin
   // prepared cursor. In fact this is wrong, but has never led to
   // prepared cursor. In fact this is wrong, but has never led to
   // problems because in SetActive(false) queries are always
   // problems because in SetActive(false) queries are always
   // unprepared. (which is also wrong, but has to be fixed later)
   // unprepared. (which is also wrong, but has to be fixed later)
-  if IsPrepared then with TSQLConnection(DataBase) do
+  if IsPrepared then with SQLConnection do
     UnPrepareStatement(Cursor);
     UnPrepareStatement(Cursor);
 end;
 end;
 
 
 function TCustomSQLQuery.LogEvent(EventType: TDBEventType): Boolean;
 function TCustomSQLQuery.LogEvent(EventType: TDBEventType): Boolean;
 begin
 begin
-  Result:=Assigned(Database) and TSQLConnection(Database).LogEvent(EventType);
+  Result:=Assigned(Database) and SQLConnection.LogEvent(EventType);
 end;
 end;
 
 
 procedure TCustomSQLQuery.Log(EventType: TDBEventType; const Msg: String);
 procedure TCustomSQLQuery.Log(EventType: TDBEventType; const Msg: String);
@@ -2338,7 +2359,7 @@ begin
     M:=Msg;
     M:=Msg;
     If (Name<>'') then
     If (Name<>'') then
       M:=Name+' : '+M;
       M:=Name+' : '+M;
-    TSQLConnection(Database).Log(EventType,M);
+    SQLConnection.Log(EventType,M);
     end;
     end;
 end;
 end;
 
 
@@ -2356,6 +2377,16 @@ begin
   FStatement.ParamCheck:=AValue;
   FStatement.ParamCheck:=AValue;
 end;
 end;
 
 
+procedure TCustomSQLQuery.SetSQLConnection(AValue: TSQLConnection);
+begin
+  Database:=AValue;
+end;
+
+procedure TCustomSQLQuery.SetSQLTransaction(AValue: TSQLTransaction);
+begin
+  Transaction:=AValue;
+end;
+
 procedure TCustomSQLQuery.SetDeleteSQL(const AValue: TStringlist);
 procedure TCustomSQLQuery.SetDeleteSQL(const AValue: TStringlist);
 begin
 begin
   FDeleteSQL.Assign(AValue);
   FDeleteSQL.Assign(AValue);