On 28/04/11 00:29, Aaron Patterson wrote:
On Tue, Apr 26, 2011 at 09:51:30PM +0200, Arthur Taylor wrote:
Aaron, all,

Did anyone have any strong feelings about the following feature - is it
something that it's going to be possible to get included (possibly in a
prettied up form), or am I better forking / monkey-patching?
The idea with the placeholders is that you could have one query which
serializes differently to SQL (or through any of the visitors)
depending on the dynamic value of the placeholders. The effect I want
(and am using in my local repo) is not to have to define the values of
all the query terms at query build time:

@placeholders = { :username =>  "bob" }

query =
@users.project(@users[:id]).where(@users[:name].eq(Arel::Nodes::Placeholder.new(:username,
@placeholders)))
query.to_sql =>   "SELECT `users`.`id` FROM `users` WHERE
`users`.`name` = 'bob'"

@placeholders[:username] = "adam"
query.to_sql =>  "SELECT `users`.`id` FROM `users` WHERE `users`.`name`
= 'adam'"

Make sense? Sound useful? I'm not sure that's the most elegant API in
the world (in fact I'm pretty sure it's not), but I'm open to
suggestions as to pretty ways to achieve the same thing.

The other design I considered would be:

@placeholders = Arel::Placeholders.new(:username =>  "bob")

query =
@users.project(@users[:id]).where(@users[:name].eq(@placeholders.for_key(:username)))
query.to_sql =>   "SELECT `users`.`id` FROM `users` WHERE `users`.`name`
= 'bob'"

@placeholders.update(:username =>  "adam")
query.to_sql =>  "SELECT `users`.`id` FROM `users` WHERE `users`.`name` =
'adam'"

which seems a little more elegant.
No, I don't want to include this in ARel.  ARel manages an SQL AST.
Since "Placeholder" isn't part of SQL, I don't want to support it.

But that doesn't mean you can't subclass the visitor and add it, or
monkeypatch the ToSql visitor and add support.  I certainly won't break
that.

Understood. Bit of a shame, but I can understand the point of keeping it pure. I'll create a separate Gem that wraps / extends Arel and includes the additional functionality.

Thanks,

Arthur

--
You received this message because you are subscribed to the Google Groups "Ruby on 
Rails: Core" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-core?hl=en.

Reply via email to