Skip to content

Instantly share code, notes, and snippets.

@daniello
Created May 11, 2009 09:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daniello/109936 to your computer and use it in GitHub Desktop.
Save daniello/109936 to your computer and use it in GitHub Desktop.
bs_by_a_id(A_id) ->
find(qlc:q([ B || B=#b{f_id=F_id} <- mnesia:table(b), F_id == A_id])).
as_with_bs() ->
find(
qlc:q([ {A,bs_by_a_id(A#a.id)} || A <- mnesia:table(a)])
).
top_as_with_some_bs(Limit) ->
top(
qlc:q([ {A,bs_by_a_id(A#a.id)} || A <- mnesia:table(a)]),
Limit,
fun(A1,A2) -> A1 < A2 end
).
% --- utils
find(Q) ->
F = fun() -> qlc:e(Q) end,
transaction(F).
% --- it returns top Limit results from query Q ordered by Order sort function
top(Q, Limit, Order) ->
{atomic, Res} = mnesia:transaction(fun() ->
OQ = qlc:sort(Q, [{order,Order}]),
QC = qlc:cursor(OQ),
Res = qlc:next_answers(QC, Limit),
qlc:delete_cursor(QC),
Res
end),
Res.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment