pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
How can I pass an array from PostgreSQL to C (libpq)?
4+ messages / 4 participants
[nested] [flat]

* How can I pass an array from PostgreSQL to C (libpq)?
@ 2003-03-26 18:24 Tony Reina <gar8@pitt.edu>
  2003-03-26 19:07 ` Re: How can I pass an array from PostgreSQL to C (libpq)? Joe Conway <mail@joeconway.com>
  0 siblings, 1 reply; 4+ messages in thread

From: gar8@pitt.edu @ 2003-03-26 18:24 UTC (permalink / raw)
  To: pgsql-sql

I'm using the C library (lipq) to grab data from a field that contains
a float array (float4[]). I'd like to fill an array in my C program
with these values. Unfortunately, Postgres returns the array as a
large string instead of individual values.

Now I could perform "select array_var[1], array_var[2], array_var[3],
..., array_var[4]" and have each array value come back as a different
field, but this seems fairly messy. I suppose I could also go through
and parse the characters in the string returned.

Has anyone else used any other strategies to fill their C array with
an array returned from the libpq? Perhaps there are some SQL functions
for arrays that might exist?

Thanks.
-Tony




^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: How can I pass an array from PostgreSQL to C (libpq)?
  2003-03-26 18:24 How can I pass an array from PostgreSQL to C (libpq)? Tony Reina <gar8@pitt.edu>
@ 2003-03-26 19:07 ` Joe Conway <mail@joeconway.com>
  2003-03-26 22:47   ` Re: How can I pass an array from PostgreSQL to C (libpq)? G. Anthony Reina <gar8+@pitt.edu>
  0 siblings, 1 reply; 4+ messages in thread

From: Joe Conway @ 2003-03-26 19:07 UTC (permalink / raw)
  To: Tony Reina <gar8@pitt.edu>; +Cc: pgsql-sql

Tony Reina wrote:
> I'm using the C library (lipq) to grab data from a field that contains
> a float array (float4[]). I'd like to fill an array in my C program
> with these values. Unfortunately, Postgres returns the array as a
> large string instead of individual values.
> 
> Now I could perform "select array_var[1], array_var[2], array_var[3],
> ..., array_var[4]" and have each array value come back as a different
> field, but this seems fairly messy. I suppose I could also go through
> and parse the characters in the string returned.
> 
> Has anyone else used any other strategies to fill their C array with
> an array returned from the libpq? Perhaps there are some SQL functions
> for arrays that might exist?
> 

PQgetvalue is always going to give you the string representation of the 
field value *unless* you specify a binary cursor. In that case you'll 
get all requested fields in binary form, and it's your responsibility to 
deal with making sense of what you get. There are some backend macros 
and functions that can help you get at array values. Take a look at 
src/utils/adt/arrayfuncs.c and src/include/utils/array.h. You might want 
to read through contrib/array and/or contrib/intarray for examples.

You could also consider writing your array manipulation code in a user 
defined C function and just return the end result to the libpq program. 
It's hard to know if this is an option without more detail on what 
you're trying to do.

Joe




^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: How can I pass an array from PostgreSQL to C (libpq)?
  2003-03-26 18:24 How can I pass an array from PostgreSQL to C (libpq)? Tony Reina <gar8@pitt.edu>
  2003-03-26 19:07 ` Re: How can I pass an array from PostgreSQL to C (libpq)? Joe Conway <mail@joeconway.com>
@ 2003-03-26 22:47   ` G. Anthony Reina <gar8+@pitt.edu>
  2003-03-27 11:36     ` Re: How can I pass an array from PostgreSQL to C (libpq)? Achilleus Mantzios <achill@matrix.gatewaynet.com>
  0 siblings, 1 reply; 4+ messages in thread

From: G. Anthony Reina @ 2003-03-26 22:47 UTC (permalink / raw)
  To: Joe Conway <mail@joeconway.com>; +Cc: pgsql-sql

Yes, in the past I had used binary cursor and things seemed to work fine. 
However, recently when I ran the binary cursor stuff, the array ends up 
getting shifted to the right one index. So what should be placed in 
array_var[n] was getting placed in array var[n+1]. Plus, I was always a 
little concerned with the binary cursor when going across platforms (e.g. 
endianess, alignment).

I ended up just writing a little script that parses the string for the 
comma separators. That works fine, but wanted to see if others had more 
elegant solutions. I'll take a look at the /utils directory you mentioned. 
Perhaps there are functions that I'm not aware of (e.g. I didn't know there 
was a built in function called array_dims that gives the array dimensions).

-Tony


--On Wednesday, March 26, 2003 11:07 AM -0800 Joe Conway 
<mail@joeconway.com> wrote:r
>
> PQgetvalue is always going to give you the string representation of the
> field value *unless* you specify a binary cursor. In that case you'll get
> all requested fields in binary form, and it's your responsibility to deal
> with making sense of what you get. There are some backend macros and
> functions that can help you get at array values. Take a look at
> src/utils/adt/arrayfuncs.c and src/include/utils/array.h. You might want
> to read through contrib/array and/or contrib/intarray for examples.
>
> You could also consider writing your array manipulation code in a user
> defined C function and just return the end result to the libpq program.
> It's hard to know if this is an option without more detail on what you're
> trying to do.
>
> Joe
>




^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: How can I pass an array from PostgreSQL to C (libpq)?
  2003-03-26 18:24 How can I pass an array from PostgreSQL to C (libpq)? Tony Reina <gar8@pitt.edu>
  2003-03-26 19:07 ` Re: How can I pass an array from PostgreSQL to C (libpq)? Joe Conway <mail@joeconway.com>
  2003-03-26 22:47   ` Re: How can I pass an array from PostgreSQL to C (libpq)? G. Anthony Reina <gar8+@pitt.edu>
@ 2003-03-27 11:36     ` Achilleus Mantzios <achill@matrix.gatewaynet.com>
  0 siblings, 0 replies; 4+ messages in thread

From: Achilleus Mantzios @ 2003-03-27 11:36 UTC (permalink / raw)
  To: G. Anthony Reina <gar8+@pitt.edu>; +Cc: Joe Conway <mail@joeconway.com>; pgsql-sql

On Wed, 26 Mar 2003, G. Anthony Reina wrote:

See contrib/intarray

> Yes, in the past I had used binary cursor and things seemed to work fine. 
> However, recently when I ran the binary cursor stuff, the array ends up 
> getting shifted to the right one index. So what should be placed in 
> array_var[n] was getting placed in array var[n+1]. Plus, I was always a 
> little concerned with the binary cursor when going across platforms (e.g. 
> endianess, alignment).
> 
> I ended up just writing a little script that parses the string for the 
> comma separators. That works fine, but wanted to see if others had more 
> elegant solutions. I'll take a look at the /utils directory you mentioned. 
> Perhaps there are functions that I'm not aware of (e.g. I didn't know there 
> was a built in function called array_dims that gives the array dimensions).
> 
> -Tony
> 
> 
> --On Wednesday, March 26, 2003 11:07 AM -0800 Joe Conway 
> <mail@joeconway.com> wrote:r
> >
> > PQgetvalue is always going to give you the string representation of the
> > field value *unless* you specify a binary cursor. In that case you'll get
> > all requested fields in binary form, and it's your responsibility to deal
> > with making sense of what you get. There are some backend macros and
> > functions that can help you get at array values. Take a look at
> > src/utils/adt/arrayfuncs.c and src/include/utils/array.h. You might want
> > to read through contrib/array and/or contrib/intarray for examples.
> >
> > You could also consider writing your array manipulation code in a user
> > defined C function and just return the end result to the libpq program.
> > It's hard to know if this is an option without more detail on what you're
> > trying to do.
> >
> > Joe
> >
> 
> 
> ---------------------------(end of broadcast)---------------------------
> TIP 2: you can get off all lists at once with the unregister command
>     (send "unregister YourEmailAddressHere" to majordomo@postgresql.org)
> 

-- 
==================================================================
Achilleus Mantzios
S/W Engineer
IT dept
Dynacom Tankers Mngmt
Nikis 4, Glyfada
Athens 16610
Greece
tel:    +30-210-8981112
fax:    +30-210-8981877
email:  achill@matrix.gatewaynet.com
        mantzios@softlab.ece.ntua.gr




^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2003-03-27 11:36 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2003-03-26 18:24 How can I pass an array from PostgreSQL to C (libpq)? Tony Reina <gar8@pitt.edu>
2003-03-26 19:07 ` Joe Conway <mail@joeconway.com>
2003-03-26 22:47   ` G. Anthony Reina <gar8+@pitt.edu>
2003-03-27 11:36     ` Achilleus Mantzios <achill@matrix.gatewaynet.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox