Page 1 of 1

escape/unescape on bytea field

Posted: Wed 25 Jan 2012 15:26
by dgriessinger
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

Posted: Thu 26 Jan 2012 09:34
by AlexP
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.

Posted: Thu 26 Jan 2012 12:57
by dgriessinger
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

Posted: Mon 30 Jan 2012 13:50
by AlexP
Hello,

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