Class: Sequel::Postgres::Database

Inherits:
Database show all
Includes:
DatabaseMethods
Defined in:
lib/sequel_core/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::SQL_BEGIN, Sequel::Postgres::DatabaseMethods::SQL_COMMIT, Sequel::Postgres::DatabaseMethods::SQL_RELEASE_SAVEPOINT, Sequel::Postgres::DatabaseMethods::SQL_ROLLBACK, Sequel::Postgres::DatabaseMethods::SQL_ROLLBACK_TO_SAVEPOINT, Sequel::Postgres::DatabaseMethods::SQL_SAVEPOINT, Sequel::Postgres::DatabaseMethods::SYSTEM_TABLE_REGEXP

Constants inherited from Database

Database::ADAPTERS, Database::SQL_BEGIN, Database::SQL_COMMIT, Database::SQL_ROLLBACK

Constants included from Schema::SQL

Schema::SQL::AUTOINCREMENT, Schema::SQL::CASCADE, Schema::SQL::COMMA_SEPARATOR, Schema::SQL::NOT_NULL, Schema::SQL::NO_ACTION, Schema::SQL::NULL, Schema::SQL::PRIMARY_KEY, Schema::SQL::RESTRICT, Schema::SQL::SET_DEFAULT, Schema::SQL::SET_NULL, Schema::SQL::TYPES, Schema::SQL::UNDERSCORE, Schema::SQL::UNIQUE, Schema::SQL::UNSIGNED

Instance Attribute Summary

Attributes inherited from Database

#default_schema, #loggers, #opts, #pool, #prepared_statements, #quote_identifiers, #upcase_identifiers

Instance Method Summary collapse

Methods included from DatabaseMethods

#create_function, #create_function_sql, #create_language, #create_language_sql, #create_trigger, #create_trigger_sql, #default_schema, #drop_function, #drop_function_sql, #drop_language, #drop_language_sql, #drop_table, #drop_table_sql, #drop_trigger, #drop_trigger_sql, #index_definition_sql, #locks, #primary_key, #rename_table_sql, #serial_primary_key_options, #server_version, #table_exists?, #tables, #transaction

Methods inherited from Database

#<<, #[], adapter_class, adapter_scheme, #add_column, #add_index, #alter_table, #call, connect, #create_or_replace_view, #create_table, #create_table!, #create_view, #disconnect, #drop_column, #drop_index, #drop_table, #drop_view, #execute_ddl, #execute_dui, #fetch, #from, #get, #inspect, #log_info, #logger, #logger=, #multi_threaded?, #query, quote_identifiers=, #quote_identifiers?, #rename_column, #rename_table, #select, #serial_primary_key_options, #set_column_default, #set_column_type, single_threaded=, #single_threaded?, #synchronize, #table_exists?, #test_connection, #transaction, #typecast_value, upcase_identifiers=, #upcase_identifiers?, #uri

Methods included from Schema::SQL

#alter_table_sql, #alter_table_sql_list, #auto_increment_sql, #column_definition_sql, #column_list_sql, #column_references_sql, #constraint_definition_sql, #create_table_sql_list, #default_index_name, #drop_index_sql, #drop_table_sql, #filter_expr, #index_definition_sql, #index_list_sql_list, #literal, #on_delete_clause, #quote_identifier, #quote_schema_table, #rename_table_sql, #schema, #schema_utility_dataset

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.



198
199
200
201
202
# File 'lib/sequel_core/adapters/postgres.rb', line 198

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.



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/sequel_core/adapters/postgres.rb', line 207

def connect(server)
  opts = server_opts(server)
  conn = Adapter.connect(
    (opts[:host] unless opts[:host].blank?),
    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.



230
231
232
# File 'lib/sequel_core/adapters/postgres.rb', line 230

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.



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

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).



248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/sequel_core/adapters/postgres.rb', line 248

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