From: Logan Capaldo Date: 2006-12-06T09:31:44+09:00 Subject: Re: Lisp comprehensions => SQL 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 > article.title =~ "%Foo Title%" && > (author.name == 'Ezra' || author.name == 'Fab') > } > BTW, I think about some common "over-DSL-ing" between DSL-addict. > It's something like "Syntactic saccharin"[1] - seems to be "very cool", but > no added value. > > V. > > 1. http://en.wikipedia.org/wiki/Syntactic_sugar#Syntactic_saccharin >