Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 53 additions & 25 deletions MiniREST.SQL.SQLDb.pas
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ interface
procedure SetDatabaseName(const ADatabaseName: string);
function GetLogEvent: TLogEvent;
procedure SetLogEvent(const ALogEvent: TLogEvent);
function GetServerHostName: string;
procedure SetServerHostName(const AServerHostName: string);
end;

TMiniRESTSQLConnectionParamsSQLDb = class(TMiniRESTSQLConnectionFactoryParams, IMiniRESTSQLConnectionFactoryParamsSQLDb)
Expand All @@ -32,6 +34,7 @@ TMiniRESTSQLConnectionParamsSQLDb = class(TMiniRESTSQLConnectionFactoryParams,
FDatabaseType: TMiniRESTSQLDatabaseType;
FDatabaseName: string;
FLogEvent: TLogEvent;
FServerHostName: string;
public
function GetConnectionString: string;
procedure SetConnectionString(const AConnectionString: string);
Expand All @@ -45,6 +48,8 @@ TMiniRESTSQLConnectionParamsSQLDb = class(TMiniRESTSQLConnectionFactoryParams,
procedure SetDatabaseName(const ADatabaseName: string);
function GetLogEvent: TLogEvent;
procedure SetLogEvent(const ALogEvent: TLogEvent);
function GetServerHostName: string;
procedure SetServerHostName(const AServerHostName: string);
end;

TMiniRESTSQLConnectionFactorySQLDb = class(TMiniRESTSQLConnectionFactoryBase)
Expand All @@ -64,6 +69,7 @@ TMiniRESTSQLConnectionFactorySQLDb = class(TMiniRESTSQLConnectionFactoryBase)
TMiniRESTSQLConnectionSQLDb = class(TMiniRESTSQLConnectionBase)
private
function GetConnectorType(const ADatabaseType: TMiniRESTSQLDatabaseType): String;
procedure OnBeforeConnect(Sender: TObject);
protected
FSQLConnection: TSQLConnector;
//FTransaction: TDBXTransaction;
Expand All @@ -76,6 +82,7 @@ TMiniRESTSQLConnectionSQLDb = class(TMiniRESTSQLConnectionBase)
procedure Log(Sender : TSQLConnection; EventType : TDBEventType; Const Msg : String);
procedure SetMiniRESTSQLParamToSQLParam(AMiniRESTSQLParam: IMiniRESTSQLParam; ASQLParam: TParam);
procedure CheckConnectionIsValid;
procedure SetConnectionParams;
public
constructor Create(AOwner: IMiniRESTSQLConnectionFactory; AParams: IMiniRESTSQLConnectionFactoryParamsSQLDb);
destructor Destroy; override;
Expand Down Expand Up @@ -209,7 +216,8 @@ constructor TMiniRESTSQLConnectionSQLDb.Create(AOwner: IMiniRESTSQLConnectionFac
FTransaction.Action := caCommit;
FConnectionParams := AParams;
FInExplicitTransaction := False;
FLogEvent := AParams.GetLogEvent;
FLogEvent := AParams.GetLogEvent;
FSQLConnection.BeforeConnect := OnBeforeConnect;
inherited Create(AOwner);
end;

Expand All @@ -221,31 +229,11 @@ destructor TMiniRESTSQLConnectionSQLDb.Destroy;
end;

procedure TMiniRESTSQLConnectionSQLDb.Connect;
var
LStringList: TStringList;
LName: string;
I: Integer;
begin
CheckConnectionIsValid;
LStringList := TStringList.Create;
try
if FSQLConnection.Connected then
Exit;
FSQLConnection.ConnectorType := GetConnectorType(FConnectionParams.GetDatabaseType);
FSQLConnection.LoginPrompt := False;
FSQLConnection.UserName := FConnectionParams.GetUserName;
FSQLConnection.Password := FConnectionParams.GetPassword;
FSQLConnection.DatabaseName := FConnectionParams.GetDatabaseName;
LStringList.Text := FConnectionParams.GetConnectionString;
for I := 0 to LStringList.Count - 1 do
begin
LName := LStringList.Names[I];
FSQLConnection.Params.Values[LName] := LStringList.Values[LName];
end;
FSQLConnection.Connected := True;
finally
LStringList.Free;
end;
if FSQLConnection.Connected then
Exit;
FSQLConnection.Connected := True;
end;

procedure TMiniRESTSQLConnectionSQLDb.StartTransaction;
Expand Down Expand Up @@ -482,6 +470,11 @@ function TMiniRESTSQLConnectionSQLDb.GetConnectorType(const ADatabaseType: TMini
end;
end;

procedure TMiniRESTSQLConnectionSQLDb.OnBeforeConnect(Sender: TObject);
begin
SetConnectionParams;
end;

function TMiniRESTSQLConnectionParamsSQLDb.GetDatabaseName: string;
begin
Result := FDatabaseName;
Expand Down Expand Up @@ -558,7 +551,42 @@ procedure TMiniRESTSQLConnectionSQLDb.Invalidate;
procedure TMiniRESTSQLConnectionSQLDb.CheckConnectionIsValid;
begin
if not IsValid then
raise Exception.Create('A conex�o foi invalidada.');
raise Exception.Create('A conexão foi invalidada.');
end;

procedure TMiniRESTSQLConnectionSQLDb.SetConnectionParams;
var
LStringList: TStringList;
LName: string;
I: Integer;
begin
LStringList := TStringList.Create;
try
FSQLConnection.ConnectorType := GetConnectorType(FConnectionParams.GetDatabaseType);
FSQLConnection.LoginPrompt := False;
FSQLConnection.UserName := FConnectionParams.GetUserName;
FSQLConnection.Password := FConnectionParams.GetPassword;
FSQLConnection.DatabaseName := FConnectionParams.GetDatabaseName;
FSQLConnection.HostName := FConnectionParams.GetServerHostName;
LStringList.Text := FConnectionParams.GetConnectionString;
for I := 0 to LStringList.Count - 1 do
begin
LName := LStringList.Names[I];
FSQLConnection.Params.Values[LName] := LStringList.Values[LName];
end;
finally
LStringList.Free;
end;
end;

function TMiniRESTSQLConnectionParamsSQLDb.GetServerHostName: string;
begin
Result := FServerHostName;
end;

procedure TMiniRESTSQLConnectionParamsSQLDb.SetServerHostName(const AServerHostName: string);
begin
FServerHostName := AServerHostName;
end;

end.
Loading