Date range and match specification syntax

Dan Gudmundsson dgud@REDACTED
Tue Oct 7 15:04:40 CEST 2003


Hi 

Massimo Cesaro writes:
 > Hello,
 > I'm trying to select-ing from a mnesia table all the records inside a
 > date range. I see that Dates are {Year, Month, Day} tuples, hence this 
 > Query = [
 >          {'$1',
 >          [{'>=', StartDate, {element, 3, '$1'}}, {'=<', EndDate,
 > {element, 3, '$1'}}],
 >           ['$_']}],
 >     mnesia:dirty_select(cdr, Query).
 > 
 > will fail with a badarg because the >= operator cannot be applied on
 > tuples (well, I think this is the resons why the shell complains).

I think you need declare the variables as const 
{const, StartDate}  and {const, EndDate}
 
 > My second approach is using date_to_gregorian_days:
 >     Query = [
 >              {'$1',
 >               [{'>=', calendar:date_to_gregorian_days(StartDate),
 > calendar:date_to_gregorian_days({element, 3, '$1'})}],
 >               ['$_']}],
 >     mnesia:dirty_select(cdr, Query).

You can't do function calls inside the select expression, they
are compiled and interpreted in it's own little machine..

 > Maybe I'm using a wrong approach; any suggestion on how to solve this
 > apparently simple problem ?
 > 
 > Massimo
 > 

Try to use Query = ets:fun2ms(fun(X) -> ... end) instead,
slightly simpler to use :-) 
It generates queries which can be used with mnesia..

Also ets:test_ms/2 can help you with the debugging.

/Dan "Answering without thinking or testing"
-- 
Dan Gudmundsson               Project:    Mnesia, Erlang/OTP




More information about the erlang-questions mailing list