Many thanks Mike and Andrus!

The third option is the one that I was looking for, thank you.

Andrea

On 2021/01/02 15:06:37, Andrus Adamchik <and...@objectstyle.org> wrote: 
> > On Jan 2, 2021, at 3:45 PM, Mike Kienenberger <mkien...@gmail.com> wrote:
> > 
> > Date date = new GregorianCalendar(2003, Calendar.JULY,17).getTime() ;
> > Expression matchDateGreaterThanDate1 = 
> > ExpressionFactory.greaterExp("MODIFIED_DATE", date);
> 
> As Mike said, the above should work, but if you are building an expression 
> from String (instead of creating all clauses via the API), using parameters 
> is the way to go as mentioned by John. Regarding the syntax... Cayenne 
> supports named and positional parameters. The String syntax is the same for 
> both ("$varname" for the param), but binding happens differently [1]:
> 
> // 1. Named params:
> Map params = Collections.singletonMap("a", date);
> Expression e = ExpressioFactory.exp("a > $a").params(params);
> 
> // 2. Positional params. Var name is ignored. Only its position is relevant, 
> //    and is matched with vararg position in the method:
> Expression e = ExpressioFactory.exp("a > $a").paramsArray(date);
> 
> // 3. A shorter form of positional params:
> Expression e = ExpressioFactory.exp("a > $a", date);
> 
> I personally use option #3 whenever I can, as it has the least amount of 
> boilerplate.
> 
> Andrus
> 
> 
> [1] 
> https://cayenne.apache.org/docs/4.1/cayenne-guide/#creating-expressions-from-strings
> 
> 
> > On Jan 2, 2021, at 5:48 PM, John Huss <johnth...@gmail.com> wrote:
> > 
> > I think the string value would be $0 or $1. Or you can use .params with a
> > map to do by name and use $date or whatever you want to name it.
> > 
> > On Sat, Jan 2, 2021 at 8:18 AM Andrea Biasillo <a...@dataloy.com> wrote:
> > 
> >> Yes, there is, but how will be the syntax?
> >> 
> >> 
> >> 
> >> On 2021/01/02 14:07:55, John Huss <johnth...@gmail.com> wrote:
> >> 
> >>> Isn’t there a version of .exp that takes args? You can pass a date object
> >> 
> >>> directly there without worrying about formatting.
> >> 
> >>> 
> >> 
> >>> On Sat, Jan 2, 2021 at 7:49 AM Andrea Biasillo <a...@dataloy.com> wrote:
> >> 
> >>> 
> >> 
> >>>> Hi Mike!
> >> 
> >>>> 
> >> 
> >>>> 
> >> 
> >>>> 
> >> 
> >>>> I have tried with:
> >> 
> >>>> 
> >> 
> >>>> 
> >> 
> >>>> 
> >> 
> >>>> Expression expDate = ExpressionFactory.exp("modifiedDate>
> >> 
> >>>> TO_DATE('2003-07-17','yyyy-mm-dd')");
> >> 
> >>>> 
> >> 
> >>>> 
> >> 
> >>>> 
> >> 
> >>>> but I get this exception:
> >> 
> >>>> 
> >> 
> >>>> 
> >> 
> >>>> 
> >> 
> >>>> Caused by: org.apache.cayenne.exp.ExpressionException: [v.4.2.M1 Nov 26
> >> 
> >>>> 2020 09:20:26] Encountered " "(" "( "" at line 1, column 22.
> >> 
> >>>> 
> >> 
> >>>> Was expecting one of:
> >> 
> >>>> 
> >> 
> >>>>    <EOF>
> >> 
> >>>> 
> >> 
> >>>>    "or" ...
> >> 
> >>>> 
> >> 
> >>>>    "and" ...
> >> 
> >>>> 
> >> 
> >>>>    "|" ...
> >> 
> >>>> 
> >> 
> >>>>    "^" ...
> >> 
> >>>> 
> >> 
> >>>>    "&" ...
> >> 
> >>>> 
> >> 
> >>>>    "<<" ...
> >> 
> >>>> 
> >> 
> >>>>    ">>" ...
> >> 
> >>>> 
> >> 
> >>>>    "+" ...
> >> 
> >>>> 
> >> 
> >>>>    "-" ...
> >> 
> >>>> 
> >> 
> >>>>    "/" ...
> >> 
> >>>> 
> >> 
> >>>>    "*" ...
> >> 
> >>>> 
> >> 
> >>>> 
> >> 
> >>>> 
> >> 
> >>>> and I really would want to have all my expression in one string,
> >> without
> >> 
> >>>> use greaterExp.
> >> 
> >>>> 
> >> 
> >>>> Like this:
> >> 
> >>>> 
> >> 
> >>>> 
> >> 
> >>>> 
> >> 
> >>>> Expression exp = ExpressionFactory.exp("businessPartnerName='andrea'
> >> and
> >> 
> >>>> businessPartnerCode!='rossi' or ((businessPartnerCode!='bianchi' and
> >> 
> >>>> maxAdvancePaymentPercent>9) or businessPartnerCode!='pippo' or
> >> 
> >>>> (maxAdvancePaymentPercent>100 or maxAdvancePaymentPercent<10)) and
> >> 
> >>>> modifiedDate> TO_DATE('2003-07-17','yyyy-mm-dd')");
> >> 
> >>>> 
> >> 
> >>>> 
> >> 
> >>>> 
> >> 
> >>>> But maybe it is not possible.
> >> 
> >>>> 
> >> 
> >>>> 
> >> 
> >>>> 
> >> 
> >>>> Andrea
> >> 
> >>>> 
> >> 
> >>>> 
> >> 
> >>>> 
> >> 
> >>>> On 2021/01/02 12:45:49, Mike Kienenberger <mkien...@gmail.com> wrote:
> >> 
> >>>> 
> >> 
> >>>>> Oracle expects SQL like the following to compare dates.
> >> 
> >>>> 
> >> 
> >>>>> 
> >> 
> >>>> 
> >> 
> >>>>>       MODIFIED_DATE > TO_DATE('2003-07-17','yyyy-mm-dd')
> >> 
> >>>> 
> >> 
> >>>>> 
> >> 
> >>>> 
> >> 
> >>>>> You either need to use that in your exp() method, or use
> >> greaterExp(),
> >> 
> >>>> 
> >> 
> >>>>> which can automatically generate the needed sql for the current
> >> database.
> >> 
> >>>> 
> >> 
> >>>>> 
> >> 
> >>>> 
> >> 
> >>>>>        Date date = new GregorianCalendar(2003, Calendar.JULY,
> >> 
> >>>> 
> >> 
> >>>>> 17).getTime() ;
> >> 
> >>>> 
> >> 
> >>>>>        Expression matchDateGreaterThanDate1 =
> >> 
> >>>> 
> >> 
> >>>>> ExpressionFactory.greaterExp("MODIFIED_DATE", date);
> >> 
> >>>> 
> >> 
> >>>>> 
> >> 
> >>>> 
> >> 
> >>>>> 
> >> 
> >>>> 
> >> 
> >>>>> 
> >> 
> >>>> 
> >> 
> >>>>> 
> >> 
> >>>> 
> >> 
> >>>>> On Sat, Jan 2, 2021 at 3:45 AM Andrea Biasillo <a...@dataloy.com>
> >> wrote:
> >> 
> >>>> 
> >> 
> >>>>> 
> >> 
> >>>> 
> >> 
> >>>>>> Hi!
> >> 
> >>>> 
> >> 
> >>>>>> 
> >> 
> >>>> 
> >> 
> >>>>>> Is it possible to use Date values in ExpressionFactory.exp?
> >> 
> >>>> 
> >> 
> >>>>>> 
> >> 
> >>>> 
> >> 
> >>>>>> This works very well:
> >> 
> >>>> 
> >> 
> >>>>>> 
> >> 
> >>>> 
> >> 
> >>>>>> Expression exp =
> >> ExpressionFactory.exp("businessPartnerName='andrea'
> >> 
> >>>> and
> >> 
> >>>> 
> >> 
> >>>>>> businessPartnerCode!='rossi' or ((businessPartnerCode!='bianchi'
> >> and
> >> 
> >>>> 
> >> 
> >>>>>> maxAdvancePaymentPercent>9) or businessPartnerCode!='pippo' or
> >> 
> >>>> 
> >> 
> >>>>>> (maxAdvancePaymentPercent>100 or maxAdvancePaymentPercent<10))");
> >> 
> >>>> 
> >> 
> >>>>>> 
> >> 
> >>>> 
> >> 
> >>>>>> but if I use a Date value like this:
> >> 
> >>>> 
> >> 
> >>>>>> 
> >> 
> >>>> 
> >> 
> >>>>>> Expression expDate = ExpressionFactory.exp("modifiedDate>
> >> 
> >>>> '2003-07-17'");
> >> 
> >>>> 
> >> 
> >>>>>> 
> >> 
> >>>> 
> >> 
> >>>>>> The database, Oracle in my case, will complain:
> >> 
> >>>> 
> >> 
> >>>>>> 
> >> 
> >>>> 
> >> 
> >>>>>> Literal does not match format string
> >> 
> >>>> 
> >> 
> >>>>>> (the database expects something like this:  MODIFIED_DATE > DATE
> >> 
> >>>> 
> >> 
> >>>>>> '2003-07-17')
> >> 
> >>>> 
> >> 
> >>>>>> 
> >> 
> >>>> 
> >> 
> >>>>>> Any input?
> >> 
> >>>> 
> >> 
> >>>>>> 
> >> 
> >>>> 
> >> 
> >>>>>> Andrea
> >> 
> >>>> 
> >> 
> >>>>>> 
> >> 
> >>>> 
> >> 
> >>>>>> 
> >> 
> >>>> 
> >> 
> >>>>>> 
> >> 
> >>>> 
> >> 
> >>>>>> 
> >> 
> >>>> 
> >> 
> >>>>>> 
> >> 
> >>>> 
> >> 
> >>>>>> 
> >> 
> >>>> 
> >> 
> >>>>> 
> >> 
> >>>> 
> >> 
> >>>> 
> >> 
> >>> 
> >> 
> >> 
> 
> 

Reply via email to