Skip to content

Commit

Permalink
Implementação parâmetro porta (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
glprog authored Mar 21, 2020
1 parent 48eb0be commit 0968ac9
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
21 changes: 21 additions & 0 deletions MiniREST.SQL.SQLDb.pas
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ interface
procedure SetLogEvent(const ALogEvent: TLogEvent);
function GetServerHostName: string;
procedure SetServerHostName(const AServerHostName: string);
function GetServerPort: Integer;
procedure SetServerPort(const AServerPort: Integer);
end;

TMiniRESTSQLConnectionParamsSQLDb = class(TMiniRESTSQLConnectionFactoryParams, IMiniRESTSQLConnectionFactoryParamsSQLDb)
Expand All @@ -35,6 +37,7 @@ TMiniRESTSQLConnectionParamsSQLDb = class(TMiniRESTSQLConnectionFactoryParams,
FDatabaseName: string;
FLogEvent: TLogEvent;
FServerHostName: string;
FPort: Integer;
public
function GetConnectionString: string;
procedure SetConnectionString(const AConnectionString: string);
Expand All @@ -50,6 +53,8 @@ TMiniRESTSQLConnectionParamsSQLDb = class(TMiniRESTSQLConnectionFactoryParams,
procedure SetLogEvent(const ALogEvent: TLogEvent);
function GetServerHostName: string;
procedure SetServerHostName(const AServerHostName: string);
function GetServerPort: Integer;
procedure SetServerPort(const AServerPort: Integer);
end;

TMiniRESTSQLConnectionFactorySQLDb = class(TMiniRESTSQLConnectionFactoryBase)
Expand Down Expand Up @@ -568,6 +573,12 @@ procedure TMiniRESTSQLConnectionSQLDb.SetConnectionParams;
FSQLConnection.Password := FConnectionParams.GetPassword;
FSQLConnection.DatabaseName := FConnectionParams.GetDatabaseName;
FSQLConnection.HostName := FConnectionParams.GetServerHostName;
if (FConnectionParams.GetServerPort > 0) and (FConnectionParams.GetDatabaseType = dbtFirebird) then
begin
FSQLConnection.HostName := '';
FSQLConnection.DatabaseName := FConnectionParams.GetServerHostName + '/' + IntToStr(FConnectionParams.GetServerPort) + ':' +
FConnectionParams.GetDatabaseName;
end;
LStringList.Text := FConnectionParams.GetConnectionString;
for I := 0 to LStringList.Count - 1 do
begin
Expand All @@ -589,4 +600,14 @@ procedure TMiniRESTSQLConnectionParamsSQLDb.SetServerHostName(const AServerHostN
FServerHostName := AServerHostName;
end;

function TMiniRESTSQLConnectionParamsSQLDb.GetServerPort: Integer;
begin
Result := FPort;
end;

procedure TMiniRESTSQLConnectionParamsSQLDb.SetServerPort(const AServerPort: Integer);
begin
FPort := AServerPort;
end;

end.
45 changes: 45 additions & 0 deletions unittest/SQL/Test.SQL.Default.pas
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ interface
TMiniRESTSQLTest = class({$IFNDEF FPC}TObject{$ELSE}TTestCase{$IFEND})
private
FServerHostName: string;
FServerPort: Integer;
procedure MethodThatRaiseException;
protected
FConnectionCount: Integer;
Expand All @@ -22,6 +23,7 @@ TMiniRESTSQLTest = class({$IFNDEF FPC}TObject{$ELSE}TTestCase{$IFEND})
function GetSequenceValue(const ASequenceName: string): Integer;
procedure LogConnectionPoolEvent(const AMessage: string);
function GetServerHostName: string;
function GetServerPort: Integer;
public
{$IFNDEF FPC}
[SetupFixture]
Expand Down Expand Up @@ -116,6 +118,14 @@ TMiniRESTSQLTest = class({$IFNDEF FPC}TObject{$ELSE}TTestCase{$IFEND})
[Test]
{$IFEND}
procedure TestFailWithInvalidServerHostName;
{$IFNDEF FPC}
[Test]
{$IFEND}
procedure TestFailWithInvalidServerPort;
{$IFNDEF FPC}
[Test]
{$IFEND}
procedure TestSuccessWithDefaultServerPort;
(* {$IFNDEF FPC}
[Test]
{$IFEND}
Expand Down Expand Up @@ -210,6 +220,8 @@ procedure TMiniRESTSQLTest.Setup;
var
LConnection: IMiniRESTSQLConnection;
begin
FServerHostName := 'LOCALHOST';
FServerPort := 3050;
{$IFDEF FPC}
FConnectionFactory := GetConnectionFactory;
{$IFEND}
Expand Down Expand Up @@ -785,4 +797,37 @@ procedure TMiniRESTSQLTest.MethodThatRaiseException;
LQry.Open;
end;

procedure TMiniRESTSQLTest.TestFailWithInvalidServerPort;
begin
FServerPort := 3099;
FConnectionFactory := GetConnectionFactory(GetConnectionFactoryParams);
CheckException(@MethodThatRaiseException, Exception);
end;

function TMiniRESTSQLTest.GetServerPort: Integer;
begin
Result := FServerPort;
end;

procedure TMiniRESTSQLTest.TestSuccessWithDefaultServerPort;
var
LConn1: IMiniRESTSQLConnection;
LQry: IMiniRESTSQLQuery;
LTotal: Integer;
begin
FServerPort := 0;
FConnectionFactory := GetConnectionFactory(GetConnectionFactoryParams);
LConn1 := FConnectionFactory.GetConnection;
LQry := LConn1.GetQuery;
LQry.SQL := 'SELECT * FROM CUSTOMER WHERE NAME = :NAME';
LQry.ParamByName('NAME').AsString := 'HUE';
LQry.Open;

{$IFNDEF FPC}
Assert.AreTrue(LQry.DataSet.Active);
{$ELSE}
CheckTrue(LQry.DataSet.Active);
{$IFEND}
end;

end.
1 change: 1 addition & 0 deletions unittest/SQL/test.sql.sqldb.pas
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ function TMiniRESTSQLTestSQLDbFPC.InternalGetConnectionFactoryParams: IMiniRESTS
Result.SetPassword('masterkey');
Result.SetLogEvent(@LogEvent);
Result.SetServerHostName(GetServerHostName);
Result.SetServerPort(GetServerPort);
finally
LConnectionInfo.Free;
end;
Expand Down
Binary file modified unittest/TEST.FDB
Binary file not shown.

0 comments on commit 0968ac9

Please sign in to comment.