Re: [HACKERS] How do I construct a varlena?

From: "Oliver Elphick" <olly(at)lfix(dot)co(dot)uk>
To: M(dot)Boekhold(at)et(dot)tudelft(dot)nl
Cc: hackers(at)postgreSQL(dot)org
Subject: Re: [HACKERS] How do I construct a varlena?
Date: 1998-08-09 19:45:24
Message-ID: 199808091945.UAA19706@linda.lfix.co.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Maarten Boekhold wrote:
>On Tue, 4 Aug 1998, Oliver Elphick wrote:
>
>> Can you tell me, please, how to construct a varlena (to be returned by
>> a function).
>>
>> I see it is declared as an int and a 1-byte array; I don't know how to
>> assign a longer string in place of that 1-byte array; the compiler
>> complains about incompatible types, whatever I do.
>
>If I'm correct:
>
> malloc(sizeof(VARLENA) + l);
>
>where 'l' is the length of the string you want to put in (without the
>trailing '\0').

Thanks for your reply; I've been away a few days so I've only just seen it.

My problem is in how to get the compiler to treat the malloced space as
a varlena.

I have this (abridged) C code, to be used with
CREATE FUNCTION cname(bpchar, bpchar, bpchar) returns bpchar ...:

char *cxname;

text cname (text s, text t, text f)
{
text *result;
...
cxname = realloc((void *) cxname, strlen(tmp)+sizeof(struct varlena));
strcpy(cxname+sizeof(int32), tmp);
-> result = &((struct varlena) cxname);
result->vl_len = strlen(tmp);

return *result;
}

but the compiler gives the error `conversion to non-scalar type requested'
at the marked line.

Once I know how to do this, I will add it to the examples in the CREATE
FUNCTION documentation, since it will no doubt be helpful to others.

--
Oliver Elphick Oliver(dot)Elphick(at)lfix(dot)co(dot)uk
Isle of Wight http://www.lfix.co.uk/oliver
PGP key from public servers; key ID 32B8FAA1
========================================
"...ask, and ye shall receive, that your joy may be
full." John 16:24

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Roland Roberts 1998-08-09 21:18:41 Re: [HACKERS] How do I find the table that an oid belongs to
Previous Message Bruce Momjian 1998-08-09 17:34:39 Re: [HACKERS] thread-safe libpq and DBD::Pg