Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert StringPath to DataPath with custom method

Tags:

querydsl

How to convert StringPath to DatePath with custom function and selected value?

I want use value from database in custom predicate like below

public static Predicate hasBirthdateGreaterOrEqual(String from) {

    //how to convert this to String and pass to PeselParser(String value)
    StringPath peselValue = QEmployee.employee.employment.person.peselValue;

    LocalDate employeeDate = new PeselParser(peselValue).getDate();

    DateExpression<LocalDate> expression = DateTemplate.create(LocalDate.class, from);
    return expression.goe(employeeDate);
}
like image 438
masel.popowo Avatar asked Aug 27 '26 02:08

masel.popowo


1 Answers

Use DateTemplate's create(Class<T> type, String template, Object one) method. Dependant upon your RDBMS, you're going to want to alter the following template String. I've gone with Oracle for the example:

DateTemplate.create(LocalDate.class, "TO_DATE({0}, 'DD/MON/YYYY')", from);

I see you're using the version 3 syntax. Querydsl 4 has been out for a while now. I'd consider upgrading. We've done it for a large reporting project and it actually wasn't too painful. In querydsl 4, I believe you would use Expressions.dateTemplate.

like image 170
Robert Bain Avatar answered Aug 30 '26 23:08

Robert Bain



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!