unit uDAPostgresDACDriver; {----------------------------------------------------------------------------} { Data Abstract Library - Driver Library { { compiler: Delphi 6 and up, Kylix 3 and up { platform: Win32, Linux { { (c)opyright RemObjects Software. all rights reserved. { { Using this code requires a valid license of the Data Abstract { which can be obtained at http://www.remobjects.com. {----------------------------------------------------------------------------} {$I ..\DataAbstract.inc} {$R DataAbstract_PostgresDACDriver_Glyphs.res} interface uses DB, Classes, uDAEngine, uDAInterfaces, uDAADOInterfaces, uROClasses, uROBinaryHelpers, uDAUtils, PSQLDbTables, uDAPostgresInterfaces; type { TDAPostgresDACDriver } TDAPostgresDACDriver = class(TDADriverReference) end; { TDAEPostgresDACDriver } TDAEPostgresDACDriver = class(TDAPostgresDriver) private protected function GetConnectionClass: TDAEConnectionClass; override; // IDADriver function GetDriverID: string; override; safecall; function GetDescription: string; override; safecall; function GetAvailableDriverOptions: TDAAvailableDriverOptions; override; safecall; end; { TDAEMyConnection } TDAEPostgresDACConnection = class(TDAEPostgresConnection) private function GetConnection: TPSQLDatabase; protected function CreateCustomConnection: TCustomConnection; override; function GetDatasetClass: TDAEDatasetClass; override; function GetStoredProcedureClass: TDAEStoredProcedureClass; override; procedure DoApplyConnectionString(aConnStrParser: TDAConnectionStringParser; aConnectionObject: TCustomConnection); override; function DoBeginTransaction: integer; override; procedure DoCommitTransaction; override; procedure DoRollbackTransaction; override; function DoGetInTransaction: boolean; override; property Connection: TPSQLDatabase read GetConnection; public end; { TDAEPostgresDACQuery } TDAEPostgresDACQuery = class(TDAEDataset,IDAMustSetParams) private protected function CreateDataset(aConnection: TDAEConnection): TDataset; override; procedure ClearParams; override; function DoExecute: integer; override;safecall; function DoGetSQL: string; override;safecall; procedure DoSetSQL(const Value: string); override;safecall; procedure DoPrepare(Value: boolean); override;safecall; procedure SetParamValues(AParams: TDAParamCollection); override;{$IFNDEF FPC_SAFECALL_BUG}safecall;{$ENDIF} procedure GetParamValues(AParams: TDAParamCollection); override;{$IFNDEF FPC_SAFECALL_BUG}safecall;{$ENDIF} public end; { TDAEPostgresDACStoredProcedure } TDAEPostgresDACStoredProcedure = class(TDAEStoredProcedure,IDAMustSetParams) private fConnection:TDAEConnection; protected function CreateDataset(aConnection: TDAEConnection): TDataset; override; function GetStoredProcedureName: string; override; safecall; procedure SetStoredProcedureName(const Name: string); override; safecall; function DoExecute: integer; override;safecall; function Execute: integer; override;safecall; procedure SetParamValues(AParams: TDAParamCollection); override;{$IFNDEF FPC_SAFECALL_BUG}safecall;{$ENDIF} procedure GetParamValues(AParams: TDAParamCollection); override;{$IFNDEF FPC_SAFECALL_BUG}safecall;{$ENDIF} procedure RefreshParams; override; {$IFNDEF FPC_SAFECALL_BUG}safecall;{$ENDIF} end; procedure Register; function GetDriverObject: IDADriver; stdcall; implementation uses SysUtils, uDADriverManager, uDARes; var _driver: TDAEDriver = nil; procedure Register; begin RegisterComponents(DAPalettePageName, [TDAPostgresDACDriver]); end; {$IFDEF DataAbstract_SchemaModelerOnly} {$INCLUDE ..\DataAbstract_SchemaModelerOnly.inc} {$ENDIF DataAbstract_SchemaModelerOnly} function GetDriverObject: IDADriver; begin {$IFDEF DataAbstract_SchemaModelerOnly} if not RunningInSchemaModeler then begin result := nil; exit; end; {$ENDIF} if (_driver = nil) then _driver := TDAEPostgresDacDriver.Create(nil); result := _driver; end; {$IFDEF LATEST_MyDAC} {$I uDACRLabsUtils.inc} {$ENDIF LATEST_MyDAC} { TDAEPostgresDacConnection } function TDAEPostgresDacConnection.DoBeginTransaction: integer; begin Connection.StartTransaction; result := 0; end; procedure TDAEPostgresDacConnection.DoCommitTransaction; begin Connection.Commit; end; function TDAEPostgresDacConnection.GetConnection: TPsqlDatabase; begin result := TPsqlDatabase(inherited ConnectionObject); end; function TDAEPostgresDacConnection.CreateCustomConnection: TCustomConnection; begin result := TPsqlDatabase.Create(nil); TPsqlDatabase(result).LoginPrompt := false; end; function TDAEPostgresDacConnection.GetDatasetClass: TDAEDatasetClass; begin result := TDAEPostgresDacQuery; end; function TDAEPostgresDacConnection.GetStoredProcedureClass: TDAEStoredProcedureClass; begin result := TDAEPostgresDacStoredProcedure; end; procedure TDAEPostgresDacConnection.DoRollbackTransaction; begin Connection.Rollback; end; function TDAEPostgresDacConnection.DoGetInTransaction: boolean; begin result := Connection.InTransaction end; procedure TDAEPostgresDacConnection.DoApplyConnectionString( aConnStrParser: TDAConnectionStringParser; aConnectionObject: TCustomConnection); var i: Integer; begin inherited; with aConnStrParser do begin Connection.DatabaseName := Database; Connection.Host := Server; if (Self.UserID <> '') then Connection.Username := Self.UserID else Connection.Username := UserID; if (Self.Password <> '') then Connection.UserPassword := Self.Password else Connection.UserPassword := Password; for i := 0 to AuxParamsCount -1 do begin if AuxParams[AuxParamNames[i]] <> '' then Connection.Params.Add(AuxParamNames[i]+'='+AuxParams[AuxParamNames[i]]); end; end; end; { TDAEPostgresDacDriver } function TDAEPostgresDacDriver.GetAvailableDriverOptions: TDAAvailableDriverOptions; begin Result := [doServerName, doDatabaseName, doLogin, doCustom]; end; function TDAEPostgresDacDriver.GetConnectionClass: TDAEConnectionClass; begin result := TDAEPostgresDacConnection; end; function TDAEPostgresDacDriver.GetDescription: string; begin result := 'MicroOlap DAC for Postgres Driver'{$IFDEF DataAbstract_SchemaModelerOnly} + SchemaModelerOnly{$ENDIF}; end; function TDAEPostgresDacDriver.GetDriverID: string; begin result := 'PostgresDAC'; end; { TDAEPostgresDacQuery } procedure TDAEPostgresDACQuery.ClearParams; begin inherited; TPSQLQuery(Dataset).Params.Clear; end; function TDAEPostgresDacQuery.CreateDataset(aConnection: TDAEConnection): TDataset; begin result := TPSQLQuery.Create(nil); TPSQLQuery(result).UniDirectional := True; TPSQLQuery(result).RequestLive := false; TPSQLQuery(result).Database := TDAEPostgresDacConnection(aConnection).Connection; end; function TDAEPostgresDacQuery.DoExecute: integer; begin TPSQLQuery(Dataset).ExecSQL; result := TPSQLQuery(Dataset).RowsAffected; end; function TDAEPostgresDacQuery.DoGetSQL: string; begin result := TPSQLQuery(Dataset).SQL.Text; end; procedure TDAEPostgresDacQuery.DoPrepare(Value: boolean); begin TPSQLQuery(Dataset).Prepared := Value; end; procedure TDAEPostgresDacQuery.DoSetSQL(const Value: string); begin TPSQLQuery(Dataset).SQL.Text := Value; end; procedure TDAEPostgresDACQuery.GetParamValues(AParams: TDAParamCollection); begin GetParamValuesStd(AParams, TPSQLQuery(Dataset).Params); end; procedure TDAEPostgresDACQuery.SetParamValues(AParams: TDAParamCollection); begin SetParamValuesStd(AParams, TPSQLQuery(Dataset).Params); end; { TDAEADOStoredProcedure } function TDAEPostgresDacStoredProcedure.CreateDataset( aConnection: TDAEConnection): TDataset; begin fConnection := aConnection; result := TPSQLStoredProc.Create(nil); TPSQLStoredProc(result).Database := TDAEPostgresDacConnection(aConnection).Connection; end; function TDAEPostgresDACStoredProcedure.DoExecute: integer; begin TPSQLStoredProc(Dataset).ExecProc; result := -1; end; function TDAEPostgresDacStoredProcedure.Execute: integer; var i: integer; _params: TDAParamCollection; begin _params := GetParams; with TPSQLStoredProc(Dataset) do begin for i := 0 to (ParamCount-1) do if (Params[i].ParamType in [ptInput, ptInputOutput]) then Params[i].Value := _params.ParamByName(Params[i].Name).Value; result := DoExecute; for i := 0 to (ParamCount-1) do if (Params[i].ParamType in [ptOutput, ptInputOutput, ptResult]) then _params.ParamByName(Params[i].Name).Value := Params[i].Value; end; end; procedure TDAEPostgresDACStoredProcedure.GetParamValues( AParams: TDAParamCollection); begin GetParamValuesStd(AParams,TPSQLStoredProc(Dataset).Params); end; function TDAEPostgresDacStoredProcedure.GetStoredProcedureName: string; begin Result := TPSQLStoredProc(Dataset).StoredProcName; end; procedure TDAEPostgresDACStoredProcedure.RefreshParams; begin RefreshParamsStd(TPSQLStoredProc(Dataset).Params); end; procedure TDAEPostgresDACStoredProcedure.SetParamValues( AParams: TDAParamCollection); begin SetParamValuesStd(AParams,TPSQLStoredProc(Dataset).Params); end; procedure TDAEPostgresDacStoredProcedure.SetStoredProcedureName( const Name: string); begin TPSQLStoredProc(Dataset).StoredProcName := Name; end; exports GetDriverObject name func_GetDriverObject; initialization _driver := nil; RegisterDriverProc(GetDriverObject); finalization UnregisterDriverProc(GetDriverObject); FreeAndNIL(_driver); end.