A Database object represents a virtual connection to a database. The Database class is meant to be subclassed by database adapters in order to provide the functionality needed for executing queries.
This methods generally execute SQL code on the database server.
Whether the schema should be cached for this database. True by default for performance, can be set to false to always issue a database query to get the schema.
The prepared statement object hash for this database, keyed by name symbol
The default transaction isolation level for this database, used for all future transactions. For MSSQL, this should be set to something if you ever plan to use the :isolation option to #transaction, as on MSSQL if affects all future transactions on the same connection.
Runs the supplied SQL statement string on the database server. Returns self so it can be safely chained:
DB << "UPDATE albums SET artist_id = NULL" << "DROP TABLE artists"
# File lib/sequel/database/query.rb, line 48 def <<(sql) run(sql) self end
Call the prepared statement with the given name with the given hash of arguments.
DB[:items].filter(:id=>1).prepare(:first, :sa) DB.call(:sa) # SELECT * FROM items WHERE id = 1
# File lib/sequel/database/query.rb, line 58 def call(ps_name, hash={}, &block) prepared_statement(ps_name).call(hash, &block) end
Executes the given SQL on the database. This method should be overridden in descendants. This method should not be called directly by user code.
# File lib/sequel/database/query.rb, line 64 def execute(sql, opts={}) raise NotImplemented, "#execute should be overridden by adapters" end
Method that should be used when submitting any DDL (Data Definition
Language) SQL, such as create_table
.
By default, calls execute_dui
. This method should not be
called directly by user code.
# File lib/sequel/database/query.rb, line 71 def execute_ddl(sql, opts={}, &block) execute_dui(sql, opts, &block) end
Method that should be used when issuing a DELETE, UPDATE, or INSERT statement. By default, calls execute. This method should not be called directly by user code.
# File lib/sequel/database/query.rb, line 78 def execute_dui(sql, opts={}, &block) execute(sql, opts, &block) end
Method that should be used when issuing a INSERT statement. By default, calls execute_dui. This method should not be called directly by user code.
# File lib/sequel/database/query.rb, line 85 def execute_insert(sql, opts={}, &block) execute_dui(sql, opts, &block) end
Returns an array of hashes containing foreign key information from the table. Each hash will contain at least the following fields:
An array of columns in the given table
The table referenced by the columns
An array of columns referenced (in the table specified by :table), but can be nil on certain adapters if the primary key is referenced.
The hash may also contain entries for:
Whether the constraint is deferrable
The name of the constraint
The action to take ON DELETE
The action to take ON UPDATE
# File lib/sequel/database/query.rb, line 103 def foreign_key_list(table, opts={}) raise NotImplemented, "#foreign_key_list should be overridden by adapters" end
Returns a single value from the database, e.g.:
DB.get(1) # SELECT 1 # => 1 DB.get{server_version{}} # SELECT server_version()
# File lib/sequel/database/query.rb, line 112 def get(*args, &block) dataset.get(*args, &block) end
Return a hash containing index information for the table. Hash keys are index name symbols. Values are subhashes with two keys, :columns and :unique. The value of :columns is an array of symbols of column names. The value of :unique is true or false depending on if the index is unique.
Should not include the primary key index, functional indexes, or partial indexes.
DB.indexes(:artists) # => {:artists_name_ukey=>{:columns=>[:name], :unique=>true}}
# File lib/sequel/database/query.rb, line 125 def indexes(table, opts={}) raise NotImplemented, "#indexes should be overridden by adapters" end
Returns the schema for the given table as an array with all members being arrays of length 2, the first member being the column name, and the second member being a hash of column information. The table argument can also be a dataset, as long as it only has one table. Available options are:
Ignore any cached results, and get fresh information from the database.
An explicit schema to use. It may also be implicitly provided via the table name.
If schema parsing is supported by the database, the column information should hash at least contain the following entries:
Whether NULL is an allowed value for the column.
The database type for the column, as a database specific string.
The database default for the column, as a database specific string.
Whether the columns is a primary key column. If this column is not present, it means that primary key information is unavailable, not that the column is not a primary key.
The database default for the column, as a ruby object. In many cases, complex database defaults cannot be parsed into ruby objects, in which case nil will be used as the value.
A symbol specifying the type, such as :integer or :string.
Example:
DB.schema(:artists) # [[:id, # {:type=>:integer, # :primary_key=>true, # :default=>"nextval('artist_id_seq'::regclass)", # :ruby_default=>nil, # :db_type=>"integer", # :allow_null=>false}], # [:name, # {:type=>:string, # :primary_key=>false, # :default=>nil, # :ruby_default=>nil, # :db_type=>"text", # :allow_null=>false}]]
# File lib/sequel/database/query.rb, line 179 def schema(table, opts={}) raise(Error, 'schema parsing is not implemented on this database') unless respond_to?(:schema_parse_table, true) opts = opts.dup if table.is_a?(Dataset) o = table.opts from = o[:from] raise(Error, "can only parse the schema for a dataset with a single from table") unless from && from.length == 1 && !o.include?(:join) && !o.include?(:sql) tab = table.first_source_table sch, table_name = schema_and_table(tab) quoted_name = table.literal(tab) opts[:dataset] = table else sch, table_name = schema_and_table(table) quoted_name = quote_schema_table(table) end opts[:schema] = sch if sch && !opts.include?(:schema) Sequel.synchronize{@schemas.delete(quoted_name)} if opts[:reload] return Sequel.synchronize{@schemas[quoted_name]} if @schemas[quoted_name] cols = schema_parse_table(table_name, opts) raise(Error, 'schema parsing returned no columns, table probably doesn\t exist') if cols.nil? || cols.empty? cols.each{|_,c| c[:ruby_default] = column_schema_to_ruby_default(c[:default], c[:type])} Sequel.synchronize{@schemas[quoted_name] = cols} if cache_schema cols end
Returns true if a table with the given name exists. This requires a query to the database.
DB.table_exists?(:foo) # => false # SELECT NULL FROM foo LIMIT 1
Note that since this does a SELECT from the table, it can give false negatives if you don't have permission to SELECT from the table.
# File lib/sequel/database/query.rb, line 215 def table_exists?(name) sch, table_name = schema_and_table(name) name = SQL::QualifiedIdentifier.new(sch, table_name) if sch _table_exists?(from(name)) true rescue DatabaseError false end
Return all tables in the database as an array of symbols.
DB.tables # => [:albums, :artists]
# File lib/sequel/database/query.rb, line 227 def tables(opts={}) raise NotImplemented, "#tables should be overridden by adapters" end
Starts a database transaction. When a database transaction is used, either all statements are successful or none of the statements are successful. Note that MySQL MyISAM tabels do not support transactions.
The following general options are respected:
The transaction isolation level to use for this transaction, should be :uncommitted, :committed, :repeatable, or :serializable, used if given and the database/adapter supports customizable transaction isolation levels.
A string to use as the transaction identifier for a prepared transaction (two-phase commit), if the database/adapter supports prepared transactions.
Can the set to :reraise to reraise any Sequel::Rollback exceptions raised, or :always to always rollback even if no exceptions occur (useful for testing).
The server to use for the transaction.
Whether to create a new savepoint for this transaction, only respected if the database/adapter supports savepoints. By default Sequel will reuse an existing transaction, so if you want to use a savepoint you must use this option.
PostgreSQL specific options:
(9.1+) If present, set to DEFERRABLE if true or NOT DEFERRABLE if false.
If present, set to READ ONLY if true or READ WRITE if false.
if non-nil, set synchronous_commit appropriately. Valid values true, :on, false, :off, :local (9.1+), and :remote_write (9.2+).
# File lib/sequel/database/query.rb, line 260 def transaction(opts={}, &block) synchronize(opts[:server]) do |conn| return yield(conn) if already_in_transaction?(conn, opts) _transaction(conn, opts, &block) end end
Return all views in the database as an array of symbols.
DB.views # => [:gold_albums, :artists_with_many_albums]
# File lib/sequel/database/query.rb, line 270 def views(opts={}) raise NotImplemented, "#views should be overridden by adapters" end
These methods execute code on the database that modifies the database's schema.
The order of column modifiers to use when defining a column.
The default options for join table columns.
Adds a column to the specified table. This method expects a column name, a datatype and optionally a hash with additional constraints and options:
DB.add_column :items, :name, :text, :unique => true, :null => false DB.add_column :items, :category, :text, :default => 'ruby'
See alter_table
.
# File lib/sequel/database/schema_methods.rb, line 31 def add_column(table, *args) alter_table(table) {add_column(*args)} end
Adds an index to a table for the given columns:
DB.add_index :posts, :title DB.add_index :posts, [:author, :title], :unique => true
Options:
Ignore any DatabaseErrors that are raised
See alter_table
.
# File lib/sequel/database/schema_methods.rb, line 44 def add_index(table, columns, options={}) e = options[:ignore_errors] begin alter_table(table){add_index(columns, options)} rescue DatabaseError raise unless e end end
Alters the given table with the specified block. Example:
DB.alter_table :items do add_column :category, :text, :default => 'ruby' drop_column :category rename_column :cntr, :counter set_column_type :value, :float set_column_default :value, :float add_index [:group, :category] drop_index [:group, :category] end
Note that add_column
accepts all the options available for
column definitions using create_table
, and
add_index
accepts all the options available for index
definition.
See Schema::AlterTableGenerator
and the "Migrations and Schema
Modification" guide.
# File lib/sequel/database/schema_methods.rb, line 70 def alter_table(name, generator=nil, &block) generator ||= Schema::AlterTableGenerator.new(self, &block) remove_cached_schema(name) apply_alter_table(name, generator.operations) nil end
Create a join table using a hash of foreign keys to referenced table names. Example:
create_join_table(:cat_id=>:cats, :dog_id=>:dogs) # CREATE TABLE cats_dogs ( # cat_id integer NOT NULL REFERENCES cats, # dog_id integer NOT NULL REFERENCES dogs, # PRIMARY KEY (cat_id, dog_id) # ) # CREATE INDEX cats_dogs_dog_id_cat_id_index ON cats_dogs(dog_id, cat_id)
The primary key and index are used so that almost all operations on the table can benefit from one of the two indexes, and the primary key ensures that entries in the table are unique, which is the typical desire for a join table.
You can provide column options by making the values in the hash be option hashes, so long as the option hashes have a :table entry giving the table referenced:
create_join_table(:cat_id=>{:table=>:cats, :type=>Bignum}, :dog_id=>:dogs)
You can provide a second argument which is a table options hash:
create_join_table({:cat_id=>:cats, :dog_id=>:dogs}, :temp=>true)
Some table options are handled specially:
The options to pass to the index
The name of the table to create
Set to true not to create the second index.
Set to true to not create the primary key.
# File lib/sequel/database/schema_methods.rb, line 109 def create_join_table(hash, options={}) keys = hash.keys.sort_by{|k| k.to_s} create_table(join_table_name(hash, options), options) do keys.each do |key| v = hash[key] unless v.is_a?(Hash) v = {:table=>v} end v = DEFAULT_JOIN_TABLE_COLUMN_OPTIONS.merge(v) foreign_key(key, v) end primary_key(keys) unless options[:no_primary_key] index(keys.reverse, options[:index_options] || {}) unless options[:no_index] end end
Creates a view, replacing it if it already exists:
DB.create_or_replace_view(:cheap_items, "SELECT * FROM items WHERE price < 100") DB.create_or_replace_view(:ruby_items, DB[:items].filter(:category => 'ruby'))
# File lib/sequel/database/schema_methods.rb, line 189 def create_or_replace_view(name, source) source = source.sql if source.is_a?(Dataset) execute_ddl("CREATE OR REPLACE VIEW #{quote_schema_table(name)} AS #{source}") remove_cached_schema(name) nil end
Creates a table with the columns given in the provided block:
DB.create_table :posts do primary_key :id column :title, :text String :content index :title end
General options:
Create the table using the value, which should be either a dataset or a literal SQL string. If this option is used, a block should not be given to the method.
Ignore any errors when creating indexes.
Create the table as a temporary table.
MySQL specific options:
The character set to use for the table.
The collation to use for the table.
The table engine to use for the table.
See Schema::Generator
and the "Schema Modification"
guide.
# File lib/sequel/database/schema_methods.rb, line 147 def create_table(name, options={}, &block) remove_cached_schema(name) options = {:generator=>options} if options.is_a?(Schema::Generator) if sql = options[:as] raise(Error, "can't provide both :as option and block to create_table") if block create_table_as(name, sql, options) else generator = options[:generator] || Schema::Generator.new(self, &block) create_table_from_generator(name, generator, options) create_table_indexes_from_generator(name, generator, options) nil end end
Forcibly create a table, attempting to drop it if it already exists, then creating it.
DB.create_table!(:a){Integer :a} # SELECT NULL FROM a LIMIT 1 -- check existence # DROP TABLE a -- drop table if already exists # CREATE TABLE a (a integer)
# File lib/sequel/database/schema_methods.rb, line 167 def create_table!(name, options={}, &block) drop_table?(name) create_table(name, options, &block) end
Creates the table unless the table already exists.
DB.create_table?(:a){Integer :a} # SELECT NULL FROM a LIMIT 1 -- check existence # CREATE TABLE a (a integer) -- if it doesn't already exist
# File lib/sequel/database/schema_methods.rb, line 177 def create_table?(name, options={}, &block) if supports_create_table_if_not_exists? create_table(name, options.merge(:if_not_exists=>true), &block) elsif !table_exists?(name) create_table(name, options, &block) end end
Creates a view based on a dataset or an SQL string:
DB.create_view(:cheap_items, "SELECT * FROM items WHERE price < 100") DB.create_view(:ruby_items, DB[:items].filter(:category => 'ruby'))
# File lib/sequel/database/schema_methods.rb, line 200 def create_view(name, source) source = source.sql if source.is_a?(Dataset) execute_ddl("CREATE VIEW #{quote_schema_table(name)} AS #{source}") end
Removes a column from the specified table:
DB.drop_column :items, :category
See alter_table
.
# File lib/sequel/database/schema_methods.rb, line 210 def drop_column(table, *args) alter_table(table) {drop_column(*args)} end
Removes an index for the given table and column/s:
DB.drop_index :posts, :title DB.drop_index :posts, [:author, :title]
See alter_table
.
# File lib/sequel/database/schema_methods.rb, line 220 def drop_index(table, columns, options={}) alter_table(table){drop_index(columns, options)} end
Drop the join table that would have been created with the same arguments to #create_join_table:
drop_join_table(:cat_id=>:cats, :dog_id=>:dogs) # DROP TABLE cats_dogs
# File lib/sequel/database/schema_methods.rb, line 229 def drop_join_table(hash, options={}) drop_table(join_table_name(hash, options), options) end
Drops one or more tables corresponding to the given names:
DB.drop_table(:posts) # DROP TABLE posts DB.drop_table(:posts, :comments) DB.drop_table(:posts, :comments, :cascade=>true)
# File lib/sequel/database/schema_methods.rb, line 238 def drop_table(*names) options = names.last.is_a?(Hash) ? names.pop : {} names.each do |n| execute_ddl(drop_table_sql(n, options)) remove_cached_schema(n) end nil end
Drops the table if it already exists. If it doesn't exist, does nothing.
DB.drop_table?(:a) # SELECT NULL FROM a LIMIT 1 -- check existence # DROP TABLE a -- if it already exists
# File lib/sequel/database/schema_methods.rb, line 253 def drop_table?(*names) options = names.last.is_a?(Hash) ? names.pop : {} if supports_drop_table_if_exists? options = options.merge(:if_exists=>true) names.each do |name| drop_table(name, options) end else names.each do |name| drop_table(name, options) if table_exists?(name) end end end
Drops one or more views corresponding to the given names:
DB.drop_view(:cheap_items) DB.drop_view(:cheap_items, :pricey_items) DB.drop_view(:cheap_items, :pricey_items, :cascade=>true)
# File lib/sequel/database/schema_methods.rb, line 272 def drop_view(*names) options = names.last.is_a?(Hash) ? names.pop : {} names.each do |n| execute_ddl(drop_view_sql(n, options)) remove_cached_schema(n) end nil end
Renames a column in the specified table. This method expects the current column name and the new column name:
DB.rename_column :items, :cntr, :counter
See alter_table
.
# File lib/sequel/database/schema_methods.rb, line 298 def rename_column(table, *args) alter_table(table) {rename_column(*args)} end
Renames a table:
DB.tables #=> [:items] DB.rename_table :items, :old_items DB.tables #=> [:old_items]
# File lib/sequel/database/schema_methods.rb, line 286 def rename_table(name, new_name) execute_ddl(rename_table_sql(name, new_name)) remove_cached_schema(name) nil end
Sets the default value for the given column in the given table:
DB.set_column_default :items, :category, 'perl!'
See alter_table
.
# File lib/sequel/database/schema_methods.rb, line 307 def set_column_default(table, *args) alter_table(table) {set_column_default(*args)} end
Set the data type for the given column in the given table:
DB.set_column_type :items, :price, :float
See alter_table
.
# File lib/sequel/database/schema_methods.rb, line 316 def set_column_type(table, *args) alter_table(table) {set_column_type(*args)} end
These methods all return instances of this database's dataset class.
Returns a dataset for the database. If the first argument is a string, the method acts as an alias for #fetch, returning a dataset for arbitrary SQL, with or without placeholders:
DB['SELECT * FROM items'].all DB['SELECT * FROM items WHERE name = ?', my_name].all
Otherwise, acts as an alias for #from, setting the primary table for the dataset:
DB[:items].sql #=> "SELECT * FROM items"
# File lib/sequel/database/dataset.rb, line 19 def [](*args) (String === args.first) ? fetch(*args) : from(*args) end
Returns a blank dataset for this database.
DB.dataset # SELECT * DB.dataset.from(:items) # SELECT * FROM items
# File lib/sequel/database/dataset.rb, line 27 def dataset(opts=nil) @dataset_class.new(self, opts) end
Dump foreign key constraints for all tables as a migration. This complements the :foreign_keys=>false option to dump_schema_migration. This only dumps the constraints (not the columns) using alter_table/add_foreign_key with an array of columns.
Note that the migration this produces does not have a down block, so you cannot reverse it.
# File lib/sequel/extensions/schema_dumper.rb, line 16 def dump_foreign_key_migration(options={}) ts = tables(options) "Sequel.migration do up do #{ts.sort_by{|t| t.to_s}.map{|t| dump_table_foreign_keys(t)}.reject{|x| x == ''}.join("\n\n").gsub(/^/o, ' ')} end end " end
Dump indexes for all tables as a migration. This complements the :indexes=>false option to dump_schema_migration. Options:
:same_db - Create a dump for the same database type, so don't ignore errors if the index statements fail.
:index_names - If set to false, don't record names of indexes. If set to :namespace, prepend the table name to the index name if the database does not use a global index namespace.
# File lib/sequel/extensions/schema_dumper.rb, line 34 def dump_indexes_migration(options={}) ts = tables(options) "Sequel.migration do up do #{ts.sort_by{|t| t.to_s}.map{|t| dump_table_indexes(t, :add_index, options)}.reject{|x| x == ''}.join("\n\n").gsub(/^/o, ' ')} end down do #{ts.sort_by{|t| t.to_s}.reverse.map{|t| dump_table_indexes(t, :drop_index, options)}.reject{|x| x == ''}.join("\n\n").gsub(/^/o, ' ')} end end " end
Dump the cached schema to the filename given in Marshal format.
# File lib/sequel/extensions/schema_caching.rb, line 52 def dump_schema_cache(file) File.open(file, 'wb'){|f| f.write(Marshal.dump(@schemas))} nil end
Dump the cached schema to the filename given unless the file already exists.
# File lib/sequel/extensions/schema_caching.rb, line 59 def dump_schema_cache?(file) dump_schema_cache(file) unless File.exist?(file) end
Return a string that contains a Sequel::Migration subclass that when run would recreate the database structure. Options:
:same_db - Don't attempt to translate database types to ruby types. If this isn't set to true, all database types will be translated to ruby types, but there is no guarantee that the migration generated will yield the same type. Without this set, types that aren't recognized will be translated to a string-like type.
:foreign_keys - If set to false, don't dump foreign_keys
:indexes - If set to false, don't dump indexes (they can be added later via dump_index_migration).
:index_names - If set to false, don't record names of indexes. If set to :namespace, prepend the table name to the index name.
# File lib/sequel/extensions/schema_dumper.rb, line 61 def dump_schema_migration(options={}) options = options.dup if options[:indexes] == false && !options.has_key?(:foreign_keys) # Unless foreign_keys option is specifically set, disable if indexes # are disabled, as foreign keys that point to non-primary keys rely # on unique indexes being created first options[:foreign_keys] = false end ts = sort_dumped_tables(tables(options), options) skipped_fks = if sfk = options[:skipped_foreign_keys] # Handle skipped foreign keys by adding them at the end via # alter_table/add_foreign_key. Note that skipped foreign keys # probably result in a broken down migration. sfka = sfk.sort_by{|table, fks| table.to_s}.map{|table, fks| dump_add_fk_constraints(table, fks.values)} sfka.join("\n\n").gsub(%r^/, ' ') unless sfka.empty? end "Sequel.migration do up do #{ts.map{|t| dump_table_schema(t, options)}.join("\n\n").gsub(/^/o, ' ')}#{"\n \n" if skipped_fks}#{skipped_fks} end down do drop_table(#{ts.reverse.inspect[1...-1]}) end end " end
Return a string with a create table block that will recreate the given table's schema. Takes the same options as dump_schema_migration.
# File lib/sequel/extensions/schema_dumper.rb, line 94 def dump_table_schema(table, options={}) table = table.value.to_s if table.is_a?(SQL::Identifier) gen = dump_table_generator(table, options) commands = [gen.dump_columns, gen.dump_constraints, gen.dump_indexes].reject{|x| x == ''}.join("\n\n") "create_table(#{table.inspect}#{', :ignore_index_errors=>true' if !options[:same_db] && options[:indexes] != false && !gen.indexes.empty?}) do\n#{commands.gsub(/^/o, ' ')}\nend" end
Fetches records for an arbitrary SQL statement. If a block is given, it is used to iterate over the records:
DB.fetch('SELECT * FROM items'){|r| p r}
The fetch
method returns a dataset instance:
DB.fetch('SELECT * FROM items').all
fetch
can also perform parameterized queries for protection
against SQL injection:
DB.fetch('SELECT * FROM items WHERE name = ?', my_name).all
# File lib/sequel/database/dataset.rb, line 44 def fetch(sql, *args, &block) ds = dataset.with_sql(sql, *args) ds.each(&block) if block ds end
Returns a new dataset with the from
method invoked. If a block
is given, it is used as a filter on the dataset.
DB.from(:items) # SELECT * FROM items DB.from(:items){id > 2} # SELECT * FROM items WHERE (id > 2)
# File lib/sequel/database/dataset.rb, line 55 def from(*args, &block) ds = dataset.from(*args) block ? ds.filter(&block) : ds end
Replace the schema cache with the data from the given file, which should be in Marshal format.
# File lib/sequel/extensions/schema_caching.rb, line 65 def load_schema_cache(file) @schemas = Marshal.load(File.read(file)) nil end
Replace the schema cache with the data from the given file if the file exists.
# File lib/sequel/extensions/schema_caching.rb, line 72 def load_schema_cache?(file) load_schema_cache(file) if File.exist?(file) end
Return a dataset modified by the query block
# File lib/sequel/extensions/query.rb, line 8 def query(&block) dataset.query(&block) end
Returns a new dataset with the select method invoked.
DB.select(1) # SELECT 1 DB.select{server_version{}} # SELECT server_version() DB.select(:id).from(:items) # SELECT id FROM items
# File lib/sequel/database/dataset.rb, line 65 def select(*args, &block) dataset.select(*args, &block) end
This methods involve the Database's connection pool.
Array of supported database adapters
The Database subclass for the given adapter scheme. Raises Sequel::AdapterNotFound if the adapter could not be loaded.
# File lib/sequel/database/connecting.rb, line 17 def self.adapter_class(scheme) return scheme if scheme.is_a?(Class) scheme = scheme.to_s.gsub('-', '_').to_sym unless klass = ADAPTER_MAP[scheme] # attempt to load the adapter file begin Sequel.tsk_require "sequel/adapters/#{scheme}" rescue LoadError => e raise Sequel.convert_exception_class(e, AdapterNotFound) end # make sure we actually loaded the adapter unless klass = ADAPTER_MAP[scheme] raise AdapterNotFound, "Could not load #{scheme} adapter: adapter class not registered in ADAPTER_MAP" end end klass end
Returns the scheme symbol for the Database class.
# File lib/sequel/database/connecting.rb, line 39 def self.adapter_scheme @scheme end
Connects to a database. See Sequel.connect.
# File lib/sequel/database/connecting.rb, line 44 def self.connect(conn_string, opts = {}) case conn_string when String if match = %r\A(jdbc|do):/.match(conn_string) c = adapter_class(match[1].to_sym) opts = opts.merge(:orig_opts=>opts.dup) opts = {:uri=>conn_string}.merge(opts) else uri = URI.parse(conn_string) scheme = uri.scheme scheme = :dbi if scheme =~ %r\Adbi-/ c = adapter_class(scheme) uri_options = c.send(:uri_to_options, uri) uri.query.split('&').collect{|s| s.split('=')}.each{|k,v| uri_options[k.to_sym] = v if k && !k.empty?} unless uri.query.to_s.strip.empty? uri_options.to_a.each{|k,v| uri_options[k] = URI.unescape(v) if v.is_a?(String)} opts = opts.merge(:orig_opts=>opts.dup) opts[:uri] = conn_string opts = uri_options.merge(opts) opts[:adapter] = scheme end when Hash opts = conn_string.merge(opts) opts = opts.merge(:orig_opts=>opts.dup) c = adapter_class(opts[:adapter_class] || opts[:adapter] || opts['adapter']) else raise Error, "Sequel::Database.connect takes either a Hash or a String, given: #{conn_string.inspect}" end # process opts a bit opts = opts.inject({}) do |m, (k,v)| k = :user if k.to_s == 'username' m[k.to_sym] = v m end begin db = c.new(opts) db.test_connection if opts[:test] && db.send(:typecast_value_boolean, opts[:test]) result = yield(db) if block_given? ensure if block_given? db.disconnect if db Sequel.synchronize{::Sequel::DATABASES.delete(db)} end end block_given? ? result : db end
Sets the default single_threaded mode for new databases. See Sequel.single_threaded=.
# File lib/sequel/database/connecting.rb, line 92 def self.single_threaded=(value) @@single_threaded = value end
Returns the scheme symbol for this instance's class, which reflects which
adapter is being used. In some cases, this can be the same as the
database_type
(for native adapters), in others (i.e. adapters
with subadapters), it will be different.
Sequel.connect('jdbc:postgres://...').adapter_scheme # => :jdbc
# File lib/sequel/database/connecting.rb, line 124 def adapter_scheme self.class.adapter_scheme end
Dynamically add new servers or modify server options at runtime. Also adds new servers to the connection pool. Intended for use with master/slave or shard configurations where it is useful to add new server hosts at runtime.
servers argument should be a hash with server name symbol keys and hash or proc values. If a servers key is already in use, it's value is overridden with the value provided.
DB.add_servers(:f=>{:host=>"hash_host_f"})
# File lib/sequel/database/connecting.rb, line 137 def add_servers(servers) if h = @opts[:servers] Sequel.synchronize{h.merge!(servers)} @pool.add_servers(servers.keys) end end
Connects to the database. This method should be overridden by descendants.
# File lib/sequel/database/connecting.rb, line 145 def connect(server) raise NotImplemented, "#connect should be overridden by adapters" end
The database type for this database object, the same as the adapter scheme by default. Should be overridden in adapters (especially shared adapters) to be the correct type, so that even if two separate Database objects are using different adapters you can tell that they are using the same database type. Even better, you can tell that two Database objects that are using the same adapter are connecting to different database types (think JDBC or DataObjects).
Sequel.connect('jdbc:postgres://...').database_type # => :postgres
# File lib/sequel/database/connecting.rb, line 158 def database_type adapter_scheme end
Disconnects all available connections from the connection pool. Any connections currently in use will not be disconnected. Options:
Should be a symbol specifing the server to disconnect from, or an array of symbols to specify multiple servers.
Example:
DB.disconnect # All servers DB.disconnect(:servers=>:server1) # Single server DB.disconnect(:servers=>[:server1, :server2]) # Multiple servers
# File lib/sequel/database/connecting.rb, line 172 def disconnect(opts = {}) pool.disconnect(opts) end
Yield a new Database instance for every server in the connection pool. Intended for use in sharded environments where there is a need to make schema modifications (DDL queries) on each shard.
DB.each_server{|db| db.create_table(:users){primary_key :id; String :name}}
# File lib/sequel/database/connecting.rb, line 181 def each_server(&block) servers.each{|s| self.class.connect(server_opts(s), &block)} end
Dynamically remove existing servers from the connection pool. Intended for use with master/slave or shard configurations where it is useful to remove existing server hosts at runtime.
servers should be symbols or arrays of symbols. If a nonexistent server is specified, it is ignored. If no servers have been specified for this database, no changes are made. If you attempt to remove the :default server, an error will be raised.
DB.remove_servers(:f1, :f2)
# File lib/sequel/database/connecting.rb, line 195 def remove_servers(*servers) if h = @opts[:servers] servers.flatten.each{|s| Sequel.synchronize{h.delete(s)}} @pool.remove_servers(servers) end end
An array of servers/shards for this Database object.
DB.servers # Unsharded: => [:default] DB.servers # Sharded: => [:default, :server1, :server2]
# File lib/sequel/database/connecting.rb, line 206 def servers pool.servers end
Returns true if the database is using a single-threaded connection pool.
# File lib/sequel/database/connecting.rb, line 211 def single_threaded? @single_threaded end
Acquires a database connection, yielding it to the passed block. This is useful if you want to make sure the same connection is used for all database queries in the block. It is also useful if you want to gain direct access to the underlying connection object if you need to do something Sequel does not natively support.
If a server option is given, acquires a connection for that specific server, instead of the :default server.
DB.synchronize do |conn| ... end
# File lib/sequel/database/connecting.rb, line 228 def synchronize(server=nil) @pool.hold(server || :default){|conn| yield conn} end
Attempts to acquire a database connection. Returns true if successful. Will probably raise an Error if unsuccessful. If a server argument is given, attempts to acquire a database connection to the given server/shard.
# File lib/sequel/database/connecting.rb, line 241 def test_connection(server=nil) synchronize(server){|conn|} true end
This methods change the default behavior of this database's datasets.
The default class to use for datasets
The class to use for creating datasets. Should respond to new with the Database argument as the first argument, and an optional options hash.
The default schema to use, generally should be nil. This sets the default schema used for some schema modification and introspection queries, but does not effect most dataset code.
The method to call on identifiers going into the database
# File lib/sequel/database/dataset_defaults.rb, line 21 def self.identifier_input_method @@identifier_input_method end
Set the method to call on identifiers going into the database See Sequel.identifier_input_method=.
# File lib/sequel/database/dataset_defaults.rb, line 27 def self.identifier_input_method=(v) @@identifier_input_method = v || "" end
The method to call on identifiers coming from the database
# File lib/sequel/database/dataset_defaults.rb, line 32 def self.identifier_output_method @@identifier_output_method end
Set the method to call on identifiers coming from the database See Sequel.identifier_output_method=.
# File lib/sequel/database/dataset_defaults.rb, line 38 def self.identifier_output_method=(v) @@identifier_output_method = v || "" end
Sets the default quote_identifiers mode for new databases. See Sequel.quote_identifiers=.
# File lib/sequel/database/dataset_defaults.rb, line 44 def self.quote_identifiers=(value) @@quote_identifiers = value end
If the database has any dataset modules associated with it, use a subclass of the given class that includes the modules as the dataset class.
# File lib/sequel/database/dataset_defaults.rb, line 61 def dataset_class=(c) unless @dataset_modules.empty? c = Class.new(c) @dataset_modules.each{|m| c.send(:include, m)} end @dataset_class = c end
Equivalent to extending all datasets produced by the database with a module. What it actually does is use a subclass of the current #dataset_class as the new #dataset_class, and include the module in the subclass. Instead of a module, you can provide a block that is used to create an anonymous module.
This allows you to override any of the dataset methods even if they are defined directly on the dataset class that this Database object uses.
Examples:
# Introspec columns for all of DB's datasets DB.extend_datasets(Sequel::ColumnsIntrospection) # Trace all SELECT queries by printing the SQL and the full backtrace DB.extend_datasets do def fetch_rows(sql) puts sql puts caller super end end
# File lib/sequel/database/dataset_defaults.rb, line 91 def extend_datasets(mod=nil, &block) raise(Error, "must provide either mod or block, not both") if mod && block reset_schema_utility_dataset mod = Module.new(&block) if block if @dataset_modules.empty? @dataset_modules = [mod] @dataset_class = Class.new(@dataset_class) else @dataset_modules << mod end @dataset_class.send(:include, mod) end
The method to call on identifiers going into the database
# File lib/sequel/database/dataset_defaults.rb, line 105 def identifier_input_method case @identifier_input_method when nil @identifier_input_method = @opts.fetch(:identifier_input_method, (@@identifier_input_method.nil? ? identifier_input_method_default : @@identifier_input_method)) @identifier_input_method == "" ? nil : @identifier_input_method when "" nil else @identifier_input_method end end
Set the method to call on identifiers going into the database:
DB[:items] # SELECT * FROM items DB.identifier_input_method = :upcase DB[:items] # SELECT * FROM ITEMS
# File lib/sequel/database/dataset_defaults.rb, line 122 def identifier_input_method=(v) reset_schema_utility_dataset @identifier_input_method = v || "" end
The method to call on identifiers coming from the database
# File lib/sequel/database/dataset_defaults.rb, line 128 def identifier_output_method case @identifier_output_method when nil @identifier_output_method = @opts.fetch(:identifier_output_method, (@@identifier_output_method.nil? ? identifier_output_method_default : @@identifier_output_method)) @identifier_output_method == "" ? nil : @identifier_output_method when "" nil else @identifier_output_method end end
Set the method to call on identifiers coming from the database:
DB[:items].first # {:id=>1, :name=>'foo'} DB.identifier_output_method = :upcase DB[:items].first # {:ID=>1, :NAME=>'foo'}
# File lib/sequel/database/dataset_defaults.rb, line 145 def identifier_output_method=(v) reset_schema_utility_dataset @identifier_output_method = v || "" end
Set whether to quote identifiers (columns and tables) for this database:
DB[:items] # SELECT * FROM items DB.quote_identifiers = true DB[:items] # SELECT * FROM "items"
# File lib/sequel/database/dataset_defaults.rb, line 155 def quote_identifiers=(v) reset_schema_utility_dataset @quote_identifiers = v end
Returns true if the database quotes identifiers.
# File lib/sequel/database/dataset_defaults.rb, line 161 def quote_identifiers? return @quote_identifiers unless @quote_identifiers.nil? @quote_identifiers = @opts.fetch(:quote_identifiers, (@@quote_identifiers.nil? ? quote_identifiers_default : @@quote_identifiers)) end
This methods affect relating to the logging of executed SQL.
Numeric specifying the duration beyond which queries are logged at warn level instead of info level.
Log level at which to log SQL queries. This is actually the method sent to the logger, so it should be the method name symbol. The default is :info, it can be set to :debug to log at DEBUG level.
Log a message at error level, with information about the exception.
# File lib/sequel/database/logging.rb, line 21 def log_exception(exception, message) log_each(:error, "#{exception.class}: #{exception.message.strip}: #{message}") end
Log a message at level info to all loggers.
# File lib/sequel/database/logging.rb, line 26 def log_info(message, args=nil) log_each(:info, args ? "#{message}; #{args.inspect}" : message) end
Yield to the block, logging any errors at error level to all loggers, and all other queries with the duration at warn or info level.
# File lib/sequel/database/logging.rb, line 32 def log_yield(sql, args=nil) return yield if @loggers.empty? sql = "#{sql}; #{args.inspect}" if args start = Time.now begin yield rescue => e log_exception(e, sql) raise ensure log_duration(Time.now - start, sql) unless e end end
Remove any existing loggers and just use the given logger:
DB.logger = Logger.new($stdout)
# File lib/sequel/database/logging.rb, line 49 def logger=(logger) @loggers = Array(logger) end
These methods don't fit neatly into another category.
Used for checking/removing leading zeroes from strings so they don't get interpreted as octal.
Replacement string when replacing leading zeroes.
The options hash for this database
Set the timezone to use for this database, overridding
Sequel.database_timezone
.
Constructs a new instance of a database connection with the specified options hash.
Accepts the following options:
The default schema to use, see default_schema.
A proc used to disconnect the connection
A string method symbol to call on identifiers going into the database
A string method symbol to call on identifiers coming from the database
A specific logger to use
An array of loggers to use
Whether to quote identifiers
A hash specifying a server/shard specific options, keyed by shard symbol
Whether to use a single-threaded connection pool
Method to use to log SQL to a logger, :info by default.
All options given are also passed to the connection pool. If a block is given, it is used as the connection_proc for the ConnectionPool.
# File lib/sequel/database/misc.rb, line 42 def initialize(opts = {}, &block) @opts ||= opts @opts = connection_pool_default_options.merge(@opts) @loggers = Array(@opts[:logger]) + Array(@opts[:loggers]) self.log_warn_duration = @opts[:log_warn_duration] @opts[:disconnection_proc] ||= proc{|conn| disconnect_connection(conn)} block ||= proc{|server| connect(server)} @opts[:servers] = {} if @opts[:servers].is_a?(String) @opts[:adapter_class] = self.class @opts[:single_threaded] = @single_threaded = typecast_value_boolean(@opts.fetch(:single_threaded, @@single_threaded)) @schemas = {} @default_schema = @opts.fetch(:default_schema, default_schema_default) @prepared_statements = {} @transactions = {} @identifier_input_method = nil @identifier_output_method = nil @quote_identifiers = nil @timezone = nil @dataset_class = dataset_class_default @cache_schema = typecast_value_boolean(@opts.fetch(:cache_schema, true)) @dataset_modules = [] self.sql_log_level = @opts[:sql_log_level] ? @opts[:sql_log_level].to_sym : :info @pool = ConnectionPool.get_pool(@opts, &block) Sequel.synchronize{::Sequel::DATABASES.push(self)} end
If a transaction is not currently in process, yield to the block immediately. Otherwise, add the block to the list of blocks to call after the currently in progress transaction commits (and only if it commits). Options:
The server/shard to use.
# File lib/sequel/database/misc.rb, line 75 def after_commit(opts={}, &block) raise Error, "must provide block to after_commit" unless block synchronize(opts[:server]) do |conn| if h = _trans(conn) raise Error, "cannot call after_commit in a prepared transaction" if h[:prepare] (h[:after_commit] ||= []) << block else yield end end end
If a transaction is not currently in progress, ignore the block. Otherwise, add the block to the list of the blocks to call after the currently in progress transaction rolls back (and only if it rolls back). Options:
The server/shard to use.
# File lib/sequel/database/misc.rb, line 92 def after_rollback(opts={}, &block) raise Error, "must provide block to after_rollback" unless block synchronize(opts[:server]) do |conn| if h = _trans(conn) raise Error, "cannot call after_rollback in a prepared transaction" if h[:prepare] (h[:after_rollback] ||= []) << block end end end
Cast the given type to a literal type
DB.cast_type_literal(Float) # double precision DB.cast_type_literal(:foo) # foo
# File lib/sequel/database/misc.rb, line 106 def cast_type_literal(type) type_literal(:type=>type) end
Convert the given timestamp from the application's timezone, to the databases's timezone or the default database timezone if the database does not have a timezone.
# File lib/sequel/database/misc.rb, line 113 def from_application_timestamp(v) Sequel.convert_output_timestamp(v, timezone) end
Whether the database uses a global namespace for the index. If false, the indexes are going to be namespaced per table.
# File lib/sequel/database/misc.rb, line 119 def global_index_namespace? true end
Return true if already in a transaction given the options, false otherwise. Respects the :server option for selecting a shard.
# File lib/sequel/database/misc.rb, line 126 def in_transaction?(opts={}) synchronize(opts[:server]){|conn| !!_trans(conn)} end
Returns a string representation of the database object including the class name and connection URI and options used when connecting (if any).
# File lib/sequel/database/misc.rb, line 132 def inspect a = [] a << uri.inspect if uri if (oo = opts[:orig_opts]) && !oo.empty? a << oo.inspect end "#<#{self.class}: #{a.join(' ')}>" end
Proxy the literal call to the dataset.
DB.literal(1) # 1 DB.literal(:a) # a DB.literal('a') # 'a'
# File lib/sequel/database/misc.rb, line 146 def literal(v) schema_utility_dataset.literal(v) end
Synchronize access to the prepared statements cache.
# File lib/sequel/database/misc.rb, line 151 def prepared_statement(name) Sequel.synchronize{prepared_statements[name]} end
Default serial primary key options, used by the table creation code.
# File lib/sequel/database/misc.rb, line 157 def serial_primary_key_options {:primary_key => true, :type => Integer, :auto_increment => true} end
Cache the prepared statement object at the given name.
# File lib/sequel/database/misc.rb, line 162 def set_prepared_statement(name, ps) Sequel.synchronize{prepared_statements[name] = ps} end
Whether the database supports CREATE TABLE IF NOT EXISTS syntax, false by default.
# File lib/sequel/database/misc.rb, line 168 def supports_create_table_if_not_exists? false end
Whether the database supports DROP TABLE IF EXISTS syntax, default is the same as supports_create_table_if_not_exists?.
# File lib/sequel/database/misc.rb, line 174 def supports_drop_table_if_exists? supports_create_table_if_not_exists? end
Whether the database and adapter support prepared transactions (two-phase commit), false by default.
# File lib/sequel/database/misc.rb, line 180 def supports_prepared_transactions? false end
Whether the database and adapter support savepoints, false by default.
# File lib/sequel/database/misc.rb, line 185 def supports_savepoints? false end
Whether the database and adapter support savepoints inside prepared transactions (two-phase commit), default is false.
# File lib/sequel/database/misc.rb, line 191 def supports_savepoints_in_prepared_transactions? supports_prepared_transactions? && supports_savepoints? end
Whether the database and adapter support transaction isolation levels, false by default.
# File lib/sequel/database/misc.rb, line 196 def supports_transaction_isolation_levels? false end
Whether DDL statements work correctly in transactions, false by default.
# File lib/sequel/database/misc.rb, line 201 def supports_transactional_ddl? false end
The timezone to use for this database, defaulting to
Sequel.database_timezone
.
# File lib/sequel/database/misc.rb, line 206 def timezone @timezone || Sequel.database_timezone end
Convert the given timestamp to the application's timezone, from the databases's timezone or the default database timezone if the database does not have a timezone.
# File lib/sequel/database/misc.rb, line 213 def to_application_timestamp(v) Sequel.convert_timestamp(v, timezone) end
Typecast the value to the given column_type. Calls typecast_value_#{column_type} if the method exists, otherwise returns the value. This method should raise Sequel::InvalidValue if assigned value is invalid.
# File lib/sequel/database/misc.rb, line 222 def typecast_value(column_type, value) return nil if value.nil? meth = "typecast_value_#{column_type}" begin respond_to?(meth, true) ? send(meth, value) : value rescue ArgumentError, TypeError => e raise Sequel.convert_exception_class(e, InvalidValue) end end
Returns the URI use to connect to the database. If a URI was not used when connecting, returns nil.
# File lib/sequel/database/misc.rb, line 234 def uri opts[:uri] end
Explicit alias of uri for easier subclassing.
# File lib/sequel/database/misc.rb, line 239 def url uri end