[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

ANN: Criteria 1.1

Ryan Pavlik

10/5/2003 12:37:00 AM

Criteria
========

Updates
-------

1.1 - Feature: SQLTable now abstracts CREATE TABLE, INSERT, UPDATE,
DELETE, and DROP TABLE. See the reference for details.
Example:

(tbl.bday == "2003/10/04").update(:age => tbl.age + 1)

# => "UPDATE Person SET age = (Person.age + 1) WHERE
# (Person.bday = '2003/10/04')"


Feature: MysqlTable updated to support new SQLTable features.
Also, #select_parse now parses output into Ruby types if
specified with create().


Feature: Criterion superclass now uses #x? to generate
Placeholder arguments, and #[] for parameterized queries.
See reference for details. Example:

TBL_AGE_RANGE = (tbl.age > tbl.x?) & (tbl.age < tbl.x?)
TBL_AGE_RANGE.order_by = [:name, :age]

TBL_AGE_RANGE[20, 30].select

# => "SELECT * FROM Person WHERE ((Person.age > 20) AND
# (Person.age < 30))"


Bugfix: Selections from a FileTable query are now sorted even
if the key is not part of the selection. Thanks to Brett
Norris for finding this one.


Description
-----------

Criteria is a module for abstracting queries to various data sets.
For instance, you might have a flat text file, or an array of Ruby
objects, or a SQL database, and wish to perform the same query on
any given source, without different versions of code for each.

This module was inspired by the work of flgr (on #ruby-talk) on
Junction, and the ENV.var work by h9k (also on #ruby-talk).

Here are some examples:

idx1 = SQLTable.new("orders")
q1 = (idx1.price > idx1.paid) & (idx1.duedate < Time.now.to_i)

q1.limit = 5
q1.order = :ASC
q1.order_by = idx1.name, idx1.age

puts q1.select

# => SELECT * FROM orders WHERE ((orders.price > orders.paid) AND
# (orders.duedate < 1062616643)) LIMIT 5 ORDER BY orders.name,
# orders.age ASC


The generic Table superclass with the same query:

idx2 = Table.new
q2 = (idx2.price > idx2.paid) & (idx2.duedate < Time.now.to_i)

puts q2.inspect
puts q1.inspect

# => (& (> :price :paid) (< :duedate 1062616719))
# => (& (> :price :paid) (< :duedate 1062616719))

The very simple 'mysql' table included works like SQLTable, except
it actually returns a MysqlRes for immediate use. There are other
included table types as well.

You can find Criteria at the following location:

http://mephle.org...

--
Ryan Pavlik <rpav@mephle.com>

1 Answer

Ryan Pavlik

10/5/2003 1:04:00 AM

0

On Sun, 5 Oct 2003 09:36:34 +0900
Ryan Pavlik <rpav@mephle.com> wrote:

> TBL_AGE_RANGE = (tbl.age > tbl.x?) & (tbl.age < tbl.x?)
> TBL_AGE_RANGE.order_by = [:name, :age]
>
> TBL_AGE_RANGE[20, 30].select
>
> # => "SELECT * FROM Person WHERE ((Person.age > 20) AND
> # (Person.age < 30))"

Of course, not 10 minutes after posting, I see a bug demonstrated in
my own post. Figures. This should have the following result:

# => "SELECT * FROM test WHERE ((test.age > ?) AND (test.age < ?))
# ORDER BY name, age"

This is fixed in 1.1a, now posted.

--
Ryan Pavlik <rpav@mephle.com>

"And I thought all thieves could do in a fight
was run away." - 8BT