d.datname "
" FROM pg_database d, pg_user u "
" WHERE d.datdba = u.usesysid";
EXEC SQL END DECLARE SECTION;
EXEC SQL CONNECT TO testdb AS con1 USER testuser;
EXEC SQL SELECT pg_catalog.set_config('search_path', '', false); EXEC SQL COMMIT;
EXEC SQL PREPARE stmt1 FROM :stmt;
EXEC SQL DECLARE cursor1 CURSOR FOR stmt1;
EXEC SQL OPEN cursor1;
EXEC SQL WHENEVER NOT FOUND DO BREAK;
while (1)
{
EXEC SQL FETCH cursor1 INTO :dbaname,:datname;
printf("dbaname=%s, datname=%s\n", dbaname, datname);
}
EXEC SQL CLOSE cursor1;
EXEC SQL COMMIT;
EXEC SQL DISCONNECT ALL;
</programlisting>
</para>
</sect2>
</sect1>
<sect1 id="ecpg-pgtypes">
<title>pgtypes Library</title>
<para>
The pgtypes library maps <productname>PostgreSQL</productname> database
types to C equivalents that can be used in C programs. It also offers
functions to do basic calculations with those types within C, i.e., without
the help of the <productname>PostgreSQL</productname> server. See the
following example:
<programlisting><![CDATA[
EXEC SQL BEGIN DECLARE SECTION;
date date1;
timestamp ts1, tsout;
interval iv1;
char *out;
EXEC SQL END DECLARE SECTION;
PGTYPESdate_today(&date1);
EXEC SQL SELECT started, duration INTO :ts1, :iv1 FROM datetbl WHERE d=:date1;
PGTYPEStimestamp_add_interval(&ts1, &iv1, &tsout);
out = PGTYPEStimestamp_to_asc(&tsout);
printf("Started + duration: %s\n", out);
PGTYPESchar_free(out);
]]>
</programlisting>
</para>
<sect2 id="ecpg-pgtypes-cstrings">
<title>Character Strings</title>
<para>
Some functions such as <function>PGTYPESnumeric_to_asc</function> return
a pointer to a freshly allocated character string. These results should be
freed with <function>PGTYPESchar_free</function> instead of
<function>free</function>. (This is important only on Windows, where
memory allocation and release sometimes need to be done by the same
library.)
</para>
</sect2>
<sect2 id="ecpg-pgtypes-numeric">
<title>The numeric Type</title>
<para>
The numeric type offers to do calculations with arbitrary precision. See
<xref linkend="datatype-numeric"/> for the equivalent type in the
<productname>PostgreSQL</productname> server. Because of the arbitrary precision this
variable needs to be able to expand and shrink dynamically. That's why you
can only create numeric variables on the heap, by means of the
<function>PGTYPESnumeric_new</function> and <function>PGTYPESnumeric_free</function>
functions. The decimal type, which is similar but limited in precision,
can be created on the stack as well as on the heap.
</para>
<para>
The following functions can be used to work with the numeric type:
<variablelist>
<varlistentry id="ecpg-pgtypes-numeric-new">
<term><function>PGTYPESnumeric_new</function></term>
<listitem>
<para>
Request a pointer to a newly allocated numeric variable.
<synopsis>
numeric *PGTYPESnumeric_new(void);
</synopsis>
</para>
</listitem>
</varlistentry>
<varlistentry id="ecpg-pgtypes-numeric-free">
<term><function>PGTYPESnumeric_free</function></term>
<listitem>
<para>
Free a numeric type, release all of its memory.
<synopsis>
void PGTYPESnumeric_free(numeric *var);
</synopsis>
</para>
</listitem>
</varlistentry>
<varlistentry id="ecpg-pgtypes-numeric-from-asc">
<term><function>PGTYPESnumeric_from_asc</function></term>
<listitem>
<para>
Parse a numeric type from its string notation.
<synopsis>
numeric *PGTYPESnumeric_from_asc(char *str, char **endptr);
</synopsis>
Valid formats are for example:
<literal>-2</literal>,
<literal>.794</literal>,
<literal>+3.44</literal>,
<literal>592.49E07</literal> or
<literal>-32.84e-4</literal>.
If the value could be parsed successfully, a valid pointer is returned,
else the NULL pointer. At the