* Fix bug in parsing escaped quotes for Postgresql + added a test

git-svn-id: trunk@6023 -
This commit is contained in:
joost 2007-01-17 11:27:52 +00:00
parent 3d99dcc178
commit fc0bb654c1
4 changed files with 36 additions and 26 deletions

View File

@ -1782,7 +1782,7 @@ Function DateTimeToDateTimeRec(DT: TFieldType; Data: TDateTime): TDateTimeRec;
procedure DisposeMem(var Buffer; Size: Integer);
function BuffersEqual(Buf1, Buf2: Pointer; Size: Integer): Boolean;
function SkipComments(var p: PChar) : boolean;
function SkipComments(var p: PChar; EscapeSlash, EscapeRepeat : Boolean) : boolean;
implementation

View File

@ -176,32 +176,37 @@ begin
Result := ParseSQL(SQL,DoCreate,ParameterStyle,ParamBinding, rs);
end;
function SkipComments(var p: PChar) : boolean;
function SkipComments(var p: PChar; EscapeSlash, EscapeRepeat : Boolean) : Boolean;
var notRepeatEscaped : boolean;
procedure SkipQuotesString(QuoteChar : char);
begin
Inc(p);
Result := True;
repeat
notRepeatEscaped := True;
while not (p^ in [#0, QuoteChar]) do
begin
if EscapeSlash and (p^='\') then Inc(p,2) // make sure we handle \' and \\ correct
else Inc(p);
end;
if p^=QuoteChar then
begin
Inc(p); // skip final '
if (p^=QuoteChar) and EscapeRepeat then // Handle escaping by ''
begin
notRepeatEscaped := False;
inc(p);
end
end;
until notRepeatEscaped;
end;
begin
result := false;
case p^ of
'''': // single quote delimited string
begin
Inc(p);
Result := True;
while not (p^ in [#0, '''']) do
begin
if p^='\' then Inc(p,2) // make sure we handle \' and \\ correct
else Inc(p);
end;
if p^='''' then Inc(p); // skip final '
end;
'"': // double quote delimited string
begin
Inc(p);
Result := True;
while not (p^ in [#0, '"']) do
begin
if p^='\' then Inc(p,2) // make sure we handle \" and \\ correct
else Inc(p);
end;
if p^='"' then Inc(p); // skip final "
end;
'''': SkipQuotesString(''''); // single quote delimited string
'"': SkipQuotesString('"'); // double quote delimited string
'-': // possible start of -- comment
begin
Inc(p);
@ -275,7 +280,7 @@ begin
p:=PChar(SQL);
BufStart:=p; // used to calculate ParamPart.Start values
repeat
SkipComments(p);
SkipComments(p,ParameterStyle<>psPostgreSQL,ParameterStyle=psPostgreSQL);
case p^ of
':','?': // parameter
begin

View File

@ -881,7 +881,7 @@ begin
begin
inc(CurrentP);
EndOfComment := SkipComments(CurrentP);
EndOfComment := SkipComments(CurrentP,True,False);
if EndOfcomment then dec(currentp);
if EndOfComment and (ParsePart = ppStart) then PhraseP := CurrentP;

View File

@ -49,6 +49,11 @@ begin
AssertEquals( 'update test set 1=$1 2=$2 3=$3 4=$4 5=$5 6=$6 7=$7 8=$8 9=$9 10=$10 11=$11 12=$5 where (id = $3) and (test=''$test'')',
params.ParseSQL('update test set 1=:1 2=:2 3=:par3 4=:par4 5=:par5 6=:par6 7=:par7 8=:par8 9=:par9 10=:par10 11=:11 12=:par5 where (id = :par3) and (test=''$test'')',true,psPostgreSQL));
AssertEquals( 'select * from table where ''id '''' = :id''',
params.ParseSQL('select * from table where ''id '''' = :id''',true,psPostgreSQL));
AssertEquals( 'select * from table where "id "" = :id"',
params.ParseSQL('select * from table where "id "" = :id"',true,psPostgreSQL));
AssertEquals( 'select * from table where id = $1',
params.ParseSQL('select * from table where id = :id',true,psSimulated,pb,ReplStr));