I need something a long these lines for doing SQL queries, but it
would be great to take it one step further toward a general purpose
LINQ-like library.
I have too much on plate right now to pursue this by myself, but I be
happy to help out anyone who wants to run with it.
Interesting. I tried translating this idea to a more complex example.
Given pseudo-LINQ like:
r = query do
from :c => :customer, :a => :account, :b => :bank
select b.name
where (( c.firstname == first1 ) |
( c.firstname == first2 ) ) &
( c.lastname =~ last ) &
( a.owner == c ) &
( a.bank == b )
end
How would you work that? I played with it a bit and thought of:
customers = from(:customers)
a = from(:account)
b = from(:bank)
r = customers.select { |c|
(c.firstname == first1 ||
c.firstname == first2) &&
c.lastname =~ last &&
a.owner == c &&
a.bank == b
}.collect { |c| c.name }
I generally like the idea, albeit the modus operandi of LINQ has been
to reflect as much as possible the original domain language (sql, xml,
etc.) in the target language. Nonetheless, I can see the advantage of
expressing queries in native Ruby instead, and having that translated
to the domain language. But is Ruby ultimately expressive enough? We
may end up adding enough new syntax (eg. inner and outer joins), that
it will largely overshadow the native syntax anyway.
r = select(customer, account, bank) do |c,a,b|
(( c.firstname == first1 ) ||
( c.firstname == first2 )) &&
( c.lastname =~ last ) &&
( a.owner == c ) &&
( a.bank == b )
end.
collect(customer){ |b| b.name }
#…
But is Ruby ultimately expressive enough? We
may end up adding enough new syntax
i think ruby syntax is more than enough (imho). In a way, i’d like to think like active records when it comes to databases (in mfowler’s sense, not rails) and use ruby to implem.
How does Fowler’s approach differ from Rails? I didn’t know there was
an essential distinction.
r = query(customer, account, bank) do |c,a,b|
select (( c.firstname == first1 ) |
( c.firstname == first2 ) ) &
( c.lastname =~ last ) &
( a.owner == c ) &
( a.bank == b ) .
collect b.name
end
#…
But is Ruby ultimately expressive enough? We
may end up adding enough new syntax
i think ruby syntax is more than enough (imho). In a way, i’d like to
think like active records when it comes to databases (in mfowler’s
sense, not rails) and use ruby to implem.
Hey, nice. That’s pretty close to botp’s thoughts.
Though again, I see potential limitations in this approach, eg. how to
select from multiple tables?
Also I’m a bit surprised to see that it uses ParseTree --that seems
like overkill on the implementation side of things. How stable is
Ambition at this point? If I were to use it for my project, I would
need to a) maybe create a DBI adapter and 2) add support for ‘insert’
and ‘update’ (along side ‘select’, ‘slice’ and ‘sort’). Possible?
Also I’m a bit surprised to see that it uses ParseTree --that seems
like overkill on the implementation side of things.
I believe its use of ParseTree is because != and !~ are not methods in
Ruby 1.8. Without being able to define != separately from ==, having
them translate to different things is a difficult task.