Bug: erlsql: group_by, order_by multiple fields

0 views
Skip to first unread message

Buggaboo

unread,
Aug 12, 2008, 9:07:24 AM8/12/08
to erl...@googlegroups.com
Hey,

I have a table called archiveT.

SQL allows order_bys on multiple fields. Erlsql doesn't support this
syntax, as far as I can tell.

It has three fields (year:int, month:int, entry_id).

The following is my function to generate a list <a
href="/blog/archives/Year/Month">s for a navigator component.

<code>
...
%% TODO - Fix the query to sort ascending, sorted by desc year, month
archives (A) ->
Query =
{
esql,
{select,distinct,[year,month],{from,archive}
% [{order_by,{year,desc}}] %% correct, but not what I want
% [{order_by,{year,month,desc}}] %% sql syntax error
% [{order_by, {[year,month], desc}}] %% erlang error
}
}
, F = fun ( [Year, Month] ) ->
YStr = integer_to_list(Year)
, MStr = integer_to_list(Month)
, {
list_to_binary
([erlyweb:get_app_root(A),<<"blog/archives/">>,YStr,$/,MStr])
, list_to_binary([YStr,$/,MStr])
}
end
, {data,{mysql_result, _, Results, _, _}} =
erlydb_mysql:q(Query)
, Archives = [ F(R) || R <- Results ]
, [ {ewc, html_list, hyperlist_item, [A, L, Ar]} || {L, Ar} <-
Archives ].
...
</code>

Notice the {esql, select...} statement with the outcommented order_bys.

The syntax I suggest to allow multiple fields in order_by, group_by
sql-expressions:

select/whatever ... [{group_by, {[fieldi,...,fieldk], desc/asc}}]

I'll have a look at it in two weeks, maybe solve it myself and send the
patches... but I have to start packing for my vacation now.

Regards,

Bgb

Buggaboo

unread,
Aug 13, 2008, 9:57:31 AM8/13/08
to erl...@googlegroups.com
Hey hey,

I should really be packing now but I could'nt resist hacking erlsql...
gawds, my gf is going to kill me.

I have no time to test, but I think it will work.

I think it's safe against injections... need someone to verify it for me
though.

Have a good one.

order_by_multiple_cols.patch

Buggaboo

unread,
Aug 27, 2008, 6:22:39 AM8/27/08
to erlyweb
Consider this problem resolved.

Apparently this construction:

<erl>
Query =
{
esql,
{... % some select, delete, insert... statement
, [{order_by, [{field_one, desc}, {field_two, desc}]}]
}
}
</erl>

Is already supported. I should've tested thoroughly first.

Impressive work, Yariv.

Yariv Sadan

unread,
Aug 27, 2008, 12:13:43 PM8/27/08
to erl...@googlegroups.com
Oh yeah, I should have mentioned that earlier. My bad.
Reply all
Reply to author
Forward
0 new messages