[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: Lisp comprehensions => SQL

Victor 'Zverok' Shepelev

12/6/2006 10:30:00 AM

From: khaines@enigo.com [mailto:khaines@enigo.com]
Sent: Wednesday, December 06, 2006 12:14 PM
>On Wed, 6 Dec 2006, Victor "Zverok" Shepelev wrote:
>
>>>> employees.select{|e| (e.name == 'John') & (e.salary > 50)}.sort_by{|e|
>>>> e.age}[2..10]
>>>>
>>>> The latter is "just Ruby".
>>>>
>>>
>>> Actually, you could do that second one with Kansas. With the current
>>> Kansas, it'd be:
>>>
>>> dbh.select(:Employees) {|e| (e.name == 'John') & (e.salary >
>>> 50)}.sort_by{|e| e.age}[2..10]
>>>
>>> The difference, though, is that it won't do all of that on the database.
>>>
>>> The database will do "select * from employees where e.name = 'John' and
>>> e.salary > 50" and then the results will be sorted by Ruby and the set
>>> extracted by Ruby.
>>>
>> This is predictable, but not very interesting.
>
>Think about the syntax of it, though.
>
>From a ruby perspective, you call select(), then sort_by(), then []
>
>Three separate method calls, and there is no way any of them can know
>which is the last one. So there's no way to assemble the SQL call you
>would like to see, where a single query is assembled to implement what is
>implied by those three separate method calls.
>
>To me, the logical thing is to have the select() call be the one that
>actually interacts with the database, and I am using "just Ruby" inside
>that select() block, and I know what I am getting back from each method
>call in the chain.
>

It's a matter of philosophy.

Trying to read this:

dbh.select(:Employees) do |e|
sort_by(e.age)
limit(2,10)
(e.name == 'John') & (e.salary > 50)
end

cause a bunch of questions from Ruby perspective. I "can't understand"
(intuitively, I mean) "what would be sorted", "what would be limited",
"where condition applies".

To the contrary, trying to read this

employees.select{|e| (e.name == 'John') & (e.salary > 50)}.sort_by{|e|
e.age}[2..10]

is very familiar for any rubyist; but also cause a bunch of questions from
SQL perspective (would it be performed in one request or several? How many
data would be selected? What's the overhead?)

Which is better? Any opinion is possible, there is no "the best for
everyone". Personally I dislike your variant as it is neither SQL now Ruby.
But you arguments are completely understandable.

V.


2 Answers

khaines

12/6/2006 11:15:00 AM

0

khaines

12/6/2006 11:32:00 AM

0