Erlang Mailing Lists

Author Message

<  Erlang questions mailing list  ~  Dynamic Mnesia Queries.

icimjs at loop.com
Posted: Fri Jun 18, 1999 6:46 am Reply with quote
Guest
Hi everyone,

first of all, thank you all for the useful responses.

Torbjorn Tornkvist wrote:
>Great ! How did it go ?

We're still waiting for their new computers to arrive, which gives me a
little time to improve my code.

We've decided to use Linux instead of MS Windows :-)

Here's what I'm currently wrestling with:

Right now I have the following "generic" function:

list_glue() ->
Fun = fun() ->
Q = query [G.product_name || G <- table(glue)] end,
mnemosyne:eval(Q)
end,
mnesia:transaction(Fun).

Fine.

What I'd like to do is something like this (which doesn't work):

list_generic(Table, Tuple, Condition) ->
Fun = fun() ->
Q = query( Tuple || ??? <- table(Table),
Condition ] end,
mnemosyne:eval(Q)
end,
mnesia:transaction(Fun).

Then I would want to be able to call it with something like this ... from
the shell

list_generic(glue,
{glue, Product_Name, _ , _ , _ ,},
{glue, _ , _ , _ , "Some glue manufacturer here"} ).

where the record is
-record(glue, {name, id, reference, manufacturer}).

in order to get all glue products this particular manufacturer carries. On
a different occasion I want to use the same function list_generic, to
identify the type of glue I need to use for a certain type of floor
covering, something like this

list_generic(linoleum,
{linoleum, Glue_ID, _ , _ },
{linoleum, _ , "00134", _ } ).

where the record is
-record(linoleum, {glue_reference, linoleum_id, manufacturer}).

How could something like this be implemented? Is it at all possible? Will I
need to use
record_info(fields, Rec) -> [Names].

in the list_generic/3 function in order to construct a record and assign
the values from the tuples Tuple and Condition to the record? How do I do
that dynamically, i.e. during runtime?

I think I'm close to the solution, but I'm still struggling with the
documentation to get it right.

Is there a better way to achieve the same effect?

TIA,

Elan

====================================
Get your copy of FaxForward
http://www.commercebox.com/FaxForward/



Post generated using Mail2Forum (http://m2f.sourceforge.net)
ulf.wiger at etx.ericsson
Posted: Fri Jun 18, 1999 8:29 am Reply with quote
Guest
I'll just address one piece of the puzzle, since it's one that I know
off the top of my head.

Elan wrote:
>
[...]
> list_generic(glue,
> {glue, Product_Name, _ , _ , _ ,},
> {glue, _ , _ , _ , "Some glue manufacturer here"} ).
>
> where the record is
> -record(glue, {name, id, reference, manufacturer}).
>

If you want such a simple query, you can skip mnemosyne and use
match_object directly. I'm afraid that you'll have to use some kind of
parser otherwise (haven't kept up with mnemosyne lately).

Take the following code for example
(written in all haste, apologies):

-module(mntest).
-export([q/3]).

q(Tab, Fields, Select) ->
Fs = fields_info(Tab),
Wild = mnesia:table_info(Tab, wild_pattern),
Sel = insert_select(Fs, Select, Wild, 2),
Proj = calc_project_positions(Fs, Fields, 2),
{atomic, Objs} = mnesia:transaction(
fun() ->
mnesia:match_object(Sel)
end),
[project(Proj, O) || O <- Objs].

insert_select([H|T], Sel, Rec, Pos) ->
case lists:keysearch(H, 1, Sel) of
{value, {_, Val}} ->
insert_select(T, Sel, setelement(Pos, Rec, Val), Pos+1);
false ->
insert_select(T, Sel, Rec, Pos+1)
end;
insert_select([], _, Rec, _) ->
Rec.

calc_project_positions(_, all, _) -> all;
calc_project_positions([H|T], Fields, Pos) ->
case lists:member(H, Fields) of
true ->
[Pos|calc_project_positions(T, Fields, Pos+1)];
false ->
calc_project_positions(T, Fields, Pos+1)
end;
calc_project_positions([], _, _) ->
[].

project(all, O) -> O;
project(Flds, O) ->
P = project1(Flds, O),
case P of
[X] -> X;
_ ->
list_to_tuple(project1(Flds, O))
end.

project1([H|T], O) ->
[element(H, O)|project1(T, O)];
project1([], _) ->
[].

fields_info(glue) -> record_info(fields, glue);
fields_info(linoleum) -> record_info(fields, linoleum).


- - - - - - - -

Example:


(foo_at_avc343)44> mntest:q(glue, [name], [{manufacturer,m1}]).
[g2,g1]
(foo_at_avc343)45> mntest:q(glue, [name,id], [{manufacturer,m1}]).
[{g2,2},{g1,1}]
(foo_at_avc343)46> mntest:q(glue, all, []).
[{glue,g4,4,undefined,m4},
{glue,g3,3,undefined,m3},
{glue,g2,2,undefined,m1},
{glue,g1,1,undefined,m1}]


--
Ulf Wiger, Chief Designer AXD 301 <ulf.wiger_at_etx.ericsson.se>
Ericsson Telecom AB tfn: +46 8 719 81 95
Varuv
icimjs at loop.com
Posted: Mon Jun 21, 1999 12:32 am Reply with quote
Guest
Ulf wrote:
>Take the following code for example
>(written in all haste, apologies):
>

Thanks, Ulf. May have been written in all haste, but it certainly compiles
and does does everything I need it to.

Still, I think it should be possible to do something like this using
Mnemosyne.

Does anyone know how Menomsyne could be used to do something similar?

Elan

====================================
Get your copy of FaxForward
http://www.commercebox.com/FaxForward/



Post generated using Mail2Forum (http://m2f.sourceforge.net)
hakan at erix.ericsson.se
Posted: Mon Jun 21, 1999 1:15 pm Reply with quote
Guest
On Sun, 20 Jun 1999, Elan <icimjs_at_loop.com> wrote:

Elan> Still, I think it should be possible to do something like this using
Elan> Mnemosyne.
Elan>
Elan> Does anyone know how Menomsyne could be used to do something similar?

There is an UNSUPPORTED function in the API that takes a Mnemosyne
query as a string and converts it into a Mnemosyne query handle:

Q = mnemosyne:string_to_handle(
"query [ X || X <- table(q),Y <- table(r),X.b = Y.f1] end. "),
mnesia:transaction(fun() -> mnemosyne:eval(Q) end).

It may still work, but perhaps it is too limited to be useful for you.

There also exists an experimental SQL compiler that generates the
internal form of Mnemosyne from SQL. This work was performed as a
master thesis.

/H
icimjs at loop.com
Posted: Mon Jun 21, 1999 5:22 pm Reply with quote
Guest
/H
hakan at erix.ericsson.se
Posted: Tue Jun 22, 1999 2:07 pm Reply with quote
Guest
On Mon, 21 Jun 1999, Elan <icimjs_at_loop.com> wrote:

>>There also exists an experimental SQL compiler that generates the
>>internal form of Mnemosyne from SQL. This work was performed as a
>>master thesis.
>
>Very interesting. Is there more information available? (A URL perhaps, or
>an email address?)

A good starting point is to read Ronny Andersson's master thesis report.
It is available at:

http://www.erlang.se/erlang/sure/main/club/university/xjobb/sql_compiler_report.pdf

It was an experimental work where a complete SQL parser was
implemented in Erlang using yecc, but only a minor subset of SQL was
mapped to Mnesia and Mnemosyne. I can dig up the code, but I don't
think that it works today.

/H

Display posts from previous:  

All times are GMT
Page 1 of 1
This forum is locked: you cannot post, reply to, or edit topics.

Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum