Date range and match specification syntax

Massimo Cesaro massimo.cesaro@REDACTED
Tue Oct 7 15:30:52 CEST 2003


Hi Dan,
> 
> 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}
Yes!! That was the problem. After your suggestion I doubled checked with
the shell and I found that my assumption about tuples comparison was
plain wrong.
This way the test works:

    Query = [
             {'$1',
              [{'>=', {const, StartDate}, {element, 1, '$1'}}],
              ['$_']}],

>  
>  > 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 :-) 
You kidding, don't you ?! :-)
Seriously, I'd like to see a short tutorial or more examples about it.
> 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
> 
Thank you for the support!!!
Massimo





More information about the erlang-questions mailing list