Re: Plperl Question - Mailing list pgsql-general

From Stuart Cooper
Subject Re: Plperl Question
Date
Msg-id 7fc8628a0703141525m3d9fd31fqc222db5802597454@mail.gmail.com
Whole thread Raw
In response to Plperl Question  ("Chris Coleman" <ChristopherC@eurocom.co.uk>)
List pgsql-general
Answers in place:

> I'm trying to write a plperl function to copy the new row e.g. NEW in
> plpgsql into another table.  I was looking for a similar effect to the

> INSERT INTO blah VALUES (NEW.*)

> Syntax that can be used in plpgsql.  So fat the best I have come up with
> is:

> $collist = "";
> $vallist = "";
> while (($col, $val) = each(%{$_TD->{new}}))
> {
>         $collist .= ($col.",");
>
>         #Need to fix issues here with quoting in the value list.
>         $vallist .= ("'".$val."',");
> }
> chop($collist);
> chop($vallist);

> However, this leads to issues with numerical columns being quoted, and
> worse still NULL numerical column being entered as '' which results in
> "Invalid syntax for integer" errors.

NULL values will have $val undefined, so you can just avoide adding them to
$collist and $vallist in the first place

next if ( ! defined $val); # don't add NULL values

as the first line of your while loop body will easily acheive this.

Numbers are trickier- you could go with the heuristic that if $val
looks like a number, it is a number and don't quote it. However then
you run into problems with number data in char columns. And then you
start thinking about your pg_catalog solution again.

> The only solution I can see at present is to look up the type of each
> column name in the pg_catalog tables, and based upon this, quote as
> necessary.

Good luck,
Stuart.

pgsql-general by date:

Previous
From: Scott Marlowe
Date:
Subject: Re: [Bulk] Re: quoted identifier behaviour
Next
From: Jaime Silvela
Date:
Subject: Re: DST failing on 8.1.3