Merge pull request #1930 from querydsl/i1929

Fix template handling
This commit is contained in:
Timo Westkämper 2016-06-30 19:43:25 +03:00 committed by GitHub
commit f7dec5b73d
4 changed files with 44 additions and 3 deletions

View File

@ -284,15 +284,20 @@ public final class Template implements Serializable {
private final int index1;
private final BigDecimal arg2;
private final Number arg2;
private final Expression<BigDecimal> expr2;
private final Expression<Number> expr2;
private final Operator operator;
private final boolean asString;
@Deprecated
public OperationConst(int index1, BigDecimal arg2, Operator operator, boolean asString) {
this(index1, (Number) arg2, operator, asString);
}
public OperationConst(int index1, Number arg2, Operator operator, boolean asString) {
this.index1 = index1;
this.arg2 = arg2;
this.expr2 = Expressions.constant(arg2);

View File

@ -231,7 +231,12 @@ public class TemplateFactory {
elements.add(new Template.Operation(index, index2, operator, asString));
} else if (m.group(5) != null) {
Operator operator = OPERATORS.get(m.group(3));
BigDecimal number = new BigDecimal(m.group(5));
Number number;
if (m.group(5).contains(".")) {
number = new BigDecimal(m.group(5));
} else {
number = Integer.valueOf(m.group(5));
}
elements.add(new Template.OperationConst(index, number, operator, asString));
} else if (asString) {
elements.add(new Template.AsString(index));

View File

@ -482,6 +482,19 @@ public abstract class StringExpression extends LiteralExpression<String> {
return Expressions.numberOperation(Integer.class, Ops.StringOps.LOCATE2, ConstantImpl.create(str), mixin, ConstantImpl.create(start));
}
/**
* Create a {@code locate(str, this, start)} expression
*
* <p>Get the position of the given String in this String, the first position is 1</p>
*
* @param str string
* @param start start
* @return locate(str, this, start)
*/
public NumberExpression<Integer> locate(String str, Expression<Integer> start) {
return Expressions.numberOperation(Integer.class, Ops.StringOps.LOCATE2, ConstantImpl.create(str), mixin, start);
}
/**
* Create a {@code this.toLowerCase()} expression
*

View File

@ -1699,6 +1699,24 @@ public class SelectBase extends AbstractBaseTest {
assertEquals(Integer.valueOf(0), firstResult(str.locate("a", 4)));
assertEquals(Integer.valueOf(4), firstResult(str.locate("b", 2)));
assertEquals(" abcd", firstResult(StringExpressions.rtrim(str)));
assertEquals("abc", firstResult(str.substring(2, 5)));
}
@Test
@ExcludeIn(SQLITE)
public void string_withTemplate() {
StringExpression str = Expressions.stringTemplate("' abcd '");
NumberExpression<Integer> four = Expressions.numberTemplate(Integer.class, "4");
NumberExpression<Integer> two = Expressions.numberTemplate(Integer.class, "2");
NumberExpression<Integer> five = Expressions.numberTemplate(Integer.class, "5");
assertEquals("abcd ", firstResult(StringExpressions.ltrim(str)));
assertEquals(Integer.valueOf(3), firstResult(str.locate("a")));
assertEquals(Integer.valueOf(0), firstResult(str.locate("a", four)));
assertEquals(Integer.valueOf(4), firstResult(str.locate("b", two)));
assertEquals(" abcd", firstResult(StringExpressions.rtrim(str)));
assertEquals("abc", firstResult(str.substring(two, five)));
}
@Test