Subject: determining Postgres data types

From: [email-masked] (Daniel Turk)
Date: Thu, 19 Aug 1993 12:59:42 -0400 (EDT)

Hi!  I'm wanting to know how to determine Postgres class field types
from within a C program.  I am able to find out class names and all the
field names within a given class.  But I'm not sure how to find out what
data type each field is specified as.  Can this be done easily?  How?

--------------------------------------------------------------------------
Dan Turk     Computer Science & Information Systems     Andrews University
Berrien Springs, MI          [email-masked]               (616) 471-3426
==========================================================================

---

From: [email-masked] (James Woods 61-89-895257)
Date: Thu, 19 Aug 93 15:51:43 -0700

> data type each field is specified as.  Can this be done easily?  How?

Try querying the

---

From: [email-masked] (James Woods 61-89-895257)
Date: Thu, 19 Aug 93 17:44:02 -0700

Oops!, That last message wasn't supposed to be sent, damn computers
that don't do what you want them to, only what you tell them!!

---

From: David Allan Finch <[email-masked]>
Date: Fri, 20 Aug 93 09:02:18 BST

In your message from [Thu, 19 Aug 1993 12:59:42 -0400 (EDT)] you wrote:
 |> Hi!  I'm wanting to know how to determine Postgres class field types
 |> from within a C program.  I am able to find out class names and all the
 |> field names within a given class.  But I'm not sure how to find out what
 |> data type each field is specified as.  Can this be done easily?  How?

The perl script on dumping the db I sent last week does it my using
the pg_class and pg_type system tables.

the following pgperl sub will set up @type and %type if you
get the type id and use it $type[$tid] it will give the string.
$type{$type_name} will do the reverse.
NOTE $tid = &PQftype( $p, $k, $i );  The same principle will
work in C except you will have to implement your own 
assosiate array stuff.

-------------8<------------------------
#!/usr/postgres/bin/pgperl
# This code is non-copyright of David Allan Finch
# and does nothing. If it does then its your
# problem for running it, You have been warned.
# Version 000.001 : 03/Aug/1993
sub get_types
    {
    local( $database ) = @_;
    &PQsetdb( "$database" );
    &PQexec( "begin" );

    &PQexec( "retrieve portal tmp ( pg_type.typname, pg_type.oid )" );
    &PQexec( "fetch all in tmp" );

    $p = &PQparray( "tmp" );
    $g = &PQngroups( $p );
    $t = 0;

    for( $k = 0; $k < $g; $k++ )
        {
        $n = &PQntuplesGroup( $p, $k );
        $m = &PQnfieldsGroup( $p, $k );

        for( $i = 0; $i < $n; $i++ )
            {
            $name   = &PQgetvalue( $p, $i, 0 );
            $value  = &PQgetvalue( $p, $i, 1 );
            $types{$name}   = $value;
            $types[$value]  = $name;
            }
        }

    &PQexec( "end" );
    &PQfinish();
    }
-------------8<------------------------

--
    /		Sarum Wizard
 /\|/\		All Hail Discordia
 | K |		[email-masked]
 \___/		[email-masked]