class Sequel::Postgres::Database

Database class for PostgreSQL databases used with Sequel and the pg, postgres, or postgres-pr driver.

Constants

DatasetClass
INFINITE_DATETIME_VALUES
INFINITE_TIMESTAMP_STRINGS

Attributes

convert_infinite_timestamps[R]

Whether infinite timestamps/dates should be converted on retrieval. By default, no conversion is done, so an error is raised if you attempt to retrieve an infinite timestamp/date. You can set this to :nil to convert to nil, :string to leave as a string, or :float to convert to an infinite float.

Public Instance Methods

bound_variable_arg(arg, conn) click to toggle source

Convert given argument so that it can be used directly by pg. Currently, pg doesn't handle fractional seconds in Time/DateTime or blobs with “0”, and it won't ever handle Sequel::SQLTime values correctly. Only public for use by the adapter, shouldn't be used by external code.

# File lib/sequel/adapters/postgres.rb, line 204
def bound_variable_arg(arg, conn)
  case arg
  when Sequel::SQL::Blob
    {:value=>arg, :type=>17, :format=>1}
  when Sequel::SQLTime
    literal(arg)
  when DateTime, Time
    literal(arg)
  else
    arg
  end
end
connect(server) click to toggle source

Connects to the database. In addition to the standard database options, using the :encoding or :charset option changes the client encoding for the connection, :connect_timeout is a connection timeout in seconds, :sslmode sets whether postgres's sslmode, and :notice_receiver handles server notices in a proc. :connect_timeout, :ssl_mode, and :notice_receiver are only supported if the pg driver is used.

# File lib/sequel/adapters/postgres.rb, line 224
def connect(server)
  opts = server_opts(server)
  if SEQUEL_POSTGRES_USES_PG
    connection_params = {
      :host => opts[:host],
      :port => opts[:port] || 5432,
      :dbname => opts[:database],
      :user => opts[:user],
      :password => opts[:password],
      :connect_timeout => opts[:connect_timeout] || 20,
      :sslmode => opts[:sslmode]
    }.delete_if { |key, value| blank_object?(value) }
    conn = Adapter.connect(connection_params)

    conn.instance_variable_set(:@prepared_statements, {})

    if receiver = opts[:notice_receiver]
      conn.set_notice_receiver(&receiver)
    end
  else
    conn = Adapter.connect(
      (opts[:host] unless blank_object?(opts[:host])),
      opts[:port] || 5432,
      nil, '',
      opts[:database],
      opts[:user],
      opts[:password]
    )
  end

  conn.instance_variable_set(:@db, self)

  if encoding = opts[:encoding] || opts[:charset]
    if conn.respond_to?(:set_client_encoding)
      conn.set_client_encoding(encoding)
    else
      conn.async_exec("set client_encoding to '#{encoding}'")
    end
  end

  connection_configuration_sqls.each{|sql| conn.execute(sql)}
  conn
end
convert_infinite_timestamps=(v) click to toggle source

Set whether to allow infinite timestamps/dates. Make sure the conversion proc for date reflects that setting.

# File lib/sequel/adapters/postgres.rb, line 270
def convert_infinite_timestamps=(v)
  @convert_infinite_timestamps = case v
  when Symbol
    v
  when 'nil'
    :nil
  when 'string'
    :string
  when 'float'
    :float
  when String
    typecast_value_boolean(v)
  else
    false
  end

  pr = old_pr = @use_iso_date_format ? TYPE_TRANSLATOR.method(:date) : Sequel.method(:string_to_date)
  if v
    pr = lambda do |val|
      case val
      when *INFINITE_TIMESTAMP_STRINGS
        infinite_timestamp_value(val)
      else
        old_pr.call(val)
      end
    end
  end
  conversion_procs[1082] = pr
end
copy_into(table, opts=OPTS) click to toggle source

copy_into uses PostgreSQL's +COPY FROM STDIN+ SQL statement to do very fast inserts into a table using input preformatting in either CSV or PostgreSQL text format. This method is only supported if pg 0.14.0+ 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+ with a filename, you should just use run instead of this method.

The following options are respected:

:columns

The columns to insert into, with the same order as the columns in the input data. If this isn't given, uses all columns in the table.

:data

The data to copy to PostgreSQL, which should already be in CSV or PostgreSQL text format. This can be either a string, or any object that responds to each and yields string.

: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 and :data option is not, this will yield to the block repeatedly. The block should return a string, or nil to signal that it is finished.

# File lib/sequel/adapters/postgres.rb, line 403
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|
    conn.execute(copy_into_sql(table, opts))
    begin
      if block_given?
        while buf = yield
          conn.put_copy_data(buf)
        end
      else
        data.each{|buff| conn.put_copy_data(buff)}
      end
    rescue Exception => e
      conn.put_copy_end("ruby exception occurred while copying data into PostgreSQL")
    ensure
      conn.put_copy_end unless e
      while res = conn.get_result
        raise e if e
        check_database_errors{res.check}
      end
    end
  end 
end
copy_table(table, opts=OPTS) { |buf| ... } click to toggle source

copy_table uses PostgreSQL's +COPY TO STDOUT+ 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 TO+ with a filename, you should just use run instead of this method.

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.

# File lib/sequel/adapters/postgres.rb, line 363
def copy_table(table, opts=OPTS)
  synchronize(opts[:server]) do |conn|
    conn.execute(copy_table_sql(table, opts))
    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
disconnect_connection(conn) click to toggle source

Disconnect given connection

# File lib/sequel/adapters/postgres.rb, line 301
def disconnect_connection(conn)
  begin
    conn.finish
  rescue PGError, IOError
  end
end
error_info(e) click to toggle source

Return a hash of information about the related PGError (or Sequel::DatabaseError that wraps a PGError), with the following entries:

:schema

The schema name related to the error

:table

The table name related to the error

:column

the column name related to the error

:constraint

The constraint name related to the error

:type

The datatype name related to the error

This requires a PostgreSQL 9.3+ server and 9.3+ client library, and ruby-pg 0.16.0+ to be supported.

# File lib/sequel/adapters/postgres.rb, line 320
def error_info(e)
  e = e.wrapped_exception if e.is_a?(DatabaseError)
  r = e.result
  h = {}
  h[:schema] = r.error_field(::PG::PG_DIAG_SCHEMA_NAME)
  h[:table] = r.error_field(::PG::PG_DIAG_TABLE_NAME)
  h[:column] = r.error_field(::PG::PG_DIAG_COLUMN_NAME)
  h[:constraint] = r.error_field(::PG::PG_DIAG_CONSTRAINT_NAME)
  h[:type] = r.error_field(::PG::PG_DIAG_DATATYPE_NAME)
  h
end
execute(sql, opts=OPTS, &block) click to toggle source

Execute the given SQL with the given args on an available connection.

# File lib/sequel/adapters/postgres.rb, line 334
def execute(sql, opts=OPTS, &block)
  synchronize(opts[:server]){|conn| check_database_errors{_execute(conn, sql, opts, &block)}}
end
listen(channels, opts=OPTS, &block) click to toggle source

Listens on the given channel (or multiple channels if channel is an array), waiting for notifications. After a notification is received, or the timeout has passed, stops listening to the channel. Options:

:after_listen

An object that responds to call that is called with the underlying connection after the LISTEN statement is sent, but before the connection starts waiting for notifications.

:loop

Whether to continually wait for notifications, instead of just waiting for a single notification. If this option is given, a block must be provided. If this object responds to call, it is called with the underlying connection after each notification is received (after the block is called). If a :timeout option is used, and a callable object is given, the object will also be called if the timeout expires. If :loop is used and you want to stop listening, you can either break from inside the block given to listen, or you can throw :stop from inside the :loop object's call method or the block.

:server

The server on which to listen, if the sharding support is being used.

:timeout

How long to wait for a notification, in seconds (can provide a float value for fractional seconds). If not given or nil, waits indefinitely.

This method is only supported if pg is used as the underlying ruby driver. It returns the channel the notification was sent to (as a string), unless :loop was used, in which case it returns nil. If a block is given, it is yielded 3 arguments:

  • the channel the notification was sent to (as a string)

  • the backend pid of the notifier (as an integer),

  • and the payload of the notification (as a string or nil).

# File lib/sequel/adapters/postgres.rb, line 456
def listen(channels, opts=OPTS, &block)
  check_database_errors do
    synchronize(opts[:server]) do |conn|
      begin
        channels = Array(channels)
        channels.each do |channel|
          sql = "LISTEN "
          dataset.send(:identifier_append, sql, channel)
          conn.execute(sql)
        end
        opts[:after_listen].call(conn) if opts[:after_listen]
        timeout = opts[:timeout] ? [opts[:timeout]] : []
        if l = opts[:loop]
          raise Error, 'calling #listen with :loop requires a block' unless block
          loop_call = l.respond_to?(:call)
          catch(:stop) do
            loop do
              conn.wait_for_notify(*timeout, &block)
              l.call(conn) if loop_call
            end
          end
          nil
        else
          conn.wait_for_notify(*timeout, &block)
        end
      ensure
        conn.execute("UNLISTEN *")
      end
    end
  end
end
to_application_timestamp(value) click to toggle source

If #convert_infinite_timestamps is true and the value is infinite, return an appropriate value based on the #convert_infinite_timestamps setting.

# File lib/sequel/adapters/postgres.rb, line 491
def to_application_timestamp(value)
  if convert_infinite_timestamps
    case value
    when *INFINITE_TIMESTAMP_STRINGS
      infinite_timestamp_value(value)
    else
      super
    end
  else
    super
  end
end

Private Instance Methods

_execute(conn, sql, opts, &block) click to toggle source

Execute the given SQL string or prepared statement on the connection object.

# File lib/sequel/adapters/postgres.rb, line 507
def _execute(conn, sql, opts, &block)
  if sql.is_a?(Symbol)
    execute_prepared_statement(conn, sql, opts, &block)
  else
    conn.execute(sql, opts[:arguments], &block)
  end
end
_execute_prepared_statement(conn, ps_name, args, opts) click to toggle source

Execute the prepared statement name with the given arguments on the connection.

# File lib/sequel/adapters/postgres.rb, line 516
def _execute_prepared_statement(conn, ps_name, args, opts)
  conn.exec_prepared(ps_name, args)
end
adapter_initialize() 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.

# File lib/sequel/adapters/postgres.rb, line 522
def adapter_initialize
  @use_iso_date_format = typecast_value_boolean(@opts.fetch(:use_iso_date_format, Postgres.use_iso_date_format))
  initialize_postgres_adapter
  conversion_procs[1082] = TYPE_TRANSLATOR.method(:date) if @use_iso_date_format
  self.convert_infinite_timestamps = @opts[:convert_infinite_timestamps]
end
check_database_errors() { || ... } click to toggle source

Convert exceptions raised from the block into DatabaseErrors.

# File lib/sequel/adapters/postgres.rb, line 530
def check_database_errors
  begin
    yield
  rescue => e
    raise_error(e, :classes=>CONVERTED_EXCEPTIONS)
  end
end
connection_configuration_sqls() click to toggle source

Set the DateStyle to ISO if configured, for faster date parsing.

# File lib/sequel/adapters/postgres.rb, line 539
def connection_configuration_sqls
  sqls = super
  sqls << "SET DateStyle = 'ISO'" if @use_iso_date_format
  sqls
end
database_error_classes() click to toggle source
# File lib/sequel/adapters/postgres.rb, line 545
def database_error_classes
  [PGError]
end
database_exception_sqlstate(exception, opts) click to toggle source
# File lib/sequel/adapters/postgres.rb, line 549
def database_exception_sqlstate(exception, opts)
  if exception.respond_to?(:result) && (result = exception.result)
    result.error_field(::PGresult::PG_DIAG_SQLSTATE)
  end
end
execute_prepared_statement(conn, name, opts=OPTS) { |q| ... } click to toggle source

Execute the prepared statement with the given name on an available connection, using the given args. If the connection has not prepared a statement with the given name yet, prepare it. If the connection has prepared a statement with the same name and different SQL, deallocate that statement first and then prepare this statement. If a block is given, yield the result, otherwise, return the number of rows changed.

# File lib/sequel/adapters/postgres.rb, line 562
def execute_prepared_statement(conn, name, opts=OPTS, &block)
  ps = prepared_statement(name)
  sql = ps.prepared_sql
  ps_name = name.to_s

  if args = opts[:arguments]
    args = args.map{|arg| bound_variable_arg(arg, conn)}
  end

  unless conn.prepared_statements[ps_name] == sql
    conn.execute("DEALLOCATE #{ps_name}") if conn.prepared_statements.include?(ps_name)
    conn.check_disconnect_errors{log_yield("PREPARE #{ps_name} AS #{sql}"){conn.prepare(ps_name, sql)}}
    conn.prepared_statements[ps_name] = sql
  end

  log_sql = "EXECUTE #{ps_name}"
  if ps.log_sql
    log_sql << " ("
    log_sql << sql
    log_sql << ")"
  end

  q = conn.check_disconnect_errors{log_yield(log_sql, args){_execute_prepared_statement(conn, ps_name, args, opts)}}
  begin
    block_given? ? yield(q) : q.cmd_tuples
  ensure
    q.clear if q && q.respond_to?(:clear)
  end
end
infinite_timestamp_value(value) click to toggle source

Return an appropriate value for the given infinite timestamp string.

# File lib/sequel/adapters/postgres.rb, line 593
def infinite_timestamp_value(value)
  case convert_infinite_timestamps
  when :nil
    nil
  when :string
    value
  else
    value == 'infinity' ? PLUS_INFINITY : MINUS_INFINITY
  end
end
log_connection_execute(conn, sql) click to toggle source

Don't log, since logging is done by the underlying connection.

# File lib/sequel/adapters/postgres.rb, line 605
def log_connection_execute(conn, sql)
  conn.execute(sql)
end
typecast_value_date(value) click to toggle source

If the value is an infinite value (either an infinite float or a string returned by by PostgreSQL for an infinite timestamp), return it without converting it if #convert_infinite_timestamps is set.

Calls superclass method Sequel::Database#typecast_value_date
# File lib/sequel/adapters/postgres.rb, line 612
def typecast_value_date(value)
  if convert_infinite_timestamps
    case value
    when *INFINITE_DATETIME_VALUES
      value
    else
      super
    end
  else
    super
  end
end
typecast_value_datetime(value) click to toggle source

If the value is an infinite value (either an infinite float or a string returned by by PostgreSQL for an infinite timestamp), return it without converting it if #convert_infinite_timestamps is set.

# File lib/sequel/adapters/postgres.rb, line 628
def typecast_value_datetime(value)
  if convert_infinite_timestamps
    case value
    when *INFINITE_DATETIME_VALUES
      value
    else
      super
    end
  else
    super
  end
end