Module: Sequel

Defined in:
lib/sequel/core.rb,
lib/sequel/sql.rb,
lib/sequel/model.rb,
lib/sequel/dataset.rb,
lib/sequel/version.rb,
lib/sequel/database.rb,
lib/sequel/exceptions.rb,
lib/sequel/model/base.rb,
lib/sequel/adapters/do.rb,
lib/sequel/dataset/sql.rb,
lib/sequel/adapters/ado.rb,
lib/sequel/adapters/db2.rb,
lib/sequel/adapters/dbi.rb,
lib/sequel/model/errors.rb,
lib/sequel/adapters/jdbc.rb,
lib/sequel/adapters/odbc.rb,
lib/sequel/dataset/graph.rb,
lib/sequel/model/plugins.rb,
lib/sequel/adapters/mysql.rb,
lib/sequel/plugins/schema.rb,
lib/sequel/adapters/oracle.rb,
lib/sequel/adapters/sqlite.rb,
lib/sequel/metaprogramming.rb,
lib/sequel/plugins/caching.rb,
lib/sequel/adapters/jdbc/h2.rb,
lib/sequel/extensions/query.rb,
lib/sequel/model/exceptions.rb,
lib/sequel/adapters/do/mysql.rb,
lib/sequel/adapters/firebird.rb,
lib/sequel/adapters/informix.rb,
lib/sequel/adapters/openbase.rb,
lib/sequel/adapters/postgres.rb,
lib/sequel/model/inflections.rb,
lib/sequel/adapters/do/sqlite.rb,
lib/sequel/model/associations.rb,
lib/sequel/adapters/amalgalite.rb,
lib/sequel/adapters/jdbc/mysql.rb,
lib/sequel/database/schema_sql.rb,
lib/sequel/dataset/convenience.rb,
lib/sequel/adapters/do/postgres.rb,
lib/sequel/adapters/jdbc/oracle.rb,
lib/sequel/adapters/jdbc/sqlite.rb,
lib/sequel/extensions/migration.rb,
lib/sequel/plugins/identity_map.rb,
lib/sequel/adapters/shared/mssql.rb,
lib/sequel/adapters/shared/mysql.rb,
lib/sequel/extensions/pagination.rb,
lib/sequel/plugins/serialization.rb,
lib/sequel/adapters/shared/oracle.rb,
lib/sequel/adapters/shared/sqlite.rb,
lib/sequel/database/schema_methods.rb,
lib/sequel/extensions/pretty_table.rb,
lib/sequel/plugins/lazy_attributes.rb,
lib/sequel/adapters/jdbc/postgresql.rb,
lib/sequel/adapters/shared/progress.rb,
lib/sequel/extensions/schema_dumper.rb,
lib/sequel/database/schema_generator.rb,
lib/sequel/plugins/many_through_many.rb,
lib/sequel/plugins/hook_class_methods.rb,
lib/sequel/plugins/validation_helpers.rb,
lib/sequel/dataset/prepared_statements.rb,
lib/sequel/plugins/tactical_eager_loading.rb,
lib/sequel/adapters/utils/stored_procedures.rb,
lib/sequel/plugins/single_table_inheritance.rb,
lib/sequel/plugins/validation_class_methods.rb

Overview

The schema_dumper extension supports dumping tables and indexes in a Sequel::Migration format, so they can be restored on another database (which can be the same type or a different type than the current database). The main interface is through Sequel::Database#dump_schema_migration.

Defined Under Namespace

Modules: ADO, Amalgalite, DB2, DBI, DataObjects, Firebird, Inflections, Informix, JDBC, MSSQL, Metaprogramming, Migrator, MySQL, ODBC, OpenBase, Oracle, Plugins, Postgres, PrettyTable, Progress, SQL, SQLite, Schema Classes: AdapterNotFound, BeforeHookFailed, ConnectionPool, Database, DatabaseConnectionError, DatabaseDisconnectError, DatabaseError, Dataset, Error, InvalidOperation, InvalidValue, LiteralString, Migration, Model, PoolTimeout, Rollback, SingleThreadedPool, ValidationFailed

Constant Summary collapse

MAJOR =
3
MINOR =
2
TINY =
0
VERSION =
[MAJOR, MINOR, TINY].join('.')
ADAPTER_MAP =

Hash of adapters that have been used. The key is the adapter scheme symbol, and the value is the Database subclass.

{}
DATABASES =

Array of all databases to which Sequel has connected. If you are developing an application that can connect to an arbitrary number of databases, delete the database objects from this or they will not get garbage collected.

[]

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.convert_tinyint_to_boolObject

Returns the value of attribute convert_tinyint_to_bool.



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

def convert_tinyint_to_bool
  @convert_tinyint_to_bool
end

.convert_two_digit_yearsObject

Returns the value of attribute convert_two_digit_years.



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

def convert_two_digit_years
  @convert_two_digit_years
end

.datetime_classObject

Returns the value of attribute datetime_class.



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

def datetime_class
  @datetime_class
end

.virtual_row_instance_evalObject

Returns the value of attribute virtual_row_instance_eval.



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

def virtual_row_instance_eval
  @virtual_row_instance_eval
end

Class Method Details

.condition_specifier?(obj) ⇒ Boolean

Returns true if the passed object could be a specifier of conditions, false otherwise. Currently, Sequel considers hashes and arrays of all two pairs as condition specifiers.

Returns:

  • (Boolean)


52
53
54
55
56
57
58
59
60
61
# File 'lib/sequel/core.rb', line 52

def self.condition_specifier?(obj)
  case obj
  when Hash
    true
  when Array
    !obj.empty? && obj.all?{|i| (Array === i) && (i.length == 2)}
  else
    false
  end
end

.connect(*args, &block) ⇒ Object

Creates a new database object based on the supplied connection string and optional arguments. The specified scheme determines the database class used, and the rest of the string specifies the connection options. For example:

DB = Sequel.connect('sqlite:/') # Memory database
DB = Sequel.connect('sqlite://blog.db') # ./blog.db
DB = Sequel.connect('sqlite:///blog.db') # /blog.db
DB = Sequel.connect('postgres://user:password@host:port/database_name')
DB = Sequel.connect('sqlite:///blog.db', :max_connections=>10)

If a block is given, it is passed the opened Database object, which is closed when the block exits. For example:

Sequel.connect('sqlite://blog.db'){|db| puts db[:users].count}


78
79
80
# File 'lib/sequel/core.rb', line 78

def self.connect(*args, &block)
  Database.connect(*args, &block)
end

.extension(*extensions) ⇒ Object

Load all Sequel extensions given. Only loads extensions included in this release of Sequel, doesn’t load external extensions.

Sequel.extension(:schema_dumper)
Sequel.extension(:pagination, :query)


126
127
128
# File 'lib/sequel/core.rb', line 126

def self.extension(*extensions)
  require(extensions, 'extensions')
end

.identifier_input_method=(value) ⇒ Object

Set the method to call on identifiers going into the database. This affects the literalization of identifiers by calling this method on them before they are input. Sequel upcases identifiers in all SQL strings for most databases, so to turn that off:

Sequel.identifier_input_method = nil

to downcase instead:

Sequel.identifier_input_method = :downcase

Other String instance methods work as well.



93
94
95
# File 'lib/sequel/core.rb', line 93

def self.identifier_input_method=(value)
  Database.identifier_input_method = value
end

.identifier_output_method=(value) ⇒ Object

Set the method to call on identifiers coming out of the database. This affects the literalization of identifiers by calling this method on them when they are retrieved from the database. Sequel downcases identifiers retrieved for most databases, so to turn that off:

Sequel.identifier_output_method = nil

to upcase instead:

Sequel.identifier_output_method = :upcase

Other String instance methods work as well.



109
110
111
# File 'lib/sequel/core.rb', line 109

def self.identifier_output_method=(value)
  Database.identifier_output_method = value
end

.inflections {|Inflections| ... } ⇒ Object

Yield the Inflections module if a block is given, and return the Inflections module.

Yields:



4
5
6
7
# File 'lib/sequel/model/inflections.rb', line 4

def self.inflections
  yield Inflections if block_given?
  Inflections
end

.Model(source) ⇒ Object

Lets you create a Model subclass with its dataset already set. source can be an existing dataset or a symbol (in which case it will create a dataset using the default database with the given symbol as the table name).

The purpose of this method is to set the dataset automatically for a model class, if the table name doesn’t match the implicit name. This is neater than using set_dataset inside the class, doesn’t require a bogus query for the schema, and allows it to work correctly in a system that uses code reloading.

Example:

class Comment < Sequel::Model(:something)
  table_name # => :something
end


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

def self.Model(source)
  Model::ANONYMOUS_MODEL_CLASSES[source] ||= Class.new(Model).set_dataset(source)
end

.quote_identifiers=(value) ⇒ Object

Set whether to quote identifiers for all databases by default. By default, Sequel quotes identifiers in all SQL strings, so to turn that off:

Sequel.quote_identifiers = false


117
118
119
# File 'lib/sequel/core.rb', line 117

def self.quote_identifiers=(value)
  Database.quote_identifiers = value
end

.require(files, subdir = nil) ⇒ Object

Require all given files which should be in the same or a subdirectory of this file. If a subdir is given, assume all files are in that subdir.



132
133
134
# File 'lib/sequel/core.rb', line 132

def self.require(files, subdir=nil)
  Array(files).each{|f| super("#{File.dirname(__FILE__)}/#{"#{subdir}/" if subdir}#{f}")}
end

.single_threaded=(value) ⇒ Object

Set whether to set the single threaded mode for all databases by default. By default, Sequel uses a threadsafe connection pool, which isn’t as fast as the single threaded connection pool. If your program will only have one thread, and speed is a priority, you may want to set this to true:

Sequel.single_threaded = true


142
143
144
# File 'lib/sequel/core.rb', line 142

def self.single_threaded=(value)
  Database.single_threaded = value
end

.string_to_date(s) ⇒ Object

Converts the given string into a Date object.



147
148
149
150
151
152
153
# File 'lib/sequel/core.rb', line 147

def self.string_to_date(s)
  begin
    Date.parse(s, Sequel.convert_two_digit_years)
  rescue => e
    raise InvalidValue, "Invalid Date value #{s.inspect} (#{e.message})"
  end
end

.string_to_datetime(s) ⇒ Object

Converts the given string into a Time or DateTime object, depending on the value of Sequel.datetime_class.



157
158
159
160
161
162
163
164
165
166
167
# File 'lib/sequel/core.rb', line 157

def self.string_to_datetime(s)
  begin
    if datetime_class == DateTime
      DateTime.parse(s, convert_two_digit_years)
    else
      datetime_class.parse(s)
    end
  rescue => e
    raise InvalidValue, "Invalid #{datetime_class} value #{s.inspect} (#{e.message})"
  end
end

.string_to_time(s) ⇒ Object

Converts the given string into a Time object.



170
171
172
173
174
175
176
# File 'lib/sequel/core.rb', line 170

def self.string_to_time(s)
  begin
    Time.parse(s)
  rescue => e
    raise InvalidValue, "Invalid Time value #{s.inspect} (#{e.message})"
  end
end

.versionObject

The version of Sequel you are using, as a string (e.g. “2.11.0”)



9
10
11
# File 'lib/sequel/version.rb', line 9

def self.version
  VERSION
end