Class: Sequel::Postgres::Database

Inherits:
Database show all
Includes:
DatabaseMethods
Defined in:
lib/sequel/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::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::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_ROLLBACK, Database::UNDERSCORE, Database::UNIQUE, Database::UNSIGNED

Instance Attribute Summary

Attributes inherited from Database

#default_schema, #loggers, #opts, #pool, #prepared_statements

Instance Method Summary collapse

Methods included from DatabaseMethods

#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, #serial_primary_key_options, #server_version, #supports_savepoints?, #table_exists?, #tables

Methods inherited from Database

#<<, #[], adapter_class, adapter_scheme, #add_column, #add_index, #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, #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=, #inspect, #literal, #log_info, #logger=, #query, quote_identifiers=, #quote_identifiers=, #quote_identifiers?, #rename_column, #rename_table, #run, #schema, #select, #serial_primary_key_options, #set_column_default, #set_column_type, single_threaded=, #single_threaded?, #supports_savepoints?, #synchronize, #table_exists?, #test_connection, #transaction, #typecast_value, #uri, #url

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.


182
183
184
185
186
# File 'lib/sequel/lib/sequel/adapters/postgres.rb', line 182

def initialize(*args)
  super
  @primary_keys = {}
  @primary_key_sequences = {}
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.


191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/sequel/lib/sequel/adapters/postgres.rb', line 191

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
  conn
end

#dataset(opts = nil) ⇒ Object

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


214
215
216
# File 'lib/sequel/lib/sequel/adapters/postgres.rb', line 214

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.


219
220
221
222
223
224
225
226
227
228
# File 'lib/sequel/lib/sequel/adapters/postgres.rb', line 219

def execute(sql, opts={}, &block)
  return execute_prepared_statement(sql, opts, &block) if Symbol === sql
  begin
    log_info(sql, opts[:arguments])
    synchronize(opts[:server]){|conn| conn.execute(sql, opts[:arguments], &block)}
  rescue => e
    log_info(e.message)
    raise_error(e, :classes=>CONVERTED_EXCEPTIONS)
  end
end

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

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


232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/sequel/lib/sequel/adapters/postgres.rb', line 232

def execute_insert(sql, opts={})
  return execute(sql, opts) if Symbol === sql
  begin 
    log_info(sql, opts[:arguments])
    synchronize(opts[:server]) do |conn|
      conn.execute(sql, opts[:arguments])
      insert_result(conn, opts[:table], opts[:values])
    end
  rescue => e
    log_info(e.message)
    raise_error(e, :classes=>CONVERTED_EXCEPTIONS)
  end
end