class PG::Connection
def copy_data( sql, coder=nil )
"some,csv,data,to,copy\n"
This prints all rows of +my_table+ to stdout:
end
end
p row
while row=conn.get_copy_data
conn.copy_data "COPY my_table TO STDOUT CSV" do
Example with CSV output format:
This creates +my_table+ and inserts two rows.
end
conn.put_copy_data "more,csv,data,to,copy\n"
conn.put_copy_data "some,csv,data,to,copy\n"
conn.copy_data "COPY my_table FROM STDOUT CSV" do
conn.exec "create table my_table (a text,b text,c text,d text,e text)"
Example with CSV input format:
of #put_copy_data, #get_copy_data and #put_copy_end.
of blocking mode of operation, #copy_data is preferred to raw calls
in case of client side or server side failures. Therefore, in case
This method ensures, that the copy process is properly terminated
connection while the COPY operation is in progress.)
(It is not possible to execute other SQL commands using the same
At this point further SQL commands can be issued via #exec.
so it isn't required to make use of any of them.
is complete. An exception is raised if some problem was encountered,
#copy_data returns another PG::Result object when the data transfer
when finished.
to receive or transmit data rows and should return from the block
The application should then use #put_copy_data or #get_copy_data
PGRES_COPY_IN (depending on the specified copy direction).
is passed to the block, bearing a status code of PGRES_COPY_OUT or
(if there is no error in the command) is a PG::Result object that
This issues the SQL COPY command via #exec. The response to this
Execute a copy process for transfering data to or from the server.
conn.copy_data( sql ) {|sql_result| ... } -> PG::Result
call-seq:
def copy_data( sql, coder=nil ) res = exec( sql ) case res.result_status when PGRES_COPY_IN begin if coder old_coder = self.encoder_for_put_copy_data self.encoder_for_put_copy_data = coder end yield res rescue Exception => err errmsg = "%s while copy data: %s" % [ err.class.name, err.message ] put_copy_end( errmsg ) get_result raise else put_copy_end get_last_result ensure self.encoder_for_put_copy_data = old_coder if coder end when PGRES_COPY_OUT begin if coder old_coder = self.decoder_for_get_copy_data self.decoder_for_get_copy_data = coder end yield res rescue Exception => err cancel while get_copy_data end while get_result end raise else res = get_last_result if res.result_status != PGRES_COMMAND_OK while get_copy_data end while get_result end raise PG::NotAllCopyDataRetrieved, "Not all COPY data retrieved" end res ensure self.decoder_for_get_copy_data = old_coder if coder end else raise ArgumentError, "SQL command is no COPY statement: #{sql}" end end