Commit c701639b authored by Thomas G. Lockhart's avatar Thomas G. Lockhart

Updates from Vince Vielhaber are the first since 1997.

parent 045013cb
<chapter id="libpqplusplus"> <chapter id="libpqplusplus">
<title>libpq C++ Binding</title> <title>libpq C++ Binding</title>
<para> <para>
<filename>libpq++</filename> is the C++ API to <filename>libpq++</filename> is the C++ API to
<productname>Postgres</productname>. <productname>Postgres</productname>.
<filename>libpq++</filename> is a set of classes which allow <filename>libpq++</filename> is a set of classes which allow
client programs to connect to the client programs to connect to the
<productname>Postgres</productname> backend server. These connections <productname>Postgres</productname> backend server. These connections
come in two forms: a Database Class and a Large Object class. come in two forms: a Database Class and a Large Object class.
</para>
<para> <para>
The Database Class is intended for manipulating a database. You can The Database Class is intended for manipulating a database. You can
send all sorts of SQL queries to the <productname>Postgres</productname> send all sorts of SQL queries to the <productname>Postgres</productname>
backend server and retrieve the responses of the server. backend server and retrieve the responses of the server.
</para>
<para> <para>
The Large Object Class is intended for manipulating a large object The Large Object Class is intended for manipulating a large object
in a database. Although a Large Object instance can send normal in a database. Although a Large Object instance can send normal
queries to the <productname>Postgres</productname> backend server queries to the <productname>Postgres</productname> backend server
it is only intended for simple it is only intended for simple
queries that do not return any data. A large object should be seen queries that do not return any data. A large object should be seen
as a file stream. In future it should behave much like the C++ file as a file stream. In future it should behave much like the C++ file
streams streams
<literal>cin</literal>, <literal>cin</literal>,
<literal>cout</literal> <literal>cout</literal>
and and
<literal>cerr</literal>. <literal>cerr</literal>.
</para>
<para> <para>
This chapter is based on the documentation This chapter is based on the documentation
for the <filename>libpq</filename> C library. Three for the <filename>libpq</filename> C library. Three
short programs are listed at the end of this section as examples of short programs are listed at the end of this section as examples of
<filename>libpq++</filename> programming <filename>libpq++</filename> programming
(though not necessarily of good programming). (though not necessarily of good programming).
There are several examples of <filename>libpq++</filename> There are several examples of <filename>libpq++</filename>
applications in applications in
<filename>src/libpq++/examples</filename>, including the source <filename>src/libpq++/examples</filename>, including the source
code for the three examples in this chapter. code for the three examples in this chapter.
</para>
<sect1> <sect1>
<title>Control and Initialization</title> <title>Control and Initialization</title>
<para> <para>
<sect2> <sect2>
<title>Environment Variables</title> <title>Environment Variables</title>
<para> <para>
The following environment variables can be used to set up default The following environment variables can be used to set up default
values for an environment and to avoid hard-coding database names into values for an environment and to avoid hard-coding database names into
an application program: an application program:
<note>
<note> <para>
<para> Refer to the <xref linkend="libpq-envars" endterm="libpq"> for a complete
Refer to the <xref linkend="libpq" endterm="libpq-envars"> for a complete list of available connection options.
list of available connection options. </para>
</note> </note>
</para>
<Para> <Para>
The following environment variables can be used to select default The following environment variables can be used to select default
connection parameter values, which will be used by PQconnectdb or connection parameter values, which will be used by PQconnectdb or
PQsetdbLogin if no value is directly specified by the calling code. PQsetdbLogin if no value is directly specified by the calling code.
These are useful to avoid hard-coding database names into simple These are useful to avoid hard-coding database names into simple
application programs. application programs.
<ItemizedList> <ItemizedList>
<ListItem> <ListItem>
<Para> <Para>
<Acronym>PGHOST</Acronym> sets the default server name. <Acronym>PGHOST</Acronym> sets the default server name.
If a non-zero-length string is specified, TCP/IP communication is used. If a non-zero-length string is specified, TCP/IP communication is used.
Without a host name, libpq will connect using a local Unix domain socket. Without a host name, libpq will connect using a local Unix domain socket.
</Para> </Para>
</ListItem> </ListItem>
<ListItem> <ListItem>
<Para> <Para>
<Acronym>PGPORT</Acronym> sets the default port or local Unix domain socket <Acronym>PGPORT</Acronym> sets the default port or local Unix domain socket
file extension for communicating with the <ProductName>Postgres</ProductName> file extension for communicating with the <ProductName>Postgres</ProductName>
backend. backend.
</Para> </Para>
</ListItem> </ListItem>
<ListItem> <ListItem>
<Para> <Para>
<Acronym>PGDATABASE</Acronym> sets the default <Acronym>PGDATABASE</Acronym> sets the default
<ProductName>Postgres</ProductName> database name. <ProductName>Postgres</ProductName> database name.
</Para> </Para>
</ListItem> </ListItem>
<ListItem> <ListItem>
<Para> <Para>
<Acronym>PGUSER</Acronym> <Acronym>PGUSER</Acronym>
sets the username used to connect to the database and for authentication. sets the username used to connect to the database and for authentication.
</Para> </Para>
</ListItem> </ListItem>
<ListItem> <ListItem>
<Para> <Para>
<Acronym>PGPASSWORD</Acronym> <Acronym>PGPASSWORD</Acronym>
sets the password used if the backend demands password authentication. sets the password used if the backend demands password authentication.
</Para> </Para>
</ListItem> </ListItem>
<ListItem> <ListItem>
<Para> <Para>
<Acronym>PGREALM</Acronym> sets the Kerberos realm to use with <Acronym>PGREALM</Acronym> sets the Kerberos realm to use with
<ProductName>Postgres</ProductName>, <ProductName>Postgres</ProductName>,
if it is different from the local realm. If if it is different from the local realm. If
<Acronym>PGREALM</Acronym> is set, <ProductName>Postgres</ProductName> <Acronym>PGREALM</Acronym> is set, <ProductName>Postgres</ProductName>
applications will attempt applications will attempt
authentication with servers for this realm and use authentication with servers for this realm and use
separate ticket files to avoid conflicts with local separate ticket files to avoid conflicts with local
ticket files. This environment variable is only ticket files. This environment variable is only
used if Kerberos authentication is selected by the backend. used if Kerberos authentication is selected by the backend.
</Para> </Para>
</ListItem> </ListItem>
<ListItem> <ListItem>
<Para> <Para>
<Acronym>PGOPTIONS</Acronym> sets additional runtime options for <Acronym>PGOPTIONS</Acronym> sets additional runtime options for
the <ProductName>Postgres</ProductName> backend. the <ProductName>Postgres</ProductName> backend.
</Para> </Para>
</ListItem> </ListItem>
<ListItem> <ListItem>
<Para> <Para>
<Acronym>PGTTY</Acronym> sets the file or tty on which debugging <Acronym>PGTTY</Acronym> sets the file or tty on which debugging
messages from the backend server are displayed. messages from the backend server are displayed.
</Para> </Para>
</ListItem> </ListItem>
</ItemizedList> </ItemizedList>
</Para> </Para>
<Para> <Para>
The following environment variables can be used to specify user-level default The following environment variables can be used to specify user-level default
behavior for every Postgres session: behavior for every Postgres session:
<ItemizedList> <ItemizedList>
<ListItem> <ListItem>
<Para> <Para>
<Acronym>PGDATESTYLE</Acronym> <Acronym>PGDATESTYLE</Acronym>
sets the default style of date/time representation. sets the default style of date/time representation.
</Para> </Para>
</ListItem> </ListItem>
<ListItem> <ListItem>
<Para> <Para>
<Acronym>PGTZ</Acronym> <Acronym>PGTZ</Acronym>
sets the default time zone. sets the default time zone.
</Para> </Para>
</ListItem> </ListItem>
</ItemizedList> </ItemizedList>
</Para> </Para>
<Para> <Para>
The following environment variables can be used to specify default internal The following environment variables can be used to specify default internal
behavior for every Postgres session: behavior for every Postgres session:
<ItemizedList> <ItemizedList>
<ListItem> <ListItem>
<Para> <Para>
<Acronym>PGGEQO</Acronym> <Acronym>PGGEQO</Acronym>
sets the default mode for the genetic optimizer. sets the default mode for the genetic optimizer.
</Para> </Para>
</ListItem> </ListItem>
<ListItem> <ListItem>
<Para> <Para>
<Acronym>PGRPLANS</Acronym> <Acronym>PGRPLANS</Acronym>
sets the default mode to allow or disable right-sided plans in the optimizer. sets the default mode to allow or disable right-sided plans in the optimizer.
</Para> </Para>
</ListItem> </ListItem>
<ListItem> <ListItem>
<Para> <Para>
<Acronym>PGCOSTHEAP</Acronym> <Acronym>PGCOSTHEAP</Acronym>
sets the default cost for heap searches for the optimizer. sets the default cost for heap searches for the optimizer.
</Para> </Para>
</ListItem> </ListItem>
<ListItem> <ListItem>
<Para> <Para>
<Acronym>PGCOSTINDEX</Acronym> <Acronym>PGCOSTINDEX</Acronym>
sets the default cost for indexed searches for the optimizer. sets the default cost for indexed searches for the optimizer.
</Para> </Para>
</ListItem> </ListItem>
<ListItem> <ListItem>
<Para> <Para>
<Acronym>PGQUERY_LIMIT</Acronym> <Acronym>PGQUERY_LIMIT</Acronym>
sets the maximum number of rows returned by a query. sets the maximum number of rows returned by a query.
</Para> </Para>
</ListItem> </ListItem>
</ItemizedList> </ItemizedList>
</Para> </Para>
<Para> <Para>
Refer to the <command>SET</command> <acronym>SQL</acronym> command Refer to the <command>SET</command> <acronym>SQL</acronym> command
for information on correct values for these environment variables. for information on correct values for these environment variables.
</Para> </Para>
</sect2>
<sect1> </sect1>
<title>Database Connection Functions</title> <sect1>
<title>Database Connection Functions</title>
<para>
<para>
<sect2>
<title>Database Environment Class: <classname>PGenv</classname></title> <sect2>
<title>Database Environment Class: <classname>PGenv</classname></title>
<para>
The database environment class provides C++ objects for manipulating the <para>
above environment variables: The database environment class provides C++ objects for manipulating the
above environment variables:
<itemizedlist>
<listitem> <itemizedlist>
<para> <listitem>
<function>PGenv</function> <para>
creates an environment for the running program. <function>PGenv</function>
creates an environment for the running program.
<synopsis>
PGenv() <synopsis>
PGenv(char* auth, char* host, char* port, char* option, char* tty) PGenv()
</synopsis> PGenv(char* auth, char* host, char* port, char* option, char* tty)
</synopsis>
The first form of this object's constructor sets up the defaults for
the program from the environment variables listed above. The first form of this object's constructor sets up the defaults for
The second allows the programmer to hardcode the values into the program. the program from the environment variables listed above.
The values of the second form relate directly to the environment variables The second allows the programmer to hardcode the values into the program.
above. The values of the second form relate directly to the environment variables
above.
</itemizedlist> </para>
</listitem>
<sect2> </itemizedlist>
<title>Database Class: <classname>PGdatabase</classname></title> </para>
</sect2>
<para>
The database class is a provides C++ objects that have a connection <sect2>
to a backend server. To create such an object one first need <title>Database Class: <classname>PGdatabase</classname></title>
the apropriate environment for the backend to access.
The following constructors deal with making a connection to a backend <para>
server from a C++ program. The database class is a provides C++ objects that have a connection
to a backend server. To create such an object one first need
<itemizedlist> the apropriate environment for the backend to access.
<listitem> The following constructors deal with making a connection to a backend
<para> server from a C++ program.
<function>PGdatabase</function>
makes a new connection to a backend database server. <itemizedlist>
<synopsis> <listitem>
PGdatabase(PGenv *env, char *dbName) <para>
</synopsis> <function>PGdatabase</function>
After a PGdatabase has been created it should be checked to make sure makes a new connection to a backend database server.
the connection to the database succeded before sending <synopsis>
queries to the object. This can easily be done by PGdatabase(PGenv *env, char *dbName)
retrieving the current status of the PGdatabase object with the </synopsis>
<function>status</function> method. After a PGdatabase has been created it should be checked to make sure
the connection to the database succeded before sending
<listitem> queries to the object. This can easily be done by
<para> retrieving the current status of the PGdatabase object with the
<function>status</function> <function>status</function> method.
returns the status of the PGdatabase object. </para>
<synopsis> </listitem>
ConnStatus PGdatabase::status() <listitem>
</synopsis> <para>
<function>status</function>
The following values are allowed: returns the status of the PGdatabase object.
<simplelist> <synopsis>
<member> ConnStatus PGdatabase::status()
CONNECTION_OK </synopsis>
<member>
CONNECTION_BAD The following values are allowed:
</simplelist> <simplelist>
<member>
</itemizedlist> CONNECTION_OK
</member>
<sect1> <member>
<title>Query Execution Functions</title> CONNECTION_BAD
</member>
<para> </simplelist>
<itemizedlist> </para>
<listitem> </listitem>
<para> </itemizedlist>
<function>PGdatabase::exec</function> </para>
submits a query to <productname>Postgres</productname> </sect2>
and returns result status. In case of an error </sect1>
<function>PGdatabase::errormessage</function> <sect1>
can be used to get more information on the error. <title>Query Execution Functions</title>
<synopsis>
void ExecStatusType PGdatabase::exec(char *query); <para>
</synopsis> <itemizedlist>
<listitem>
The following status results can be expected: <para>
<function>PGdatabase::exec</function>
<simplelist> submits a query to <productname>Postgres</productname>
<member> and returns result status. In case of an error
PGRES_EMPTY_QUERY <function>PGdatabase::errormessage</function>
<member> can be used to get more information on the error.
PGRES_COMMAND_OK, if the query was a command <synopsis>
<member> void ExecStatusType PGdatabase::exec(char *query);
PGRES_TUPLES_OK, if the query successfully returned tuples </synopsis>
<member>
PGRES_COPY_OUT The following status results can be expected:
<member>
PGRES_COPY_IN <simplelist>
<member> <member>
PGRES_BAD_RESPONSE, if an unexpected response was received PGRES_EMPTY_QUERY
<member> </member>
PGRES_NONFATAL_ERROR <member>
<member> PGRES_COMMAND_OK, if the query was a command
PGRES_FATAL_ERROR </member>
</simplelist> <member>
PGRES_TUPLES_OK, if the query successfully returned tuples
</itemizedlist> </member>
<member>
<para> PGRES_COPY_OUT
If the result status is PGRES_TUPLES_OK, then the following routines can </member>
be used to retrieve the tuples returned by the query. <member>
PGRES_COPY_IN
<itemizedlist> </member>
<listitem> <member>
<para> PGRES_BAD_RESPONSE, if an unexpected response was received
<function>PGdatabase::ntuples</function> </member>
returns the number of tuples (instances) in the query result. <member>
<synopsis> PGRES_NONFATAL_ERROR
int PGdatabase::ntuples() </member>
</synopsis> <member>
PGRES_FATAL_ERROR
<listitem> </member>
<para> </simplelist>
<function>PGdatabase::nfields</function> </para>
returns the number of fields (attributes) in the query result. </listitem>
<synopsis> </itemizedlist>
int PGdatabase::nfields() </para>
</synopsis> <para>
If the result status is PGRES_TUPLES_OK, then the following routines can
<listitem> be used to retrieve the tuples returned by the query.
<para>
<function>PGdatabase::fieldname</function> <itemizedlist>
returns the field (attribute) name associated with the given field index. <listitem>
Field indices start at zero. <para>
<synopsis> <function>PGdatabase::ntuples</function>
char* PGdatabase::fieldname(int field_index) returns the number of tuples (instances) in the query result.
</synopsis> <synopsis>
int PGdatabase::ntuples()
<listitem> </synopsis>
<para> </para>
<function>PGdatabase::fieldnum</function> </listitem>
returns the field (attribute) index associated with the given field name. <listitem>
<synopsis> <para>
int PGdatabase::fieldnum(char* field_name) <function>PGdatabase::nfields</function>
</synopsis> returns the number of fields (attributes) in the query result.
<synopsis>
<listitem> int PGdatabase::nfields()
<para> </synopsis>
<function>PGdatabase::fieldtype</function> </para>
returns the field type of associated with the given field index or name. </listitem>
The integer returned is an internal coding of the type. Field indices start <listitem>
at zero. <para>
<synopsis> <function>PGdatabase::fieldname</function>
Oid PGdatabase::fieldtype(int field_index) returns the field (attribute) name associated with the given field index.
Oid PGdatabase::fieldtype(char* field_name) Field indices start at zero.
</synopsis> <synopsis>
char* PGdatabase::fieldname(int field_index)
<listitem> </synopsis>
<para> </para>
<function>PGdatabase::fieldsize</function> </listitem>
returns the size in bytes of the field associated with the given field <listitem>
index or name. If the size returned is -1, the field is a variable length <para>
field. Field indices start at zero. <function>PGdatabase::fieldnum</function>
<synopsis> returns the field (attribute) index associated with the given field name.
int2 PGdatabase::fieldsize(int field_index) <synopsis>
int2 PGdatabase::fieldsize(char* field_name) int PGdatabase::fieldnum(char* field_name)
</synopsis> </synopsis>
</para>
<listitem> </listitem>
<para> <listitem>
<function>PGdatabase::getvalue</function> <para>
returns the field (attribute) value. For most queries, the values <function>PGdatabase::fieldtype</function>
returned by returns the field type of associated with the given field index or name.
<function>PGdatabase::getvalue</function> The integer returned is an internal coding of the type. Field indices start
is a null-terminated ASCII string representation at zero.
of the attribute value. If the query was a result of a <synopsis>
<parameter>BINARY</parameter> Oid PGdatabase::fieldtype(int field_index)
cursor, then the values returned by Oid PGdatabase::fieldtype(char* field_name)
<function>PGdatabase::getvalue</function> </synopsis>
is the binary representation of the type in the internal format of the </para>
backend server. It is the programmer's responsibility to cast and </listitem>
convert the data to the correct C++ type. The value return by <listitem>
<function>PGdatabase::getvalue</function> <para>
points to storage that is part of the <classname>PGdatabase</classname> structure. <function>PGdatabase::fieldsize</function>
One must returns the size in bytes of the field associated with the given field
explicitly copy the value into other storage if it is to be used past index or name. If the size returned is -1, the field is a variable length
the next query. field. Field indices start at zero.
<synopsis> <synopsis>
char* PGdatabase::getvalue(int tup_num, int field_index) int2 PGdatabase::fieldsize(int field_index)
char* PGdatabase::getvalue(int tup_num, char* field_name) int2 PGdatabase::fieldsize(char* field_name)
</synopsis> </synopsis>
</para>
<listitem> </listitem>
<para> <listitem>
<function>PGdatabase::getlength</function> <para>
returns the length of a field (attribute) in bytes. If the field <function>PGdatabase::getvalue</function>
is a <literal>struct varlena</literal>, returns the field (attribute) value. For most queries, the values
the length returned here does returned by
<emphasis>not</emphasis> <function>PGdatabase::getvalue</function>
include the size field of the <literal>varlena</literal>, is a null-terminated ASCII string representation
i.e., it is 4 bytes less. of the attribute value. If the query was a result of a
<synopsis> <parameter>BINARY</parameter>
int PGdatabase::getlength(int tup_num, int field_index) cursor, then the values returned by
int PGdatabase::getlength(int tup_num, char* field_name) <function>PGdatabase::getvalue</function>
</synopsis> is the binary representation of the type in the internal format of the
backend server. It is the programmer's responsibility to cast and
<listitem> convert the data to the correct C++ type. The value return by
<para> <function>PGdatabase::getvalue</function>
<function>PGdatabase::printtuples</function> points to storage that is part of the <classname>PGdatabase</classname> structure.
prints out all the tuples and, optionally, the attribute names to the One must
specified output stream. explicitly copy the value into other storage if it is to be used past
<synopsis> the next query.
void PGdatabase::printtuples( <synopsis>
FILE* fout, /* output stream */ char* PGdatabase::getvalue(int tup_num, int field_index)
int printAttName,/* print attribute names or not*/ char* PGdatabase::getvalue(int tup_num, char* field_name)
int terseOutput, /* delimiter bars or not?*/ </synopsis>
int width /* width of column, variable width if 0*/ </para>
); </listitem>
</synopsis> <listitem>
<para>
</itemizedlist> <function>PGdatabase::getlength</function>
returns the length of a field (attribute) in bytes. If the field
<sect1> is a <literal>struct varlena</literal>,
<title>Asynchronous Notification</title> the length returned here does
<emphasis>not</emphasis>
<para> include the size field of the <literal>varlena</literal>,
<productname>Postgres</productname> supports asynchronous notification i.e., it is 4 bytes less.
via the <command>LISTEN</command> and <command>NOTIFY</command> <synopsis>
commands. A backend registers its interest in a particular semaphore int PGdatabase::getlength(int tup_num, int field_index)
with the <command>LISTEN</command> command. int PGdatabase::getlength(int tup_num, char* field_name)
All backends that are listening on a </synopsis>
particular named semaphore will be notified asynchronously when </para>
a <command>NOTIFY</command> of </listitem>
that name is executed by another backend. No additional <listitem>
information is passed from the notifier to the listener. Thus, <para>
typically, any actual data that needs to be communicated is transferred <function>PGdatabase::printtuples</function>
through the relation. prints out all the tuples and, optionally, the attribute names to the
specified output stream.
<note> <synopsis>
<para> void PGdatabase::printtuples(
In the past, the documentation has associated the names used for asyncronous FILE* fout, /* output stream */
notification with relations or classes. However, there is in fact no int printAttName,/* print attribute names or not*/
direct linkage of the two concepts in the implementation, and the int terseOutput, /* delimiter bars or not?*/
named semaphore in fact does not need to have a corresponding relation int width /* width of column, variable width if 0*/
previously defined. );
</note> </synopsis>
</para>
<para> </listitem>
<filename>libpq++</filename> applications are notified whenever a </itemizedlist>
connected backend has </para>
received an asynchronous notification. However, the communication from </sect1>
the backend to the frontend is not asynchronous. <sect1>
The <filename>libpq++</filename> application <title>Asynchronous Notification</title>
must poll the backend to see if there is any pending notification
information. After the execution of a query, a frontend may call <para>
<function>PGdatabase::notifies</function> <productname>Postgres</productname> supports asynchronous notification
to see if any notification data is currently available from the backend. via the <command>LISTEN</command> and <command>NOTIFY</command>
<function>PGdatabase::notifies</function> commands. A backend registers its interest in a particular semaphore
returns the notification from a list of unhandled notifications from the with the <command>LISTEN</command> command.
backend. The function eturns NULL if there is no pending notifications from the All backends that are listening on a
backend. particular named semaphore will be notified asynchronously when
<function>PGdatabase::notifies</function> a <command>NOTIFY</command> of
behaves like the popping of a stack. Once a notification is returned that name is executed by another backend. No additional
from <function>PGdatabase::notifies</function>, information is passed from the notifier to the listener. Thus,
it is considered handled and will be removed from the list of typically, any actual data that needs to be communicated is transferred
notifications. through the relation.
<itemizedlist> <note>
<listitem> <para>
<para> In the past, the documentation has associated the names used for asyncronous
<function>PGdatabase::notifies</function> notification with relations or classes. However, there is in fact no
retrieves pending notifications from the server. direct linkage of the two concepts in the implementation, and the
named semaphore in fact does not need to have a corresponding relation
<synopsis> previously defined.
PGnotify* PGdatabase::notifies() </para>
</synopsis> </note>
</para>
</itemizedlist> <para>
<filename>libpq++</filename> applications are notified whenever a
<para> connected backend has
The second sample program gives an example of the use of asynchronous received an asynchronous notification. However, the communication from
notification. the backend to the frontend is not asynchronous.
The <filename>libpq++</filename> application
<sect1> must poll the backend to see if there is any pending notification
<title>Functions Associated with the COPY Command</title> information. After the execution of a query, a frontend may call
<function>PGdatabase::notifies</function>
<para> to see if any notification data is currently available from the backend.
The <command>copy</command> command in <productname>Postgres</productname> <function>PGdatabase::notifies</function>
has options to read from or write to the network returns the notification from a list of unhandled notifications from the
connection used by <filename>libpq++</filename>. backend. The function eturns NULL if there is no pending notifications from the
Therefore, functions are necessary to backend.
access this network connection directly so applications may take full <function>PGdatabase::notifies</function>
advantage of this capability. behaves like the popping of a stack. Once a notification is returned
from <function>PGdatabase::notifies</function>,
<itemizedlist> it is considered handled and will be removed from the list of
<listitem> notifications.
<para>
<function>PGdatabase::getline</function> <itemizedlist>
reads a newline-terminated line of characters (transmitted by the <listitem>
backend server) into a buffer <para>
<replaceable class="parameter">string</replaceable> <function>PGdatabase::notifies</function>
of size <replaceable class="parameter">length</replaceable>. retrieves pending notifications from the server.
<synopsis>
int PGdatabase::getline(char* string, int length) <synopsis>
</synopsis> PGnotify* PGdatabase::notifies()
</synopsis>
<para> </para>
Like the Unix system routine </listitem>
<function>fgets (3)</function>, </itemizedlist>
this routine copies up to </para>
<literal><replaceable class="parameter">length</replaceable>-1</literal> <para>
characters into The second sample program gives an example of the use of asynchronous
<replaceable class="parameter">string</replaceable>. notification.
It is like </para>
<function>gets (3)</function>, </sect1>
however, in that it converts the terminating newline into a null <sect1>
character. <title>Functions Associated with the COPY Command</title>
<para> <para>
<function>PGdatabase::getline</function> The <command>copy</command> command in <productname>Postgres</productname>
returns EOF at end of file, 0 if the entire line has been read, and 1 if the has options to read from or write to the network
buffer is full but the terminating newline has not yet been read. connection used by <filename>libpq++</filename>.
Therefore, functions are necessary to
<para> access this network connection directly so applications may take full
Notice that the application must check to see if a new line consists advantage of this capability.
of a single period ("."), which indicates that the backend
server has finished sending the results of the <itemizedlist>
<command>copy</command>. <listitem>
Therefore, if the application ever expects to receive lines <para>
that are more than <function>PGdatabase::getline</function>
<literal><replaceable class="parameter">length</replaceable>-1</literal> reads a newline-terminated line of characters (transmitted by the
characters long, the application must be sure to check the return backend server) into a buffer
value of <function>PGdatabase::getline</function> very carefully. <replaceable class="parameter">string</replaceable>
of size <replaceable class="parameter">length</replaceable>.
<listitem> <synopsis>
<para> int PGdatabase::getline(char* string, int length)
<function>PGdatabase::putline</function> </synopsis>
Sends a null-terminated <replaceable class="parameter">string</replaceable> </para>
to the backend server. <para>
<synopsis> Like the Unix system routine
void PGdatabase::putline(char* string) <function>fgets (3)</function>,
</synopsis> this routine copies up to
<literal><replaceable class="parameter">length</replaceable>-1</literal>
<para> characters into
The application must explicitly send a single period character (".") <replaceable class="parameter">string</replaceable>.
to indicate to the backend that it has finished sending its data. It is like
<function>gets (3)</function>,
<listitem> however, in that it converts the terminating newline into a null
<para> character.
<function>PGdatabase::endcopy</function> </para>
syncs with the backend. <para>
<synopsis> <function>PGdatabase::getline</function>
int PGdatabase::endcopy() returns EOF at end of file, 0 if the entire line has been read, and 1 if the
</synopsis> buffer is full but the terminating newline has not yet been read.
This function waits until the backend has </para>
finished processing the <command>copy</command>. <para>
It should either be issued when the Notice that the application must check to see if a new line consists
last string has been sent to the backend using of a single period ("."), which indicates that the backend
<function>PGdatabase::putline</function> server has finished sending the results of the
or when the last string has been received from the backend using <command>copy</command>.
<function>PGdatabase::getline</function>. Therefore, if the application ever expects to receive lines
It must be issued or the backend may get <quote>out of sync</quote> with that are more than
the frontend. Upon return from this function, the backend is ready to <literal><replaceable class="parameter">length</replaceable>-1</literal>
receive the next query. characters long, the application must be sure to check the return
value of <function>PGdatabase::getline</function> very carefully.
<para> </para>
The return value is 0 on successful completion, nonzero otherwise. </listitem>
<listitem>
</itemizedlist> <para>
<function>PGdatabase::putline</function>
<para> Sends a null-terminated <replaceable class="parameter">string</replaceable>
As an example: to the backend server.
<synopsis>
<programlisting> void PGdatabase::putline(char* string)
PGdatabase data; </synopsis>
data.exec("create table foo (a int4, b char16, d float8)"); </para>
data.exec("copy foo from stdin"); <para>
data.putline("3\etHello World\et4.5\en"); The application must explicitly send a single period character (".")
data.putline("4\etGoodbye World\et7.11\en"); to indicate to the backend that it has finished sending its data.
\&... </para>
data.putline(".\en"); </listitem>
data.endcopy(); <listitem>
<para>
</programlisting> <function>PGdatabase::endcopy</function>
syncs with the backend.
<sect1> <synopsis>
<title>Caveats</title> int PGdatabase::endcopy()
</synopsis>
<para> This function waits until the backend has
The query buffer is 8192 bytes long, and queries over that length will finished processing the <command>copy</command>.
be silently truncated. It should either be issued when the
last string has been sent to the backend using
<para> <function>PGdatabase::putline</function>
The <classname>PGlobj</classname> class is largely untested. Use with caution. or when the last string has been received from the backend using
<function>PGdatabase::getline</function>.
It must be issued or the backend may get <quote>out of sync</quote> with
the frontend. Upon return from this function, the backend is ready to
receive the next query.
</para>
<para>
The return value is 0 on successful completion, nonzero otherwise.
</para>
</listitem>
</itemizedlist>
</para>
<para>
As an example:
<programlisting>
PGdatabase data;
data.exec("create table foo (a int4, b char16, d float8)");
data.exec("copy foo from stdin");
data.putline("3\etHello World\et4.5\en");
data.putline("4\etGoodbye World\et7.11\en");
\&...
data.putline(".\en");
data.endcopy();
</programlisting>
</para>
</sect1>
<sect1>
<title>Caveats</title>
<para>
The query buffer is 8192 bytes long, and queries over that length will
be silently truncated.
</para>
<para>
The <classname>PGlobj</classname> class is largely untested. Use with caution.
</para>
</sect1>
</chapter> </chapter>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment