Skip to content

Commit 78864a9

Browse files
authored
Fix connection charset and params (#10)
1 parent 0267c67 commit 78864a9

File tree

6 files changed

+140
-1
lines changed

6 files changed

+140
-1
lines changed

MiniREST.SQL.Base.pas

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,15 @@ TMiniRESTSQLConnectionFactoryParams = class(TInterfacedObject, IMiniRESTSQLCon
137137
private
138138
FConnectionCount: Integer;
139139
FConnectionFactoryEventLogger: IMiniRESTSQLConnectionFactoryEventLogger;
140+
FCharSet: string;
140141
public
141142
function GetConnectionsCount: Integer;
142143
procedure SetConnectionsCount(const ACount: Integer);
143144
function GetObject: TObject;
144145
function GetConnectionFactoryEventLogger: IMiniRESTSQLConnectionFactoryEventLogger;
145146
procedure SetConnectionFactoryEventLogger(ALogger: IMiniRESTSQLConnectionFactoryEventLogger);
147+
function GetCharSet: string;
148+
procedure SetCharSet(const ACharSet: string);
146149
end;
147150

148151
implementation
@@ -515,6 +518,16 @@ procedure TMiniRESTSQLConnectionBase.SetValid(const AValid: Boolean);
515518
FValid := AValid;
516519
end;
517520

521+
function TMiniRESTSQLConnectionFactoryParams.GetCharSet: string;
522+
begin
523+
Result := FCharSet;
524+
end;
525+
526+
procedure TMiniRESTSQLConnectionFactoryParams.SetCharSet(const ACharSet: string);
527+
begin
528+
FCharSet := ACharSet;
529+
end;
530+
518531
initialization
519532
gConnectionIDCounter := 0;
520533

MiniREST.SQL.Common.pas

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ interface
3232
function GetAsVariant: Variant;
3333
procedure SetAsVariant(const AValue: Variant);
3434
function GetParamType: TMiniRESTSQLParamType;
35+
function GetParamSize: Integer;
36+
function SetParamSize(const AParamSize: Integer): IMiniRESTSQLParam;
3537
property AsString: string read GetAsString write SetAsString;
3638
property AsFloat: Double read GetAsFloat write SetAsFloat;
3739
property AsInteger: Integer read GetAsInteger write SetAsInteger;
@@ -44,6 +46,7 @@ TMiniRESTSQLParam = class(TInterfacedObject, IMiniRESTSQLParam)
4446
private
4547
FParamName: string;
4648
FParamType: TMiniRESTSQLParamType;
49+
FParamSize: Integer;
4750
{$IFNDEF FPC}
4851
FValue: TValue;
4952
{$ELSE}
@@ -75,6 +78,8 @@ TMiniRESTSQLParam = class(TInterfacedObject, IMiniRESTSQLParam)
7578
function GetAsVariant: Variant;
7679
procedure SetAsVariant(const AValue: Variant);
7780
function GetParamType: TMiniRESTSQLParamType;
81+
function GetParamSize: Integer;
82+
function SetParamSize(const AParamSize: Integer): IMiniRESTSQLParam;
7883
end;
7984

8085
implementation
@@ -239,4 +244,15 @@ procedure TMiniRESTSQLParam.SetParamName(const AName: string);
239244
FParamName := UpperCase(AName);
240245
end;
241246

247+
function TMiniRESTSQLParam.GetParamSize: Integer;
248+
begin
249+
Result := FParamSize;
250+
end;
251+
252+
function TMiniRESTSQLParam.SetParamSize(const AParamSize: Integer): IMiniRESTSQLParam;
253+
begin
254+
FParamSize := AParamSize;
255+
Result := Self;
256+
end;
257+
242258
end.

MiniREST.SQL.Intf.pas

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ interface
2020
procedure SetSQL(const ASQL: string);
2121
function ParamByName(const AParamName: string): IMiniRESTSQLParam;
2222
function AddParam(AParam: IMiniRESTSQLParam): IMiniRESTSQLQuery;
23+
function GetParams: TArray<IMiniRESTSQLParam>;
2324
{function GetValue(AField: string) : Variant; overload;
2425
function GetValue(AField: string; ADefault: Variant): Variant; overload;
2526
function FieldByName(const AFieldName: string): TField;
@@ -139,6 +140,8 @@ interface
139140
procedure SetConnectionFactoryEventLogger(ALogger: IMiniRESTSQLConnectionFactoryEventLogger);
140141
function GetConnectionsCount: Integer;
141142
procedure SetConnectionsCount(const ACount: Integer);
143+
function GetCharSet: string;
144+
procedure SetCharSet(const ACharSet: string);
142145
function GetObject: TObject;
143146
end;
144147

MiniREST.SQL.SQLDb.pas

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ TMiniRESTSQLQuerySQLDb = class(TInterfacedObject, IMiniRESTSQLQuery)
126126
function ParamByName(const AParamName: string): IMiniRESTSQLParam;
127127
function AddParam(AParam: IMiniRESTSQLParam): IMiniRESTSQLQuery;
128128
function ApplyUpdates(const AMaxErrors: Integer): Integer;
129-
function GetDataSet: TDataSet;
129+
function GetDataSet: TDataSet;
130+
function GetParams: TArray<IMiniRESTSQLParam>;
130131
end;
131132

132133
implementation
@@ -424,6 +425,15 @@ function TMiniRESTSQLQuerySQLDb.GetDataSet: TDataSet;
424425
Result := FQry;
425426
end;
426427

428+
function TMiniRESTSQLQuerySQLDb.GetParams: TArray<IMiniRESTSQLParam>;
429+
var
430+
I: Integer;
431+
begin
432+
SetLength(Result, FParams.Count);
433+
for I := 0 to FParams.Count -1 do
434+
Result[I] := FParams.Items[I];
435+
end;
436+
427437
procedure TMiniRESTSQLQuerySQLDb.BeforeOpenMiniRESTDataSet(DataSet: TDataSet);
428438
var
429439
LMiniRESTSQLParam: IMiniRESTSQLParam;
@@ -439,6 +449,8 @@ procedure TMiniRESTSQLQuerySQLDb.BeforeOpenMiniRESTDataSet(DataSet: TDataSet);
439449
//LMiniRESTSQLParam := FParams.Data[I];
440450
LMiniRESTSQLParam := FParams.Items[I];
441451
LParam := FQry.ParamByName(LMiniRESTSQLParam.GetParamName);
452+
if LMiniRESTSQLParam.GetParamSize > 0 then
453+
LParam.Size := LMiniRESTSQLParam.GetParamSize;
442454
TMiniRESTSQLConnectionSQLDb(FConnection.GetObject).SetMiniRESTSQLParamToSQLParam(LMiniRESTSQLParam, LParam);
443455
end;
444456
end;
@@ -526,6 +538,8 @@ procedure TMiniRESTSQLConnectionSQLDb.SetMiniRESTSQLParamToSQLParam(AMiniRESTSQL
526538
stBoolean: ASQLParam.AsBoolean := AMiniRESTSQLParam.AsBoolean;
527539
stVariant, stUndefined: ASQLParam.Value := AMiniRESTSQLParam.GetAsVariant;
528540
end;
541+
if (AMiniRESTSQLParam.GetParamType = stString) and (AMiniRESTSQLParam.GetParamSize > 0) then
542+
ASQLParam.AsString := Copy(ASQLParam.AsString, 1, AMiniRESTSQLParam.GetParamSize);
529543
end;
530544

531545
function TMiniRESTSQLConnectionSQLDb.InTransaction: Boolean;
@@ -568,6 +582,8 @@ procedure TMiniRESTSQLConnectionSQLDb.SetConnectionParams;
568582
LStringList := TStringList.Create;
569583
try
570584
FSQLConnection.ConnectorType := GetConnectorType(FConnectionParams.GetDatabaseType);
585+
if FConnectionParams.GetCharSet <> '' then
586+
FSQLConnection.CharSet := FConnectionParams.GetCharSet;
571587
FSQLConnection.LoginPrompt := False;
572588
FSQLConnection.UserName := FConnectionParams.GetUserName;
573589
FSQLConnection.Password := FConnectionParams.GetPassword;

unittest/SQL/Test.SQL.Default.pas

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,14 @@ TMiniRESTSQLTest = class({$IFNDEF FPC}TObject{$ELSE}TTestCase{$IFEND})
126126
[Test]
127127
{$IFEND}
128128
procedure TestSuccessWithDefaultServerPort;
129+
{$IFNDEF FPC}
130+
[Test]
131+
{$IFEND}
132+
procedure TestParamSize;
133+
{$IFNDEF FPC}
134+
[Test]
135+
{$IFEND}
136+
procedure TestCharSet;
129137
(* {$IFNDEF FPC}
130138
[Test]
131139
{$IFEND}
@@ -830,4 +838,87 @@ procedure TMiniRESTSQLTest.TestSuccessWithDefaultServerPort;
830838
{$IFEND}
831839
end;
832840

841+
procedure TMiniRESTSQLTest.TestParamSize;
842+
var
843+
LConn1: IMiniRESTSQLConnection;
844+
LQry, LQryID: IMiniRESTSQLQuery;
845+
LId: Integer;
846+
I: Integer;
847+
begin
848+
{
849+
LENGTH OF PHONE IS 15, THEN THE PARAM SIZE SHOULD BE 15,
850+
IF THE VALUE SIZE OF PARAM ARE BIGGER THAN SIZE,
851+
THE VALUE WILL TRUNCATED
852+
}
853+
LConn1 := FConnectionFactory.GetConnection;
854+
LQry := LConn1.GetQuery;
855+
LQryID := LConn1.GetQuery;
856+
LQry.SQL := 'SELECT * FROM CUSTOMER WHERE 1=0';
857+
LQry.Open;
858+
for I := 0 to 5 do
859+
begin
860+
LQryID.Close;
861+
LQryID.SQL := 'select gen_id(gen_customer_id, 1) from rdb$database';
862+
LQryID.Open;
863+
LId := LQryID.DataSet.FieldByName('GEN_ID').AsInteger;
864+
LQry.DataSet.Append;
865+
LQry.DataSet.FieldByName('ID').AsInteger := LId;
866+
LQry.DataSet.FieldByName('NAME').AsString := 'HUE';
867+
LQry.DataSet.FieldByName('PHONE').AsString := '999999999999999';
868+
LQry.DataSet.Post;
869+
end;
870+
LQry.ApplyUpdates(0);
871+
LQry := LConn1.GetQuery;
872+
LQry.SQL := 'SELECT * FROM CUSTOMER WHERE PHONE = :PHONE';
873+
LQry.ParamByName('PHONE').SetParamSize(15);
874+
LQry.ParamByName('PHONE').AsString := 'BIG BIG BIG BIG 999999999999999 BIG BIG';
875+
LQry.Open;
876+
{$IFNDEF FPC}
877+
Assert.AreTrue(LQry.DataSet.Active);
878+
{$ELSE}
879+
CheckTrue(LQry.DataSet.Active);
880+
{$IFEND}
881+
end;
882+
883+
procedure TMiniRESTSQLTest.TestCharSet;
884+
var
885+
LConn1: IMiniRESTSQLConnection;
886+
LQry: IMiniRESTSQLQuery;
887+
LConnectionFactory: IMiniRESTSQLConnectionFactory;
888+
LConnectionFactoryParams: IMiniRESTSQLConnectionFactoryParams;
889+
LExpectedFilePath: string;
890+
begin
891+
LConnectionFactoryParams := GetConnectionFactoryParams;
892+
LConnectionFactoryParams.SetCharSet('UTF8');
893+
LConnectionFactory := GetConnectionFactory(LConnectionFactoryParams);
894+
LExpectedFilePath := ParamStr(0);
895+
LConn1 := LConnectionFactory.GetConnection;
896+
LQry := LConn1.GetQuery;
897+
LQry.SQL := 'SELECT * FROM MON$ATTACHMENTS WHERE MON$CHARACTER_SET_ID = :CHARSET_ID ' +
898+
' AND MON$REMOTE_PROCESS = :REMOTE_PROCESS';
899+
LQry.ParamByName('CHARSET_ID').AsInteger := 4; // UTF8 FIREBIRD 2.5;
900+
LQry.ParamByName('REMOTE_PROCESS').AsString := LExpectedFilePath;
901+
LQry.Open;
902+
{$IFNDEF FPC}
903+
Assert.AreTrue(LQry.DataSet.RecordCount = 1);
904+
{$ELSE}
905+
CheckTrue(LQry.DataSet.RecordCount = 1);
906+
{$IFEND}
907+
908+
LConnectionFactoryParams.SetCharSet('WIN1252');
909+
LConnectionFactory := GetConnectionFactory(LConnectionFactoryParams);
910+
LConn1 := LConnectionFactory.GetConnection;
911+
LQry := LConn1.GetQuery;
912+
LQry.SQL := 'SELECT * FROM MON$ATTACHMENTS WHERE MON$CHARACTER_SET_ID = :CHARSET_ID ' +
913+
' AND MON$REMOTE_PROCESS = :REMOTE_PROCESS';
914+
LQry.ParamByName('CHARSET_ID').AsInteger := 53; // WIN1252 FIREBIRD 2.5;
915+
LQry.ParamByName('REMOTE_PROCESS').AsString := LExpectedFilePath;
916+
LQry.Open;
917+
{$IFNDEF FPC}
918+
Assert.AreTrue(LQry.DataSet.RecordCount = 1);
919+
{$ELSE}
920+
CheckTrue(LQry.DataSet.RecordCount = 1);
921+
{$IFEND}
922+
end;
923+
833924
end.

unittest/TEST.FDB

512 KB
Binary file not shown.

0 commit comments

Comments
 (0)