[erlang-questions] QLC question

Chris Hicks <>
Mon May 2 22:39:21 CEST 2011


Makes sense, I'll certainly be playing around with it a lot more, learning via failure and all that. I ended up figuring out how to what I was actually working towards but I'd like to open myself to criticism and see if there is anything about the QLC that could be improved. Here is the code:
mnesia:activity(transaction, fun(Nickname) -> qlc:eval(qlc:q([Exists || {A, B, C, D} <- mnesia:table(emuf_accounts), Exists = ((emuf_crypt:decrypt(D))#account_data.nickname == Nickname)])) end, [N])
Now, the emuf_decrypt call is to my own module/function which handles the encrypting/decrypting of my data, so that I'm not storing any user account information in the open (except, of course, the Username but this should be plenty secure in my case). The data has to be decrypted each time it is read, of course.
Is this a naive solution? Is there anything about it that might slowdown the query or is there just a plain more efficient way of doing it?
Chris Hicks.
Subject: Re: [erlang-questions] QLC question
From: 
Date: Mon, 2 May 2011 22:11:51 +0200
CC: 
To: 




If you have a record within a record, you can simply use pattern matching:
1> rd(data, {id, nick}).data2> rd(wrapper, {id, data}).wrapper
3> L = [#wrapper{id=1,data=#data{id=1}}, #wrapper{id=2,data=#data{id=2,nick=n}}].[#wrapper{id = 1,data = #data{id = 1,nick = undefined}}, #wrapper{id = 2,data = #data{id = 2,nick = n}}]
4> qlc:e(qlc:q([W || #wrapper{data = #data{nick = N}} = W <- L, N =/= undefined])).[#wrapper{id = 2,data = #data{id = 2,nick = n}}]
If this becomes to hairy, you can use a guard function which does deep inspection on the record. In LCs, a guard function can be any Erlang function.
A generator in QLC must either be a list, or a special QLC generator (e.g. what you get as a return value from qlc:q/1 or mnesia:table/1). In this particular case, QLC seems to complain already in the query analysis stage, as it doesn't like the fact that a dynamic variable is assumed to be a valid generator.
BR,Ulf W
On 2 May 2011, at 21:50, Chris Hicks wrote:I've got a record nested within a record like this:
{account_wrapper, "Username", {account_data, ...}}
And what I want to do is use QLC to search that inner record and see if someone already has chosen a nickname for an account during registration. However, since I've never used QLC before I wanted to start out slow and just get a feel, creating more complex QLC's as I went along until I got where I wanted to go. First I tried this:
mnesia:activity(transaction, fun() -> qlc:eval(qlc:q([{A, B, C} || Wrapper <- mnesia:table(accounts), {A, B, C} <- Wrapper])) end, []).
I just wanted to make sure I understood how to unpack data structures...however I keep getting a qlc error:
** exception exit: {aborted,{badarg,[{qlc,eval,                                          [{error,qlc,                                                  {1,qlc,{used_generator_variable,'Wrapper'}}},                                           []]},                                     {mnesia_tm,apply_fun,3},                                     {mnesia_tm,execute_transaction,5},                                     {mnesia,wrap_trans,6},                                     {erl_eval,do_apply,5},                                     {shell,exprs,7},                                     {shell,eval_exprs,7},                                     {shell,eval_loop,3}]}}     in function  mnesia:wrap_trans/6

Now from everything I've read...I thought I'd be able to use the Wrapper variable later on in the query as a generator...thus allowing me to unpack things. Why would this be giving me an error? If I change it to:
mnesia:activity(transaction, fun() -> qlc:eval(qlc:q([{A, B, C} || {A, B, C} <- mnesia:table(accounts)])) end, []).
It works just fine and returns the record...and I can successfully match the pattern {A, B, C} with the account_wrapper record elsewhere. So what am I missing?
Chris Hicks.
_______________________________________________
erlang-questions mailing list

http://erlang.org/mailman/listinfo/erlang-questions


Ulf Wiger, CTO, Erlang Solutions, Ltd.http://erlang-solutions.com



 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20110502/c4c951a8/attachment.html>


More information about the erlang-questions mailing list