module Sequel::JDBC::Postgres::DatabaseMethods

  1. lib/sequel/adapters/jdbc/postgresql.rb
Parent: Postgres

Methods to add to Database instances that access PostgreSQL via JDBC.

Methods

Public Class

  1. extended

Public Instance

  1. copy_into
  2. copy_table

Public Class methods

extended (db)

Add the primary_keys and primary_key_sequences instance variables, so we can get the correct return values for inserted rows.

[show source]
# File lib/sequel/adapters/jdbc/postgresql.rb, line 18
def self.extended(db)
  super
  db.send(:initialize_postgres_adapter)
end

Public Instance methods

copy_into (table, opts=OPTS)

See Sequel::Postgres::Adapter#copy_into

[show source]
# File lib/sequel/adapters/jdbc/postgresql.rb, line 24
def copy_into(table, opts=OPTS)
  data = opts[:data]
  data = Array(data) if data.is_a?(String)

  if block_given? && data
    raise Error, "Cannot provide both a :data option and a block to copy_into"
  elsif !block_given? && !data
    raise Error, "Must provide either a :data option or a block to copy_into"
  end

  synchronize(opts) do |conn|
    begin
      copy_manager = org.postgresql.copy.CopyManager.new(conn)
      copier = copy_manager.copy_in(copy_into_sql(table, opts))
      if block_given?
        while buf = yield
          copier.writeToCopy(buf.to_java_bytes, 0, buf.length)
        end
      else
        data.each { |d| copier.writeToCopy(d.to_java_bytes, 0, d.length) }
      end
    rescue Exception => e
      copier.cancelCopy
      raise
    ensure
      unless e
        begin
          copier.endCopy
        rescue NativeException => e2
          raise_error(e2)
        end
      end
    end
  end
end
copy_table (table, opts=OPTS)

See Sequel::Postgres::Adapter#copy_table

[show source]
# File lib/sequel/adapters/jdbc/postgresql.rb, line 61
def copy_table(table, opts=OPTS)
  synchronize(opts[:server]) do |conn|
    copy_manager = org.postgresql.copy.CopyManager.new(conn)
    copier = copy_manager.copy_out(copy_table_sql(table, opts))
    begin
      if block_given?
        while buf = copier.readFromCopy
          yield(String.from_java_bytes(buf))
        end
        nil
      else
        b = ''
        while buf = copier.readFromCopy
          b << String.from_java_bytes(buf)
        end
        b
      end
    ensure
      raise DatabaseDisconnectError, "disconnecting as a partial COPY may leave the connection in an unusable state" if buf
    end
  end
end