Skip to content

Commit 0968ac9

Browse files
authored
Implementação parâmetro porta (#9)
1 parent 48eb0be commit 0968ac9

File tree

4 files changed

+67
-0
lines changed

4 files changed

+67
-0
lines changed

MiniREST.SQL.SQLDb.pas

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ interface
2424
procedure SetLogEvent(const ALogEvent: TLogEvent);
2525
function GetServerHostName: string;
2626
procedure SetServerHostName(const AServerHostName: string);
27+
function GetServerPort: Integer;
28+
procedure SetServerPort(const AServerPort: Integer);
2729
end;
2830

2931
TMiniRESTSQLConnectionParamsSQLDb = class(TMiniRESTSQLConnectionFactoryParams, IMiniRESTSQLConnectionFactoryParamsSQLDb)
@@ -35,6 +37,7 @@ TMiniRESTSQLConnectionParamsSQLDb = class(TMiniRESTSQLConnectionFactoryParams,
3537
FDatabaseName: string;
3638
FLogEvent: TLogEvent;
3739
FServerHostName: string;
40+
FPort: Integer;
3841
public
3942
function GetConnectionString: string;
4043
procedure SetConnectionString(const AConnectionString: string);
@@ -50,6 +53,8 @@ TMiniRESTSQLConnectionParamsSQLDb = class(TMiniRESTSQLConnectionFactoryParams,
5053
procedure SetLogEvent(const ALogEvent: TLogEvent);
5154
function GetServerHostName: string;
5255
procedure SetServerHostName(const AServerHostName: string);
56+
function GetServerPort: Integer;
57+
procedure SetServerPort(const AServerPort: Integer);
5358
end;
5459

5560
TMiniRESTSQLConnectionFactorySQLDb = class(TMiniRESTSQLConnectionFactoryBase)
@@ -568,6 +573,12 @@ procedure TMiniRESTSQLConnectionSQLDb.SetConnectionParams;
568573
FSQLConnection.Password := FConnectionParams.GetPassword;
569574
FSQLConnection.DatabaseName := FConnectionParams.GetDatabaseName;
570575
FSQLConnection.HostName := FConnectionParams.GetServerHostName;
576+
if (FConnectionParams.GetServerPort > 0) and (FConnectionParams.GetDatabaseType = dbtFirebird) then
577+
begin
578+
FSQLConnection.HostName := '';
579+
FSQLConnection.DatabaseName := FConnectionParams.GetServerHostName + '/' + IntToStr(FConnectionParams.GetServerPort) + ':' +
580+
FConnectionParams.GetDatabaseName;
581+
end;
571582
LStringList.Text := FConnectionParams.GetConnectionString;
572583
for I := 0 to LStringList.Count - 1 do
573584
begin
@@ -589,4 +600,14 @@ procedure TMiniRESTSQLConnectionParamsSQLDb.SetServerHostName(const AServerHostN
589600
FServerHostName := AServerHostName;
590601
end;
591602

603+
function TMiniRESTSQLConnectionParamsSQLDb.GetServerPort: Integer;
604+
begin
605+
Result := FPort;
606+
end;
607+
608+
procedure TMiniRESTSQLConnectionParamsSQLDb.SetServerPort(const AServerPort: Integer);
609+
begin
610+
FPort := AServerPort;
611+
end;
612+
592613
end.

unittest/SQL/Test.SQL.Default.pas

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ interface
1010
TMiniRESTSQLTest = class({$IFNDEF FPC}TObject{$ELSE}TTestCase{$IFEND})
1111
private
1212
FServerHostName: string;
13+
FServerPort: Integer;
1314
procedure MethodThatRaiseException;
1415
protected
1516
FConnectionCount: Integer;
@@ -22,6 +23,7 @@ TMiniRESTSQLTest = class({$IFNDEF FPC}TObject{$ELSE}TTestCase{$IFEND})
2223
function GetSequenceValue(const ASequenceName: string): Integer;
2324
procedure LogConnectionPoolEvent(const AMessage: string);
2425
function GetServerHostName: string;
26+
function GetServerPort: Integer;
2527
public
2628
{$IFNDEF FPC}
2729
[SetupFixture]
@@ -116,6 +118,14 @@ TMiniRESTSQLTest = class({$IFNDEF FPC}TObject{$ELSE}TTestCase{$IFEND})
116118
[Test]
117119
{$IFEND}
118120
procedure TestFailWithInvalidServerHostName;
121+
{$IFNDEF FPC}
122+
[Test]
123+
{$IFEND}
124+
procedure TestFailWithInvalidServerPort;
125+
{$IFNDEF FPC}
126+
[Test]
127+
{$IFEND}
128+
procedure TestSuccessWithDefaultServerPort;
119129
(* {$IFNDEF FPC}
120130
[Test]
121131
{$IFEND}
@@ -210,6 +220,8 @@ procedure TMiniRESTSQLTest.Setup;
210220
var
211221
LConnection: IMiniRESTSQLConnection;
212222
begin
223+
FServerHostName := 'LOCALHOST';
224+
FServerPort := 3050;
213225
{$IFDEF FPC}
214226
FConnectionFactory := GetConnectionFactory;
215227
{$IFEND}
@@ -785,4 +797,37 @@ procedure TMiniRESTSQLTest.MethodThatRaiseException;
785797
LQry.Open;
786798
end;
787799

800+
procedure TMiniRESTSQLTest.TestFailWithInvalidServerPort;
801+
begin
802+
FServerPort := 3099;
803+
FConnectionFactory := GetConnectionFactory(GetConnectionFactoryParams);
804+
CheckException(@MethodThatRaiseException, Exception);
805+
end;
806+
807+
function TMiniRESTSQLTest.GetServerPort: Integer;
808+
begin
809+
Result := FServerPort;
810+
end;
811+
812+
procedure TMiniRESTSQLTest.TestSuccessWithDefaultServerPort;
813+
var
814+
LConn1: IMiniRESTSQLConnection;
815+
LQry: IMiniRESTSQLQuery;
816+
LTotal: Integer;
817+
begin
818+
FServerPort := 0;
819+
FConnectionFactory := GetConnectionFactory(GetConnectionFactoryParams);
820+
LConn1 := FConnectionFactory.GetConnection;
821+
LQry := LConn1.GetQuery;
822+
LQry.SQL := 'SELECT * FROM CUSTOMER WHERE NAME = :NAME';
823+
LQry.ParamByName('NAME').AsString := 'HUE';
824+
LQry.Open;
825+
826+
{$IFNDEF FPC}
827+
Assert.AreTrue(LQry.DataSet.Active);
828+
{$ELSE}
829+
CheckTrue(LQry.DataSet.Active);
830+
{$IFEND}
831+
end;
832+
788833
end.

unittest/SQL/test.sql.sqldb.pas

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ function TMiniRESTSQLTestSQLDbFPC.InternalGetConnectionFactoryParams: IMiniRESTS
113113
Result.SetPassword('masterkey');
114114
Result.SetLogEvent(@LogEvent);
115115
Result.SetServerHostName(GetServerHostName);
116+
Result.SetServerPort(GetServerPort);
116117
finally
117118
LConnectionInfo.Free;
118119
end;

unittest/TEST.FDB

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)