[erlang-questions] qlc parse error (?)

Paul Mineiro <>
Sat May 5 23:31:53 CEST 2007


This might be a bug; if so, i'll repost to erlang-bugs.

The following

-----------
  Q  = qlc:q ([ C ||
                C <- mnesia:table (campaign),
                C#campaign.id =:= Id,
                C#campaign.status =:= online,
                A <- mnesia:table (account),
                A#account.id =:= C#campaign.account_id,
                A#account.status =:= C#campaign.status ]),
-----------

results in

-----------
Erlang (BEAM) emulator version 5.5.4 [source] [async-threads:0] [hipe] [kernel-poll:false]

Eshell V5.5.4  (abort with ^G)

1> { ok, offerdb_2 } = c (offerdb_2), offerdb_2:test ().
./offerdb_2.erl:none: error in parse transform 'qlc': {{badarg,{var,0,'Id'}},
                                 [{erl_parse,normalise,1},
                                  {qlc_pt,'-qcon/1-lc$^1/1-1-',1},
                                  {qlc_pt,'-qcon/1-lc$^0/1-0-',1},
                                  {qlc_pt,qcon,1},
                                  {qlc_pt,qdata,2},
                                  {qlc_pt,qdata,2},
                                  {qlc_pt,qdata,2},
                                  {qlc_pt,qdata,2}]}
-----------

although the following does not

-----------
  Q  = qlc:q ([ C ||
                C <- qlc:q ([ C ||
                              C <- mnesia:table (campaign),
                              C#campaign.id =:= Id,
                              C#campaign.status =:= online ]),
                A <- mnesia:table (account),
                A#account.id =:= C#campaign.account_id,
                A#account.status =:= C#campaign.status ]),

-----------

Is this expected or a known problem?

Attached is a complete file.

Thanks in advance,

-- p

Many parts of Iraq are stable now.  But of course what we see on television
is the one bombing a day that discourages everybody.

        -- First Lady Laura Bush
-------------- next part --------------
-module (offerdb_2).
-include_lib("stdlib/include/qlc.hrl").
-include_lib("eunit/include/eunit.hrl").
-compile (export_all).

-record (account, { id, status }).
-record (campaign, { id, status, account_id }).

start () ->
  mnesia:create_schema ([node ()]),
  mnesia:start ().

stop () ->
  mnesia:stop (),
  mnesia:delete_schema ([node ()]).

init () ->
  mnesia:create_table (account,
                       [ { attributes, record_info (fields, account) } ]),
  mnesia:create_table (campaign,
                       [ { attributes, record_info (fields, campaign) } ]).

insert_account (Acc=#account{ }) ->
  mnesia:transaction (fun () -> mnesia:write (Acc) end).

insert_campaign (Campaign=#campaign{ }) ->
  mnesia:transaction (fun () -> mnesia:write (Campaign) end).

update_account_status (Id, Status) ->
  mnesia:transaction (fun () -> 
                        [ Acc ] = mnesia:read ({ account, Id }),
                        mnesia:write (Acc#account{ status = Status})
                      end).

update_campaign_status (Id, Status) ->
  mnesia:transaction (fun () -> 
                        [ Campaign ] = mnesia:read ({ campaign, Id }),
                        mnesia:write (Campaign#campaign{ status = Status })
                      end).

select_campaign (Id) ->
% this doesn't
  Q  = qlc:q ([ C || 
                C <- mnesia:table (campaign),
                C#campaign.id =:= Id,
                C#campaign.status =:= online,
                A <- mnesia:table (account),
                A#account.id =:= C#campaign.account_id,
                A#account.status =:= C#campaign.status ]),

% this works
%
%  Q  = qlc:q ([ C || 
%                C <- qlc:q ([ C || 
%                              C <- mnesia:table (campaign),
%                              C#campaign.id =:= Id,
%                              C#campaign.status =:= online ]),
%                A <- mnesia:table (account),
%                A#account.id =:= C#campaign.account_id,
%                A#account.status =:= C#campaign.status ]),
  io:format (user, "~s~n", [ qlc:info (Q) ]),
  mnesia:async_dirty (fun () -> qlc:e (Q) end).

basic_test_ () ->
  { setup, 
    fun start/0, 
    fun (_) -> stop () end,
    fun () ->
      init (),

      insert_account (#account{ id = 36#kfed, status = online }),
      insert_account (#account{ id = 36#brittney, status = offline }),

      insert_campaign (#campaign{ id = 36#playingwithfire, 
                                  status = online,
                                  account_id = 36#kfed }),

      insert_campaign (#campaign{ id = 36#chaoticthedvd,
                                  status = offline,
                                  account_id = 36#kfed }),

      insert_campaign (#campaign{ id = 36#hitmebabyonemoretime,
                                  status = online,
                                  account_id = 36#brittney }),

      [ X ] = select_campaign (36#playingwithfire),
      ?assert (X#campaign.id =:= 36#playingwithfire),
      ?assert (X#campaign.status =:= online),
      ?assert (X#campaign.account_id =:= 36#kfed),

      Y = select_campaign (36#chaoticthedvd),
      ?assert (Y =:= []),

      Z = select_campaign (36#hitmebabyonemoretime),
      ?assert (Z =:= [])

    end
  }.


More information about the erlang-questions mailing list