public
Last active

  • Download Gist
gistfile1.hrl
Erlang
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
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.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.