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::RELATION_FILTER, Sequel::Postgres::DatabaseMethods::RELATION_QUERY, 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

#loggers, #opts, #pool, #prepared_statements, #quote_identifiers

Instance Method Summary collapse

Methods included from DatabaseMethods

#drop_table_sql, #index_definition_sql, #insert_result, #locks, #primary_key_for_table, #serial_primary_key_options, #server_version, #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, #drop_column, #drop_index, #drop_table, #drop_view, #execute_ddl, #execute_dui, #fetch, #from, #get, #initialize, #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, #uri

Methods included from Schema::SQL

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

Constructor Details

This class inherits a constructor from Sequel::Database

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.



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/sequel_core/adapters/postgres.rb', line 168

def connect(server)
  opts = server_opts(server)
  conn = Adapter.connect(
    opts[:host] || 'localhost',
    opts[:port] || 5432,
    '', '',
    opts[:database],
    opts[:user],
    opts[:password]
  )
  if encoding = opts[:encoding] || opts[:charset]
    conn.set_client_encoding(encoding)
  end
  conn
end

#dataset(opts = nil) ⇒ Object

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



185
186
187
# File 'lib/sequel_core/adapters/postgres.rb', line 185

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

#disconnectObject

Disconnect all active connections.



190
191
192
# File 'lib/sequel_core/adapters/postgres.rb', line 190

def disconnect
  @pool.disconnect {|c| c.finish}
end

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

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



195
196
197
198
199
200
201
202
203
204
# File 'lib/sequel_core/adapters/postgres.rb', line 195

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 convert_pgerror(e)
  end
end

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

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



208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/sequel_core/adapters/postgres.rb', line 208

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 convert_pgerror(e)
  end
end