escape/unescape on bytea field

Discussion of open issues, suggestions and bugs regarding PgDAC (PostgreSQL Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
dgriessinger
Posts: 4
Joined: Wed 25 Jan 2012 15:13

escape/unescape on bytea field

Post by dgriessinger » Wed 25 Jan 2012 15:26

Dear all,

I have move from anydac to pgdac, and I have one issue with byteA field.

I request directly from SQL queries, I don't use tfields.

the following code doesn't work, is it exists a similar function like "pg_escape_bytea" ?:

Code: Select all

function EncodeBYTEA(Value: ansistring): ansistring;
var encoded:pansichar;
    len:integer;
    leng:cardinal;
begin
 leng:=Length(Value);
 encoded:=PQEscapeByteaConn(TNativeConnect(dbcnx.Handle).Handle,pansichar(value),leng,len);
 setlength(result,len-1);
 StrCopy(pansichar(result),encoded);
 PQFreemem(encoded);
 result:=''''+result+'''';
end;


function DecodeBYTEA(value: ansistring): ansistring;
var decoded:pansichar;
    len:integer;
begin
  decoded:=PQUnescapeBytea(pansichar(value),len);
  SetLength(result,len);
  if (len > 0) then Move(decoded^,result[1],len);
  PQFreemem(decoded);
end;


I just want escape/unescape byteA field in string.
If somebody have an idea?

Cheers
Dams

AlexP
Devart Team
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Post by AlexP » Thu 26 Jan 2012 09:34

Hello,

In the latest version of PgDAC, we added the correct support for work with the Bytea type fields for PostgreSQL servers versions 9 and higher. If you are using a server version 9 and higher, try installing the latest version of PgDAC 3.1.4, it may solve your problem. If the problem persists with the latest version anyway, please send a sample that reproduces the problem to alexp.devart.com. If you are using a lower than 9 version. please specify the exact version of your server.

dgriessinger
Posts: 4
Joined: Wed 25 Jan 2012 15:13

Post by dgriessinger » Thu 26 Jan 2012 12:57

Thanks Alex for your answer.

I have find a solution, it seems working with 3.1.3

Code: Select all

function DecodeBYTEA(value: ansistring): ansistring;
begin
  result:=value;
end;


function EncodeBYTEA(Value: ansistring): ansistring;
var i:Integer;
begin
  Result:= '';
  for I := 1 to length(value) do
    Result:= Result+IntToHex(ord(value[i]),2);
  result:='$$\x'+result+'$$';
end;

function EncodeBYTEA(Value: TStringStream): ansistring;
var i:Integer;
begin
  try
    value.Position := 0;
    SetLength(Result, value.Size * 2);
    BinToHex(PAnsiChar(value.Memory), PAnsiChar(Result), value.Size);
  finally
  end;
  Result:='$$\x'+result+'$$';
end;

I have the 9.1 version of Pg.

I have install the update, I forgot to check if an update was available
:oops: :oops:

Thanks a lot

AlexP
Devart Team
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Post by AlexP » Mon 30 Jan 2012 13:50

Hello,

Nice to hear that you solved the problem.
If you have any other questions, feel free to contact us.

Post Reply