Class: Sequel::Postgres::Database

Inherits:
Database show all
Includes:
DatabaseMethods
Defined in:
lib/sequel/adapters/postgres.rb

Overview

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

Constant Summary

Constants included from DatabaseMethods

Sequel::Postgres::DatabaseMethods::EXCLUDE_SCHEMAS, Sequel::Postgres::DatabaseMethods::PREPARED_ARG_PLACEHOLDER, Sequel::Postgres::DatabaseMethods::RE_CURRVAL_ERROR, Sequel::Postgres::DatabaseMethods::SYSTEM_TABLE_REGEXP

Constants inherited from Database

Database::ADAPTERS, Database::AUTOINCREMENT, Database::CASCADE, Database::COLUMN_DEFINITION_ORDER, Database::COMMA_SEPARATOR, Database::MSSQL_DEFAULT_RE, Database::MYSQL_TIMESTAMP_RE, Database::NOT_NULL, Database::NO_ACTION, Database::NULL, Database::POSTGRES_DEFAULT_RE, Database::PRIMARY_KEY, Database::RESTRICT, Database::SET_DEFAULT, Database::SET_NULL, Database::SQL_BEGIN, Database::SQL_COMMIT, Database::SQL_RELEASE_SAVEPOINT, Database::SQL_ROLLBACK, Database::SQL_ROLLBACK_TO_SAVEPOINT, Database::SQL_SAVEPOINT, Database::STRING_DEFAULT_RE, Database::TEMPORARY, Database::TRANSACTION_BEGIN, Database::TRANSACTION_COMMIT, Database::TRANSACTION_ISOLATION_LEVELS, Database::TRANSACTION_ROLLBACK, Database::UNDERSCORE, Database::UNIQUE, Database::UNSIGNED

Instance Attribute Summary collapse

Attributes inherited from Database

#default_schema, #log_warn_duration, #loggers, #opts, #pool, #prepared_statements, #sql_log_level, #transaction_isolation_level

Instance Method Summary collapse

Methods included from DatabaseMethods

#commit_prepared_transaction, #create_function, #create_language, #create_trigger, #database_type, #drop_function, #drop_language, #drop_table, #drop_trigger, #indexes, #locks, #primary_key, #primary_key_sequence, #reset_primary_key_sequence, #rollback_prepared_transaction, #serial_primary_key_options, #server_version, #supports_prepared_transactions?, #supports_savepoints?, #supports_transaction_isolation_levels?, #tables, #views

Methods inherited from Database

#<<, #[], adapter_class, #adapter_scheme, adapter_scheme, #add_column, #add_index, #add_servers, #alter_table, #call, #cast_type_literal, connect, #create_or_replace_view, #create_table, #create_table!, #create_table?, #create_view, #database_type, #disconnect, #drop_column, #drop_index, #drop_table, #drop_view, #dump_indexes_migration, #dump_schema_migration, #dump_table_schema, #each_server, #execute_ddl, #execute_dui, #fetch, #from, #get, identifier_input_method, #identifier_input_method, #identifier_input_method=, identifier_input_method=, identifier_output_method, #identifier_output_method, #identifier_output_method=, identifier_output_method=, #indexes, #inspect, #literal, #log_info, #log_yield, #logger=, #query, #quote_identifiers=, quote_identifiers=, #quote_identifiers?, #remove_servers, #rename_column, #rename_table, #run, #schema, #select, #serial_primary_key_options, #servers, #set_column_default, #set_column_type, single_threaded=, #single_threaded?, #supports_create_table_if_not_exists?, #supports_prepared_transactions?, #supports_savepoints?, #supports_transaction_isolation_levels?, #synchronize, #table_exists?, #tables, #test_connection, #transaction, #typecast_value, #uri, #url, #views

Methods included from Metaprogramming

#meta_def

Constructor Details

#initialize(*args) ⇒ Database

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



215
216
217
218
219
# File 'lib/sequel/adapters/postgres.rb', line 215

def initialize(*args)
  super
  @primary_keys = {}
  @primary_key_sequences = {}
end

Instance Attribute Details

#conversion_procsObject (readonly)

A hash of conversion procs, keyed by type integer (oid) and having callable values for the conversion proc for that type.



211
212
213
# File 'lib/sequel/adapters/postgres.rb', line 211

def conversion_procs
  @conversion_procs
end

Instance Method Details

#connect(server) ⇒ Object

Connects to the database. In addition to the standard database options, using the :encoding or :charset option changes the client encoding for the connection.



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/sequel/adapters/postgres.rb', line 224

def connect(server)
  opts = server_opts(server)
  conn = Adapter.connect(
    (opts[:host] unless blank_object?(opts[:host])),
    opts[:port] || 5432,
    nil, '',
    opts[:database],
    opts[:user],
    opts[:password]
  )
  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
  conn.db = self
  conn.apply_connection_settings
  @conversion_procs ||= get_conversion_procs(conn)
  conn
end

#dataset(opts = nil) ⇒ Object

Return instance of Sequel::Postgres::Dataset with the given options.



248
249
250
# File 'lib/sequel/adapters/postgres.rb', line 248

def dataset(opts = nil)
  Postgres::Dataset.new(self, opts)
end

#execute(sql, opts = {}, &block) ⇒ Object

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



253
254
255
256
257
258
# File 'lib/sequel/adapters/postgres.rb', line 253

def execute(sql, opts={}, &block)
  check_database_errors do
    return execute_prepared_statement(sql, opts, &block) if Symbol === sql
    synchronize(opts[:server]){|conn| conn.execute(sql, opts[:arguments], &block)}
  end
end

#execute_insert(sql, opts = {}) ⇒ Object

Insert the values into the table and return the primary key (if automatically generated).



262
263
264
265
266
267
268
269
270
# File 'lib/sequel/adapters/postgres.rb', line 262

def execute_insert(sql, opts={})
  return execute(sql, opts) if Symbol === sql
  check_database_errors do
    synchronize(opts[:server]) do |conn|
      conn.execute(sql, opts[:arguments])
      insert_result(conn, opts[:table], opts[:values])
    end
  end
end