Re: PL/PGSQL: Dynamic Record Introspection

From: Pavel Stehule <stehule(at)kix(dot)fsv(dot)cvut(dot)cz>
To: Titus von Boxberg <ut(at)bhi-hamburg(dot)de>
Cc: pgsql-patches(at)postgresql(dot)org
Subject: Re: PL/PGSQL: Dynamic Record Introspection
Date: 2005-07-14 06:38:32
Message-ID: Pine.LNX.4.44.0507140836300.31492-100000@kix.fsv.cvut.cz
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-patches

good idea. it's what can eliminate not neccessery using plperl. I would to
see it in plpgsql.

regards
Pavel

On Thu, 14 Jul 2005, Titus von Boxberg wrote:

> Hi all,
>
> I needed introspection capabilities for record types to write more generic
> trigger procedures in PL/PGSQL.
>
> With the following patch it's possible to
> - extract all field names of a record into an array
> - extract field count of a record
> - address a single field of a record with a variable
> containing the field name (additional to the usual record.fieldname
> notation where the fieldname is hardcoded).
>
> The syntax is
> - record%NFIELDS gives the number of fields in the record
> - record%FIELDNAMES gives the array of the field names
> - record%scalarvariable extracts the field whose name
> is equal to the contents of scalarvariable
>
> ------------
>
> The patch is nonintrusive in the sense that it only adds things
> with one exception:
> In function exec_eval_datum(), file pl_exec.c, line 3557 (after the patch)
> I chose to convert the record field values to TEXT if the caller does
> not require a certain type (expectedtypeid == InvalidOid).
> Additionally, I cast the value from the record field type if
> the destination Datum is of different type.
>
> As far as I can see, this does no harm because in most cases
> the expectedtypeid is correctly set. I just wanted to avoid that
> if it is not set, the returned datum is of a more restrictive type
> than TEXT.
>
> ------------
>
> The patch is against a HEAD checkout from 07/12/05
> The output comes from difforig.
>
> Test code for the patch can be extracted from an example I put into
> plpgsql.sgml
>
> ------------
>
> Here is a summary of things that get patched by the file:
> - add three new special parsing functions to pl_comp.c
> (plpgsql_parse_wordpercentword, plpgsql_parse_wordnfields,
> plpgsql_parse_wordfieldnames).
> - modify PLpgSQL_recfield in plpgsql.h to either hold
> a conventional field name (record.fieldname) or a dno
> for the variable (record%variable).
> - add two PLPGSQL_DTYPEs for the two new % notations
> - modify "case PLPGSQL_DTYPE_RECFIELD:"
> in exec_eval_datum() and exec_assign_value()
> to deal with index strings from a variable
> - add "case PLPGSQL_DTYPE_RECFIELDNAMES"
> and "case PLPGSQL_DTYPE_NRECFIELD"
> to exec_eval_datum() to evaluate %FIELDNAMES
> and %NFIELDS expressions.
> - update plpgsql.sgml in the docs directory
>
> ------------
>
> Please notify me if I can be of further assistance.
>
> Regards
> Titus
>

In response to

Browse pgsql-patches by date

  From Date Subject
Next Message Bruce Momjian 2005-07-14 06:45:44 Re: thousands comma numeric formatting in psql
Previous Message Neil Conway 2005-07-14 06:18:05 Re: Final cleanup of SQL:1999 references