Dynamic Mnesia Queries.
Ulf Wiger
ulf.wiger@REDACTED
Fri Jun 18 10:28:48 CEST 1999
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@REDACTED)44> mntest:q(glue, [name], [{manufacturer,m1}]).
[g2,g1]
(foo@REDACTED)45> mntest:q(glue, [name,id], [{manufacturer,m1}]).
[{g2,2},{g1,1}]
(foo@REDACTED)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@REDACTED>
Ericsson Telecom AB tfn: +46 8 719 81 95
Varuvägen 9, Älvsjö mob: +46 70 519 81 95
S-126 25 Stockholm, Sweden fax: +46 8 719 43 44
More information about the erlang-questions
mailing list