module Sequel::JDBC::Postgres::DatabaseMethods

Constants

DATABASE_ERROR_CLASSES

Public Class Methods

extended(db) click to toggle source

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

Calls superclass method
# File lib/sequel/adapters/jdbc/postgresql.rb, line 39
def self.extended(db)
  super
  db.send(:initialize_postgres_adapter)
end

Public Instance Methods

add_conversion_proc(oid, *) click to toggle source

Remove any current entry for the oid in the oid_convertor_map.

# File lib/sequel/adapters/jdbc/postgresql.rb, line 45
def add_conversion_proc(oid, *)
  super
  Sequel.synchronize{@oid_convertor_map.delete(oid)}
end
copy_into(table, opts=OPTS) click to toggle source

See Sequel::Postgres::Adapter#copy_into

# File lib/sequel/adapters/jdbc/postgresql.rb, line 51
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[:server]) 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
          java_bytes = buf.to_java_bytes
          copier.writeToCopy(java_bytes, 0, java_bytes.length)
        end
      else
        data.each do |d|
          java_bytes = d.to_java_bytes
          copier.writeToCopy(java_bytes, 0, java_bytes.length)
        end
      end
    rescue Exception => e
      copier.cancelCopy if copier
      raise
    ensure
      unless e
        begin
          copier.endCopy
        rescue NativeException => e2
          raise_error(e2)
        end
      end
    end
  end
end
copy_table(table, opts=OPTS) { |from_java_bytes| ... } click to toggle source

See Sequel::Postgres::Adapter#copy_table

# File lib/sequel/adapters/jdbc/postgresql.rb, line 92
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 = String.new
        while buf = copier.readFromCopy
          b << String.from_java_bytes(buf)
        end
        b
      end
    rescue => e
      raise_error(e, :disconnect=>true)
    ensure
      if buf && !e
        raise DatabaseDisconnectError, "disconnecting as a partial COPY may leave the connection in an unusable state"
      end
    end
  end
end
oid_convertor_proc(oid) click to toggle source
# File lib/sequel/adapters/jdbc/postgresql.rb, line 119
def oid_convertor_proc(oid)
  if (conv = Sequel.synchronize{@oid_convertor_map[oid]}).nil?
    conv = if pr = conversion_procs[oid]
      lambda do |r, i|
        if v = r.getString(i)
          pr.call(v)
        end
      end
    else
      false
    end
    Sequel.synchronize{@oid_convertor_map[oid] = conv}
  end
  conv
end

Private Instance Methods

bound_variable_arg(arg, conn) click to toggle source

For PostgreSQL-specific types, return the string that should be used as the PGObject value. Returns nil by default, loading pg_* extensions will override this to add support for specific types.

# File lib/sequel/adapters/jdbc/postgresql.rb, line 149
def bound_variable_arg(arg, conn)
  nil
end
database_error_classes() click to toggle source
# File lib/sequel/adapters/jdbc/postgresql.rb, line 138
def database_error_classes
  DATABASE_ERROR_CLASSES
end
disconnect_error?(exception, opts) click to toggle source
Calls superclass method
# File lib/sequel/adapters/jdbc/postgresql.rb, line 142
def disconnect_error?(exception, opts)
  super || exception.message =~ /\A(This connection has been closed\.|FATAL: terminating connection due to administrator command|An I\/O error occurred while sending to the backend\.)\z/
end
prepare_jdbc_statement(conn, sql, opts) click to toggle source

Work around issue when using Sequel's bound variable support where the same SQL is used in different bound variable calls, but the schema has changed between the calls. This is necessary as jdbc-postgres versions after 9.4.1200 violate the JDBC API. These versions cache separate PreparedStatement instances, which are eventually prepared server side after the prepareThreshold is met. The JDBC API violation is that PreparedStatement#close does not release the server side prepared statement.

Calls superclass method
# File lib/sequel/adapters/jdbc/postgresql.rb, line 160
def prepare_jdbc_statement(conn, sql, opts)
  ps = super
  unless opts[:name]
    ps.prepare_threshold = 0
  end
  ps
end
set_ps_arg(cps, arg, i) click to toggle source

If the given argument is a recognized PostgreSQL-specific type, create a PGObject instance with unknown type and the bound argument string value, and set that as the prepared statement argument.

Calls superclass method
# File lib/sequel/adapters/jdbc/postgresql.rb, line 171
def set_ps_arg(cps, arg, i)
  if v = bound_variable_arg(arg, nil)
    obj = org.postgresql.util.PGobject.new
    obj.setType("unknown")
    obj.setValue(v)
    cps.setObject(i, obj)
  else
    super
  end
end
set_ps_arg_nil(cps, i) click to toggle source

Use setNull for nil arguments as the default behavior of setString with nil doesn't appear to work correctly on PostgreSQL.

# File lib/sequel/adapters/jdbc/postgresql.rb, line 184
def set_ps_arg_nil(cps, i)
  cps.setNull(i, JavaSQL::Types::NULL)
end
setup_connection(conn) click to toggle source

Execute the connection configuration SQL queries on the connection.

Calls superclass method
# File lib/sequel/adapters/jdbc/postgresql.rb, line 189
def setup_connection(conn)
  conn = super(conn)
  statement(conn) do |stmt|
    connection_configuration_sqls.each{|sql| log_connection_yield(sql, conn){stmt.execute(sql)}}
  end
  conn
end
setup_type_convertor_map() click to toggle source
Calls superclass method
# File lib/sequel/adapters/jdbc/postgresql.rb, line 197
def setup_type_convertor_map
  super
  @oid_convertor_map = {}
end