Re: [HACKERS] How do I construct a varlena? - Mailing list pgsql-hackers

From Oliver Elphick
Subject Re: [HACKERS] How do I construct a varlena?
Date
Msg-id 199808091945.UAA19706@linda.lfix.co.uk
Whole thread Raw
In response to Re: [HACKERS] How do I construct a varlena?  (Maarten Boekhold <maartenb@dutepp2.et.tudelft.nl>)
Responses Re: [HACKERS] How do I construct a varlena?
List 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.Elphick@lfix.co.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



pgsql-hackers by date:

Previous
From: Bruce Momjian
Date:
Subject: Re: [HACKERS] thread-safe libpq and DBD::Pg
Next
From: Roland Roberts
Date:
Subject: Re: [HACKERS] How do I find the table that an oid belongs to