@ SuppressWarnings ( "unchecked" ) public static Expression < String > regexToLike ( Expression < String > expr ) { if ( expr instanceof Constant < ? > ) { final String str = expr . toString ( ) ; final StringBuilder rv = new StringBuilder ( str . length ( ) + 2 ) ; boolean escape = false ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { final char ch = str . charAt ( i ) ; if ( ! escape && ch == ' ' ) { if ( i < str . length ( ) - 1 && str . charAt ( i + 1 ) == ' ' ) { rv . append ( ' ' ) ; i ++ ; } else { rv . append ( ' ' ) ; } continue ; } else if ( ! escape && ch == ' ' ) { escape = true ; continue ; } else if ( ! escape && ( ch == ' ' || ch == ' ' || ch == ' ' || ch == ' ' || ch == ' ' ) ) { throw new QueryException ( "'" + str + "' can't be converted to like form" ) ; } else if ( escape && ( ch == ' ' || ch == ' ' || ch == ' ' || ch == ' ' || ch == ' ' || ch == ' ' ) ) { throw new QueryException ( "'" + str + "' can't be converted to like form" ) ; } rv . append ( ch ) ; escape = false ; } if ( ! rv . toString ( ) . equals ( str ) ) { return ConstantImpl . create ( rv . toString ( ) ) ; } } else if ( expr instanceof Operation < ? > ) { Operation < ? > o = ( Operation < ? > ) expr ; if ( o . getOperator ( ) == Ops . CONCAT ) { Expression < String > lhs = regexToLike ( ( Expression < String > ) o . getArg ( 0 ) ) ; Expression < String > rhs = regexToLike ( ( Expression < String > ) o . getArg ( 1 ) ) ; if ( lhs != o . getArg ( 0 ) || rhs != o . getArg ( 1 ) ) { return operation ( String . class , Ops . CONCAT , lhs , rhs ) ; } } } return expr ; }
Convert the given expression from regex form to like