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

338 lines
8.6 KiB
ObjectPascal

unit uDAMySQLDACDriver;
{----------------------------------------------------------------------------}
{ 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_MySQLDACDriver_Glyphs.res}
interface
uses DB, Classes, uDAEngine, uDAInterfaces, uDAADOInterfaces, uROClasses,
uROBinaryHelpers, uDAUtils, mySQLAccess, mySQLDbTables, mySQLTypes, uDAMySQLInterfaces;
type { TDAMySQLDACDriver }
TDAMySQLDacDriver = class(TDADriverReference)
end;
{ TDAEMySQLDacDriver }
TDAEMySQLDacDriver = class(TDAMySQLDriver)
private
protected
function GetConnectionClass: TDAEConnectionClass; override;
// IDADriver
function GetDriverID: string; override; safecall;
function GetDescription: string; override; safecall;
end;
{ TDAEMyConnection }
TDAEMySQLDacConnection = class(TDAMySQLConnection, IDACanQueryDatabaseNames,IDAMySQLConnection)
private
function GetConnection: TmySQLDatabase;
protected
function GetTableSchema: string; override;
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: TmySQLDatabase read GetConnection;
public
end;
{ TDAEMySQLDacQuery }
TDAEMySQLDacQuery = 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;
{ TDAEMySQLDacStoredProcedure }
TDAEMySQLDacStoredProcedure = class(TDAEStoredProcedure)
protected
function CreateDataset(aConnection: TDAEConnection): TDataset; override;
procedure RefreshParams; override; safecall;
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, uDAMacroProcessors;
var
_driver: TDAEDriver = nil;
procedure Register;
begin
RegisterComponents(DAPalettePageName, [TDAMySQLDACDriver]);
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 := TDAEMySQLDacDriver.Create(nil);
result := _driver;
end;
{$IFDEF LATEST_MyDAC}
{$I uDACRLabsUtils.inc}
{$ENDIF LATEST_MyDAC}
{ TDAEMySQLDacConnection }
function TDAEMySQLDacConnection.DoBeginTransaction: integer;
begin
Connection.StartTransaction;
result := 0;
end;
procedure TDAEMySQLDacConnection.DoCommitTransaction;
begin
Connection.Commit;
end;
function TDAEMySQLDacConnection.GetConnection: TmySQLDatabase;
begin
result := TmySQLDatabase(inherited ConnectionObject);
end;
function TDAEMySQLDacConnection.CreateCustomConnection: TCustomConnection;
begin
result := TmySQLDatabase.Create(nil);
TmySQLDatabase(result).LoginPrompt := false;
end;
function TDAEMySQLDacConnection.GetDatasetClass: TDAEDatasetClass;
begin
result := TDAEMySQLDacQuery;
end;
function TDAEMySQLDacConnection.GetStoredProcedureClass: TDAEStoredProcedureClass;
begin
result := TDAEMySQLDacStoredProcedure;
end;
procedure TDAEMySQLDacConnection.DoRollbackTransaction;
begin
Connection.Rollback;
end;
function TDAEMySQLDacConnection.DoGetInTransaction: boolean;
begin
result := Connection.InTransaction
end;
procedure TDAEMySQLDacConnection.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;
if AuxParams['Port'] <> '' then Connection.Port := StrToIntDef(AuxParams['Port'],3306);
for i := 0 to AuxParamsCount -1 do
begin
if SameText(AuxParamNames[i], 'Port') then continue;
if AuxParams[AuxParamNames[i]] <> '' then
Connection.Params.Add(AuxParamNames[i]+'='+AuxParams[AuxParamNames[i]]);
end;
end;
end;
function TDAEMySQLDacConnection.GetTableSchema: string;
begin
Result:=Connection.DatabaseName;
end;
{ TDAEMySQLDacDriver }
function TDAEMySQLDacDriver.GetConnectionClass: TDAEConnectionClass;
begin
result := TDAEMySQLDacConnection;
end;
function TDAEMySQLDacDriver.GetDescription: string;
begin
result := 'MicroOlap DAC for MySQL Driver'{$IFDEF DataAbstract_SchemaModelerOnly} + SchemaModelerOnly{$ENDIF};
end;
function TDAEMySQLDacDriver.GetDriverID: string;
begin
result := 'MySQLDAC';
end;
{ TDAEMySQLDacQuery }
function TDAEMySQLDacQuery.CreateDataset(aConnection: TDAEConnection): TDataset;
begin
result := TmySQLQuery.Create(nil);
TmySQLQuery(result).RequestLive := false;
TmySQLQuery(result).UniDirectional:=True;
TMySqlQuery(result).Database := TDAEMySQLDacConnection(aConnection).Connection;
end;
function TDAEMySQLDacQuery.DoExecute: integer;
begin
inherited DoExecute;
result := TmySQLQuery(Dataset).RowsAffected;
end;
function TDAEMySQLDacQuery.DoGetSQL: string;
begin
result := TmySQLQuery(Dataset).SQL.Text;
end;
procedure TDAEMySQLDacQuery.DoPrepare(Value: boolean);
begin
TmySQLQuery(Dataset).Prepared := Value;
end;
procedure TDAEMySQLDacQuery.DoSetSQL(const Value: string);
begin
TmySQLQuery(Dataset).SQL.Text := Value;
end;
{ TDAEADOStoredProcedure }
function TDAEMySQLDacStoredProcedure.CreateDataset(
aConnection: TDAEConnection): TDataset;
begin
result := TmySQLStoredProc.Create(nil);
TmySQLStoredProc(result).Database := TDAEMySQLDacConnection(aConnection).Connection;
end;
function TDAEMySQLDacStoredProcedure.Execute: integer;
var
i: integer;
_params: TDAParamCollection;
lParam: TmySQLSPParam;
begin
_params := GetParams;
with TmySQLStoredProc(Dataset) do begin
for i := 0 to (Params.Count-1) do begin
lParam:=Params[i];
if (lParam.ParamType in [ptInput, ptInputOutput]) then
lParam.Value := _params.ParamByName(lParam.Name).Value;
end;
ExecProc;
result := -1;
for i := 0 to (Params.Count-1) do begin
lParam:=Params[i];
if (lParam.ParamType in [ptOutput, ptInputOutput, ptResult]) then
_params.ParamByName(lParam.Name).Value := lParam.Value;
end;
end;
end;
function TDAEMySQLDacStoredProcedure.GetStoredProcedureName: string;
begin
Result := TmySQLStoredProc(Dataset).ProcedureName;
end;
procedure TDAEMySQLDacStoredProcedure.SetStoredProcedureName(
const Name: string);
begin
TmySQLStoredProc(Dataset).ProcedureName := Name;
end;
procedure TDAEMySQLDacStoredProcedure.RefreshParams;
var
lParams: TParams;
i: integer;
par: TDAParam;
begin
lParams := TmySQLStoredProc(Dataset).Params;
getParams.Clear;
for i := 0 to (lParams.Count - 1) do begin
par := getParams.Add;
par.Name := lParams[i].Name;
par.DataType := intVCLTypeToDAType(lParams[i].DataType);
par.ParamType := TDAParamType(lParams[i].ParamType);
par.Size := lParams[i].Size;
end;
end;
exports
GetDriverObject name func_GetDriverObject;
initialization
_driver := nil;
RegisterDriverProc(GetDriverObject);
finalization
UnregisterDriverProc(GetDriverObject);
FreeAndNIL(_driver);
end.