Giter Site home page Giter Site logo

sequel's Introduction

Sequel: The Database Toolkit for Ruby

Sequel is a simple, flexible, and powerful SQL database access toolkit for Ruby.

  • Sequel provides thread safety, connection pooling and a concise DSL for constructing SQL queries and table schemas.

  • Sequel includes a comprehensive ORM layer for mapping records to Ruby objects and handling associated records.

  • Sequel supports advanced database features such as prepared statements, bound variables, savepoints, two-phase commit, transaction isolation, primary/replica configurations, and database sharding.

  • Sequel currently has adapters for ADO, Amalgalite, IBM_DB, JDBC, MySQL, Mysql2, ODBC, Oracle, PostgreSQL, SQLAnywhere, SQLite3, TinyTDS, and Trilogy.

Resources

Website

sequel.jeremyevans.net

RDoc Documentation

sequel.jeremyevans.net/rdoc

Source Code

github.com/jeremyevans/sequel

Bug tracking (GitHub Issues)

github.com/jeremyevans/sequel/issues

Discussion Forum (GitHub Discussions)

github.com/jeremyevans/sequel/discussions

Archived Discussion Forum (sequel-talk Google Group)

www.mail-archive.com/[email protected]/

If you have questions about how to use Sequel, please ask on GitHub Discussions. Only use the the bug tracker to report bugs in Sequel, not to ask for help on using Sequel.

To check out the source code:

git clone git://github.com/jeremyevans/sequel.git

Contact

If you have any comments or suggestions please post to the Google group.

Installation

gem install sequel

A Short Example

require 'sequel'

DB = Sequel.sqlite # memory database, requires sqlite3

DB.create_table :items do
  primary_key :id
  String :name
  Float :price
end

items = DB[:items] # Create a dataset

# Populate the table
items.insert(name: 'abc', price: rand * 100)
items.insert(name: 'def', price: rand * 100)
items.insert(name: 'ghi', price: rand * 100)

# Print out the number of records
puts "Item count: #{items.count}"

# Print out the average price
puts "The average price is: #{items.avg(:price)}"

The Sequel Console

Sequel includes an IRB console for quick access to databases (usually referred to as bin/sequel). You can use it like this:

sequel sqlite://test.db # test.db in current directory

You get an IRB session with the Sequel::Database object stored in DB.

In addition to providing an IRB shell (the default behavior), bin/sequel also has support for migrating databases, dumping schema migrations, and copying databases. See the bin/sequel guide for more details.

An Introduction

Sequel is designed to take the hassle away from connecting to databases and manipulating them. Sequel deals with all the boring stuff like maintaining connections, formatting SQL correctly and fetching records so you can concentrate on your application.

Sequel uses the concept of datasets to retrieve data. A Dataset object encapsulates an SQL query and supports chainability, letting you fetch data using a convenient Ruby DSL that is both concise and flexible.

For example, the following one-liner returns the average GDP for countries in the middle east region:

DB[:countries].where(region: 'Middle East').avg(:GDP)

Which is equivalent to:

SELECT avg(GDP) FROM countries WHERE region = 'Middle East'

Since datasets retrieve records only when needed, they can be stored and later reused. Records are fetched as hashes, and are accessed using an Enumerable interface:

middle_east = DB[:countries].where(region: 'Middle East')
middle_east.order(:name).each{|r| puts r[:name]}

Sequel also offers convenience methods for extracting data from Datasets, such as an extended map method:

middle_east.map(:name) # => ['Egypt', 'Turkey', 'Israel', ...]
middle_east.map([:id, :name]) # => [[1, 'Egypt'], [3, 'Turkey'], [2, 'Israel'], ...]

Or getting results as a hash via as_hash, with one column as key and another as value:

middle_east.as_hash(:name, :area) # => {'Israel' => 20000, 'Turkey' => 120000, ...}

Getting Started

Connecting to a database

To connect to a database you simply provide Sequel.connect with a URL:

require 'sequel'
DB = Sequel.connect('sqlite://blog.db') # requires sqlite3

The connection URL can also include such stuff as the user name, password, and port:

DB = Sequel.connect('postgres://user:password@host:port/database_name') # requires pg

You can also specify optional parameters, such as the connection pool size, or loggers for logging SQL queries:

DB = Sequel.connect("postgres://user:password@host:port/database_name",
  max_connections: 10, logger: Logger.new('log/db.log'))

It is also possible to use a hash instead of a connection URL, but make sure to include the :adapter option in this case:

DB = Sequel.connect(adapter: :postgres, user: 'user', password: 'password', host: 'host', port: port,
  database: 'database_name', max_connections: 10, logger: Logger.new('log/db.log'))

You can specify a block to connect, which will disconnect from the database after it completes:

Sequel.connect('postgres://user:password@host:port/database_name'){|db| db[:posts].delete}

The DB convention

Throughout Sequel’s documentation, you will see the DB constant used to refer to the Sequel::Database instance you create. This reflects the recommendation that for an app with a single Sequel::Database instance, the Sequel convention is to store the instance in the DB constant. This is just a convention, it’s not required, but it is recommended.

Note that some frameworks that use Sequel may create the Sequel::Database instance for you, and you might not know how to access it. In most cases, you can access the Sequel::Database instance through Sequel::Model.db.

Arbitrary SQL queries

You can execute arbitrary SQL code using Database#run:

DB.run("create table t (a text, b text)")
DB.run("insert into t values ('a', 'b')")

You can also create datasets based on raw SQL:

dataset = DB['select id from items']
dataset.count # will return the number of records in the result set
dataset.map(:id) # will return an array containing all values of the id column in the result set

You can also fetch records with raw SQL through the dataset:

DB['select * from items'].each do |row|
  p row
end

You can use placeholders in your SQL string as well:

name = 'Jim'
DB['select * from items where name = ?', name].each do |row|
  p row
end

Getting Dataset Instances

Datasets are the primary way records are retrieved and manipulated. They are generally created via the Database#from or Database#[] methods:

posts = DB.from(:posts)
posts = DB[:posts] # same

Datasets will only fetch records when you tell them to. They can be manipulated to filter records, change ordering, join tables, etc. Datasets are always frozen, and they are safe to use by multiple threads concurrently.

Retrieving Records

You can retrieve all records by using the all method:

posts.all
# SELECT * FROM posts

The all method returns an array of hashes, where each hash corresponds to a record.

You can also iterate through records one at a time using each:

posts.each{|row| p row}

Or perform more advanced stuff:

names_and_dates = posts.map([:name, :date])
old_posts, recent_posts = posts.partition{|r| r[:date] < Date.today - 7}

You can also retrieve the first record in a dataset:

posts.order(:id).first
# SELECT * FROM posts ORDER BY id LIMIT 1

Note that you can get the first record in a dataset even if it isn’t ordered:

posts.first
# SELECT * FROM posts LIMIT 1

If the dataset is ordered, you can also ask for the last record:

posts.order(:stamp).last
# SELECT * FROM posts ORDER BY stamp DESC LIMIT 1

You can also provide a filter when asking for a single record:

posts.first(id: 1)
# SELECT * FROM posts WHERE id = 1 LIMIT 1

Or retrieve a single value for a specific record:

posts.where(id: 1).get(:name)
# SELECT name FROM posts WHERE id = 1 LIMIT 1

Filtering Records

The most common way to filter records is to provide a hash of values to match to where:

my_posts = posts.where(category: 'ruby', author: 'david')
# WHERE ((category = 'ruby') AND (author = 'david'))

You can also specify ranges:

my_posts = posts.where(stamp: (Date.today - 14)..(Date.today - 7))
# WHERE ((stamp >= '2010-06-30') AND (stamp <= '2010-07-07'))

Or arrays of values:

my_posts = posts.where(category: ['ruby', 'postgres', 'linux'])
# WHERE (category IN ('ruby', 'postgres', 'linux'))

By passing a block to where, you can use expressions (this is fairly “magical”):

my_posts = posts.where{stamp > Date.today << 1}
# WHERE (stamp > '2010-06-14')
my_posts = posts.where{stamp =~ Date.today}
# WHERE (stamp = '2010-07-14')

If you want to wrap the objects yourself, you can use expressions without the “magic”:

my_posts = posts.where(Sequel[:stamp] > Date.today << 1)
# WHERE (stamp > '2010-06-14')
my_posts = posts.where(Sequel[:stamp] =~ Date.today)
# WHERE (stamp = '2010-07-14')

Some databases such as PostgreSQL and MySQL also support filtering via Regexps:

my_posts = posts.where(category: /ruby/i)
# WHERE (category ~* 'ruby')

You can also use an inverse filter via exclude:

my_posts = posts.exclude(category: ['ruby', 'postgres', 'linux'])
# WHERE (category NOT IN ('ruby', 'postgres', 'linux'))

But note that this does a full inversion of the filter:

my_posts = posts.exclude(category: ['ruby', 'postgres', 'linux'], id: 1)
# WHERE ((category NOT IN ('ruby', 'postgres', 'linux')) OR (id != 1))

If at any point you want to use a custom SQL fragment for part of a query, you can do so via Sequel.lit:

posts.where(Sequel.lit('stamp IS NOT NULL'))
# WHERE (stamp IS NOT NULL)

You can safely interpolate parameters into the custom SQL fragment by providing them as additional arguments:

author_name = 'JKR'
posts.where(Sequel.lit('(stamp < ?) AND (author != ?)', Date.today - 3, author_name))
# WHERE ((stamp < '2010-07-11') AND (author != 'JKR'))

Datasets can also be used as subqueries:

DB[:items].where(Sequel[:price] > DB[:items].select{avg(price) + 100})
# WHERE (price > (SELECT avg(price) + 100 FROM items))

After filtering, you can retrieve the matching records by using any of the retrieval methods:

my_posts.each{|row| p row}

See the Dataset Filtering file for more details.

Security

Designing apps with security in mind is a best practice. Please read the Security Guide for details on security issues that you should be aware of when using Sequel.

Summarizing Records

Counting records is easy using count:

posts.where(Sequel.like(:category, '%ruby%')).count
# SELECT COUNT(*) FROM posts WHERE (category LIKE '%ruby%' ESCAPE '\')

And you can also query maximum/minimum values via max and min:

max = DB[:history].max(:value)
# SELECT max(value) FROM history

min = DB[:history].min(:value)
# SELECT min(value) FROM history

Or calculate a sum or average via sum and avg:

sum = DB[:items].sum(:price)
# SELECT sum(price) FROM items
avg = DB[:items].avg(:price)
# SELECT avg(price) FROM items

Ordering Records

Ordering datasets is simple using order:

posts.order(:stamp)
# ORDER BY stamp
posts.order(:stamp, :name)
# ORDER BY stamp, name

order always overrides the existing order:

posts.order(:stamp).order(:name)
# ORDER BY name

If you would like to add to the existing order, use order_append or order_prepend:

posts.order(:stamp).order_append(:name)
# ORDER BY stamp, name
posts.order(:stamp).order_prepend(:name)
# ORDER BY name, stamp

You can also specify descending order:

posts.reverse_order(:stamp)
# ORDER BY stamp DESC
posts.order(Sequel.desc(:stamp))
# ORDER BY stamp DESC

Core Extensions

Note the use of Sequel.desc(:stamp) in the above example. Much of Sequel’s DSL uses this style, calling methods on the Sequel module that return SQL expression objects. Sequel also ships with a core_extensions extension that integrates Sequel’s DSL better into the Ruby language, allowing you to write:

:stamp.desc

instead of:

Sequel.desc(:stamp)

Selecting Columns

Selecting specific columns to be returned is also simple using select:

posts.select(:stamp)
# SELECT stamp FROM posts
posts.select(:stamp, :name)
# SELECT stamp, name FROM posts

Like order, select overrides an existing selection:

posts.select(:stamp).select(:name)
# SELECT name FROM posts

As you might expect, there are order_append and order_prepend equivalents for select called select_append and select_prepend:

posts.select(:stamp).select_append(:name)
# SELECT stamp, name FROM posts
posts.select(:stamp).select_prepend(:name)
# SELECT name, stamp FROM posts

Deleting Records

Deleting records from the table is done with delete:

posts.where(Sequel[:stamp] < Date.today - 3).delete
# DELETE FROM posts WHERE (stamp < '2010-07-11')

Be very careful when deleting, as delete affects all rows in the dataset. Call where first and delete second:

# DO THIS:
posts.where(Sequel[:stamp] < Date.today - 7).delete
# NOT THIS:
posts.delete.where(Sequel[:stamp] < Date.today - 7)

Inserting Records

Inserting records into the table is done with insert:

posts.insert(category: 'ruby', author: 'david')
# INSERT INTO posts (category, author) VALUES ('ruby', 'david')

Updating Records

Updating records in the table is done with update:

posts.where(Sequel[:stamp] < Date.today - 7).update(state: 'archived')
# UPDATE posts SET state = 'archived' WHERE (stamp < '2010-07-07')

You can provide arbitrary expressions when choosing what values to set:

posts.where(Sequel[:stamp] < Date.today - 7).update(backup_number: Sequel[:backup_number] + 1)
# UPDATE posts SET backup_number = (backup_number + 1) WHERE (stamp < '2010-07-07'))))

As with delete, update affects all rows in the dataset, so where first, update second:

# DO THIS:
posts.where(Sequel[:stamp] < Date.today - 7).update(state: 'archived')
# NOT THIS:
posts.update(state: 'archived').where(Sequel[:stamp] < Date.today - 7)

Merging records

Merging records using the SQL MERGE statement is done using merge* methods. You use merge_using to specify the merge source and join conditions. You can use merge_insert, merge_delete, and/or merge_update to set the INSERT, DELETE, and UPDATE clauses for the merge. merge_insert takes the same arguments as insert, and merge_update takes the same arguments as update. merge_insert, merge_delete, and merge_update can all be called with blocks, to set the conditions for the related INSERT, DELETE, or UPDATE.

Finally, after calling all of the other merge_* methods, you call merge to run the MERGE statement on the database.

ds = DB[:m1]
  merge_using(:m2, i1: :i2).
  merge_insert(i1: :i2, a: Sequel[:b]+11).
  merge_delete{a > 30}.
  merge_update(i1: Sequel[:i1]+:i2+10, a: Sequel[:a]+:b+20)

ds.merge
# MERGE INTO m1 USING m2 ON (i1 = i2)
# WHEN NOT MATCHED THEN INSERT (i1, a) VALUES (i2, (b + 11))
# WHEN MATCHED AND (a > 30) THEN DELETE
# WHEN MATCHED THEN UPDATE SET i1 = (i1 + i2 + 10), a = (a + b + 20)

Transactions

You can wrap a block of code in a database transaction using the Database#transaction method:

DB.transaction do
  # BEGIN
  posts.insert(category: 'ruby', author: 'david')
  # INSERT
  posts.where(Sequel[:stamp] < Date.today - 7).update(state: 'archived')
  # UPDATE
end
# COMMIT

If the block does not raise an exception, the transaction will be committed. If the block does raise an exception, the transaction will be rolled back, and the exception will be reraised. If you want to rollback the transaction and not raise an exception outside the block, you can raise the Sequel::Rollback exception inside the block:

DB.transaction do
  # BEGIN
  posts.insert(category: 'ruby', author: 'david')
  # INSERT
  if posts.where('stamp < ?', Date.today - 7).update(state: 'archived') == 0
  # UPDATE
    raise Sequel::Rollback
  end
end
# ROLLBACK

Joining Tables

Sequel makes it easy to join tables:

order_items = DB[:items].join(:order_items, item_id: :id).where(order_id: 1234)
# SELECT * FROM items
# INNER JOIN order_items ON (order_items.item_id = items.id)
# WHERE (order_id = 1234)

The important thing to note here is that item_id is automatically qualified with the table being joined, and id is automatically qualified with the last table joined.

You can then do anything you like with the dataset:

order_total = order_items.sum(:price)
# SELECT sum(price) FROM items
# INNER JOIN order_items ON (order_items.item_id = items.id)
# WHERE (order_id = 1234)

Note that the default selection in Sequel is *, which includes all columns in all joined tables. Because Sequel returns results as a hash keyed by column name symbols, if any tables have columns with the same name, this will clobber the columns in the returned hash. So when joining you are usually going to want to change the selection using select, select_all, and/or select_append.

Column references in Sequel

Sequel expects column names to be specified using symbols. In addition, returned hashes always use symbols as their keys. This allows you to freely mix literal values and column references in many cases. For example, the two following lines produce equivalent SQL:

items.where(x: 1)
# SELECT * FROM items WHERE (x = 1)
items.where(1 => :x)
# SELECT * FROM items WHERE (1 = x)"

Ruby strings are generally treated as SQL strings:

items.where(x: 'x')
# SELECT * FROM items WHERE (x = 'x')

Qualifying identifiers (column/table names)

An identifier in SQL is a name that represents a column, table, or schema. The recommended way to qualify columns is to use Sequel[][] or Sequel.qualify

Sequel[:table][:column]
Sequel.qualify(:table, :column)
# table.column

You can also qualify tables with schemas:

Sequel[:schema][:table]
# schema.table

or use multi-level qualification:

Sequel[:schema][:table][:column]
# schema.table.column

Expression aliases

You can alias identifiers using Sequel[].as or Sequel.as:

Sequel[:column].as(:alias)
Sequel.as(:column, :alias)
# column AS alias

You can use the Sequel.as method to alias arbitrary expressions, not just identifiers:

Sequel.as(DB[:posts].select{max(id)}, :p)
# (SELECT max(id) FROM posts) AS p

And most Sequel expression objects support an as method for aliasing:

(Sequel[:column] + 2).as(:c_plus_2)
# (column + 2) AS c_plus_2

Sequel Models

A model class wraps a dataset, and an instance of that class wraps a single record in the dataset.

Model classes are defined as regular Ruby classes inheriting from Sequel::Model:

DB = Sequel.connect('sqlite://blog.db')
class Post < Sequel::Model
end

When a model class is created, it parses the schema in the table from the database, and automatically sets up accessor methods for all of the columns in the table (Sequel::Model implements the active record pattern).

Sequel model classes assume that the table name is an underscored plural of the class name:

Post.table_name # => :posts

You can explicitly set the table name or even the dataset used:

class Post < Sequel::Model(:my_posts); end
# or:
class Post < Sequel::Model(DB[:my_posts]); end

If you pass a symbol to the Sequel::Model method, it assumes you are referring to the table with the same name. You can also call it with a dataset, which will set the defaults for all retrievals for that model:

class Post < Sequel::Model(DB[:my_posts].where(category: 'ruby')); end
class Post < Sequel::Model(DB[:my_posts].select(:id, :name).order(:date)); end

Model instances

Model instances are identified by a primary key. Sequel queries the database to determine the primary key for each model. The Model.[] method can be used to fetch records by their primary key:

post = Post[123]

The pk method is used to retrieve the record’s primary key value:

post.pk # => 123

If you want to override which column(s) to use as the primary key, you can use set_primary_key:

class Post < Sequel::Model
  set_primary_key [:category, :title]
end

post = Post['ruby', 'hello world']
post.pk # => ['ruby', 'hello world']

You can also define a model class that does not have a primary key via no_primary_key, but then you lose the ability to easily update and delete records:

Post.no_primary_key

A single model instance can also be fetched by specifying a condition:

post = Post.first(title: 'hello world')
post = Post.first{num_comments < 10}

The dataset for a model class returns rows of model instances instead of plain hashes:

DB[:posts].first.class # => Hash
Post.first.class # => Post

Acts like a dataset

A model class forwards many methods to the underlying dataset. This means that you can use most of the Dataset API to create customized queries that return model instances, e.g.:

Post.where(category: 'ruby').each{|post| p post}

You can also manipulate the records in the dataset:

Post.where{num_comments < 7}.delete
Post.where(Sequel.like(:title, /ruby/)).update(category: 'ruby')

Accessing record values

A model instance stores its values as a hash with column symbol keys, which you can access directly via the values method:

post.values # => {:id => 123, :category => 'ruby', :title => 'hello world'}

You can read the record values as object attributes, assuming the attribute names are valid columns in the model’s dataset:

post.id # => 123
post.title # => 'hello world'

If the record’s attributes names are not valid columns in the model’s dataset (maybe because you used select_append to add a computed value column), you can use Model#[] to access the values:

post[:id] # => 123
post[:title] # => 'hello world'

You can also modify record values using attribute setters or the []= method.

post.title = 'hey there'
post[:title] = 'hey there'

That will just change the value for the object, it will not update the row in the database. To update the database row, call the save method:

post.save

Mass assignment

You can also set the values for multiple columns in a single method call, using one of the mass-assignment methods. See the mass assignment guide for details. For example set updates the model’s column values without saving:

post.set(title: 'hey there', updated_by: 'foo')

and update updates the model’s column values and then saves the changes to the database:

post.update(title: 'hey there', updated_by: 'foo')

Creating new records

New model instances can be created by calling Model.new, which returns a new model instance without updating the database:

post = Post.new(title: 'hello world')

You can save the record to the database later by calling save on the model instance:

post.save

If you want to create a new record and save it to the database at the same time, you can use Model.create:

post = Post.create(title: 'hello world')

You can also supply a block to Model.new and Model.create:

post = Post.new do |p|
  p.title = 'hello world'
end

post = Post.create{|p| p.title = 'hello world'}

Hooks

You can execute custom code when creating, updating, or deleting records by defining hook methods. The before_create and after_create hook methods wrap record creation. The before_update and after_update hook methods wrap record updating. The before_save and after_save hook methods wrap record creation and updating. The before_destroy and after_destroy hook methods wrap destruction. The before_validation and after_validation hook methods wrap validation. Example:

class Post < Sequel::Model
  def after_create
    super
    author.increase_post_count
  end

  def after_destroy
    super
    author.decrease_post_count
  end
end

Note the use of super if you define your own hook methods. Almost all Sequel::Model class and instance methods (not just hook methods) can be overridden safely, but you have to make sure to call super when doing so, otherwise you risk breaking things.

For the example above, you should probably use a database trigger if you can. Hooks can be used for data integrity, but they will only enforce that integrity when you are modifying the database through model instances, and even then they are often subject to race conditions. It’s best to use database triggers and database constraints to enforce data integrity.

Deleting records

You can delete individual records by calling delete or destroy. The only difference between the two methods is that destroy invokes before_destroy and after_destroy hook methods, while delete does not:

post.delete # => bypasses hooks
post.destroy # => runs hooks

Records can also be deleted en-masse by calling delete and destroy on the model’s dataset. As stated above, you can specify filters for the deleted records:

Post.where(category: 32).delete # => bypasses hooks
Post.where(category: 32).destroy # => runs hooks

Please note that if destroy is called, each record is deleted separately, but delete deletes all matching records with a single SQL query.

Associations

Associations are used in order to specify relationships between model classes that reflect relationships between tables in the database, which are usually specified using foreign keys. You specify model associations via class methods:

class Post < Sequel::Model
  many_to_one :author
  one_to_many :comments
  one_to_one :first_comment, class: :Comment, order: :id
  many_to_many :tags
  one_through_one :first_tag, class: :Tag, order: :name, right_key: :tag_id
end

many_to_one and one_to_one create a getter and setter for each model object:

post = Post.create(name: 'hi!')
post.author = Author.first(name: 'Sharon')
post.author

one_to_many and many_to_many create a getter method, a method for adding an object to the association, a method for removing an object from the association, and a method for removing all associated objects from the association:

post = Post.create(name: 'hi!')
post.comments

comment = Comment.create(text: 'hi')
post.add_comment(comment)
post.remove_comment(comment)
post.remove_all_comments

tag = Tag.create(tag: 'interesting')
post.add_tag(tag)
post.remove_tag(tag)
post.remove_all_tags

Note that the remove_* and remove_all_* methods do not delete the object from the database, they merely disassociate the associated object from the receiver.

All associations add a dataset method that can be used to further filter or reorder the returned objects, or modify all of them:

# Delete all of this post's comments from the database
post.comments_dataset.destroy

# Return all tags related to this post with no subscribers, ordered by the tag's name
post.tags_dataset.where(subscribers: 0).order(:name).all

Eager Loading

Associations can be eagerly loaded via eager and the :eager association option. Eager loading is used when loading a group of objects. It loads all associated objects for all of the current objects in one query, instead of using a separate query to get the associated objects for each current object. Eager loading requires that you retrieve all model objects at once via all (instead of individually by each). Eager loading can be cascaded, loading association’s associated objects.

class Person < Sequel::Model
  one_to_many :posts, eager: [:tags]
end

class Post < Sequel::Model
  many_to_one :person
  one_to_many :replies
  many_to_many :tags
end

class Tag < Sequel::Model
  many_to_many :posts
  many_to_many :replies
end

class Reply < Sequel::Model
  many_to_one :person
  many_to_one :post
  many_to_many :tags
end

# Eager loading via .eager
Post.eager(:person).all

# eager is a dataset method, so it works with filters/orders/limits/etc.
Post.where{topic > 'M'}.order(:date).limit(5).eager(:person).all

person = Person.first
# Eager loading via :eager (will eagerly load the tags for this person's posts)
person.posts

# These are equivalent
Post.eager(:person, :tags).all
Post.eager(:person).eager(:tags).all

# Cascading via .eager
Tag.eager(posts: :replies).all

# Will also grab all associated posts' tags (because of :eager)
Reply.eager(person: :posts).all

# No depth limit (other than memory/stack), and will also grab posts' tags
# Loads all people, their posts, their posts' tags, replies to those posts,
# the person for each reply, the tag for each reply, and all posts and
# replies that have that tag.  Uses a total of 8 queries.
Person.eager(posts: {replies: [:person, {tags: [:posts, :replies]}]}).all

In addition to using eager, you can also use eager_graph, which will use a single query to get the object and all associated objects. This may be necessary if you want to filter or order the result set based on columns in associated tables. It works with cascading as well, the API is similar. Note that using eager_graph to eagerly load multiple *_to_many associations will cause the result set to be a cartesian product, so you should be very careful with your filters when using it in that case.

You can dynamically customize the eagerly loaded dataset by using a proc. This proc is passed the dataset used for eager loading, and should return a modified copy of that dataset:

# Eagerly load only replies containing 'foo'
Post.eager(replies: proc{|ds| ds.where(Sequel.like(text, '%foo%'))}).all

This also works when using eager_graph, in which case the proc is called with dataset to graph into the current dataset:

Post.eager_graph(replies: proc{|ds| ds.where(Sequel.like(text, '%foo%'))}).all

You can dynamically customize eager loads for both eager and eager_graph while also cascading, by making the value a single entry hash with the proc as a key, and the cascaded associations as the value:

# Eagerly load only replies containing 'foo', and the person and tags for those replies
Post.eager(replies: {proc{|ds| ds.where(Sequel.like(text, '%foo%'))} => [:person, :tags]}).all

Joining with Associations

You can use the association_join method to add a join to the model’s dataset based on the association:

Post.association_join(:author)
# SELECT * FROM posts
# INNER JOIN authors AS author ON (author.id = posts.author_id)

This comes with variants for different join types:

Post.association_left_join(:replies)
# SELECT * FROM posts
# LEFT JOIN replies ON (replies.post_id = posts.id)

Similar to the eager loading methods, you can use multiple associations and nested associations:

Post.association_join(:author, replies: :person).all
# SELECT * FROM posts
# INNER JOIN authors AS author ON (author.id = posts.author_id)
# INNER JOIN replies ON (replies.post_id = posts.id)
# INNER JOIN people AS person ON (person.id = replies.person_id)

Extending the underlying dataset

The recommended way to implement table-wide logic by defining methods on the dataset using dataset_module:

class Post < Sequel::Model
  dataset_module do
    def with_few_comments
      where{num_comments < 30}
    end

    def clean_boring
      with_few_comments.delete
    end
  end
end

This allows you to have access to your model API from filtered datasets as well:

Post.where(category: 'ruby').clean_boring
# DELETE FROM posts WHERE ((category = 'ruby') AND (num_comments < 30))

Inside dataset_module blocks, there are numerous methods that support easy creation of dataset methods. Most of these methods are named after the dataset methods themselves, such as select, order, and group:

class Post < Sequel::Model
  dataset_module do
    where(:with_few_comments, Sequel[:num_comments] < 30)
    select :with_title_and_date, :id, :title, :post_date
    order :by_post_date, :post_date
    limit :top10, 10
  end
end

Post.with_few_comments.with_title_and_date.by_post_date.top10
# SELECT id, title, post_date
# FROM posts
# ORDER BY post_date
# LIMIT 10

One advantage of using these methods inside dataset_module blocks, instead of defining methods manually, is that the created methods will generally cache the resulting values and result in better performance.

Model Validations

You can define a validate method for your model, which save will check before attempting to save the model in the database. If an attribute of the model isn’t valid, you should add an error message for that attribute to the model object’s errors. If an object has any errors added by the validate method, save will raise an error by default:

class Post < Sequel::Model
  def validate
    super
    errors.add(:name, "can't be empty") if name.empty?
    errors.add(:written_on, "should be in the past") if written_on >= Time.now
  end
end

Testing Sequel

Please see the testing guide for recommendations on testing applications that use Sequel, as well as the how to run the tests for Sequel itself.

Sequel Release Policy

New major versions of Sequel do not have a defined release policy, but historically have occurred once every few years.

New minor versions of Sequel are released around once a month near the start of the month.

New tiny versions of Sequel are only released to address security issues or regressions in the most current release.

Ruby Support Policy

Sequel fully supports the currently supported versions of Ruby (MRI) and JRuby. It may support unsupported versions of Ruby or JRuby, but such support may be dropped in any minor version if keeping it becomes a support issue. The minimum Ruby version required to run the current version of Sequel is 1.9.2, and the minimum JRuby version is 9.2.0.0 (due to the bigdecimal dependency).

Maintainer

Jeremy Evans <[email protected]>

sequel's People

Contributors

adam12 avatar alexwayfer avatar benkoshy avatar celsworth avatar chanks avatar danielb2 avatar dividedmind avatar eppo avatar hgmnz avatar isc avatar janko avatar jeremyevans avatar jlmuir avatar jmhodges avatar jrgns avatar kenaniah avatar knaveofdiamonds avatar lancecarlson avatar michaeldiamond avatar noteflakes avatar oldgreen avatar olleolleolle avatar roylez avatar sensadrome avatar tmm1 avatar tsmmark avatar v-kolesnikov avatar vais avatar wayneeseguin avatar wishdev avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

sequel's Issues

Enhancement: Character set settings for Mysql (e.g. UTF-8)

The attached diff adds the :charset option for Mysql, so that you can now
write:

DB = Sequel.open('mysql://...', :charset => 'utf8')

It's impossible to do outside of Sequel, because of connection pooling!

Google Code Info:
Issue #: 40
Author: [email protected]
Created On: 2007-09-05T15:28:19.000Z
Closed On: 2007-09-05T16:05:46.000Z

Models can't be created with no values

Suppose I have a table having one primary key and all other fields
optional.

And I try to create corresponding model WITHOUT ANY VALUES:

obj = MyModel.create #see?

I receive SQLite's error 'near ")": syntax error', which is
understandable, as generated clause looks like

INSERT INTO test () VALUES ();

which is definitely wrong (at leaset, for SQLite). Maybe we need something
like:

INSERT INTO test (<primary_key>) VALUES (null);

It's not that critical, but at least inaccurate.

Google Code Info:
Issue #: 41
Author: [email protected]
Created On: 2007-09-08T00:32:59.000Z
Closed On: 2007-09-11T11:30:04.000Z

method_missing in Model does not correctly handle e.g. Model.each

Now that methods like Model.each, Model.filter are to be handled by the
Model.method_missing, constructs like Book.each {|book| puts book.title}
will raise an exception because no block parameter is passed in method_missing.

Attached is a patch which includes the block; however I'm not sure if this
will interfere with filter expressions.

Google Code Info:
Issue #: 14
Author: [email protected]
Created On: 2007-05-31T23:47:20.000Z
Closed On: 2007-06-01T05:47:38.000Z

Model#set has string/symbol confusion

Currently the #set method will successfully complete an update when passed a hash with String
keys ie.

@person.set('name' => 'jeff')

but, the @values variable is not successfully updated since it is #merge'd and @values ends up
containing both symbol and string keys and the outdated values are returned.

is this an issue, or are you being explicit about only using symbol keys for @values

pseudo example:

load an instance of a model

record = SomeModel.first

look at one of its columns

record.value [=>] 5

set that column to something else

record.set('value' => 10)

look again NOT UPDATED!

record.value [=>] 5

could be solved by symbolizing the keys in #set in record.rb:93 ie..

def set(values)
this.update(values)
@values.merge!(values.inject({}){|hsh,pair| hsh[pair[0].to_sym]=pair[1]; hsh})
end

but I'm not a big fan of this practice

Google Code Info:
Issue #: 47
Author: [email protected]
Created On: 2007-09-18T09:59:28.000Z
Closed On: 2007-09-18T13:02:17.000Z

bin/sequel will exit if any SQLite3 exceptions are raised

All SQLite 3 exceptions inherit from Exception rather than StandardError,
so IRB won't catch them. I've searched the ruby mailing list and can't find
a solution thus far. There isn't a short method we can monkey-patch in IRB.
The relevant line is 152 of irb.rb where it does rescue StandardError,
ScriptError, Abort.

This issue probably isn't worth a lot of time, but it does make playing
with sequel incredibly frustrating (because if you do something wrong, irb
exits). This is so frustrating, I really don't know what to do about it
other than persuade the developer of the SQLite3 library that exceptions
should inherit from StandardError so they are caught by a rescue as people
expect.

Google Code Info:
Issue #: 10
Author: [email protected]
Created On: 2007-04-13T15:37:55.000Z
Closed On: 2007-04-13T19:34:29.000Z

0.1.9 MySQL field quoting totally destroys any multi-table selects

Using the MySQL adapter:

DB[:posts].left_outer_join(:authors, :id =>
:author_id).select("posts.title AS post_title", "authors.name AS author_name")

produces:

SELECT posts.title AS post_title, authors.name AS author_name FROM
posts LEFT OUTER JOIN authors ON (post.author_id = author.id);

Also:

left_outer_join(:things, "things.id" => "other_stuff.thing_id")

produces:

LEFT OUTER JOIN things ON things.id = other_stuff.thing_id.

Google Code Info:
Issue #: 21
Author: [email protected]
Created On: 2007-07-21T20:36:54.000Z
Closed On: 2007-07-21T21:34:17.000Z

Dataset#exclude produces incorrect logic with multiple arguments

d.exclude(:region => 'Asia', :name => 'Japan').select_sql should produce
SELECT * FROM test WHERE NOT ((region = 'Asia') AND (name = 'Japan')) but
the current implementation does not do this. Queries will not be what the
user intends, because NOT binds tightly. Included are specs that hopefully
catch all cases.

I changed the behaviour of the parenthesize argument to where_list so it
will always insure that the return string has parentheses around it if
parenthesize=true is given (it makes sure only one pair is around the
string if a hash of size 1 is given).

There's one failing spec that I found while writing this. I'm not sure how
to easily fix this case at the moment:
@dataset.exclude(:region => 'Asia').exclude(:name =>
'Japan').select_sql.should ==
"SELECT * FROM test WHERE NOT (region = 'Asia') AND NOT (name = 'Japan')"

Google Code Info:
Issue #: 8
Author: [email protected]
Created On: 2007-04-13T11:37:22.000Z
Closed On: 2007-04-13T15:20:15.000Z

Allow Dataset#from to take another dataset and add Dataset#*

In SQL you can SELECT FROM a subquery (i.e., it's part of the SQL-92
standard). This certainly works in SQLite. I've added a method
Dataset#to_table_reference so a dataset can give a suitable representation
of itself to be used in the FROM clause. In the very simple case of DB[:a],
it will just return the table name. In any other case, it will return a
subquery.

The second patch adds Dataset#*, which to me is much more intuitive.
(DB[:employee] * DB[:department]).filter(:employee__department_id =>
:department__department_id).sql results in the implicit inner join:
SELECT *
FROM employee, department
WHERE (employee.department_id = department.department_id)

I specifically don't spec for whether (DB[:a] * :b) works, although in the
current implementation it will.

opts[:from] is now always an array. Personally I'd rather see opts[:select]
to always be an array also. That way it matches the SQL syntax - select is
followed by a list of columns, from is followed by a list of table references.

Google Code Info:
Issue #: 7
Author: [email protected]
Created On: 2007-04-13T10:17:39.000Z
Closed On: 2007-04-13T15:17:39.000Z

DB[:table].where { :n == "#{1+2}" } fails

DB[:table].where { :n == "#{1+2}" }.sql
SequelError: Invalid expression tree: [:dstr, "", [:call, [:lit, 1], :+, [:array, [:lit, 2]]]]
from lib/sequel/../sequel/dataset/sequelizer.rb:222:in eval_expr' from lib/sequel/../sequel/dataset/sequelizer.rb:118:incall_expr'
from lib/sequel/../sequel/dataset/sequelizer.rb:177:in eval_expr' from lib/sequel/../sequel/dataset/sequelizer.rb:240:inpt_expr'
from lib/sequel/../sequel/dataset/sequelizer.rb:253:in proc_to_sql' from lib/sequel/../sequel/dataset/sql.rb:118:inexpression_list'
from lib/sequel/../sequel/dataset/sql.rb:215:in filter' from lib/sequel/../sequel/dataset/sql.rb:269:inwhere'
from (irb):41
from :0

reported by apeiros in #sequel @ irc.freenode.net

Google Code Info:
Issue #: 49
Author: [email protected]
Created On: 2007-09-20T16:36:05.000Z
Closed On: 2007-09-20T17:01:00.000Z

Database#literal is open to SQL injection attacks

I haven't attached a patch because the solution is some sort of fairly
trivial refactoring, and I'm not sure what is most appropriate.
Dataset#literal is of course fine. I'm thinking that perhaps it makes more
sense for literal to be a method in the Database class rather than Dataset.

I don't have MySQL around to test it, but I think that the MySQL adaptor
should have it's own literal definition to deal with backslash escape
sequences.

Google Code Info:
Issue #: 11
Author: [email protected]
Created On: 2007-04-14T15:13:09.000Z
Closed On: 2007-04-14T17:27:59.000Z

Sequel is missing specs

I'm cautious of leaping in there and writing tests in case you already have
some written but not in the repository yet, or have specific ideas about
how testing should be done.

I notice you're investigating the idea of a "scenario" testing library.
Have you seen test-spec? (http://test-spec.rubyforge.org). It uses
test/unit to provide a behaviour-driven framework, which looks a lot like
what you're getting at with scenario, unless I'm missing something?

Google Code Info:
Issue #: 1
Author: [email protected]
Created On: 2007-04-04T08:01:41.000Z
Closed On: 2007-04-22T19:08:02.000Z

Count does not seem to work on linux install

What steps will reproduce the problem?

  1. require 'rubygems'
  2. require 'sequel/sqlite'
  3. do a count on any dataset

What is the expected output? What do you see instead?
It's supposed to return the number of records in the dataset - I've tried
it with multiple versions and I receive the same error:

irb(main):004:0> @db[:users].count
NoMethodError: undefined method upcase' for nil:NilClass from /usr/lib/ruby/1.8/sqlite3/translator.rb:85:intype_name'
from /usr/lib/ruby/1.8/sqlite3/translator.rb:77:in translate' from /usr/lib/ruby/1.8/sqlite3/resultset.rb:135:innext'
from /usr/lib/ruby/1.8/sqlite3/resultset.rb:134:in map' from /usr/lib/ruby/1.8/sqlite3/resultset.rb:134:innext'
from /usr/lib/ruby/1.8/sqlite3/resultset.rb:160:in each' from /usr/lib/ruby/gems/1.8/gems/sequel-0.1.9.2/lib/sequel/sqlite.rb:107:in fetch_rows'
from /usr/lib/ruby/1.8/sqlite3/database.rb:277:in query' from /usr/lib/ruby/gems/1.8/gems/sequel-0.1.9.2/lib/sequel/sqlite.rb:48:inquery'
from
/usr/lib/ruby/gems/1.8/gems/sequel-0.1.9.2/lib/sequel/../sequel/connection_pool.rb:62:in
hold' from /usr/lib/ruby/gems/1.8/gems/sequel-0.1.9.2/lib/sequel/sqlite.rb:48:inquery'
from
/usr/lib/ruby/gems/1.8/gems/sequel-0.1.9.2/lib/sequel/sqlite.rb:104:in
fetch_rows' from /usr/lib/ruby/gems/1.8/gems/sequel-0.1.9.2/lib/sequel/../sequel/dataset.rb:153:in each'
from
/usr/lib/ruby/gems/1.8/gems/sequel-0.1.9.2/lib/sequel/../sequel/dataset/dataset_convenience.rb:16:in
single_value' from /usr/lib/ruby/gems/1.8/gems/sequel-0.1.9.2/lib/sequel/../sequel/dataset/dataset_sql.rb:560:in count'
from (irb):4

What version of the product are you using? On what operating system?
0.1.9.9, 0.1.9.8, 0.1.9.2 all had the same issues on Ubuntu Linux 7.04
(feisty)

Please provide any additional information below.

Google Code Info:
Issue #: 32
Author: [email protected]
Created On: 2007-08-19T19:42:52.000Z
Closed On: 2007-08-19T20:02:30.000Z

Schema generator should accept field sizes

Currently there's no way to define field sizes, apart from passing a
literal string as the field type, e.g.:

DB.create_table :items do
column :name, 'varchar(20)'
end

The schema generator should accept a size option, e.g.:

DB.create_table :items do
column :name, :varchar, :size => 20
end

Google Code Info:
Issue #: 20
Author: [email protected]
Created On: 2007-07-21T07:19:00.000Z
Closed On: 2007-08-10T18:21:12.000Z

primary_key implies :type => :serial which doesn't work as expected with sqlite

require 'rubygems'
require 'sequel/sqlite'
DB = Sequel('sqlite:/')

DB.drop_table :items if DB.table_exists?(:items)
DB.create_table :items do
primary_key :id
text :name
end

DB[:items] << {:name => 'one'}
DB[:items] << {:name => 'two'}

:id field remains empty in the database

DB[:items].print

Google Code Info:
Issue #: 22
Author: [email protected]
Created On: 2007-07-24T05:51:38.000Z
Closed On: 2007-08-13T05:50:58.000Z

PGconn is used unconditionally in schema.rb

schema.rb, line 39, looks like:

c << DEFAULT % PGconn.quote(column[:default]) if column.include?(:default)

where PGconn is applicable only to Postgress DB, but Sequel tries to use
it unconditionally for any type of DB, which leads to errors like
"uninitialized constant Sequel::Schema::PGconn"

Google Code Info:
Issue #: 24
Author: [email protected]
Created On: 2007-08-11T05:00:47.000Z
Closed On: 2007-08-11T19:33:30.000Z

some filter block examples don't work

These examples from the Sequel 0.2.0 announcement don't work for me.

irb(main):007:0> DB[:items].filter {:id => [1, 2, 3]}.sql
SyntaxError: compile error
(irb):7: parse error, unexpected tASSOC, expecting '}'
DB[:items].filter {:id => [1, 2, 3]}.sql
^
from (irb):7

irb(main):008:0> DB[:orders].filter {:price >= DB[:items].select(:price)}.sql
=> "SELECT * FROM orders WHERE (price >= #Sequel::MySQL::Dataset:0x2b280e2eab88)"

Also, an exclude block should generate "(NOT (condition))" instead of "NOT ((condition))"

irb(main):006:0> dataset.exclude {:value <= 100}.sql
=> "SELECT * FROM test WHERE NOT ((value <= 100))"

Google Code Info:
Issue #: 38
Author: [email protected]
Created On: 2007-09-05T15:22:11.000Z
Closed On: 2007-09-05T16:01:49.000Z

Dataset specs rely on hash ordering

At least these cases are broken:
1)
'A simple dataset should format an insert statement' FAILED
expected "INSERT INTO test (name, price) VALUES ('wxyz', 342)", got "INSERT
INTO test (price, name) VALUES (342, 'wxyz')" (using ==)
./spec/dataset_spec.rb:75:

'Dataset filters should work with hashes' FAILED
expected "SELECT * FROM test WHERE (name = 'xyz') AND (price = 342)", got
"SELECT * FROM test WHERE (price = 342) AND (name = 'xyz')" (using ==)
./spec/dataset_spec.rb:96:

I'm not sure what the best way to fix this is without overcomplicating the
specs.

Google Code Info:
Issue #: 4
Author: [email protected]
Created On: 2007-04-09T14:52:40.000Z
Closed On: 2007-04-10T09:05:43.000Z

Transactions fial for Mysql

What steps will reproduce the problem?

  1. Connect to a Mysql database (DB = Sequel...)
  2. Start a transaction (DB.transaction do...)
  3. Failure

The mysql driver does not define 'transaction', you can monkey patch it with:

class Mysql
def transaction
begin
query "begin"
yield
query "commit"
rescue Exception=>ex
query "rollback"
end
end
end

Google Code Info:
Issue #: 13
Author: [email protected]
Created On: 2007-05-10T18:49:29.000Z
Closed On: 2007-05-19T12:06:56.000Z

Sequel::DBI::Dataset#fetch_rows borked when it comes to empty column names

In Sequel::DBI::Dataset#fetch_rows, the column names are processed with
this line:
@columns = stmt.column_names.map {|c| c.to_sym}

This is broken in two places. The first is detailed (and patched) in
ticket #29.

The other is that it calls #to_sym on each column name. Calling #to_sym on
an empty string will raise an error. MSSQL returns an empty string for the
result of a calculated field that is not named using AS. And, you can not
name a field using AS if you use the #avg(field) notation. This raises two
questions:

  1. Should you be able to AS a column after being calculated with #avg?
  2. Should Symbols be used for column names, because empty column names are
    valid results in some (if not all) SQL servers?

Google Code Info:
Issue #: 30
Author: [email protected]
Created On: 2007-08-16T20:11:59.000Z
Closed On: 2007-08-17T06:04:50.000Z

ODBC timestamp data-type fails for dates outside &lt; 1970 or &gt; 2040

ODBC date/time datatypes come in three flavours: TIMESTAMP, DATE and TIME,
in sequel/odbc.rb these are mapped to Time, Date and Time objects
respectively.

However, date/times (TIMESTAMPS) before 1970 or after 2040 fall outside the
range of the Ruby Time class. This patch converts an odbc TIMESTAMP to a
DateTime instance.

Google Code Info:
Issue #: 16
Author: [email protected]
Created On: 2007-05-31T23:52:40.000Z
Closed On: 2007-06-01T05:47:16.000Z

PrettyTable doesn't calculate column sizes for Model derived classes.

PrettyTable doesn't calculate maximum column-sizes correctly when called
from a Model derived class:

Sequel::PrettyTable.print DB[:book], [:id,:title,:author]
produces a really pretty table

Sequel::PrettyTable.print Book, [:id,:title,:author]
only calculates the maximum length of the column names

Attached is a patch to correct it.

cheers,

snok

Google Code Info:
Issue #: 15
Author: [email protected]
Created On: 2007-05-31T23:51:42.000Z
Closed On: 2007-06-01T05:47:56.000Z

execute performs only first instruction in SQLite

sqlite3-ruby's Database class have two "execute" methods:

execute(sql) executes only FIRST statement of SQL provided

execute_batch(sql) executes all of them

In Sequel, erroneous use of SQLite3::Database#execute causes problems at
least while creating tables with indexes (Sequel generates sql string with
table and indexes creation statements, SQLite performs only first,
creating table only).

Change need (at least, but maybe something more):

sqlite.rb, line 37:
was: @pool.hold {|conn| conn.execute(sql)}
now: @pool.hold {|conn| conn.execute_batch(sql)}

Google Code Info:
Issue #: 28
Author: [email protected]
Created On: 2007-08-14T20:47:49.000Z
Closed On: 2007-08-15T13:38:37.000Z

Sequel does not support SELECT DISTINCT (patch included)

This patch implements Dataset#uniq and an alias Dataset#distinct that will
mean a SELECT DISTINCT query is generated. It's not possible to un-uniq a
Dataset. It's trivial to add an #undistinct of allow distinct(false), but
I'm not sure about it

Google Code Info:
Issue #: 2
Author: [email protected]
Created On: 2007-04-08T12:26:08.000Z
Closed On: 2007-04-08T16:39:45.000Z

Sequel does not support OFFSET clauses (patch included)

Neither LIMIT or OFFSET are (to my knowledge) in the SQL standard, but they
are supported across all databases with existing adaptors in Sequel. An
OFFSET is only valid is specified with a LIMIT, so it would not sensible to
provide a separate method for it (it would be confusing to the user).

There are two common forms of supported LIMIT and OFFSET, either LIMIT x
OFFSET y or LIMIT offset, limit. I think it would be confusing to have a
Dataset#limit(x, y) where x is sometimes the limit and sometimes the
offset. It would perhaps be nice to use Ruby's array slicing semantics, so
dset[4] will perform a suitable query and give you the result immediately
much like #first, dset[4..4] will not. dset[offset, lim] would be a natural
alternate representation like with Ruby's Array. I haven't tried to make
Dataset#[] behave this way. I'm unconvinced of the current behaviour of
that method. Perhaps it makes sense as an alias to where, but I don't see
how it makes sense to return the first result.

Google Code Info:
Issue #: 3
Author: [email protected]
Created On: 2007-04-08T15:11:52.000Z
Closed On: 2007-04-08T16:40:42.000Z

Dataset::import doesn't accept a dataset as value

The documentation claims that the following should work:
dataset.import([:x, :y], other_dataset.select(:a, :b))

If you actually try to do this, it fails with SQL like:
INSERT INTO table1(x,y) VALUES (SELECT a,a FROM table2)

0.2.0.2 not tagged

What steps will reproduce the problem?

  1. svn ls http://ruby-sequel.googlecode.com/svn/tags
    2.
    3.

What is the expected output? What do you see instead?
I expected to see 0.2.0.2 in that list. The most recent tag was 0.2.0.1.

What version of the product are you using? On what operating system?
0.2.0.2 on Mac OS X

Please provide any additional information below.
Not a big deal, just details.

Google Code Info:
Issue #: 44
Author: [email protected]
Created On: 2007-09-13T20:26:17.000Z
Closed On: 2007-09-14T14:23:06.000Z

Dataset#empty?

Subj. Not a bug, just a suggestion.

Google Code Info:
Issue #: 46
Author: [email protected]
Created On: 2007-09-15T15:56:46.000Z
Closed On: 2007-09-15T18:27:07.000Z

Sequel::Model.create_table now (Sequel 0.1.9.12) broken

  1. Setup a Model with class MyModel < Sequel::Model.
  2. Define a schema using Sequel:::Model.set_schema do.
  3. Invoke Sequel::Model.create_table

I'd expected that this 'll create a table for my model, but instead I get NoMethodError right into my
face telling me that Sequel::Schema::Generator#create_sql does not exist.

I'm using Sequel 0.1.9.12...

Google Code Info:
Issue #: 37
Author: [email protected]
Created On: 2007-08-28T11:17:34.000Z
Closed On: 2007-08-28T11:47:55.000Z

use the model's setter methods to set @values

It's often useful to redefine how default setter methods for your Model's attributes.

An example of this is hashing a password on the way in i.e.

User.create(:password => 'jeff123')

By using the setter methods (or method_missing's ones) during initialize this becomes possible.

It also opens up the ability to throw in other assignments during #create/#new, often useful if
you want to convert or process something.

User.create(:some_other_method => true)

Google Code Info:
Issue #: 48
Author: [email protected]
Created On: 2007-09-18T13:22:06.000Z
Closed On: 2007-09-18T18:45:53.000Z

[PATCH] Sequel ADO Adapter

I have included a Sequel ADO adapter based roughly on the code in ADO.rb
from DBI, and also from the rubyonwindows blog. The code is still rough,
and needs some testing.

Google Code Info:
Issue #: 31
Author: [email protected]
Created On: 2007-08-17T15:54:37.000Z
Closed On: 2007-08-17T16:45:17.000Z

Sequel does not support GROUP BY or HAVING (patch included)

I've added support for GROUP BY and HAVING in the attached patches. A
future refactoring might rename methods such as Dataset#where_list to
something more generic (as the same formatting is used for the HAVING
clause). Dataset#filter is no longer an alias for where. #filter will apply
conditions to the WHERE or HAVING clause depending on whether the dataset
has been grouped or not. It is assumed that if you're using #where or

having, you specifically want your conditions put into those clauses. An

exception is raised if you try to add a having clause to an ungrouped
dataset through #having, or if you try to modify the where clause on a
grouped dataset through #where. Some basic specs are included in the second
patch.

Comments/suggestions are very welcome if you don't like this approach.

Google Code Info:
Issue #: 5
Author: [email protected]
Created On: 2007-04-09T18:14:42.000Z
Closed On: 2007-04-10T09:03:32.000Z

Sequel does not support multiple tables in the FROM clause

More accurately, it does only if you do dset.from([:t1, :t2]).

I've been feeling a little uncertain about this, mainly because it doesn't
seem obvious that this would result in a cartesian product, and I don't
like encouraging ambiguous code. Specs are included.

Google Code Info:
Issue #: 6
Author: [email protected]
Created On: 2007-04-10T16:53:03.000Z
Closed On: 2007-04-10T18:57:11.000Z

Add a "this" or "self_set" method to model

It would be nice to see a "this" or "self_set" method in the Model class, e.g.:

def this
model.dataset.filter(:"#{model.table_name}__#{primary_key}" => @pkey)
end

The method could then be used to factor out line 185, "model.filter(primary_key => @pkey)",
and similarly, line 216.

It would be useful in other model instance methods as well. For example:

def owns_blog?(blog_id)
this.left_outer_join(:memberships, :user_id => :id).filter(:blog_id => blog_id, :owner =>
true).first != nil
end

Google Code Info:
Issue #: 42
Author: [email protected]
Created On: 2007-09-10T02:44:19.000Z
Closed On: 2007-09-11T11:31:41.000Z

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.