<HTML ><HEAD ><TITLE >Functions Associated with the COPY Command</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.64 "><LINK REV="MADE" HREF="mailto:pgsql-docs@postgresql.org"><LINK REL="HOME" TITLE="libpq++ - C++ Binding Library" HREF="index.html"><LINK REL="PREVIOUS" TITLE="Asynchronous Notification" HREF="libpqpp-notify.html"><LINK REL="STYLESHEET" TYPE="text/css" HREF="stylesheet.css"><META NAME="creation" CONTENT="2002-11-27T04:23:11"></HEAD ><BODY CLASS="SECT1" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" ><SPAN CLASS="APPLICATION" >libpq++</SPAN > - C++ Binding Library</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="libpqpp-notify.html" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" > </TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="SECT1" ><H1 CLASS="SECT1" ><A NAME="libpqpp-copy" >1.7. Functions Associated with the COPY Command</A ></H1 > <P > The <TT CLASS="COMMAND" >COPY</TT > command in <SPAN CLASS="PRODUCTNAME" >PostgreSQL</SPAN > has options to read from or write to the network connection used by <SPAN CLASS="APPLICATION" >libpq++</SPAN >. Therefore, functions are necessary to access this network connection directly so applications may take full advantage of this capability. <P ></P ></P><UL ><LI > <P > <TT CLASS="FUNCTION" >PgDatabase::GetLine</TT > reads a newline-terminated line of characters (transmitted by the backend server) into a buffer <TT CLASS="REPLACEABLE" ><I >string</I ></TT > of size <TT CLASS="REPLACEABLE" ><I >length</I ></TT >. </P><PRE CLASS="SYNOPSIS" >int PgDatabase::GetLine(char* string, int length)</PRE ><P> </P > <P > Like the Unix system routine <TT CLASS="FUNCTION" >fgets()</TT >, this routine copies up to <TT CLASS="LITERAL" ><TT CLASS="REPLACEABLE" ><I >length</I ></TT >-1</TT > characters into <TT CLASS="REPLACEABLE" ><I >string</I ></TT >. It is like <TT CLASS="FUNCTION" >gets()</TT >, however, in that it converts the terminating newline into a zero byte. </P > <P > <TT CLASS="FUNCTION" >PgDatabase::GetLine</TT > returns <TT CLASS="SYMBOL" >EOF</TT > at end of file, 0 if the entire line has been read, and 1 if the buffer is full but the terminating newline has not yet been read. </P > <P > Notice that the application must check to see if a new line consists of a backslash followed by a period (<TT CLASS="LITERAL" >\.</TT >), which indicates that the backend server has finished sending the results of the <TT CLASS="COMMAND" >COPY</TT >. Therefore, if the application ever expects to receive lines that are more than <TT CLASS="LITERAL" ><TT CLASS="REPLACEABLE" ><I >length</I ></TT >-1</TT > characters long, the application must be sure to check the return value of <TT CLASS="FUNCTION" >PgDatabase::GetLine</TT > very carefully. </P > </LI ><LI > <P > <TT CLASS="FUNCTION" >PgDatabase::PutLine</TT > Sends a null-terminated <TT CLASS="REPLACEABLE" ><I >string</I ></TT > to the backend server. </P><PRE CLASS="SYNOPSIS" >void PgDatabase::PutLine(char* string)</PRE ><P> </P > <P > The application must explicitly send the characters <TT CLASS="LITERAL" >\.</TT > to indicate to the backend that it has finished sending its data. </P > </LI ><LI > <P > <TT CLASS="FUNCTION" >PgDatabase::EndCopy</TT > synchronizes with the backend. </P><PRE CLASS="SYNOPSIS" >int PgDatabase::EndCopy()</PRE ><P> This function waits until the backend has finished processing the <TT CLASS="COMMAND" >COPY</TT >. It should either be issued when the last string has been sent to the backend using <TT CLASS="FUNCTION" >PgDatabase::PutLine</TT > or when the last string has been received from the backend using <TT CLASS="FUNCTION" >PgDatabase::GetLine</TT >. It must be issued or the backend may get <SPAN CLASS="QUOTE" >"out of sync"</SPAN > with the frontend. Upon return from this function, the backend is ready to receive the next command. </P > <P > The return value is 0 on successful completion, nonzero otherwise. </P > </LI ></UL ><P> </P > <P > As an example: </P><PRE CLASS="PROGRAMLISTING" >PgDatabase data; data.Exec("CREATE TABLE foo (a int4, b char(16), d double precision)"); data.Exec("COPY foo FROM STDIN"); data.PutLine("3\tHello World\t4.5\n"); data.PutLine("4\tGoodbye World\t7.11\n"); ... data.PutLine("\\.\n"); data.EndCopy();</PRE ><P> </P > </DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="libpqpp-notify.html" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" > </TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >Asynchronous Notification</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" > </TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" > </TD ></TR ></TABLE ></DIV ></BODY ></HTML >