Componentes.Terceros.RemObj.../internal/5.0.24.615/1/Data Abstract for Delphi/Source/Drivers/uDAPostgresDACDriver.pas

344 lines
9.0 KiB
ObjectPascal

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;
procedure FixSQLNames(items: TStrings);
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;
procedure DoGetTableNames(out List: IROStrings); override;
procedure DoGetStoredProcedureNames(out List: IROStrings); override;
property Connection: TPSQLDatabase read GetConnection;
public
end;
{ TDAEPostgresDACQuery }
TDAEPostgresDACQuery = class(TDAEDataset)
private
protected
function CreateDataset(aConnection: TDAEConnection): TDataset; override;
function DoExecute: integer; override;
function DoGetSQL: string; override;
procedure DoSetSQL(const Value: string); override;
procedure DoPrepare(Value: boolean); override;
public
end;
{ TDAEPostgresDACStoredProcedure }
TDAEPostgresDACStoredProcedure = class(TDAEStoredProcedure)
protected
function CreateDataset(aConnection: TDAEConnection): TDataset; override;
function GetStoredProcedureName: string; override;
procedure SetStoredProcedureName(const Name: string); override;
function Execute: integer; override;
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.DoGetStoredProcedureNames(out List: IROStrings);
begin
List := TROStrings.Create;
Connection.GetStoredProcNames('', List.Strings);
end;
procedure TDAEPostgresDacConnection.DoGetTableNames(out List: IROStrings);
begin
List := TROStrings.Create;
Connection.GetTableNames('', false, List.Strings);
FixSQLNames(List.Strings);
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;
procedure TDAEPostgresDacConnection.FixSQLNames(items: TStrings);
var
i: Integer;
s: string;
begin
Items.Text := StringReplace(Items.Text, '"', '', [rfReplaceAll]);
for i := Items.Count -1 downto 0 do
begin
s := Items[i];
if lowercase(copy(s,1,7)) = 'public.' then
begin
Delete(s,1,7);
Items[i] := s;
end;
end;
// the driver adds quotes around it, while it shouldn't do that, we do that ourselves.
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 }
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
inherited DoExecute;
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;
{ TDAEADOStoredProcedure }
function TDAEPostgresDacStoredProcedure.CreateDataset(
aConnection: TDAEConnection): TDataset;
begin
result := TPSQLStoredProc.Create(nil);
TPSQLStoredProc(result).Database := TDAEPostgresDacConnection(aConnection).Connection;
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[i].Value;
ExecProc;
result := -1;
for i := 0 to (ParamCount-1) do
if (Params[i].ParamType in [ptOutput, ptInputOutput, ptResult])
then _params[i].Value := Params[i].Value;
end;
end;
function TDAEPostgresDacStoredProcedure.GetStoredProcedureName: string;
begin
Result := TPSQLStoredProc(Dataset).StoredProcName;
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.