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

Top level module for Sequel

There are some class methods that are added via metaprogramming, one for each supported adapter. For example:

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

If a block is given to these methods, it is passed the opened Database object, which is closed (disconnected) when the block exits. For example:

Sequel.sqlite('blog.db'){|db| puts db.users.count}

Sequel can use either Time or DateTime for times returned from the database. It defaults to Time. To change it to DateTime, use:

Sequel.datetime_class = DateTime

Sequel can either use ParseTree for block filters (deprecated but works), or it can use the block filter syntax inside block filters (which will be the only behavior allowed in Sequel 2.2). To set it not to use ParseTree filters:

Sequel.use_parse_tree = false

Defined Under Namespace

Modules: ADO, Adapter, DB2, DBI, Deprecation, Informix, JDBC, Migrator, MySQL, ODBC, OpenBase, Oracle, Postgres, PrettyTable, SQL, SQLite, Schema Classes: ConnectionPool, Database, Dataset, Error, LiteralString, Migration, SingleThreadedPool, 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

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}

This is also aliased as Sequel.open.



62
63
64
# File 'lib/sequel_core.rb', line 62

def self.connect(*args, &block)
  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


71
72
73
# File 'lib/sequel_core.rb', line 71

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.



97
98
99
# File 'lib/sequel_core.rb', line 97

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