Module: Sequel
- Defined in:
- lib/sequel_core.rb,
lib/sequel_core/sql.rb,
lib/sequel_core/worker.rb,
lib/sequel_core/dataset.rb,
lib/sequel_core/database.rb,
lib/sequel_core/migration.rb,
lib/sequel_core/deprecated.rb,
lib/sequel_core/exceptions.rb,
lib/sequel_core/schema/sql.rb,
lib/sequel_core/dataset/sql.rb,
lib/sequel_core/adapters/ado.rb,
lib/sequel_core/adapters/db2.rb,
lib/sequel_core/adapters/dbi.rb,
lib/sequel_core/object_graph.rb,
lib/sequel_core/pretty_table.rb,
lib/sequel_core/adapters/jdbc.rb,
lib/sequel_core/adapters/odbc.rb,
lib/sequel_core/dataset/query.rb,
lib/sequel_core/dataset/schema.rb,
lib/sequel_core/adapters/oracle.rb,
lib/sequel_core/adapters/sqlite.rb,
lib/sequel_core/database/schema.rb,
lib/sequel_core/dataset/callback.rb,
lib/sequel_core/schema/generator.rb,
lib/sequel_core/adapters/informix.rb,
lib/sequel_core/adapters/openbase.rb,
lib/sequel_core/adapters/postgres.rb,
lib/sequel_core/dataset/pagination.rb,
lib/sequel_core/adapters/odbc_mssql.rb,
lib/sequel_core/dataset/convenience.rb,
lib/sequel_core/adapters/adapter_skeleton.rb,
lib/sequel_core/dataset/parse_tree_sequelizer.rb,
lib/sequel_core/adapters/mysql.rb
Overview
This file includes dataset methods for translating Ruby expressions into SQL expressions, making it possible to specify dataset filters using blocks, e.g.:
DB[:items].filter {:price < 100}
DB[:items].filter {:category == 'ruby' && :date < Date.today - 7}
Block filters can refer to literals, variables, constants, arguments, instance variables or anything else in order to create parameterized queries. Block filters can also refer to other dataset objects as sub-queries. Block filters are pretty much limitless!
Block filters are based on ParseTree. If you do not have the ParseTree gem installed, block filters will raise an error.
To enable full block filter support make sure you have both ParseTree and Ruby2Ruby installed:
sudo gem install parsetree
sudo gem install ruby2ruby
Defined Under Namespace
Modules: ADO, Adapter, DB2, DBI, Deprecation, Informix, JDBC, Migrator, MySQL, ODBC, OpenBase, Oracle, Postgres, PrettyTable, SQL, SQLite, Schema Classes: Database, Dataset, Error, LiteralString, Migration, Worker
Constant Summary collapse
- 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 Method Summary collapse
-
.adapter_method(adapter, *args, &block) ⇒ Object
Helper method that the database adapter class methods that are added to Sequel via metaprogramming use to parse arguments.
-
.connect(*args, &block) ⇒ Object
Creates a new database object based on the supplied connection string and optional arguments.
-
.def_adapter_method(*adapters) ⇒ Object
Method that adds a database adapter class method to Sequel that calls Sequel.adapter_method.
-
.quote_identifiers=(value) ⇒ Object
Set whether to quote identifiers for all databases by default.
-
.single_threaded=(value) ⇒ Object
Set whether to set the single threaded mode for all databases by default.
Class Method Details
.adapter_method(adapter, *args, &block) ⇒ Object
Helper method that the database adapter class methods that are added to Sequel via metaprogramming use to parse arguments.
91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/sequel_core.rb', line 91 def self.adapter_method(adapter, *args, &block) # :nodoc: raise(::Sequel::Error, "Wrong number of arguments, 0-2 arguments valid") if args.length > 2 opts = {:adapter=>adapter.to_sym} opts[:database] = args.shift if args.length >= 1 && !(args[0].is_a?(Hash)) if Hash === (arg = args[0]) opts.merge!(arg) elsif !arg.nil? raise ::Sequel::Error, "Wrong format of arguments, either use (), (String), (Hash), or (String, Hash)" end connect(opts, &block) 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}
48 49 50 |
# File 'lib/sequel_core.rb', line 48 def self.connect(*args, &block) Database.connect(*args, &block) end |
.def_adapter_method(*adapters) ⇒ Object
Method that adds a database adapter class method to Sequel that calls Sequel.adapter_method.
105 106 107 108 109 |
# File 'lib/sequel_core.rb', line 105 def self.def_adapter_method(*adapters) # :nodoc: adapters.each do |adapter| instance_eval("def #{adapter}(*args, &block); adapter_method('#{adapter}', *args, &block) end") end 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
57 58 59 |
# File 'lib/sequel_core.rb', line 57 def self.quote_identifiers=(value) Database.quote_identifiers = value 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
Note that some database adapters (e.g. MySQL) have issues with single threaded mode if you try to perform more than one query simultaneously. For example, the following code will not work well in single threaded mode on MySQL:
DB[:items].each{|i| DB[:nodes].filter(:item_id=>i[:id]).each{|n| puts "#{i} #{n}"}}
Basically, you can’t issue another query inside a call to Dataset#each in single threaded mode. There is a fairly easy fix, just use Dataset#all inside Dataset#each for the outer query:
DB[:items].all{|i| DB[:nodes].filter(:item_id=>i[:id]).each{|n| puts "#{i} #{n}"}}
Dataset#all gets all of the returned objects before calling the block, so the query isn’t left open. Some of the adapters do this internally, and thus don’t have a problem issuing queries inside of Dataset#each.
83 84 85 |
# File 'lib/sequel_core.rb', line 83 def self.single_threaded=(value) Database.single_threaded = value end |