Method: Sequel::Postgres::Database#copy_table

Defined in:
lib/sequel/adapters/postgres.rb

#copy_table(table, opts = {}) ⇒ Object

copy_table uses PostgreSQL’s COPY SQL statement to return formatted results directly to the caller. This method is only supported if pg is the underlying ruby driver. This method should only be called if you want results returned to the client. If you are using COPY FROM or COPY TO with a filename, you should just use run instead of this method. This method does not currently support COPY FROM STDIN, but that may be supported in the future.

The table argument supports the following types:

String

Uses the first argument directly as literal SQL. If you are using a version of PostgreSQL before 9.0, you will probably want to use a string if you are using any options at all, as the syntax Sequel uses for options is only compatible with PostgreSQL 9.0+.

Dataset

Uses a query instead of a table name when copying.

other

Uses a table name (usually a symbol) when copying.

The following options are respected:

:format

The format to use. text is the default, so this should be :csv or :binary.

:options

An options SQL string to use, which should contain comma separated options.

:server

The server on which to run the query.

If a block is provided, the method continually yields to the block, one yield per row. If a block is not provided, a single string is returned with all of the data.



330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# File 'lib/sequel/adapters/postgres.rb', line 330

def copy_table(table, opts={})
  sql = if table.is_a?(String)
    sql = table
  else
    if opts[:options] || opts[:format]
      options = " ("
      options << "FORMAT #{opts[:format]}" if opts[:format]
      options << "#{', ' if opts[:format]}#{opts[:options]}" if opts[:options]
      options << ')'
    end
    table = if table.is_a?(::Sequel::Dataset)
      "(#{table.sql})"
    else
      literal(table)
    end
   sql = "COPY #{table} TO STDOUT#{options}"
  end
  synchronize(opts[:server]) do |conn| 
    conn.execute(sql)
    begin
      if block_given?
        while buf = conn.get_copy_data
          yield buf
        end
        nil
      else
        b = ''
        b << buf while buf = conn.get_copy_data
        b
      end
    ensure
      raise DatabaseDisconnectError, "disconnecting as a partial COPY may leave the connection in an unusable state" if buf
    end
  end 
end