Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delphi XE2 backwards compatibility fix #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
126 changes: 65 additions & 61 deletions sources/Redis.Client.pas
Original file line number Diff line number Diff line change
Expand Up @@ -253,12 +253,34 @@ function NewRedisCommand(const RedisCommandString: string): IRedisCommand;

implementation

uses Redis.NetLib.Factory, System.Generics.Collections;
uses
System.Generics.Collections, Redis.NetLib.Factory, Redis.Utils;

const
REDIS_GEO_UNIT_STRING: array [TRedisGeoUnit.Meters .. TRedisGeoUnit.Feet]
of string = ('m', 'km', 'mi', 'ft');

function NewRedisClient(const AHostName: string; const APort: Word;
const ALibName: string): IRedisClient;
var
TCPLibInstance: IRedisNetLibAdapter;
begin
TCPLibInstance := TRedisNetLibFactory.GET(ALibName);
Result := TRedisClient.Create(TCPLibInstance, AHostName, APort);
try
TRedisClient(Result).Connect;
except
Result := nil;
raise;
end;
end;

function NewRedisCommand(const RedisCommandString: string): IRedisCommand;
begin
Result := TRedisCommand.Create;
TRedisCommand(Result).SetCommand(RedisCommandString);
end;

{ TRedisClient }

function TRedisClient.SADD(const aKey, aValue: TBytes): Integer;
Expand All @@ -281,7 +303,7 @@ function TRedisClient.SCARD(const aKey: string): Integer;

procedure TRedisClient.SELECT(const ADBIndex: Integer);
begin
FNextCMD := GetCmdList('SELECT').Add(ADBIndex.ToString);
FNextCMD := GetCmdList('SELECT').Add(IntToStr(ADBIndex));
ExecuteWithStringResult(FNextCMD);
end;

Expand Down Expand Up @@ -332,7 +354,7 @@ function TRedisClient.BLPOP(const AKeys: array of string; const ATimeout: Int32;
lRes := InternalBlockingLeftOrRightPOP(FNextCMD, AKeys, ATimeout);
Result := lRes.HasValue;
if Result then
Value := lRes.ToArray;
Value := RedisArrayToArray(lRes);
// Value := InternalBlockingLeftOrRightPOP(FNextCMD, AKeys, ATimeout,
// FIsValidResponse);
// Result := FIsValidResponse;
Expand All @@ -354,13 +376,13 @@ function TRedisClient.BRPOP(const AKeys: array of string; const ATimeout: Int32;
lRes := InternalBlockingLeftOrRightPOP(FNextCMD, AKeys, ATimeout);
Result := lRes.HasValue;
if Result then
Value := lRes.ToArray;
Value := RedisArrayToArray(lRes);
end;

procedure TRedisClient.CheckResponseError(const aResponse: string);
begin
if aResponse.Chars[0] = '-' then
raise ERedisException.Create(aResponse.Substring(1))
if StringChars(aResponse, 0) = '-' then
raise ERedisException.Create(StringSubstring(aResponse, 1))
end;

procedure TRedisClient.CheckResponseType(Expected, Actual: string);
Expand Down Expand Up @@ -471,7 +493,7 @@ function TRedisClient.EXPIRE(const aKey: string;
AExpireInSecond: UInt32): boolean;
begin
FTCPLibInstance.Write(GetCmdList('EXPIRE').Add(aKey)
.Add(AExpireInSecond.ToString).ToRedisCommand);
.Add(IntToStr(AExpireInSecond)).ToRedisCommand);

{
1 if the timeout was set.
Expand Down Expand Up @@ -785,7 +807,7 @@ function TRedisClient.InternalBlockingLeftOrRightPOP(NextCMD: IRedisCommand;
AKeys: array of string; ATimeout: Int32): TRedisArray;
begin
NextCMD.AddRange(AKeys);
NextCMD.Add(ATimeout.ToString);
NextCMD.Add(IntToStr(ATimeout));
FTCPLibInstance.SendCmd(NextCMD);
Result := ParseArrayResponseNULL;
end;
Expand Down Expand Up @@ -849,8 +871,8 @@ function TRedisClient.LRANGE(const aListKey: string;
begin
FNextCMD := GetCmdList('LRANGE');
FNextCMD.Add(aListKey);
FNextCMD.Add(aIndexStart.ToString);
FNextCMD.Add(aIndexStop.ToString);
FNextCMD.Add(IntToStr(aIndexStart));
FNextCMD.Add(IntToStr(aIndexStop));
FTCPLibInstance.SendCmd(FNextCMD);
Result := ParseArrayResponseNULL;
end;
Expand All @@ -860,7 +882,7 @@ function TRedisClient.LREM(const aListKey: string; const ACount: Integer;
begin
FNextCMD := GetCmdList('LREM');
FNextCMD.Add(aListKey);
FNextCMD.Add(ACount.ToString);
FNextCMD.Add(IntToStr(ACount));
FNextCMD.Add(aValue);
FTCPLibInstance.SendCmd(FNextCMD);
Result := ParseIntegerResponse(FValidResponse);
Expand All @@ -871,16 +893,16 @@ procedure TRedisClient.LTRIM(const aListKey: string;
var
lResult: string;
begin
FNextCMD := GetCmdList('LTRIM').Add(aListKey).Add(aIndexStart.ToString)
.Add(aIndexStop.ToString);
FNextCMD := GetCmdList('LTRIM').Add(aListKey).Add(IntToStr(aIndexStart))
.Add(IntToStr(aIndexStop));
lResult := ExecuteWithStringResult(FNextCMD);
if lResult <> 'OK' then
raise ERedisException.Create(lResult);
end;

function TRedisClient.MOVE(const aKey: string; const aDB: Byte): boolean;
begin
FNextCMD := GetCmdList('MOVE').Add(aKey).Add(aDB.ToString);
FNextCMD := GetCmdList('MOVE').Add(aKey).Add(IntToStr(aDB));
Result := ExecuteWithIntegerResult(FNextCMD) = 1;
end;

Expand Down Expand Up @@ -1011,9 +1033,9 @@ function TRedisClient.ParseArrayResponseNULL: TRedisArray;
Exit;
end;

if R.Chars[0] = '*' then
if StringChars(R, 0) = '*' then
begin
ArrLength := R.Substring(1).ToInteger;
ArrLength := StringSubstringToInteger(R, 1);
end
else
raise ERedisException.Create(TRedisConsts.ERR_NOT_A_VALID_ARRAY_RESPONSE);
Expand Down Expand Up @@ -1052,18 +1074,18 @@ function TRedisClient.ParseIntegerResponse(var AValidResponse: boolean): Int64;
Exit;
CheckResponseError(R);

case R.Chars[0] of
case StringChars(R, 0) of
':':
begin
if not TryStrToInt(R.Substring(1), I) then
if not TryStrToInt(StringSubstring(R, 1), I) then
raise ERedisException.CreateFmt
(TRedisConsts.ERR_NOT_A_VALID_INTEGER_RESPONSE +
' - Expected Integer got [%s]', [R]);
Result := I;
end;
'$':
begin
HowMany := R.Substring(1).ToInteger;
HowMany := StringSubstringToInteger(R, 1);
if HowMany = -1 then
begin
AValidResponse := False;
Expand Down Expand Up @@ -1102,9 +1124,9 @@ function TRedisClient.ParseMatrixResponse: TRedisMatrix;
Exit;
end;

if R.Chars[0] = '*' then
if StringChars(R, 0) = '*' then
begin
ArrLength := R.Substring(1).ToInteger;
ArrLength := StringSubstringToInteger(R, 1);
end
else
raise ERedisException.Create(TRedisConsts.ERR_NOT_A_VALID_ARRAY_RESPONSE);
Expand Down Expand Up @@ -1161,14 +1183,14 @@ function TRedisClient.ParseSimpleStringResponseAsByteNULL: TRedisBytes;
// For Bulk Strings the first byte of the reply is "$"
// For Arrays the first byte of the reply is "*"

case R.Chars[0] of
case StringChars(R, 0) of
'+':
Result := BytesOf(R.Substring(1));
Result := BytesOf(StringSubstring(R, 1));
':':
Result := BytesOf(R.Substring(1));
Result := BytesOf(StringSubstring(R, 1));
'$':
begin
HowMany := R.Substring(1).ToInteger;
HowMany := StringSubstringToInteger(R, 1);
Result := FTCPLibInstance.ReceiveBytes(HowMany, FCommandTimeout);
// eat crlf
FTCPLibInstance.ReceiveBytes(2, FCommandTimeout);
Expand Down Expand Up @@ -1249,7 +1271,7 @@ function TRedisClient.BRPOPLPUSH(const ARightListKey, ALeftListKey: string;
FNextCMD := GetCmdList('BRPOPLPUSH');
FNextCMD.Add(ARightListKey);
FNextCMD.Add(ALeftListKey);
FNextCMD.Add(ATimeout.ToString);
FNextCMD.Add(IntToStr(ATimeout));
FTCPLibInstance.SendCmd(FNextCMD);

lValue := ParseSimpleStringResponse(FValidResponse);
Expand Down Expand Up @@ -1320,14 +1342,17 @@ function TRedisClient.&SET(const aKey: string; aValue: TBytes;

function TRedisClient.&SET(const aKey: TBytes; aValue: TBytes;
ASecsExpire: UInt64): boolean;
var
lRes: TRedisBytes;
begin
FNextCMD := GetCmdList('SET');
FNextCMD.Add(aKey);
FNextCMD.Add(aValue);
FNextCMD.Add('EX');
FNextCMD.Add(IntToStr(ASecsExpire));
FTCPLibInstance.SendCmd(FNextCMD);
Result := ParseSimpleStringResponseAsByteNULL.HasValue
lRes := ParseSimpleStringResponseAsByteNULL; { Avoid XE2 F2084 Internal Error: URW1147 }
Result := lRes.HasValue;
end;

function TRedisClient.&SET(const aKey: string; aValue: string;
Expand Down Expand Up @@ -1399,10 +1424,10 @@ procedure TRedisClient.SUBSCRIBE(const AChannels: array of string;
for I := 0 to Length(AChannels) - 1 do
begin
lArrNull := ParseArrayResponseNULL;
if (lArrNull.Items[0].Value.ToLower <> 'subscribe') or
(lArrNull.Items[1] <> AChannels[I]) then
if (LowerCase(RedisArrayGetItems(lArrNull, 0).Value) <> 'subscribe') or
(RedisArrayGetItems(lArrNull, 1) <> AChannels[I]) then
raise ERedisException.Create('Invalid subscription response: ' +
string.Join('-', lArrNull.ToArray))
StringJoin('-', RedisArrayToArray(lArrNull)))
end;
// all is fine, now read the callbacks message
if Assigned(aAfterSubscribe) then
Expand All @@ -1426,9 +1451,9 @@ procedure TRedisClient.SUBSCRIBE(const AChannels: array of string;
end
else
begin
if lArrNull.Items[0] <> 'message' then
if RedisArrayGetItems(lArrNull, 0) <> 'message' then
raise ERedisException.CreateFmt('Invalid reply: %s',
[string.Join('-', lArrNull.ToArray)]);
[StringJoin('-', RedisArrayToArray(lArrNull))]);
lChannel := lArrNull.Value[1];
lValue := lArrNull.Value[2];
try
Expand Down Expand Up @@ -1490,15 +1515,15 @@ function TRedisClient.Tokenize(const ARedisCommand: string): TArray<string>;
if C = '"' then
begin
CurState := SQUOTED;
if not Piece.IsEmpty then
if not StringIsEmpty(Piece) then
begin
List.Add(Piece);
Piece := '';
end;
end
else if C = ' ' then
begin
if not Piece.IsEmpty then
if not StringIsEmpty(Piece) then
begin
List.Add(Piece);
Piece := '';
Expand All @@ -1513,7 +1538,7 @@ function TRedisClient.Tokenize(const ARedisCommand: string): TArray<string>;
if CurState <> SSINK then
raise ERedisException.Create(TRedisConsts.ERR_NOT_A_VALID_COMMAND);

if not Piece.IsEmpty then
if not StringIsEmpty(Piece) then
List.Add(Piece);

Result := List.ToArray;
Expand Down Expand Up @@ -1546,7 +1571,7 @@ function TRedisClient.ZADD(const aKey: string; const AScore: Int64;
const AMember: string): Integer;
begin
FNextCMD := GetCmdList('ZADD');
FNextCMD.Add(aKey).Add(AScore.ToString).Add(AMember);
FNextCMD.Add(aKey).Add(IntToStr(AScore)).Add(AMember);
Result := ExecuteWithIntegerResult(FNextCMD);
end;

Expand All @@ -1560,31 +1585,31 @@ function TRedisClient.ZCOUNT(const aKey: string;
const AMin, AMax: Int64): Integer;
begin
FNextCMD := GetCmdList('ZCOUNT');
FNextCMD.Add(aKey).Add(AMin.ToString).Add(AMax.ToString);
FNextCMD.Add(aKey).Add(IntToStr(AMin)).Add(IntToStr(AMax));
Result := ExecuteWithIntegerResult(FNextCMD);
end;

function TRedisClient.ZINCRBY(const aKey: string; const AIncrement: Int64;
const AMember: string): string;
begin
FNextCMD := GetCmdList('ZINCRBY');
FNextCMD.Add(aKey).Add(AIncrement.ToString).Add(AMember);
FNextCMD.Add(aKey).Add(IntToStr(AIncrement)).Add(AMember);
Result := ExecuteWithStringResult(FNextCMD);
end;

function TRedisClient.ZRANGE(const aKey: string; const AStart, AStop: Int64)
: TRedisArray;
begin
FNextCMD := GetCmdList('ZRANGE');
FNextCMD.Add(aKey).Add(AStart.ToString).Add(AStop.ToString);
FNextCMD.Add(aKey).Add(IntToStr(AStart)).Add(IntToStr(AStop));
Result := ExecuteAndGetArrayNULL(FNextCMD);
end;

function TRedisClient.ZRANGEWithScore(const aKey: string;
const AStart, AStop: Int64): TRedisArray;
begin
FNextCMD := GetCmdList('ZRANGE');
FNextCMD.Add(aKey).Add(AStart.ToString).Add(AStop.ToString).Add('WITHSCORES');
FNextCMD.Add(aKey).Add(IntToStr(AStart)).Add(IntToStr(AStop)).Add('WITHSCORES');
Result := ExecuteAndGetArrayNULL(FNextCMD);
end;

Expand All @@ -1604,27 +1629,6 @@ function TRedisClient.ZREM(const aKey, AMember: string): Integer;
Result := ExecuteWithIntegerResult(FNextCMD);
end;

function NewRedisClient(const AHostName: string; const APort: Word;
const ALibName: string): IRedisClient;
var
TCPLibInstance: IRedisNetLibAdapter;
begin
TCPLibInstance := TRedisNetLibFactory.GET(ALibName);
Result := TRedisClient.Create(TCPLibInstance, AHostName, APort);
try
TRedisClient(Result).Connect;
except
Result := nil;
raise;
end;
end;

function NewRedisCommand(const RedisCommandString: string): IRedisCommand;
begin
Result := TRedisCommand.Create;
TRedisCommand(Result).SetCommand(RedisCommandString);
end;

function TRedisClient.GEOADD(const Key: string;
const Latitude, Longitude: Extended; Member: string): Integer;
var
Expand Down
5 changes: 5 additions & 0 deletions sources/Redis.NetLib.INDY.pas
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,13 @@ function TRedisTCPLibINDY.LibName: string;

function TRedisTCPLibINDY.Receive(const Timeout: Int32): string;
begin
{$IF CompilerVersion < 24}
Result := FTCPClient.IOHandler.ReadLn(LF, Timeout, -1,
TIdTextEncoding.Default);
{$ELSE}
Result := FTCPClient.IOHandler.ReadLn(LF, Timeout, -1,
IndyTextEncoding_Default);
{$IFEND}
end;

function TRedisTCPLibINDY.ReceiveBytes(const ACount: Int64;
Expand Down
Loading