Thread: array_agg and libpq(xx)

array_agg and libpq(xx)

From
Grzegorz Jaśkiewicz
Date:
Hi,

Anyone here passed array to C/c++ code via libpq(xx) ??
I need to pass on an array of strings, some of them might contain coma symbol.
I am wondering, if there's anything already in libpq(xx) that would
chop value into an array of char* , or will it just return a string
and it is up to user to chop it.

thanks.


--
GJ

Re: array_agg and libpq(xx)

From
John R Pierce
Date:
Grzegorz Jaśkiewicz wrote:
> Hi,
>
> Anyone here passed array to C/c++ code via libpq(xx) ??
>

Do you mean, fetching the output of a query like

    SELECT ARRAY['123','456','abc','def'];

?

> I need to pass on an array of strings, some of them might contain coma symbol.
> I am wondering, if there's anything already in libpq(xx) that would
> chop value into an array of char* , or will it just return a string
> and it is up to user to chop it.
>

    pgResult = PQexec(pgConn, "select array['123','456','abc','def'];");
    pgarray = PQgetvalue(pgResult,0,0);

will return a pointer to the ARRAY in postgres format as defined by the
typsend/recieve internal functions for the ARRAY type.   you would need
to walk this and copy the strings comprising the array to your own
managed storage (malloc, or whatever) prior to calling
PQclear(pgResult).   I'm trying to find the docs on what the typsend
format for ARRAY looks like and failing.




Re: array_agg and libpq(xx)

From
Merlin Moncure
Date:
2009/3/19 Grzegorz Jaśkiewicz <gryzman@gmail.com>:
> Hi,
>
> Anyone here passed array to C/c++ code via libpq(xx) ??
> I need to pass on an array of strings, some of them might contain coma symbol.
> I am wondering, if there's anything already in libpq(xx) that would
> chop value into an array of char* , or will it just return a string
> and it is up to user to chop it.
>

if you are moving arrays (and/or composites) into out of database
through libpq, check out libpqtypes:

http://libpqtypes.esilo.com/

merlin

Re: array_agg and libpq(xx)

From
Grzegorz Jaśkiewicz
Date:
2009/3/19 Merlin Moncure <mmoncure@gmail.com>:

> if you are moving arrays (and/or composites) into out of database
> through libpq, check out libpqtypes:
>
> http://libpqtypes.esilo.com/

Thanks, I will have to do it via libpqxx unfortunately. Which
complicates matter a bit, since they don't expose libpq. (which for
instance sux, because there's no equivalent of execParam() in pqxx,
and prepared statements aren't always what I am aiming for..).



--
GJ

Re: array_agg and libpq(xx)

From
Grzegorz Jaśkiewicz
Date:
2009/3/19 John R Pierce <pierce@hogranch.com>:
>   pgResult = PQexec(pgConn, "select array['123','456','abc','def'];");
>   pgarray = PQgetvalue(pgResult,0,0);
>
> will return a pointer to the ARRAY in postgres format as defined by the
> typsend/recieve internal functions for the ARRAY type.   you would need to
> walk this and copy the strings comprising the array to your own managed
> storage (malloc, or whatever) prior to calling PQclear(pgResult).   I'm
> trying to find the docs on what the typsend format for ARRAY looks like and
> failing.

Clearly looking like libpqq(xx) are missing that functionality built
in to protocol/api...



--
GJ

Re: array_agg and libpq(xx)

From
Merlin Moncure
Date:
2009/3/19 Grzegorz Jaśkiewicz <gryzman@gmail.com>:
> 2009/3/19 Merlin Moncure <mmoncure@gmail.com>:
>
>> if you are moving arrays (and/or composites) into out of database
>> through libpq, check out libpqtypes:
>>
>> http://libpqtypes.esilo.com/
>
> Thanks, I will have to do it via libpqxx unfortunately. Which
> complicates matter a bit, since they don't expose libpq. (which for
> instance sux, because there's no equivalent of execParam() in pqxx,
> and prepared statements aren't always what I am aiming for..).

I see.  well, libpqxx is just a wrapper to libpq, same as libpqtypes.
Barring that, your best bet is to parse the string or unnest the array
in the query.

merlin