On Wednesday, July 4, 2012 2:39:11 PM UTC-7, Rodrigo Rosenfeld Rosas wrote:
>
> There is a critical part in my application where I need to generate the
> SQL by myself.
>
> It would be easier to process the results of this generated dynamic SQL if
> I could iterate over each row by index.
>
> I mean, instead of having DB[sql].all to return something like [{tid: 23,
> tname: 'Some name'}, ...] I'd prefer to get something like: [[23, "Some
> name"], [...], ...].
>
> Currently I'm doing something like below, but I'd like to know if there is
> some method that already does that and that I'm not aware of:
>
> builder = QueryBuilder.new(params)
> json = DB[builder.sql].map do |r|
> r = r.map{|k, v| v} # this is the trick I'm currently using
> builder.columns.map do |c|
> raw = r.shift
> case c[:type]
> when 'range' then [raw, r.shift]
> ...
> else raw
> end
> end
>
>
You can provide an argument to map:
ds = DB[builder.sql]
ds.map(ds.columns)
Your current code (r = r.map{|k, v| v}) is not guaranteed to be portable
across adapters (as adapters make no guarantee that hash entry order is the
same as column order), and certainly is unlikely to work on ruby 1.8
because hashes aren't ordered there.
Jeremy
--
You received this message because you are subscribed to the Google Groups
"sequel-talk" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/sequel-talk/-/bzz_DrJHuKsJ.
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/sequel-talk?hl=en.