Class: Sequel::Database

Inherits:
Object show all
Defined in:
lib/sequel/database.rb,
lib/sequel/database/misc.rb,
lib/sequel/database/query.rb,
lib/sequel/database/dataset.rb,
lib/sequel/database/logging.rb,
lib/sequel/database/features.rb,
lib/sequel/database/connecting.rb,
lib/sequel/database/transactions.rb,
lib/sequel/database/schema_methods.rb,
lib/sequel/database/dataset_defaults.rb,
lib/sequel/extensions/run_transaction_hooks.rb

Overview

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.

Defined Under Namespace

Modules: AsyncThreadPool, RunTransactionHooks, SQLComments, SplitAlterTable

Constant Summary collapse

OPTS =
Sequel::OPTS
EXTENSIONS =

Hash of extension name symbols to callable objects to load the extension into the Database object (usually by extending it with a module defined in the extension).

{}
DEFAULT_STRING_COLUMN_SIZE =

The general default size for string columns for all Sequel::Database instances.

255
DEFAULT_DATABASE_ERROR_REGEXPS =

Empty exception regexp to class map, used by default if Sequel doesn’t have specific support for the database in use.

{}.freeze
SCHEMA_TYPE_CLASSES =

Mapping of schema type symbols to class or arrays of classes for that symbol.

{:string=>String, :integer=>Integer, :date=>Date, :datetime=>[Time, DateTime].freeze,
:time=>Sequel::SQLTime, :boolean=>[TrueClass, FalseClass].freeze, :float=>Float, :decimal=>BigDecimal,
:blob=>Sequel::SQL::Blob}.freeze
COLUMN_SCHEMA_DATETIME_TYPES =

:section: 1 - Methods that execute queries and/or return results This methods generally execute SQL code on the database server.


[:date, :datetime].freeze
COLUMN_SCHEMA_STRING_TYPES =
[:string, :blob, :date, :datetime, :time, :enum, :set, :interval].freeze
ADAPTERS =

Array of supported database adapters

%w'ado amalgalite ibmdb jdbc mock mysql mysql2 odbc oracle postgres sqlanywhere sqlite tinytds trilogy'.map(&:to_sym)
TRANSACTION_ISOLATION_LEVELS =

:section: 8 - Methods related to database transactions Database transactions make multiple queries atomic, so that either all of the queries take effect or none of them do.


{:uncommitted=>'READ UNCOMMITTED'.freeze,
:committed=>'READ COMMITTED'.freeze,
:repeatable=>'REPEATABLE READ'.freeze,
:serializable=>'SERIALIZABLE'.freeze}.freeze
COLUMN_DEFINITION_ORDER =

The order of column modifiers to use when defining a column.

[:collate, :default, :null, :unique, :primary_key, :auto_increment, :references].freeze
COMBINABLE_ALTER_TABLE_OPS =

The alter table operations that are combinable.

[:add_column, :drop_column, :rename_column,
:set_column_type, :set_column_default, :set_column_null,
:add_constraint, :drop_constraint].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = OPTS) ⇒ Database

Constructs a new instance of a database connection with the specified options hash.

Accepts the following options:

:after_connect

A callable object called after each new connection is made, with the connection object (and server argument if the callable accepts 2 arguments), useful for customizations that you want to apply to all connections.

:before_preconnect

Callable that runs after extensions from :preconnect_extensions are loaded, but before any connections are created.

:cache_schema

Whether schema should be cached for this Database instance

:check_string_typecast_bytesize

Whether to check the bytesize of strings before typecasting.

:connect_sqls

An array of sql strings to execute on each new connection, after :after_connect runs.

:default_string_column_size

The default size of string columns, 255 by default.

:extensions

Extensions to load into this Database instance. Can be a symbol, array of symbols, or string with extensions separated by columns. These extensions are loaded after connections are made by the :preconnect option.

:keep_reference

Whether to keep a reference to this instance in Sequel::DATABASES, true by default.

:logger

A specific logger to use.

:loggers

An array of loggers to use.

:log_connection_info

Whether connection information should be logged when logging queries.

:log_warn_duration

The number of elapsed seconds after which queries should be logged at warn level.

:name

A name to use for the Database object, displayed in PoolTimeout.

:preconnect

Automatically create the maximum number of connections, so that they don’t need to be created as needed. This is useful when connecting takes a long time and you want to avoid possible latency during runtime. Set to :concurrently to create the connections in separate threads. Otherwise they’ll be created sequentially.

:preconnect_extensions

Similar to the :extensions option, but loads the extensions before the connections are made by the :preconnect option.

:quote_identifiers

Whether to quote identifiers.

:servers

A hash specifying a server/shard specific options, keyed by shard symbol.

:single_threaded

Whether to use a single-threaded connection pool.

:sql_log_level

Method to use to log SQL to a logger, :info by default.

For sharded connection pools, :after_connect and :connect_sqls can be specified per-shard.

All options given are also passed to the connection pool. Additional options respected by the connection pool are :max_connections, :pool_timeout, :servers, and :servers_hash. See the connection pool documentation for details.



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/sequel/database/misc.rb', line 138

def initialize(opts = OPTS)
  @opts ||= opts
  @opts = connection_pool_default_options.merge(@opts)
  @loggers = Array(@opts[:logger]) + Array(@opts[:loggers])
  @opts[:servers] = {} if @opts[:servers].is_a?(String)
  @sharded = !!@opts[:servers]
  @opts[:adapter_class] = self.class
  @opts[:single_threaded] = @single_threaded = typecast_value_boolean(@opts.fetch(:single_threaded, Sequel.single_threaded))
  @default_string_column_size = @opts[:default_string_column_size] || DEFAULT_STRING_COLUMN_SIZE
  @check_string_typecast_bytesize = typecast_value_boolean(@opts.fetch(:check_string_typecast_bytesize, true))

  @schemas = {}
  @prepared_statements = {}
  @transactions = {}
  @symbol_literal_cache = {}

  @timezone = nil

  @dataset_class = dataset_class_default
  @cache_schema = typecast_value_boolean(@opts.fetch(:cache_schema, true))
  @dataset_modules = []
  @loaded_extensions = []
  @schema_type_classes = SCHEMA_TYPE_CLASSES.dup

  self.sql_log_level = @opts[:sql_log_level] ? @opts[:sql_log_level].to_sym : :info
  self.log_warn_duration = @opts[:log_warn_duration]
  self.log_connection_info = typecast_value_boolean(@opts[:log_connection_info])

  @pool = ConnectionPool.get_pool(self, @opts)

  reset_default_dataset
  adapter_initialize

  keep_reference = typecast_value_boolean(@opts[:keep_reference]) != false
  begin
    Sequel.synchronize{::Sequel::DATABASES.push(self)} if keep_reference
    Sequel::Database.run_after_initialize(self)

    initialize_load_extensions(:preconnect_extensions)

    if before_preconnect = @opts[:before_preconnect]
      before_preconnect.call(self)
    end

    if typecast_value_boolean(@opts[:preconnect]) && @pool.respond_to?(:preconnect, true)
      concurrent = typecast_value_string(@opts[:preconnect]) == "concurrently"
      @pool.send(:preconnect, concurrent)
    end

    initialize_load_extensions(:extensions)
    test_connection if typecast_value_boolean(@opts.fetch(:test, true)) && respond_to?(:connect, true)
  rescue
    Sequel.synchronize{::Sequel::DATABASES.delete(self)} if keep_reference
    raise
  end
end

Instance Attribute Details

#cache_schemaObject

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.



19
20
21
# File 'lib/sequel/database/query.rb', line 19

def cache_schema
  @cache_schema
end

#check_string_typecast_bytesizeObject

Whether to check the bytesize of strings before typecasting (to avoid typecasting strings that would be too long for the given type), true by default. Strings that are too long will raise a typecasting error.



97
98
99
# File 'lib/sequel/database/misc.rb', line 97

def check_string_typecast_bytesize
  @check_string_typecast_bytesize
end

#dataset_classObject

The class to use for creating datasets. Should respond to new with the Database argument as the first argument, and an optional options hash.



13
14
15
# File 'lib/sequel/database/dataset_defaults.rb', line 13

def dataset_class
  @dataset_class
end

#default_string_column_sizeObject

The specific default size of string columns for this Sequel::Database, usually 255 by default.



92
93
94
# File 'lib/sequel/database/misc.rb', line 92

def default_string_column_size
  @default_string_column_size
end

#log_connection_infoObject

Whether to include information about the connection in use when logging queries.



18
19
20
# File 'lib/sequel/database/logging.rb', line 18

def log_connection_info
  @log_connection_info
end

#log_warn_durationObject

Numeric specifying the duration beyond which queries are logged at warn level instead of info level.



12
13
14
# File 'lib/sequel/database/logging.rb', line 12

def log_warn_duration
  @log_warn_duration
end

#loggersObject

Array of SQL loggers to use for this database.



15
16
17
# File 'lib/sequel/database/logging.rb', line 15

def loggers
  @loggers
end

#optsObject (readonly)

The options hash for this database



86
87
88
# File 'lib/sequel/database/misc.rb', line 86

def opts
  @opts
end

#poolObject (readonly)

The connection pool for this Database instance. All Database instances have their own connection pools.



152
153
154
# File 'lib/sequel/database/connecting.rb', line 152

def pool
  @pool
end

#prepared_statementsObject (readonly)

The prepared statement object hash for this database, keyed by name symbol



14
15
16
# File 'lib/sequel/database/query.rb', line 14

def prepared_statements
  @prepared_statements
end

#sql_log_levelObject

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.



23
24
25
# File 'lib/sequel/database/logging.rb', line 23

def sql_log_level
  @sql_log_level
end

#timezoneObject

The timezone to use for this database, defaulting to Sequel.database_timezone.



317
318
319
# File 'lib/sequel/database/misc.rb', line 317

def timezone
  @timezone || Sequel.database_timezone
end

#transaction_isolation_levelObject

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 Database#transaction, as on MSSQL if affects all future transactions on the same connection.



22
23
24
# File 'lib/sequel/database/transactions.rb', line 22

def transaction_isolation_level
  @transaction_isolation_level
end

Class Method Details

.adapter_class(scheme) ⇒ Object

The Database subclass for the given adapter scheme. Raises Sequel::AdapterNotFound if the adapter could not be loaded.



16
17
18
# File 'lib/sequel/database/connecting.rb', line 16

def self.adapter_class(scheme)
  scheme.is_a?(Class) ? scheme : load_adapter(scheme.to_sym)
end

.adapter_schemeObject

Returns the scheme symbol for the Database class.



21
22
23
# File 'lib/sequel/database/connecting.rb', line 21

def self.adapter_scheme
  @scheme
end

.after_initialize(&block) ⇒ Object

Register a hook that will be run when a new Database is instantiated. It is called with the new database handle.

Raises:



34
35
36
37
38
39
40
41
42
43
# File 'lib/sequel/database/misc.rb', line 34

def self.after_initialize(&block)
  raise Error, "must provide block to after_initialize" unless block
  Sequel.synchronize do
    previous = @initialize_hook
    @initialize_hook = proc do |db|
      previous.call(db)
      block.call(db)
    end
  end
end

.connect(conn_string, opts = OPTS) ⇒ Object

Connects to a database. See Sequel.connect.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/sequel/database/connecting.rb', line 26

def self.connect(conn_string, opts = OPTS)
  case conn_string
  when String
    if conn_string.start_with?('jdbc:')
      c = adapter_class(:jdbc)
      opts = opts.merge(:orig_opts=>opts.dup)
      opts = {:uri=>conn_string}.merge!(opts)
    else
      uri = URI.parse(conn_string)
      scheme = uri.scheme
      c = adapter_class(scheme)
      uri_options = c.send(:uri_to_options, uri)
      uri.query.split('&').map{|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::DEFAULT_PARSER.unescape(v) if v.is_a?(String)}
      opts = uri_options.merge(opts).merge!(:orig_opts=>opts.dup, :uri=>conn_string, :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

  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)
    if defined?(yield)
      return yield(db)
    end
  ensure
    if defined?(yield)
      db.disconnect if db
      Sequel.synchronize{::Sequel::DATABASES.delete(db)}
    end
  end
  db
end

.extension(*extensions) ⇒ Object

Apply an extension to all Database objects created in the future.



46
47
48
# File 'lib/sequel/database/misc.rb', line 46

def self.extension(*extensions)
  after_initialize{|db| db.extension(*extensions)}
end

.load_adapter(scheme, opts = OPTS) ⇒ Object

Load the adapter from the file system. Raises Sequel::AdapterNotFound if the adapter cannot be loaded, or if the adapter isn’t registered correctly after being loaded. Options:

:map

The Hash in which to look for an already loaded adapter (defaults to ADAPTER_MAP).

:subdir

The subdirectory of sequel/adapters to look in, only to be used for loading subadapters.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/sequel/database/connecting.rb', line 76

def self.load_adapter(scheme, opts=OPTS)
  map = opts[:map] || ADAPTER_MAP
  if subdir = opts[:subdir]
    file = "#{subdir}/#{scheme}"
  else
    file = scheme
  end
  
  unless obj = Sequel.synchronize{map[scheme]}
    # attempt to load the adapter file
    begin
      require "sequel/adapters/#{file}"
    rescue LoadError => e
      # If subadapter file doesn't exist, just return, 
      # using the main adapter class without database customizations.
      return if subdir
      raise Sequel.convert_exception_class(e, AdapterNotFound)
    end
    
    # make sure we actually loaded the adapter
    unless obj = Sequel.synchronize{map[scheme]}
      raise AdapterNotFound, "Could not load #{file} adapter: adapter class not registered in ADAPTER_MAP"
    end
  end

  obj
end

.register_extension(ext, mod = nil, &block) ⇒ Object

Register an extension callback for Database objects. ext should be the extension name symbol, and mod should either be a Module that the database is extended with, or a callable object called with the database object. If mod is not provided, a block can be provided and is treated as the mod object.



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/sequel/database/misc.rb', line 55

def self.register_extension(ext, mod=nil, &block)
  if mod
    raise(Error, "cannot provide both mod and block to Database.register_extension") if block
    if mod.is_a?(Module)
      block = proc{|db| db.extend(mod)}
    else
      block = mod
    end
  end
  Sequel.synchronize{EXTENSIONS[ext] = block}
end

.run_after_initialize(instance) ⇒ Object

Run the after_initialize hook for the given instance.



68
69
70
# File 'lib/sequel/database/misc.rb', line 68

def self.run_after_initialize(instance)
  @initialize_hook.call(instance)
end

.set_shared_adapter_scheme(scheme, mod) ⇒ Object

Sets the given module as the shared adapter module for the given scheme. Used to register shared adapters for use by the mock adapter. Example:

# in file sequel/adapters/shared/mydb.rb
module Sequel::MyDB
  Sequel::Database.set_shared_adapter_scheme :mydb, self

  def self.mock_adapter_setup(db)
    # ...
  end

  module DatabaseMethods
    # ...
  end

  module DatasetMethods
    # ...
  end
end

would allow the mock adapter to return a Database instance that supports the MyDB syntax via:

Sequel.connect('mock://mydb')


146
147
148
# File 'lib/sequel/database/connecting.rb', line 146

def self.set_shared_adapter_scheme(scheme, mod)
  Sequel.synchronize{SHARED_ADAPTER_MAP[scheme] = mod}
end

Instance Method Details

#<<(sql) ⇒ Object

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"


25
26
27
28
# File 'lib/sequel/database/query.rb', line 25

def <<(sql)
  run(sql)
  self
end

#[](*args) ⇒ Object

Returns a dataset for the database. If the first argument is a string, the method acts as an alias for Database#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 Database#from, setting the primary table for the dataset:

DB[:items].sql #=> "SELECT * FROM items"


21
22
23
# File 'lib/sequel/database/dataset.rb', line 21

def [](*args)
  args.first.is_a?(String) ? fetch(*args) : from(*args)
end

#adapter_schemeObject

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


161
162
163
# File 'lib/sequel/database/connecting.rb', line 161

def adapter_scheme
  self.class.adapter_scheme
end

#add_column(table, *args) ⇒ Object

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, String, unique: true, null: false
DB.add_column :items, :category, String, default: 'ruby'

See alter_table.



25
26
27
# File 'lib/sequel/database/schema_methods.rb', line 25

def add_column(table, *args)
  alter_table(table) {add_column(*args)}
end

#add_index(table, columns, options = OPTS) ⇒ Object

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_errors

Ignore any DatabaseErrors that are raised

:name

Name to use for index instead of default

See alter_table.



40
41
42
43
44
45
46
47
48
# File 'lib/sequel/database/schema_methods.rb', line 40

def add_index(table, columns, options=OPTS)
  e = options[:ignore_errors]
  begin
    alter_table(table){add_index(columns, options)}
  rescue DatabaseError
    raise unless e
  end
  nil
end

#add_servers(servers) ⇒ Object

Dynamically add new servers or modify server options at runtime. Also adds new servers to the connection pool. Only usable when using a sharded connection pool.

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"})


173
174
175
176
177
178
179
180
181
# File 'lib/sequel/database/connecting.rb', line 173

def add_servers(servers)
  unless sharded?
    raise Error, "cannot call Database#add_servers on a Database instance that does not use a sharded connection pool"
  end

  h = @opts[:servers]
  Sequel.synchronize{h.merge!(servers)}
  @pool.add_servers(servers.keys)
end

#after_commit(opts = OPTS, &block) ⇒ Object

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:

:savepoint

If currently inside a savepoint, only run this hook on transaction commit if all enclosing savepoints have been released.

:server

The server/shard to use.

Raises:



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/sequel/database/transactions.rb', line 31

def after_commit(opts=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]
      if opts[:savepoint] && in_savepoint?(conn)
        add_savepoint_hook(conn, :after_commit, block)
      else
        add_transaction_hook(conn, :after_commit, block)
      end
    else
      yield
    end
  end
end

#after_rollback(opts = OPTS, &block) ⇒ Object

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:

:savepoint

If currently inside a savepoint, run this hook immediately when any enclosing savepoint is rolled back, which may be before the transaction commits or rollsback.

:server

The server/shard to use.

Raises:



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/sequel/database/transactions.rb', line 55

def after_rollback(opts=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]
      if opts[:savepoint] && in_savepoint?(conn)
        add_savepoint_hook(conn, :after_rollback, block)
      else
        add_transaction_hook(conn, :after_rollback, block)
      end
    end
  end
end

#alter_table(name, &block) ⇒ Object

Alters the given table with the specified block. Example:

DB.alter_table :items do
  add_column :category, String, default: 'ruby'
  drop_column :category
  rename_column :cntr, :counter
  set_column_type :value, Float
  set_column_default :value, 4.2
  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 guide.



67
68
69
70
71
72
# File 'lib/sequel/database/schema_methods.rb', line 67

def alter_table(name, &block)
  generator = alter_table_generator(&block)
  remove_cached_schema(name)
  apply_alter_table_generator(name, generator)
  nil
end

#alter_table_generator(&block) ⇒ Object

Return a new Schema::AlterTableGenerator instance with the receiver as the database and the given block.



76
77
78
# File 'lib/sequel/database/schema_methods.rb', line 76

def alter_table_generator(&block)
  alter_table_generator_class.new(self, &block)
end

#call(ps_name, hash = OPTS, &block) ⇒ Object

Call the prepared statement with the given name with the given hash of arguments.

DB[:items].where(id: 1).prepare(:first, :sa)
DB.call(:sa) # SELECT * FROM items WHERE id = 1


35
36
37
# File 'lib/sequel/database/query.rb', line 35

def call(ps_name, hash=OPTS, &block)
  prepared_statement(ps_name).call(hash, &block)
end

#cast_type_literal(type) ⇒ Object

Cast the given type to a literal type

DB.cast_type_literal(Float) # double precision
DB.cast_type_literal(:foo)  # foo


222
223
224
# File 'lib/sequel/database/misc.rb', line 222

def cast_type_literal(type)
  type_literal(:type=>type)
end

#create_join_table(hash, options = OPTS) ⇒ Object

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.

The default table name this will create is the sorted version of the two hash values, joined by an underscore. So the following two method calls create the same table:

create_join_table(cat_id: :cats, dog_id: :dogs) # cats_dogs
create_join_table(dog_id: :dogs, cat_id: :cats) # cats_dogs

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:

:index_options

The options to pass to the index

:name

The name of the table to create

:no_index

Set to true not to create the second index.

:no_primary_key

Set to true to not create the primary key.



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/sequel/database/schema_methods.rb', line 119

def create_join_table(hash, options=OPTS)
  keys = hash.keys.sort
  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[:null] = false unless v.has_key?(:null)
      foreign_key(key, v)
    end
    primary_key(keys) unless options[:no_primary_key]
    index(keys.reverse, options[:index_options] || OPTS) unless options[:no_index]
  end
  nil
end

#create_join_table!(hash, options = OPTS) ⇒ Object

Forcibly create a join table, attempting to drop it if it already exists, then creating it.



137
138
139
140
# File 'lib/sequel/database/schema_methods.rb', line 137

def create_join_table!(hash, options=OPTS)
  drop_table?(join_table_name(hash, options))
  create_join_table(hash, options)
end

#create_join_table?(hash, options = OPTS) ⇒ Boolean

Creates the join table unless it already exists.

Returns:

  • (Boolean)


143
144
145
146
147
148
149
# File 'lib/sequel/database/schema_methods.rb', line 143

def create_join_table?(hash, options=OPTS)
  if supports_create_table_if_not_exists? && options[:no_index]
    create_join_table(hash, options.merge(:if_not_exists=>true))
  elsif !table_exists?(join_table_name(hash, options))
    create_join_table(hash, options)
  end
end

#create_or_replace_view(name, source, options = OPTS) ⇒ Object

Creates a view, replacing a view with the same name if one already exists.

DB.create_or_replace_view(:some_items, "SELECT * FROM items WHERE price < 100")
DB.create_or_replace_view(:some_items, DB[:items].where(category: 'ruby'))

For databases where replacing a view is not natively supported, support is emulated by dropping a view with the same name before creating the view.



248
249
250
251
252
253
254
255
256
257
# File 'lib/sequel/database/schema_methods.rb', line 248

def create_or_replace_view(name, source, options = OPTS)
  if supports_create_or_replace_view?
    options = options.merge(:replace=>true)
  else
    swallow_database_error{drop_view(name)}
  end

  create_view(name, source, options)
  nil
end

#create_table(name, options = OPTS, &block) ⇒ Object

Creates a table with the columns given in the provided block:

DB.create_table :posts do
  primary_key :id
  column :title, String
  String :content
  index :title
end

General options:

:as

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_index_errors

Ignore any errors when creating indexes.

:temp

Create the table as a temporary table.

MySQL specific options:

:charset

The character set to use for the table.

:collate

The collation to use for the table.

:engine

The table engine to use for the table.

PostgreSQL specific options:

:on_commit

Either :preserve_rows (default), :drop or :delete_rows. Should only be specified when creating a temporary table.

:foreign

Create a foreign table. The value should be the name of the foreign server that was specified in CREATE SERVER.

:inherits

Inherit from a different table. An array can be specified to inherit from multiple tables.

:unlogged

Create the table as an unlogged table.

:options

The OPTIONS clause to use for foreign tables. Should be a hash where keys are option names and values are option values. Note that option names are unquoted, so you should not use untrusted keys.

:tablespace

The tablespace to use for the table.

SQLite specific options:

:strict

Create a STRICT table, which checks that the values for the columns are the correct type (similar to all other SQL databases). Note that when using this option, all column types used should be one of the following: int, integer, real, text, blob, and any. The any type is treated like a SQLite column in a non-strict table, allowing any type of data to be stored. This option is supported on SQLite 3.37.0+.

See Schema::CreateTableGenerator and the “Schema Modification” guide.



196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/sequel/database/schema_methods.rb', line 196

def create_table(name, options=OPTS, &block)
  remove_cached_schema(name)
  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] || create_table_generator(&block)
    create_table_from_generator(name, generator, options)
    create_table_indexes_from_generator(name, generator, options)
  end
  nil
end

#create_table!(name, options = OPTS, &block) ⇒ Object

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)


215
216
217
218
# File 'lib/sequel/database/schema_methods.rb', line 215

def create_table!(name, options=OPTS, &block)
  drop_table?(name)
  create_table(name, options, &block)
end

#create_table?(name, options = OPTS, &block) ⇒ Boolean

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

Returns:

  • (Boolean)


225
226
227
228
229
230
231
232
233
# File 'lib/sequel/database/schema_methods.rb', line 225

def create_table?(name, options=OPTS, &block)
  options = options.dup
  generator = options[:generator] ||= create_table_generator(&block)
  if generator.indexes.empty? && supports_create_table_if_not_exists?
    create_table(name, options.merge!(:if_not_exists=>true))
  elsif !table_exists?(name)
    create_table(name, options)
  end
end

#create_table_generator(&block) ⇒ Object

Return a new Schema::CreateTableGenerator instance with the receiver as the database and the given block.



237
238
239
# File 'lib/sequel/database/schema_methods.rb', line 237

def create_table_generator(&block)
  create_table_generator_class.new(self, &block)
end

#create_view(name, source, options = OPTS) ⇒ Object

Creates a view based on a dataset or an SQL string:

DB.create_view(:cheap_items, "SELECT * FROM items WHERE price < 100")
# CREATE VIEW cheap_items AS
# SELECT * FROM items WHERE price < 100

DB.create_view(:ruby_items, DB[:items].where(category: 'ruby'))
# CREATE VIEW ruby_items AS
# SELECT * FROM items WHERE (category = 'ruby')

DB.create_view(:checked_items, DB[:items].where(:foo), check: true)
# CREATE VIEW checked_items AS
# SELECT * FROM items WHERE foo
# WITH CHECK OPTION

DB.create_view(:bar_items, DB[:items].select(:foo), columns: [:bar])
# CREATE VIEW bar_items (bar) AS
# SELECT foo FROM items

Options:

:columns

The column names to use for the view. If not given, automatically determined based on the input dataset.

:check

Adds a WITH CHECK OPTION clause, so that attempting to modify rows in the underlying table that would not be returned by the view is not allowed. This can be set to :local to use WITH LOCAL CHECK OPTION.

PostgreSQL/SQLite specific option:

:temp

Create a temporary view, automatically dropped on disconnect.

PostgreSQL specific options:

:materialized

Creates a materialized view, similar to a regular view, but backed by a physical table.

:recursive

Creates a recursive view. As columns must be specified for recursive views, you can also set them as the value of this option. Since a recursive view requires a union that isn’t in a subquery, if you are providing a Dataset as the source argument, if should probably call the union method with the all: true and from_self: false options.

:security_invoker

Set the security_invoker property on the view, making the access to the view use the current user’s permissions, instead of the view owner’s permissions.

:tablespace

The tablespace to use for materialized views.



302
303
304
305
306
# File 'lib/sequel/database/schema_methods.rb', line 302

def create_view(name, source, options = OPTS)
  execute_ddl(create_view_sql(name, source, options))
  remove_cached_schema(name)
  nil
end

#database_typeObject

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.

Sequel.connect('jdbc:postgres://...').database_type
# => :postgres


192
193
194
# File 'lib/sequel/database/connecting.rb', line 192

def database_type
  adapter_scheme
end

#datasetObject

Returns a blank dataset for this database.

DB.dataset # SELECT *
DB.dataset.from(:items) # SELECT * FROM items


29
30
31
# File 'lib/sequel/database/dataset.rb', line 29

def dataset
  @dataset_class.new(self)
end

#disconnect(opts = OPTS) ⇒ Object

Disconnects all available connections from the connection pool. Any connections currently in use will not be disconnected. Options:

:server

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(server: :server1) # Single server
DB.disconnect(server: [:server1, :server2]) # Multiple servers


206
207
208
# File 'lib/sequel/database/connecting.rb', line 206

def disconnect(opts = OPTS)
  pool.disconnect(opts)
end

#disconnect_connection(conn) ⇒ Object

Should only be called by the connection pool code to disconnect a connection. By default, calls the close method on the connection object, since most adapters use that, but should be overwritten on other adapters.



213
214
215
# File 'lib/sequel/database/connecting.rb', line 213

def disconnect_connection(conn)
  conn.close
end

#drop_column(table, *args) ⇒ Object

Removes a column from the specified table:

DB.drop_column :items, :category

See alter_table.



313
314
315
# File 'lib/sequel/database/schema_methods.rb', line 313

def drop_column(table, *args)
  alter_table(table) {drop_column(*args)}
end

#drop_index(table, columns, options = OPTS) ⇒ Object

Removes an index for the given table and column(s):

DB.drop_index :posts, :title
DB.drop_index :posts, [:author, :title]

See alter_table.



323
324
325
# File 'lib/sequel/database/schema_methods.rb', line 323

def drop_index(table, columns, options=OPTS)
  alter_table(table){drop_index(columns, options)}
end

#drop_join_table(hash, options = OPTS) ⇒ Object

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


332
333
334
# File 'lib/sequel/database/schema_methods.rb', line 332

def drop_join_table(hash, options=OPTS)
  drop_table(join_table_name(hash, options), options)
end

#drop_table(*names) ⇒ Object

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)


341
342
343
344
345
346
347
348
# File 'lib/sequel/database/schema_methods.rb', line 341

def drop_table(*names)
  options = names.last.is_a?(Hash) ? names.pop : OPTS 
  names.each do |n|
    execute_ddl(drop_table_sql(n, options))
    remove_cached_schema(n)
  end
  nil
end

#drop_table?(*names) ⇒ Boolean

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

Returns:

  • (Boolean)


356
357
358
359
360
361
362
363
364
365
366
367
368
369
# File 'lib/sequel/database/schema_methods.rb', line 356

def drop_table?(*names)
  options = names.last.is_a?(Hash) ? names.pop : OPTS
  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
  nil
end

#drop_view(*names) ⇒ Object

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)
DB.drop_view(:cheap_items, :pricey_items, if_exists: true)

Options:

:cascade

Also drop objects depending on this view.

:if_exists

Do not raise an error if the view does not exist.

PostgreSQL specific options:

:materialized

Drop a materialized view.



384
385
386
387
388
389
390
391
# File 'lib/sequel/database/schema_methods.rb', line 384

def drop_view(*names)
  options = names.last.is_a?(Hash) ? names.pop : OPTS
  names.each do |n|
    execute_ddl(drop_view_sql(n, options))
    remove_cached_schema(n)
  end
  nil
end

#execute_ddl(sql, opts = OPTS, &block) ⇒ Object

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.



42
43
44
# File 'lib/sequel/database/query.rb', line 42

def execute_ddl(sql, opts=OPTS, &block)
  execute_dui(sql, opts, &block)
end

#execute_dui(sql, opts = OPTS, &block) ⇒ Object

Method that should be used when issuing a DELETE or UPDATE statement. By default, calls execute. This method should not be called directly by user code.



49
50
51
# File 'lib/sequel/database/query.rb', line 49

def execute_dui(sql, opts=OPTS, &block)
  execute(sql, opts, &block)
end

#execute_insert(sql, opts = OPTS, &block) ⇒ Object

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.



56
57
58
# File 'lib/sequel/database/query.rb', line 56

def execute_insert(sql, opts=OPTS, &block)
  execute_dui(sql, opts, &block)
end

#extend_datasets(mod = nil, &block) ⇒ Object

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.

If a block is given, a Dataset::DatasetModule instance is created, allowing for the easy creation of named dataset methods that will do caching.

Examples:

# Introspect 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

# Add some named dataset methods
DB.extend_datasets do
  order :by_id, :id
  select :with_id_and_name, :id, :name
  where :active, :active
end

DB[:table].active.with_id_and_name.by_id
# SELECT id, name FROM table WHERE active ORDER BY id

Raises:



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/sequel/database/dataset_defaults.rb', line 62

def extend_datasets(mod=nil, &block)
  raise(Error, "must provide either mod or block, not both") if mod && block
  mod = Dataset::DatasetModule.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)
  reset_default_dataset
end

#extension(*exts) ⇒ Object

Load an extension into the receiver. In addition to requiring the extension file, this also modifies the database to work with the extension (usually extending it with a module defined in the extension file). If no related extension file exists or the extension does not have specific support for Database objects, an Error will be raised. Returns self.



231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/sequel/database/misc.rb', line 231

def extension(*exts)
  Sequel.extension(*exts)
  exts.each do |ext|
    if pr = Sequel.synchronize{EXTENSIONS[ext]}
      if Sequel.synchronize{@loaded_extensions.include?(ext) ? false : (@loaded_extensions << ext)}
        pr.call(self)
      end
    else
      raise(Error, "Extension #{ext} does not have specific support handling individual databases (try: Sequel.extension #{ext.inspect})")
    end
  end
  self
end

#fetch(sql, *args, &block) ⇒ Object

Returns a dataset instance for the given SQL string:

ds = DB.fetch('SELECT * FROM items')

You can then call methods on the dataset to retrieve results:

ds.all
# SELECT * FROM items
# => [{:column=>value, ...}, ...]

If a block is given, it is passed to #each on the resulting dataset to iterate over the records returned by the query:

DB.fetch('SELECT * FROM items'){|r| p r}
# {:column=>value, ...}
# ...

fetch can also perform parameterized queries for protection against SQL injection:

ds = DB.fetch('SELECT * FROM items WHERE name = ?', "my name")
ds.all
# SELECT * FROM items WHERE name = 'my name'

See caveats listed in Dataset#with_sql regarding datasets using custom SQL and the methods that can be called on them.



59
60
61
62
63
# File 'lib/sequel/database/dataset.rb', line 59

def fetch(sql, *args, &block)
  ds = @default_dataset.with_sql(sql, *args)
  ds.each(&block) if block
  ds
end

#freezeObject

Freeze internal data structures for the Database instance.



196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/sequel/database/misc.rb', line 196

def freeze
  valid_connection_sql
  
  @opts.freeze
  @loggers.freeze
  @pool.freeze
  @dataset_class.freeze
  @dataset_modules.freeze
  @schema_type_classes.freeze
  @loaded_extensions.freeze
  
  super
end

#from(*args, &block) ⇒ Object

Returns a new dataset with the from method invoked. If a block is given, it acts as a virtual row block

DB.from(:items) # SELECT * FROM items
DB.from{schema[:table]} # SELECT * FROM schema.table


70
71
72
73
74
75
76
77
78
# File 'lib/sequel/database/dataset.rb', line 70

def from(*args, &block)
  if block
    @default_dataset.from(*args, &block)
  elsif args.length == 1 && (table = args[0]).is_a?(Symbol)
    @default_dataset.send(:cached_dataset, :"_from_#{table}_ds"){@default_dataset.from(table)}
  else
    @default_dataset.from(*args)
  end
end

#from_application_timestamp(v) ⇒ Object

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.



248
249
250
# File 'lib/sequel/database/misc.rb', line 248

def from_application_timestamp(v)
  Sequel.convert_output_timestamp(v, timezone)
end

#get(*args, &block) ⇒ Object

Returns a single value from the database, see Dataset#get.

DB.get(1) # SELECT 1
# => 1
DB.get{server_version.function} # SELECT server_version()


65
66
67
# File 'lib/sequel/database/query.rb', line 65

def get(*args, &block)
  @default_dataset.get(*args, &block)
end

#global_index_namespace?Boolean

Whether the database uses a global namespace for the index, true by default. If false, the indexes are going to be namespaced per table.

Returns:

  • (Boolean)


13
14
15
# File 'lib/sequel/database/features.rb', line 13

def global_index_namespace?
  true
end

#in_transaction?(opts = OPTS) ⇒ Boolean

Return true if already in a transaction given the options, false otherwise. Respects the :server option for selecting a shard.

Returns:

  • (Boolean)


113
114
115
# File 'lib/sequel/database/transactions.rb', line 113

def in_transaction?(opts=OPTS)
  synchronize(opts[:server]){|conn| !!_trans(conn)}
end

#inspectObject

Returns a string representation of the database object including the class name and connection URI and options used when connecting (if any).



254
255
256
257
258
259
260
261
# File 'lib/sequel/database/misc.rb', line 254

def inspect
  a = []
  a << uri.inspect if uri
  if (oo = opts[:orig_opts]) && !oo.empty?
    a << oo.inspect
  end
  "#<#{self.class}: #{a.join(' ')}>"
end

#literal(v) ⇒ Object

Proxy the literal call to the dataset.

DB.literal(1)   # 1
DB.literal(:a)  # a
DB.literal('a') # 'a'


268
269
270
# File 'lib/sequel/database/misc.rb', line 268

def literal(v)
  schema_utility_dataset.literal(v)
end

#literal_symbol(sym) ⇒ Object

Return the literalized version of the symbol if cached, or nil if it is not cached.



274
275
276
# File 'lib/sequel/database/misc.rb', line 274

def literal_symbol(sym)
  Sequel.synchronize{@symbol_literal_cache[sym]}
end

#literal_symbol_set(sym, lit) ⇒ Object

Set the cached value of the literal symbol.



279
280
281
# File 'lib/sequel/database/misc.rb', line 279

def literal_symbol_set(sym, lit)
  Sequel.synchronize{@symbol_literal_cache[sym] = lit}
end

#log_connection_yield(sql, conn, args = nil) ⇒ Object

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.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/sequel/database/logging.rb', line 37

def log_connection_yield(sql, conn, args=nil)
  return yield if skip_logging?
  sql = "#{connection_info(conn) if conn && log_connection_info}#{sql}#{"; #{args.inspect}" if args}"
  timer = Sequel.start_timer

  begin
    yield
  rescue => e
    log_exception(e, sql)
    raise
  ensure
    log_duration(Sequel.elapsed_seconds_since(timer), sql) unless e
  end
end

#log_exception(exception, message) ⇒ Object

Log a message at error level, with information about the exception.



26
27
28
# File 'lib/sequel/database/logging.rb', line 26

def log_exception(exception, message)
  log_each(:error, "#{exception.class}: #{exception.message.strip if exception.message}: #{message}")
end

#log_info(message, args = nil) ⇒ Object

Log a message at level info to all loggers.



31
32
33
# File 'lib/sequel/database/logging.rb', line 31

def log_info(message, args=nil)
  log_each(:info, args ? "#{message}; #{args.inspect}" : message)
end

#logger=(logger) ⇒ Object

Remove any existing loggers and just use the given logger:

DB.logger = Logger.new($stdout)


55
56
57
# File 'lib/sequel/database/logging.rb', line 55

def logger=(logger)
  @loggers = Array(logger)
end

#new_connection(server) ⇒ Object

Connect to the given server/shard. Handles database-generic post-connection setup not handled by #connect, using the :after_connect and :connect_sqls options.



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/sequel/database/connecting.rb', line 247

def new_connection(server)
  conn = connect(server)
  opts = server_opts(server)

  if ac = opts[:after_connect]
    if ac.arity == 2
      ac.call(conn, server)
    else
      ac.call(conn)
    end
  end

  if cs = opts[:connect_sqls]
    cs.each do |sql|
      log_connection_execute(conn, sql)
    end
  end

  conn
end

#prepared_statement(name) ⇒ Object

Synchronize access to the prepared statements cache.



284
285
286
# File 'lib/sequel/database/misc.rb', line 284

def prepared_statement(name)
  Sequel.synchronize{prepared_statements[name]}
end

#quote_identifier(v) ⇒ Object

Proxy the quote_identifier method to the dataset, useful for quoting unqualified identifiers for use outside of datasets.



291
292
293
# File 'lib/sequel/database/misc.rb', line 291

def quote_identifier(v)
  schema_utility_dataset.quote_identifier(v)
end

#remove_servers(*servers) ⇒ Object

Dynamically remove existing servers from the connection pool. Only usable when using a sharded connection pool

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)


226
227
228
229
230
231
232
233
234
# File 'lib/sequel/database/connecting.rb', line 226

def remove_servers(*servers)
  unless sharded?
    raise Error, "cannot call Database#remove_servers on a Database instance that does not use a sharded connection pool"
  end

  h = @opts[:servers]
  servers.flatten.each{|s| Sequel.synchronize{h.delete(s)}}
  @pool.remove_servers(servers)
end

#rename_column(table, *args) ⇒ Object

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.



410
411
412
# File 'lib/sequel/database/schema_methods.rb', line 410

def rename_column(table, *args)
  alter_table(table) {rename_column(*args)}
end

#rename_table(name, new_name) ⇒ Object

Renames a table:

DB.tables #=> [:items]
DB.rename_table :items, :old_items
DB.tables #=> [:old_items]


398
399
400
401
402
# File 'lib/sequel/database/schema_methods.rb', line 398

def rename_table(name, new_name)
  execute_ddl(rename_table_sql(name, new_name))
  remove_cached_schema(name)
  nil
end

#rollback_checker(opts = OPTS) ⇒ Object

Returns a proc that you can call to check if the transaction has been rolled back. The proc will return nil if the transaction is still in progress, true if the transaction was rolled back, and false if it was committed. Raises an Error if called outside a transaction. Respects the :server option for selecting a shard.



123
124
125
126
127
128
# File 'lib/sequel/database/transactions.rb', line 123

def rollback_checker(opts=OPTS)
  synchronize(opts[:server]) do |conn|
    raise Error, "not in a transaction" unless t = _trans(conn)
    t[:rollback_checker] ||= proc{Sequel.synchronize{t[:rolled_back]}}
  end
end

#rollback_on_exit(opts = OPTS) ⇒ Object

When exiting the transaction block through methods other than an exception (e.g. normal exit, non-local return, or throw), set the current transaction to rollback instead of committing. This is designed for use in cases where you want to preform a non-local return but also want to rollback instead of committing. Options:

:cancel

Cancel the current rollback_on_exit setting, so exiting will commit instead of rolling back.

:savepoint

Rollback only the current savepoint if inside a savepoint. Can also be an positive integer value to rollback that number of enclosing savepoints, up to and including the transaction itself. If the database does not support savepoints, this option is ignored and the entire transaction is affected.

:server

The server/shard the transaction is being executed on.



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/sequel/database/transactions.rb', line 83

def rollback_on_exit(opts=OPTS)
  synchronize(opts[:server]) do |conn|
    raise Error, "Cannot call Sequel:: Database#rollback_on_exit unless inside a transaction" unless h = _trans(conn)
    rollback = !opts[:cancel]

    if supports_savepoints?
      savepoints = h[:savepoints]

      if level = opts[:savepoint]
        level = 1 if level == true
        raise Error, "invalid :savepoint option to Database#rollback_on_exit: #{level.inspect}" unless level.is_a?(Integer)
        raise Error, "cannot pass nonpositive integer (#{level.inspect}) as :savepoint option to Database#rollback_on_exit" if level < 1
        level.times do |i|
          break unless savepoint = savepoints[-1 - i]
          savepoint[:rollback_on_exit] = rollback
        end
      else
        savepoints[0][:rollback_on_exit] = rollback
      end
    else
      h[:rollback_on_exit] = rollback
    end
  end

  nil
end

#run(sql, opts = OPTS) ⇒ Object

Runs the supplied SQL statement string on the database server. Returns nil. Options:

:server

The server to run the SQL on.

DB.run("SET some_server_variable = 42")


74
75
76
77
78
# File 'lib/sequel/database/query.rb', line 74

def run(sql, opts=OPTS)
  sql = literal(sql) if sql.is_a?(SQL::PlaceholderLiteralString)
  execute_ddl(sql, opts)
  nil
end

#schema(table, opts = OPTS) ⇒ Object

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:

:reload

Ignore any cached results, and get fresh information from the database.

:schema

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 hash should contain at least the following entries:

:allow_null

Whether NULL is an allowed value for the column.

:db_type

The database type for the column, as a database specific string.

:default

The database default for the column, as a database specific string, or nil if there is no default value.

:primary_key

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.

:ruby_default

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.

:type

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}]]

Raises:



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/sequel/database/query.rb', line 121

def schema(table, opts=OPTS)
  raise(Error, 'schema parsing is not implemented on this database') unless supports_schema_parsing?

  opts = opts.dup
  tab = 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)
    table.first_source_table
  else
    table
  end

  qualifiers = split_qualifiers(tab)
  table_name = qualifiers.pop
  sch = qualifiers.pop
  information_schema_schema = case qualifiers.length
  when 1
    Sequel.identifier(*qualifiers)
  when 2
    Sequel.qualify(*qualifiers)
  end

  if table.is_a?(Dataset)
    quoted_name = table.literal(tab)
    opts[:dataset] = table
  else
    quoted_name = schema_utility_dataset.literal(table)
  end

  opts[:schema] = sch if sch && !opts.include?(:schema)
  opts[:information_schema_schema] = information_schema_schema if information_schema_schema && !opts.include?(:information_schema_schema)

  Sequel.synchronize{@schemas.delete(quoted_name)} if opts[:reload]
  if v = Sequel.synchronize{@schemas[quoted_name]}
    return v
  end

  cols = schema_parse_table(table_name, opts)
  raise(Error, "schema parsing returned no columns, table #{table_name.inspect} probably doesn't exist") if cols.nil? || cols.empty?

  primary_keys = 0
  auto_increment_set = false
  cols.each do |_,c|
    auto_increment_set = true if c.has_key?(:auto_increment)
    primary_keys += 1 if c[:primary_key]
  end

  cols.each do |_,c|
    c[:ruby_default] = column_schema_to_ruby_default(c[:default], c[:type]) unless c.has_key?(:ruby_default)
    if c[:primary_key] && !auto_increment_set
      # If adapter didn't set it, assume that integer primary keys are auto incrementing
      c[:auto_increment] = primary_keys == 1 && !!(c[:db_type] =~ /int/io)
    end
    if !c[:max_length] && c[:type] == :string && (max_length = column_schema_max_length(c[:db_type]))
      c[:max_length] = max_length
    end
    if !c[:max_value] && !c[:min_value]
      min_max = case c[:type]
      when :integer
        column_schema_integer_min_max_values(c)
      when :decimal
        column_schema_decimal_min_max_values(c)
      end
      c[:min_value], c[:max_value] = min_max if min_max
    end
  end
  schema_post_process(cols)

  Sequel.synchronize{@schemas[quoted_name] = cols} if cache_schema
  cols
end

#schema_type_class(type) ⇒ Object

Return ruby class or array of classes for the given type symbol.



296
297
298
# File 'lib/sequel/database/misc.rb', line 296

def schema_type_class(type)
  @schema_type_classes[type]
end

#select(*args, &block) ⇒ Object

Returns a new dataset with the select method invoked.

DB.select(1) # SELECT 1
DB.select{server_version.function} # SELECT server_version()
DB.select(:id).from(:items) # SELECT id FROM items


85
86
87
# File 'lib/sequel/database/dataset.rb', line 85

def select(*args, &block)
  @default_dataset.select(*args, &block)
end

#serial_primary_key_optionsObject

Default serial primary key options, used by the table creation code.



301
302
303
# File 'lib/sequel/database/misc.rb', line 301

def serial_primary_key_options
  {:primary_key => true, :type => Integer, :auto_increment => true}
end

#serversObject

An array of servers/shards for this Database object.

DB.servers # Unsharded: => [:default]
DB.servers # Sharded:   => [:default, :server1, :server2]


240
241
242
# File 'lib/sequel/database/connecting.rb', line 240

def servers
  pool.servers
end

#set_column_default(table, *args) ⇒ Object

Sets the default value for the given column in the given table:

DB.set_column_default :items, :category, 'perl!'

See alter_table.



419
420
421
# File 'lib/sequel/database/schema_methods.rb', line 419

def set_column_default(table, *args)
  alter_table(table) {set_column_default(*args)}
end

#set_column_type(table, *args) ⇒ Object

Set the data type for the given column in the given table:

DB.set_column_type :items, :price, :float

See alter_table.



428
429
430
# File 'lib/sequel/database/schema_methods.rb', line 428

def set_column_type(table, *args)
  alter_table(table) {set_column_type(*args)}
end

#set_prepared_statement(name, ps) ⇒ Object

Cache the prepared statement object at the given name.



306
307
308
# File 'lib/sequel/database/misc.rb', line 306

def set_prepared_statement(name, ps)
  Sequel.synchronize{prepared_statements[name] = ps}
end

#sharded?Boolean

Whether this database instance uses multiple servers, either for sharding or for primary/replica configurations.

Returns:

  • (Boolean)


312
313
314
# File 'lib/sequel/database/misc.rb', line 312

def sharded?
  @sharded
end

#single_threaded?Boolean

Returns true if the database is using a single-threaded connection pool.

Returns:

  • (Boolean)


269
270
271
# File 'lib/sequel/database/connecting.rb', line 269

def single_threaded?
  @single_threaded
end

#supports_create_table_if_not_exists?Boolean

Whether the database supports CREATE TABLE IF NOT EXISTS syntax, false by default.

Returns:

  • (Boolean)


19
20
21
# File 'lib/sequel/database/features.rb', line 19

def supports_create_table_if_not_exists?
  false
end

#supports_deferrable_constraints?Boolean

Whether the database supports deferrable constraints, false by default as few databases do.

Returns:

  • (Boolean)


25
26
27
# File 'lib/sequel/database/features.rb', line 25

def supports_deferrable_constraints?
  false
end

#supports_deferrable_foreign_key_constraints?Boolean

Whether the database supports deferrable foreign key constraints, false by default as few databases do.

Returns:

  • (Boolean)


31
32
33
# File 'lib/sequel/database/features.rb', line 31

def supports_deferrable_foreign_key_constraints?
  supports_deferrable_constraints?
end

#supports_drop_table_if_exists?Boolean

Whether the database supports DROP TABLE IF EXISTS syntax, false by default.

Returns:

  • (Boolean)


37
38
39
# File 'lib/sequel/database/features.rb', line 37

def supports_drop_table_if_exists?
  supports_create_table_if_not_exists?
end

#supports_foreign_key_parsing?Boolean

Whether the database supports Database#foreign_key_list for parsing foreign keys.

Returns:

  • (Boolean)


43
44
45
# File 'lib/sequel/database/features.rb', line 43

def supports_foreign_key_parsing?
  respond_to?(:foreign_key_list)
end

#supports_index_parsing?Boolean

Whether the database supports Database#indexes for parsing indexes.

Returns:

  • (Boolean)


48
49
50
# File 'lib/sequel/database/features.rb', line 48

def supports_index_parsing?
  respond_to?(:indexes)
end

#supports_partial_indexes?Boolean

Whether the database supports partial indexes (indexes on a subset of a table), false by default.

Returns:

  • (Boolean)


54
55
56
# File 'lib/sequel/database/features.rb', line 54

def supports_partial_indexes?
  false
end

#supports_prepared_transactions?Boolean

Whether the database and adapter support prepared transactions (two-phase commit), false by default.

Returns:

  • (Boolean)


60
61
62
# File 'lib/sequel/database/features.rb', line 60

def supports_prepared_transactions?
  false
end

#supports_savepoints?Boolean

Whether the database and adapter support savepoints, false by default.

Returns:

  • (Boolean)


65
66
67
# File 'lib/sequel/database/features.rb', line 65

def supports_savepoints?
  false
end

#supports_savepoints_in_prepared_transactions?Boolean

Whether the database and adapter support savepoints inside prepared transactions (two-phase commit), false by default.

Returns:

  • (Boolean)


71
72
73
# File 'lib/sequel/database/features.rb', line 71

def supports_savepoints_in_prepared_transactions?
  supports_prepared_transactions? && supports_savepoints?
end

#supports_schema_parsing?Boolean

Whether the database supports schema parsing via Database#schema.

Returns:

  • (Boolean)


76
77
78
# File 'lib/sequel/database/features.rb', line 76

def supports_schema_parsing?
  respond_to?(:schema_parse_table, true)
end

#supports_table_listing?Boolean

Whether the database supports Database#tables for getting list of tables.

Returns:

  • (Boolean)


81
82
83
# File 'lib/sequel/database/features.rb', line 81

def supports_table_listing?
  respond_to?(:tables)
end

#supports_transaction_isolation_levels?Boolean

Whether the database and adapter support transaction isolation levels, false by default.

Returns:

  • (Boolean)


91
92
93
# File 'lib/sequel/database/features.rb', line 91

def supports_transaction_isolation_levels?
  false
end

#supports_transactional_ddl?Boolean

Whether DDL statements work correctly in transactions, false by default.

Returns:

  • (Boolean)


96
97
98
# File 'lib/sequel/database/features.rb', line 96

def supports_transactional_ddl?
  false
end

#supports_view_listing?Boolean

Whether the database supports Database#views for getting list of views.

Returns:

  • (Boolean)


86
87
88
# File 'lib/sequel/database/features.rb', line 86

def supports_view_listing?
  respond_to?(:views)
end

#supports_views_with_check_option?Boolean

Whether CREATE VIEW … WITH CHECK OPTION is supported, false by default.

Returns:

  • (Boolean)


101
102
103
# File 'lib/sequel/database/features.rb', line 101

def supports_views_with_check_option?
  !!view_with_check_option_support
end

#supports_views_with_local_check_option?Boolean

Whether CREATE VIEW … WITH LOCAL CHECK OPTION is supported, false by default.

Returns:

  • (Boolean)


106
107
108
# File 'lib/sequel/database/features.rb', line 106

def supports_views_with_local_check_option?
  view_with_check_option_support == :local
end

#synchronize(server = nil, &block) ⇒ Object

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


275
276
277
# File 'lib/sequel/database/connecting.rb', line 275

def synchronize(server=nil)
  @pool.hold(server || :default){|conn| yield conn}
end

#table_exists?(name) ⇒ Boolean

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.

Returns:

  • (Boolean)


202
203
204
205
206
207
208
209
210
# File 'lib/sequel/database/query.rb', line 202

def table_exists?(name)
  sch, table_name = schema_and_table(name)
  name = SQL::QualifiedIdentifier.new(sch, table_name) if sch
  ds = from(name)
  transaction(:savepoint=>:only){_table_exists?(ds)}
  true
rescue DatabaseError
  false
end

#test_connection(server = nil) ⇒ Object

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.



301
302
303
304
# File 'lib/sequel/database/connecting.rb', line 301

def test_connection(server=nil)
  synchronize(server){|conn|}
  true
end

#to_application_timestamp(v) ⇒ Object

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.



324
325
326
# File 'lib/sequel/database/misc.rb', line 324

def to_application_timestamp(v)
  Sequel.convert_timestamp(v, timezone)
end

#transaction(opts = OPTS, &block) ⇒ Object

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 tables do not support transactions.

The following general options are respected:

:auto_savepoint

Automatically use a savepoint for Database#transaction calls inside this transaction block.

:isolation

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.

:num_retries

The number of times to retry if the :retry_on option is used. The default is 5 times. Can be set to nil to retry indefinitely, but that is not recommended.

:before_retry

Proc to execute before retrying if the :retry_on option is used. Called with two arguments: the number of retry attempts (counting the current one) and the error the last attempt failed with.

:prepare

A string to use as the transaction identifier for a prepared transaction (two-phase commit), if the database/adapter supports prepared transactions.

:retry_on

An exception class or array of exception classes for which to automatically retry the transaction. Can only be set if not inside an existing transaction. Note that this should not be used unless the entire transaction block is idempotent, as otherwise it can cause non-idempotent behavior to execute multiple times.

:rollback

Can be set to :reraise to reraise any Sequel::Rollback exceptions raised, or :always to always rollback even if no exceptions occur (useful for testing).

:server

The server to use for the transaction. Set to :default, :read_only, or whatever symbol you used in the connect string when naming your servers.

:savepoint

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. If the surrounding transaction uses :auto_savepoint, you can set this to false to not use a savepoint. If the value given for this option is :only, it will only create a savepoint if it is inside a transaction.

:skip_transaction

If set, do not actually open a transaction or savepoint, just checkout a connection and yield it.

PostgreSQL specific options:

:deferrable

(9.1+) If present, set to DEFERRABLE if true or NOT DEFERRABLE if false.

:read_only

If present, set to READ ONLY if true or READ WRITE if false.

:synchronous

if non-nil, set synchronous_commit appropriately. Valid values true, :on, false, :off, :local (9.1+), and :remote_write (9.2+).



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/sequel/database/transactions.rb', line 179

def transaction(opts=OPTS, &block)
  opts = Hash[opts]
  if retry_on = opts[:retry_on]
    tot_retries = opts.fetch(:num_retries, 5)
    num_retries = 0
    begin
      opts[:retry_on] = nil
      opts[:retrying] = true
      transaction(opts, &block)
    rescue *retry_on => e
      num_retries += 1
      if tot_retries.nil? || num_retries <= tot_retries
        opts[:before_retry].call(num_retries, e) if opts[:before_retry]
        retry
      end
      raise
    end
  else
    synchronize(opts[:server]) do |conn|
      if opts[:skip_transaction]
        return yield(conn)
      end

      if opts[:savepoint] == :only
        if supports_savepoints?
          if _trans(conn)
            opts[:savepoint] = true
          else
            return yield(conn)
          end
        else
          opts[:savepoint] = false
        end
      end

      if opts[:savepoint] && !supports_savepoints?
        raise Sequel::InvalidOperation, "savepoints not supported on #{database_type}"
      end

      if already_in_transaction?(conn, opts)
        if opts[:rollback] == :always && !opts.has_key?(:savepoint)
          if supports_savepoints? 
            opts[:savepoint] = true
          else
            raise Sequel::Error, "cannot set :rollback=>:always transaction option if already inside a transaction"
          end
        end

        if opts[:savepoint] != false && (stack = _trans(conn)[:savepoints]) && stack.last[:auto_savepoint]
          opts[:savepoint] = true
        end

        unless opts[:savepoint]
          if opts[:retrying]
            raise Sequel::Error, "cannot set :retry_on options if you are already inside a transaction"
          end
          return yield(conn)
        end
      end

      _transaction(conn, opts, &block)
    end
  end
end

#typecast_value(column_type, value) ⇒ Object

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.



333
334
335
336
337
338
339
340
341
342
# File 'lib/sequel/database/misc.rb', line 333

def typecast_value(column_type, value)
  return nil if value.nil?
  meth = "typecast_value_#{column_type}"
  begin
    # Allow calling private methods as per-type typecasting methods are private
    respond_to?(meth, true) ? send(meth, value) : value
  rescue ArgumentError, TypeError => e
    raise Sequel.convert_exception_class(e, InvalidValue)
  end
end

#uriObject

Returns the URI use to connect to the database. If a URI was not used when connecting, returns nil.



346
347
348
# File 'lib/sequel/database/misc.rb', line 346

def uri
  opts[:uri]
end

#urlObject

Explicit alias of uri for easier subclassing.



351
352
353
# File 'lib/sequel/database/misc.rb', line 351

def url
  uri
end

#valid_connection?(conn) ⇒ Boolean

Check whether the given connection is currently valid, by running a query against it. If the query fails, the connection should probably be removed from the connection pool.

Returns:

  • (Boolean)


310
311
312
313
314
315
316
317
318
319
# File 'lib/sequel/database/connecting.rb', line 310

def valid_connection?(conn)
  sql = valid_connection_sql
  begin
    log_connection_execute(conn, sql)
  rescue Sequel::DatabaseError, *database_error_classes
    false
  else
    true
  end
end