[erlang-questions] QLC cursor with QLC query using nested QLC.

Daniel Kwiecinski daniel.kwiecinski@REDACTED
Mon May 11 12:07:32 CEST 2009


Hi,

   I'm trying to implement kind of SELECT COUNT(B.ID) C, A.* FROM A, B WHERE
A.ID = B.F_ID GROUP BY A.ID HAVING C > 0

So I need to make join with count on details. The thing is, I want also to
involve sorting and limiting with is but it seams qlc cursor don't like my
approach. *as_with_bs() *works fine but* **top_as_with_some_bs(10) *returns*
:**

{aborted,{badarg,[{ets,first,[106291236]},
                  {mnesia_tm,arrange,3},
                  {mnesia_tm,t_commit,1},
                  {mnesia_tm,apply_fun,3},
                  {mnesia_tm,execute_transaction,5},
                  {erl_eval,do_apply,5},
                  {shell,exprs,6},
                  {shell,eval_exprs,6}]}}*

QLC with nested QLC queries works fine as long not used with cursors.
Could anyone help me with it?

Here is what I wrote: (http://gist.github.com/109936)

*
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.*

Regards,
Daniel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20090511/e49d10d4/attachment.htm>


More information about the erlang-questions mailing list