Class: Sequel::Postgres::Database
- 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 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
Instance Method Summary collapse
-
#connect(server) ⇒ Object
Connects to the database.
-
#dataset(opts = nil) ⇒ Object
Return instance of Sequel::Postgres::Dataset with the given options.
-
#execute(sql, opts = {}, &block) ⇒ Object
Execute the given SQL with the given args on an available connection.
-
#execute_insert(sql, opts = {}) ⇒ Object
Insert the values into the table and return the primary key (if automatically generated).
-
#initialize(*args) ⇒ Database
constructor
Add the primary_keys and primary_key_sequences instance variables, so we can get the correct return values for inserted rows.
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, #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, #log_info, #logger, #logger=, #multi_threaded?, #query, quote_identifiers=, #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=, #upcase_identifiers?, #uri, #url
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.
186 187 188 189 190 |
# File 'lib/sequel_core/adapters/postgres.rb', line 186 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.
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/sequel_core/adapters/postgres.rb', line 195 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.
218 219 220 |
# File 'lib/sequel_core/adapters/postgres.rb', line 218 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.
223 224 225 226 227 228 229 230 231 232 |
# File 'lib/sequel_core/adapters/postgres.rb', line 223 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.) 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).
236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/sequel_core/adapters/postgres.rb', line 236 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.) raise_error(e, :classes=>CONVERTED_EXCEPTIONS) end end |