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/migration.rb,
lib/sequel/deprecated.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/object_graph.rb,
lib/sequel/adapters/jdbc.rb,
lib/sequel/adapters/odbc.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/deprecated.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/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/adapters/shared/mssql.rb,
lib/sequel/adapters/shared/mysql.rb,
lib/sequel/extensions/pagination.rb,
lib/sequel/adapters/shared/oracle.rb,
lib/sequel/adapters/shared/sqlite.rb,
lib/sequel/model/deprecated_hooks.rb,
lib/sequel/database/schema_methods.rb,
lib/sequel/extensions/pretty_table.rb,
lib/sequel/adapters/jdbc/postgresql.rb,
lib/sequel/adapters/shared/progress.rb,
lib/sequel/database/schema_generator.rb,
lib/sequel/plugins/hook_class_methods.rb,
lib/sequel/dataset/prepared_statements.rb,
lib/sequel/model/deprecated_validations.rb,
lib/sequel/adapters/utils/stored_procedures.rb,
lib/sequel/plugins/single_table_inheritance.rb,
lib/sequel/plugins/validation_class_methods.rb
Overview
Associations are used in order to specify relationships between model classes that reflect relations between tables in the database using foreign keys.
Each kind of association adds a number of methods to the model class which are specialized according to the association type and optional parameters given in the definition. Example:
class Project < Sequel::Model
many_to_one :portfolio
one_to_many :milestones
end
The project class now has the following instance methods:
-
portfolio - Returns the associated portfolio.
-
portfolio=(obj) - Sets the associated portfolio to the object, but the change is not persisted until you save the record.
-
portfolio_dataset - Returns a dataset that would return the associated portfolio, only useful in fairly specific circumstances.
-
milestones - Returns an array of associated milestones
-
add_milestone(obj) - Associates the passed milestone with this object.
-
remove_milestone(obj) - Removes the association with the passed milestone.
-
remove_all_milestones - Removes associations with all associated milestones.
-
milestones_dataset - Returns a dataset that would return the associated milestones, allowing for further filtering/limiting/etc.
If you want to override the behavior of the add_/remove_/remove_all_ methods, there are private instance methods created that a prepended with an underscore (e.g. _add_milestone). The private instance methods can be easily overridden, but you shouldn’t override the public instance methods, as they deal with how associations are cached.
By default the classes for the associations are inferred from the association name, so for example the Project#portfolio will return an instance of Portfolio, and Project#milestones will return an array of Milestone instances, in similar fashion to how ActiveRecord infers class names.
Association definitions are also reflected by the class, e.g.:
Project.associations
=> [:portfolio, :milestones]
Project.association_reflection(:portfolio)
=> {:type => :many_to_one, :name => :portfolio, :class_name => "Portfolio"}
Associations can be defined by either using the associate method, or by calling one of the three methods: many_to_one, one_to_many, many_to_many. Sequel::Model also provides aliases for these methods that conform to ActiveRecord conventions: belongs_to, has_many, has_and_belongs_to_many. For example, the following three statements are equivalent:
associate :one_to_many, :attributes
one_to_many :attributes
has_many :attributes
Defined Under Namespace
Modules: ADO, DB2, DBI, DataObjects, Deprecation, Firebird, Inflections, Informix, JDBC, MSSQL, Metaprogramming, Migrator, MySQL, ODBC, OpenBase, Oracle, Plugins, Postgres, PrettyTable, Progress, SQL, SQLite, Schema Classes: BeforeHookFailed, ConnectionPool, Database, DatabaseConnectionError, DatabaseDisconnectError, DatabaseError, Dataset, Error, LiteralString, Migration, Model, SingleThreadedPool, ValidationFailed
Constant Summary collapse
- MAJOR =
2- MINOR =
11- TINY =
0- VERSION =
[MAJOR, MINOR, TINY].join('.')
- 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
-
.convert_tinyint_to_bool ⇒ Object
Returns the value of attribute convert_tinyint_to_bool.
-
.convert_two_digit_years ⇒ Object
Returns the value of attribute convert_two_digit_years.
-
.datetime_class ⇒ Object
Returns the value of attribute datetime_class.
-
.virtual_row_instance_eval ⇒ Object
Returns the value of attribute virtual_row_instance_eval.
Class Method Summary collapse
-
.connect(*args, &block) ⇒ Object
Creates a new database object based on the supplied connection string and optional arguments.
-
.identifier_input_method=(value) ⇒ Object
Set the method to call on identifiers going into the database.
-
.identifier_output_method=(value) ⇒ Object
Set the method to call on identifiers coming out of the database.
-
.inflections {|Inflections| ... } ⇒ Object
Yield the Inflections module if a block is given, and return the Inflections module.
-
.Model(source) ⇒ Object
Lets you create a Model subclass with its dataset already set.
- .open(*args, &block) ⇒ Object
-
.quote_identifiers=(value) ⇒ Object
Set whether to quote identifiers for all databases by default.
-
.require(files, subdir = nil) ⇒ Object
Require all given files which should be in the same or a subdirectory of this file.
-
.single_threaded=(value) ⇒ Object
Set whether to set the single threaded mode for all databases by default.
-
.string_to_date(s) ⇒ Object
Converts a string into a Date object.
-
.string_to_datetime(s) ⇒ Object
Converts a string into a Time or DateTime object, depending on the value of Sequel.datetime_class.
-
.string_to_time(s) ⇒ Object
Converts a string into a Time object.
- .upcase_identifiers=(value) ⇒ Object
- .use_parse_tree ⇒ Object
- .use_parse_tree=(val) ⇒ Object
- .version ⇒ Object
Class Attribute Details
.convert_tinyint_to_bool ⇒ Object
Returns the value of attribute convert_tinyint_to_bool.
63 64 65 |
# File 'lib/sequel/core.rb', line 63 def @convert_tinyint_to_bool end |
.convert_two_digit_years ⇒ Object
Returns the value of attribute convert_two_digit_years.
63 64 65 |
# File 'lib/sequel/core.rb', line 63 def convert_two_digit_years @convert_two_digit_years end |
.datetime_class ⇒ Object
Returns the value of attribute datetime_class.
63 64 65 |
# File 'lib/sequel/core.rb', line 63 def datetime_class @datetime_class end |
.virtual_row_instance_eval ⇒ Object
Returns the value of attribute virtual_row_instance_eval.
63 64 65 |
# File 'lib/sequel/core.rb', line 63 def virtual_row_instance_eval @virtual_row_instance_eval end |
Class Method Details
.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}
81 82 83 |
# File 'lib/sequel/core.rb', line 81 def self.connect(*args, &block) Database.connect(*args, &block) 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 methods work as well.
96 97 98 |
# File 'lib/sequel/core.rb', line 96 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 methods work as well.
112 113 114 |
# File 'lib/sequel/core.rb', line 112 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.
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
18 19 20 |
# File 'lib/sequel/model.rb', line 18 def self.Model(source) @models[source] ||= Class.new(Model).set_dataset(source) end |
.open(*args, &block) ⇒ Object
39 40 41 42 |
# File 'lib/sequel/deprecated.rb', line 39 def self.open(*args, &block) Deprecation.deprecate('Sequel.open', 'Use Sequel.connect') Database.connect(*args, &block) 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
120 121 122 |
# File 'lib/sequel/core.rb', line 120 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
126 127 128 |
# File 'lib/sequel/core.rb', line 126 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
136 137 138 |
# File 'lib/sequel/core.rb', line 136 def self.single_threaded=(value) Database.single_threaded = value end |
.string_to_date(s) ⇒ Object
Converts a string into a Date object.
141 142 143 144 145 146 147 |
# File 'lib/sequel/core.rb', line 141 def self.string_to_date(s) begin Date.parse(s, Sequel.convert_two_digit_years) rescue => e raise Error::InvalidValue, "Invalid Date value '#{self}' (#{e.})" end end |
.string_to_datetime(s) ⇒ Object
Converts a string into a Time or DateTime object, depending on the value of Sequel.datetime_class.
151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/sequel/core.rb', line 151 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 Error::InvalidValue, "Invalid #{datetime_class} value '#{self}' (#{e.})" end end |
.string_to_time(s) ⇒ Object
Converts a string into a Time object.
164 165 166 167 168 169 170 |
# File 'lib/sequel/core.rb', line 164 def self.string_to_time(s) begin Time.parse(s) rescue => e raise Error::InvalidValue, "Invalid Time value '#{self}' (#{e.})" end end |
.upcase_identifiers=(value) ⇒ Object
44 45 46 47 |
# File 'lib/sequel/deprecated.rb', line 44 def self.upcase_identifiers=(value) Deprecation.deprecate('Sequel.upcase_identifiers=', 'Use Sequel.identifier_input_method = :upcase or nil') Database.identifier_input_method = value ? :upcase : nil end |
.use_parse_tree ⇒ Object
49 50 51 52 |
# File 'lib/sequel/deprecated.rb', line 49 def self.use_parse_tree Deprecation.deprecate('Sequel.use_parse_tree', 'Sequel has not used ParseTree since 2.2') false end |
.use_parse_tree=(val) ⇒ Object
54 55 56 57 |
# File 'lib/sequel/deprecated.rb', line 54 def self.use_parse_tree=(val) Deprecation.deprecate('Sequel.use_parse_tree=', 'Sequel has not used ParseTree since 2.2') raise(Error, 'ParseTree support has been removed from Sequel') if val end |