On Wed, 06 Dec 2006 09:31:44 +0900, Logan Capaldo wrote: > On Wed, Dec 06, 2006 at 07:27:31AM +0900, Victor Zverok Shepelev wrote: >> From: Logan Capaldo [mailto:logancapaldo / gmail.com] >> Sent: Wednesday, December 06, 2006 12:15 AM >> >To: ruby-talk ML >> >Subject: Re: Lisp comprehensions => SQL >> > >> >On Wed, Dec 06, 2006 at 06:58:49AM +0900, Victor Zverok Shepelev wrote: >> >> Hi all. >> >> >> >> Random idea, just for fun - using "list comprehensions" for SQL queries >> >> generation. >> >> >> >> employees = Table.new(:id, :name, :sec_name, :salary, :age) >> >> >> >> employees.select{|e| e.name == 'John' && e.salary > 50}.sort_by{|e| >> >> e.age}[2,10] >> >> >> >> #generates "select * from Employees where name = 'John' and salary > 50 >> >> order by age limit 2,10" >> >> >> >> employees.select{|e| e.salary < 150}.count >> >> >> >> #generates "select count(*) from Employees where salary < 150 >> [...] >> >The ez_where plugin for rails does this. >> >http://brainspl.at/articles/2006/01/30/i-have-been-busy >> >> Yeah, seems to be close to my proposition. >> What I dislike in the solution (and in Mongoose, which repeats the solution) >> that conditions is NOT plain Ruby, but custom DSL. >> >> #ez_where: >> >> articles = Article.ez_find(:all, :include => :author) do |article, author| >> article.title =~ "%Foo Title%" >> author.any do >> name == 'Ezra' >> name == 'Fab' >> end >> end > There's a reason for this, see below >> >> #my idea, thus lacking knowledge about tables relationships: >> >> (articles+authors).select{|article, author| >> article.author_id == author.id && >> article.title =~ "%Foo Title%" && >> (author.name == 'Ezra' || author.name == 'Fab') >> } >> >> Seems to read as "just ruby". >> What do you think about? >> > There's a problem: >> (articles+authors).select{|article, author| > articles+authors perfectly doable. Same with select >> article.author_id == author.id && > article.authod_id == author.id # doable with appropiate def's of > author_id, #==, id, etc. > What's _NOT_ doable is &&. You can't override > that. You have to use another operator or something like any do ... end That's not so much of a problem, as one can simply use & (which is implemented to be a non-short-circuit version of && on booleans anyway, so it's not all that out of place) A much bigger problem is !, not and !=. For this, I resigned myself to using Dominik Bathon's RubyNode-based solution in my own SqlStatement library (which you should definitely check out). --Ken -- Ken Bloom. PhD candidate. Linguistic Cognition Laboratory. Department of Computer Science. Illinois Institute of Technology. http://www.iit.edu/~kbloom1/