mirror of
https://github.com/querydsl/querydsl.git
synced 2026-06-16 21:01:10 +08:00
#209 fixed asString serialization by restricting it to constants
This commit is contained in:
parent
e5f0a080f7
commit
a8a8d97e4d
@ -321,7 +321,8 @@ public abstract class SerializerBase<S extends SerializerBase<S>> implements Vis
|
||||
for (Template.Element element : template.getElements()) {
|
||||
if (element.getStaticText() != null) {
|
||||
append(element.getStaticText());
|
||||
} else if (element.isAsString()) {
|
||||
} else if (element.isAsString() && args.get(element.getIndex()) instanceof Constant) {
|
||||
// serialize only constants directly
|
||||
appendAsString(args.get(element.getIndex()));
|
||||
} else {
|
||||
int i = element.getIndex();
|
||||
|
||||
@ -589,6 +589,30 @@ public abstract class StringExpression extends ComparableExpression<String> {
|
||||
public StringExpression substring(int beginIndex, int endIndex) {
|
||||
return StringOperation.create(Ops.SUBSTR_2ARGS, this, ConstantImpl.create(beginIndex), ConstantImpl.create(endIndex));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the given substring
|
||||
*
|
||||
* @param beginIndex
|
||||
* @param endIndex
|
||||
* @return this.substring(beginIndex, endIndex)
|
||||
* @see java.lang.String#substring(int, int)
|
||||
*/
|
||||
public StringExpression substring(Expression<Integer> beginIndex, int endIndex) {
|
||||
return StringOperation.create(Ops.SUBSTR_2ARGS, this, beginIndex, ConstantImpl.create(endIndex));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the given substring
|
||||
*
|
||||
* @param beginIndex
|
||||
* @param endIndex
|
||||
* @return this.substring(beginIndex, endIndex)
|
||||
* @see java.lang.String#substring(int, int)
|
||||
*/
|
||||
public StringExpression substring(int beginIndex, Expression<Integer> endIndex) {
|
||||
return StringOperation.create(Ops.SUBSTR_2ARGS, this, ConstantImpl.create(beginIndex), endIndex);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the given substring
|
||||
|
||||
@ -98,8 +98,8 @@ public class JPQLTemplates extends Templates {
|
||||
add(Ops.ENDS_WITH_IC, "{0l} like {%%1} escape '"+escape+"'");
|
||||
add(Ops.STARTS_WITH, "{0} like {1%} escape '"+escape+"'");
|
||||
add(Ops.STARTS_WITH_IC, "{0l} like {1%%} escape '"+escape+"'");
|
||||
add(Ops.INDEX_OF, "locate({1},{0}) - 1");
|
||||
add(Ops.INDEX_OF_2ARGS, "locate({1},{0},{2s}+1) - 1");
|
||||
add(Ops.INDEX_OF, "locate({1},{0})-1");
|
||||
add(Ops.INDEX_OF_2ARGS, "locate({1},{0},{2s}+1)-1");
|
||||
|
||||
// date time
|
||||
add(Ops.DateTimeOps.SYSDATE, "sysdate");
|
||||
|
||||
@ -73,6 +73,7 @@ import com.mysema.query.jpa.domain.Show;
|
||||
import com.mysema.query.jpa.domain4.QBookMark;
|
||||
import com.mysema.query.jpa.domain4.QBookVersion;
|
||||
import com.mysema.query.jpa.hibernate.HibernateSubQuery;
|
||||
import com.mysema.query.jpa.impl.JPASubQuery;
|
||||
import com.mysema.query.types.ArrayConstructorExpression;
|
||||
import com.mysema.query.types.Concatenation;
|
||||
import com.mysema.query.types.ConstructorExpression;
|
||||
@ -830,6 +831,17 @@ public abstract class AbstractStandardTest {
|
||||
.list(cat);
|
||||
assertNotNull(cats);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void SubQuery3() {
|
||||
QCat cat = QCat.cat;
|
||||
QCat other = new QCat("other");
|
||||
query().from(cat)
|
||||
.where(cat.name.eq(new JPASubQuery().from(other)
|
||||
.where(other.name.indexOf("B").eq(0))
|
||||
.unique(other.name)))
|
||||
.list(cat);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void Count(){
|
||||
|
||||
@ -15,6 +15,15 @@ package com.mysema.query.jpa;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.mysema.query.domain.QCat;
|
||||
import com.mysema.query.support.Expressions;
|
||||
import com.mysema.query.types.Expression;
|
||||
import com.mysema.query.types.Ops;
|
||||
import com.mysema.query.types.Path;
|
||||
import com.mysema.query.types.expr.NumberOperation;
|
||||
import com.mysema.query.types.expr.StringOperation;
|
||||
import com.mysema.query.types.path.StringPath;
|
||||
|
||||
public class StringOperationsTest extends AbstractQueryTest{
|
||||
|
||||
@Test
|
||||
@ -33,4 +42,24 @@ public class StringOperationsTest extends AbstractQueryTest{
|
||||
assertToString("lower(cat.name)", cat.name.lower());
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("rawtypes")
|
||||
public void IndexOf() {
|
||||
Path path = QCat.cat.name;
|
||||
Expression startIndex = Expressions.constant(0);
|
||||
Expression endIndex = NumberOperation.create(Integer.class, Ops.INDEX_OF, path, Expressions.constant("x"));
|
||||
Expression substr = StringOperation.create(Ops.SUBSTR_2ARGS, path, startIndex, endIndex);
|
||||
assertToString("substring(cat.name,1,locate(?1,cat.name)-1)", substr);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void IndexOf2() {
|
||||
StringPath str = QCat.cat.name;
|
||||
assertToString("substring(cat.name,1,locate(?1,cat.name)-1)", str.substring(0, str.indexOf("x")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void IndexOf3() {
|
||||
assertToString("substring(cat.name,2,1)", QCat.cat.name.substring(1,2));
|
||||
}
|
||||
}
|
||||
|
||||
@ -185,4 +185,11 @@ public class SubQueryTest extends AbstractQueryTest{
|
||||
sub().from(cat).where(cat.kittens.get(cat.id).name.eq("Kate")).count());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void IndexOf() {
|
||||
assertToString("(select count(cat) from Cat cat where locate(?1,cat.name) - 1 = ?2)",
|
||||
|
||||
sub().from(cat).where(cat.name.indexOf("a").eq(1)).count());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user