ANN: Sequel 0.3 Released

S

Sharon Rosner

Sequel version 0.3 has just been released. This release includes
support for retrieving tuples as arrays instead of hashes, a more
mature model implementation with support for composite primary keys
and caching, and a new way for quickly retrieving records, as well as
many other minor improvements and bug fixes.

Sequel is a lightweight ORM library for Ruby. Sequel provides thread
safety, connection pooling and a simple and expressive API for
constructing database queries and table schemas.

Following is a discussion of the major changes:

=== Retrieving tuples as arrays

By default Sequel represents record tuples using Ruby hashes. Some
people, however, have expressed a desire to retrieve records as
arrays, in order to have a predictable column order. Sequel version
0.3 can provide array tuples, and also extends them with key access
(in a similar fashion to Ara Howard's arrayfields). To use array
tuples you should add the following line to your code:

Sequel.use_array_tuples

Working with the returned array tuples is basically the same as
working with hash tuples:

DB[:items].first.class #=> Array
# key access lets you treat arrays as if they were hashes
DB[:items].each {|r| p r[:name]}
DB[:items].first.each_pair {|k, v| p [k, v]}

=== Sequel models: composite primary keys, caching

The model implementation has been largely refactored, documented and
spec'd out and is now much more mature. Sequel models now support
composite primary keys, which are useful especially for auxiliary
tables:

class Attribute < Sequel::Model:)attributes)
set_primary_key [:node_id, :kind]
end

class Node < Sequel::Model:)nodes)
def attribute(kind)
Attribute[pkey, kind]
end
end

Also new in this release is support for caching using memcached:

require 'memcache'
CACHE = MemCache.new 'localhost:11211', :namespace => 'dbstuff'

class Attribute < Sequel::Model:)attributes)
set_primary_key [:node_id, :kind]
set_cache CACHE, :ttl => 3600
end

Attribute[1, 2] # database hit
Attribute[1, 2] # cache hit

=== New API for retrieving records with plain SQL

While Sequel is great for expressing queries in Ruby, sometimes you
just want to retrieve records with plain SQL. The new Database#fetch
method lets you do that:

DB.fetch('select * from items') {|r| p r}

The #fetch method will return an enumerator if a block is not given,
letting you do stuff like:

names = DB.fetch('select * from items').map {|r| r[:name]}
records = DB.fetch('select * from items').to_a

You can also create parameterized queries and enjoy protection from
SQL injection:

DB.fetch('select * from items where name like ?', client_name).to_a

The Database#[] method has also been modified to act as shortcut to
#fetch if a string is given:

DB['select * from items'].each {|r| p r}

=== Other improvements and bug fixes

* Added Database#logger= method for setting the database logger
object.

* Implemented Database#disconnect method for all adapters.

* Changed Database#<< to strip comments and whitespace only when an
array is given.

* Added support for old and new decimal types in MySQL adapter, and
updated MYSQL_TYPES with MySQL 5.0 constants (#72).

* Fixed bug in postgres adapter where a LiteralString would be
literalized as a regular String.

* Fixed SQLite insert with subquery (#68).

* Rewrote SQLite::Database#transaction to use sqlite3-ruby library
implementation of transactions.

* Model hooks can now be prepended or appended in order to change
order of execution.

* Enhanced Model.find method to accept block filters.

* Added stock transforms to Dataset#transform. Refactored
Model.serialize.

* Enhanced Dataset#first to accept a filter block. Model#find can also
now accept a filter block.

* Changed Dataset#join methods to correctly literalize values in join
conditions (#70).

* Fixed #filter with ranges to correctly literalize field names (#69).

* Added Dataset#to_csv method.

* Extended Dataset#from to auto alias sub-queries and accept hash for
aliasing tables.

* Changed Schema::Generator#primary_key to accept calls with the type
argument omitted.

=== More info

Sequel project page:
<http://code.google.com/p/ruby-sequel>

Sequel documentation:
<http://sequel.rubyforge.org>

Join the Sequel-talk group:
<http://groups.google.com/group/sequel-talk>

Install the gem:
sudo gem install sequel

Or check out the source and install manually:
svn co http://ruby-sequel.googlecode.com/svn/trunk sequel
cd sequel
rake install
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top