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 inherited from Database

Database::ADAPTERS, Database::AUTOINCREMENT, Database::CASCADE, Database::COMMA_SEPARATOR, 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 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, #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.



177
178
179
180
181
# File 'lib/sequel/adapters/postgres.rb', line 177

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.



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/sequel/adapters/postgres.rb', line 186

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.



209
210
211
# File 'lib/sequel/adapters/postgres.rb', line 209

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.



214
215
216
217
218
219
220
221
222
223
# File 'lib/sequel/adapters/postgres.rb', line 214

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



227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/sequel/adapters/postgres.rb', line 227

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