MySql REGEXP

21 views
Skip to first unread message

Michael Igler

unread,
Apr 13, 2016, 2:26:48 PM4/13/16
to Querydsl

Trying to achieve the following result with QueryDSL:

select id, identifier
from eip_transportcode_message
order by
  case
    when identifier REGEXP '^[[:digit:]]+$'
      then cast(identifier as signed)
      else identifier end, 
  LENGTH(identifier) ASC;

Tried the following code:

PathBuilder path = new PathBuilder(TransportCode.class, "transportCode");
StringPath regexPath = Expressions.stringPath("REGEXP '^[[:digit:]]+$'");
StringPath property = orderByExpression.getString(o.getProperty());
Expression identifierTarget = path
        .when(regexPath)
        .then(property.castToNum(Integer.class))
        .otherwise(property);

The outcome is:

order by case when transportCode = REGEXP '^[[:digit:]]+$' then cast(transportCode.identifier as integer) else...

The Problem is the "=" in front of REGEXP and the missing "transportCode.identifier".

timowest

unread,
Apr 13, 2016, 3:00:19 PM4/13/16
to Querydsl
You can express the case via

new CaseBuilder()
    .when(identifier.matches(regexp)).then(identifier.castToNum(Integer.class)
    .otherwise(...)

Michael Igler

unread,
Apr 15, 2016, 11:52:28 AM4/15/16
to Querydsl
Thanx for you reply. If I do so I get an exception because the generated like-statement can't parse the regular expression. Implemented another solution, so you can close this issue.

timowest

unread,
Apr 15, 2016, 12:40:49 PM4/15/16
to Querydsl
The code snippet I provided works with Querydsl SQL, not Querydsl JPA.
Reply all
Reply to author
Forward
0 new messages