Thread: helpers to convert C types to postgres types (Array)

helpers to convert C types to postgres types (Array)

From
Ivan Sergio Borgonovo
Date:
I'm still trying to collect all the bits to be able to read and
return several types of data in C functions.

I'm looking for quick ways to deal with ArrayType.

I'd expect some helper because these kind of operation should be
frequent and without any helper (function/macro) they really make
the code awful.

Generally you work with C types that later you've to "convert" to
Postgres types.

So for example you may have an array of int2 that then you've to
place into an ArrayType.

I think there are 3 kinds of operation you may have to do:

1 You may have an already "formed" C array type and you'd just copy it into an ArrayType
2 You may know the # of elements of the C array type but you're filling it an element at a time
3 You don't know the number of elements in the array in advance so you'd like to append to the ArrayType one element at
atime
 

1 seems to require
- creating an array of Datum
- looping over the C array
- assign to each Datum element the "converted" C value
- construct_array the Postgres array
That's a pain. Any other way? macro?

2 Seems the easiest

3 ???
Is there any function in postgres that let you append elements to
an ArrayType?

thanks

-- 
Ivan Sergio Borgonovo
http://www.webthatworks.it



Re: helpers to convert C types to postgres types (Array)

From
Merlin Moncure
Date:
On Fri, Jan 29, 2010 at 7:34 AM, Ivan Sergio Borgonovo
<mail@webthatworks.it> wrote:
> I'm still trying to collect all the bits to be able to read and
> return several types of data in C functions.
>
> I'm looking for quick ways to deal with ArrayType.
>
> I'd expect some helper because these kind of operation should be
> frequent and without any helper (function/macro) they really make
> the code awful.
>
> Generally you work with C types that later you've to "convert" to
> Postgres types.
>
> So for example you may have an array of int2 that then you've to
> place into an ArrayType.
>
> I think there are 3 kinds of operation you may have to do:
>
> 1 You may have an already "formed" C array type and you'd just copy
>  it into an ArrayType
> 2 You may know the # of elements of the C array type but you're
>  filling it an element at a time
> 3 You don't know the number of elements in the array in advance so
>  you'd like to append to the ArrayType one element at a time
>
> 1 seems to require
> - creating an array of Datum
> - looping over the C array
> - assign to each Datum element the "converted" C value
> - construct_array the Postgres array
> That's a pain. Any other way? macro?
>
> 2 Seems the easiest
>
> 3 ???
> Is there any function in postgres that let you append elements to
> an ArrayType?

well, you have the entire backend array api available.  you can always
call array_cat or array_push depending on how you want to do it.  if
you are worried about performance though the fastest way is going to
be to collect your element datums in a vector and calling
construct_array however.

merlin