syntax to three statements (INSERT, SELECT, and UPDATE) that seem
useful for most people, who are performing queries and
Criteria[3]
measure
because it hasn’t attempted to deal with serious CREATE TABLE
statements,
where a lot of syntax concerning types, keys and sequences is much
more
variable.
–Ken
Hey Ken-
You may be interested in taking a look at a library I wrote called
ez-where[1]. It is distributed as a plugin for rals and focuses on
building the WHERE clause for active record queries. But most of it
can be used for generating where clauses and could be used wihout
rails as a stand alone where generator. Its focus is on having a nice
ruby dsl style syntax for declaring the conditions as objects.
Here are a few simple examples from the test cases:
def test_cc_with_multiple_statements
a = c { foo == 'bar' }
b = c { baz =~ '%qux%' }
c = c { age > 20 }
d = c { gemz === (1..5) }
expected = ["((foo = ?) AND (baz LIKE ?)) OR ((age > ?) AND
NOT (gemz IN (?)))", “bar”, “%qux%”, 20, [1, 2, 3, 4, 5]]
assert_equal expected, (a + b | c - d).to_sql
end
def test_multi_clause
expected = ["(my_table.title LIKE ? OR my_table.subtitle
LIKE ? OR my_table.body LIKE ? OR my_table.footnotes LIKE ? OR
my_table.keywords LIKE ?)", “%package%”, “%package%”, “%package%”, “%
package%”, “%package%”]
multi = EZ::Where::MultiClause.new
([:title, :subtitle, :body, :footnotes, :keywords], :my_table)
multi =~ ‘%package%’
assert_equal expected, multi.to_sql
cond = EZ::Where::Condition.new :my_table
cond.any_of(:title, :subtitle, :body, :footnotes, :keywords)
=~ ‘%package%’
assert_equal expected, cond.to_sql
cond = EZ::Where::Condition.new :my_table
cond.any_of(:title, :subtitle, :body, :footnotes, :keywords)
=~ ‘%package%’
cond.all_of(:active, :flagged) == true
expected = ["(my_table.title LIKE ? OR my_table.subtitle
LIKE ? OR my_table.body LIKE ? OR my_table.footnotes LIKE ? OR
my_table.keywords LIKE ?) AND (my_table.active = ? AND
my_table.flagged = ?)", “%package%”, “%package%”, “%package%”, “%
package%”, “%package%”, true, true]
assert_equal expected, cond.to_sql
expected = ["(my_table.title LIKE ? OR my_table.subtitle
LIKE ? OR my_table.body LIKE ? OR my_table.footnotes LIKE ? OR
my_table.keywords LIKE ?) OR (my_table.active = ? AND
my_table.flagged = ?)", “%package%”, “%package%”, “%package%”, “%
package%”, “%package%”, true, true]
assert_equal expected, cond.to_sql(:or)
end
Cheers-
-Ezra
[1] svn://rubyforge.org/var/svn/ez-where