mirror of
https://github.com/querydsl/querydsl.git
synced 2026-06-13 21:01:01 +08:00
Add support for literal serialization #552
This commit is contained in:
parent
685000cee4
commit
ca29d3bb12
@ -6,37 +6,42 @@ import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public final class Normalization {
|
||||
|
||||
|
||||
private static final String WS = "\\s*";
|
||||
|
||||
|
||||
private static final String START = "\\b";
|
||||
|
||||
|
||||
private static final String NUMBER = "([+\\-]?\\d+\\.?\\d*)";
|
||||
|
||||
|
||||
private static final Pattern OPERATOR = Pattern.compile(WS + "[+\\-/*]" + WS);
|
||||
|
||||
|
||||
private static final Pattern OPERATION = Pattern.compile(START + NUMBER + OPERATOR.pattern() + NUMBER);
|
||||
|
||||
|
||||
private static final Pattern ADDITION = Pattern.compile(START + NUMBER + WS + "\\+" + WS + NUMBER);
|
||||
|
||||
|
||||
private static final Pattern SUBTRACTION = Pattern.compile(START + NUMBER + WS + "\\-" + WS + NUMBER);
|
||||
|
||||
|
||||
private static final Pattern DIVISION = Pattern.compile(START + NUMBER + WS + "/" + WS + NUMBER);
|
||||
|
||||
|
||||
private static final Pattern MULTIPLICATION = Pattern.compile(START + NUMBER + WS + "\\*" + WS + NUMBER);
|
||||
|
||||
|
||||
public static final String normalize(String queryString) {
|
||||
if (!hasOperators(queryString)) {
|
||||
return queryString;
|
||||
}
|
||||
|
||||
|
||||
StringBuilder rv = null;
|
||||
Matcher m = OPERATION.matcher(queryString);
|
||||
int end = 0;
|
||||
while (m.find()) {
|
||||
if (rv == null) {
|
||||
rv = new StringBuilder(queryString.length());
|
||||
}
|
||||
}
|
||||
if (m.start() > 0 && queryString.charAt(m.start() - 1) == '\'') {
|
||||
continue;
|
||||
} else if (m.end() < queryString.length() && queryString.charAt(m.end()) == '\'') {
|
||||
continue;
|
||||
}
|
||||
if (m.start() > end) {
|
||||
rv.append(queryString.subSequence(end, m.start()));
|
||||
}
|
||||
@ -46,14 +51,14 @@ public final class Normalization {
|
||||
boolean divide = DIVISION.matcher(str).matches();
|
||||
boolean multiply = MULTIPLICATION.matcher(str).matches();
|
||||
Matcher matcher = OPERATION.matcher(str);
|
||||
matcher.matches();
|
||||
matcher.matches();
|
||||
BigDecimal first = new BigDecimal(matcher.group(1));
|
||||
BigDecimal second = new BigDecimal(matcher.group(2));
|
||||
String result = null;
|
||||
if (multiply) {
|
||||
result = first.multiply(second).toString();
|
||||
} else if (divide) {
|
||||
result = first.divide(second, 10, RoundingMode.HALF_UP).toString();
|
||||
result = first.divide(second, 10, RoundingMode.HALF_UP).toString();
|
||||
} else if (subtract) {
|
||||
result = first.subtract(second).toString();
|
||||
} else if (add) {
|
||||
@ -64,7 +69,7 @@ public final class Normalization {
|
||||
while (result.contains(".") && (result.endsWith("0") || result.endsWith("."))) {
|
||||
result = result.substring(0, result.length()-1);
|
||||
}
|
||||
rv.append(result);
|
||||
rv.append(result);
|
||||
end = m.end();
|
||||
}
|
||||
if (end > 0) {
|
||||
@ -75,12 +80,12 @@ public final class Normalization {
|
||||
return rv.toString();
|
||||
} else {
|
||||
return normalize(rv.toString());
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return queryString;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private static final boolean hasOperators(String queryString) {
|
||||
for (int i = 0; i < queryString.length(); i++) {
|
||||
char ch = queryString.charAt(i);
|
||||
@ -90,7 +95,7 @@ public final class Normalization {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
private Normalization() {}
|
||||
|
||||
}
|
||||
|
||||
@ -1,11 +1,11 @@
|
||||
package com.mysema.query.support;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class NormalizationTest {
|
||||
|
||||
|
||||
@Test
|
||||
public void Performance() {
|
||||
int iterations = 1000000;
|
||||
@ -15,12 +15,12 @@ public class NormalizationTest {
|
||||
}
|
||||
System.err.println(System.currentTimeMillis() - start);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void Variables() {
|
||||
assertEquals("var1 + 3", Normalization.normalize("var1 + 3"));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void Normalize_Addition() {
|
||||
assertEquals("3", Normalization.normalize("1+2"));
|
||||
@ -28,7 +28,7 @@ public class NormalizationTest {
|
||||
assertEquals("where 3.3 = 3.3", Normalization.normalize("where 1.1+2.2 = 3.3"));
|
||||
assertEquals("where 3.3 = 3.3", Normalization.normalize("where 1.1 + 2.2 = 3.3"));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void Normalize_Subtraction() {
|
||||
assertEquals("3", Normalization.normalize("5-2"));
|
||||
@ -43,7 +43,7 @@ public class NormalizationTest {
|
||||
assertEquals("where 10 = 10", Normalization.normalize("where 5*2 = 10"));
|
||||
assertEquals("where 11 = 11", Normalization.normalize("where 5.5*2 = 11"));
|
||||
assertEquals("where 10.8 = 10.8", Normalization.normalize("where 5.4 * 2 = 10.8"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void Normalize_Division() {
|
||||
@ -51,17 +51,22 @@ public class NormalizationTest {
|
||||
assertEquals("where 2.5 = 2.5", Normalization.normalize("where 5/2 = 2.5"));
|
||||
assertEquals("where 2.6 = 2.6", Normalization.normalize("where 5.2/2 = 2.6"));
|
||||
assertEquals("where 2.6 = 2.6", Normalization.normalize("where 5.2 / 2 = 2.6"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void Mixed() {
|
||||
assertEquals("13", Normalization.normalize("2 * 5 + 3"));
|
||||
assertEquals("-2.5", Normalization.normalize("2.5 * -1"));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void PI() {
|
||||
assertEquals("0.1591549431", Normalization.normalize("0.5 / " + Math.PI));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void DateTimeLiterals() {
|
||||
assertEquals("'1980-10-10'", Normalization.normalize("'1980-10-10'"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -24,7 +24,6 @@ import java.util.Map;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import com.mysema.query.types.*;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@ -43,6 +42,17 @@ import com.mysema.query.Tuple;
|
||||
import com.mysema.query.support.Expressions;
|
||||
import com.mysema.query.support.ProjectableQuery;
|
||||
import com.mysema.query.support.QueryMixin;
|
||||
import com.mysema.query.types.EntityPath;
|
||||
import com.mysema.query.types.Expression;
|
||||
import com.mysema.query.types.ExpressionUtils;
|
||||
import com.mysema.query.types.FactoryExpression;
|
||||
import com.mysema.query.types.OperationImpl;
|
||||
import com.mysema.query.types.ParamExpression;
|
||||
import com.mysema.query.types.ParamNotSetException;
|
||||
import com.mysema.query.types.Path;
|
||||
import com.mysema.query.types.Predicate;
|
||||
import com.mysema.query.types.QTuple;
|
||||
import com.mysema.query.types.SubQueryExpression;
|
||||
import com.mysema.query.types.query.ListSubQuery;
|
||||
import com.mysema.query.types.template.NumberTemplate;
|
||||
import com.mysema.query.types.template.SimpleTemplate;
|
||||
@ -82,6 +92,8 @@ public abstract class AbstractSQLQuery<Q extends AbstractSQLQuery<Q> & Query<Q>>
|
||||
|
||||
protected boolean unionAll;
|
||||
|
||||
private boolean useLiterals;
|
||||
|
||||
public AbstractSQLQuery(@Nullable Connection conn, Configuration configuration) {
|
||||
this(conn, configuration, new DefaultQueryMetadata().noValidate());
|
||||
}
|
||||
@ -94,6 +106,7 @@ public abstract class AbstractSQLQuery<Q extends AbstractSQLQuery<Q> & Query<Q>>
|
||||
this.conn = conn;
|
||||
this.configuration = configuration;
|
||||
this.listeners = new SQLListeners(configuration.getListeners());
|
||||
this.useLiterals = configuration.getUseLiterals();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -219,7 +232,9 @@ public abstract class AbstractSQLQuery<Q extends AbstractSQLQuery<Q> & Query<Q>>
|
||||
}
|
||||
|
||||
protected SQLSerializer createSerializer() {
|
||||
return new SQLSerializer(configuration);
|
||||
SQLSerializer serializer = new SQLSerializer(configuration);
|
||||
serializer.setUseLiterals(useLiterals);
|
||||
return serializer;
|
||||
}
|
||||
|
||||
public Q from(Expression<?> arg) {
|
||||
@ -795,4 +810,8 @@ public abstract class AbstractSQLQuery<Q extends AbstractSQLQuery<Q> & Query<Q>>
|
||||
}
|
||||
}
|
||||
|
||||
public void setUseLiterals(boolean useLiterals) {
|
||||
this.useLiterals = useLiterals;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -57,6 +57,8 @@ public final class Configuration {
|
||||
|
||||
private boolean hasTableColumnTypes = false;
|
||||
|
||||
private boolean useLiterals = false;
|
||||
|
||||
/**
|
||||
* Create a new Configuration instance
|
||||
*
|
||||
@ -259,4 +261,18 @@ public final class Configuration {
|
||||
return listeners;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return
|
||||
*/
|
||||
public boolean getUseLiterals() {
|
||||
return useLiterals;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param useLiterals
|
||||
*/
|
||||
public void setUseLiterals(boolean useLiterals) {
|
||||
this.useLiterals = useLiterals;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -81,6 +81,8 @@ public class SQLSerializer extends SerializerBase<SQLSerializer> {
|
||||
|
||||
private boolean inJoin = false;
|
||||
|
||||
private boolean useLiterals = false;
|
||||
|
||||
public SQLSerializer(Configuration conf) {
|
||||
this(conf, false);
|
||||
}
|
||||
@ -409,9 +411,11 @@ public class SQLSerializer extends SerializerBase<SQLSerializer> {
|
||||
append("\n");
|
||||
serialize(subQuery.getMetadata(), false);
|
||||
} else {
|
||||
for (int i = 0; i < columns.size(); i++) {
|
||||
if (values.get(i) instanceof Constant<?>) {
|
||||
constantPaths.add(columns.get(i));
|
||||
if (!useLiterals) {
|
||||
for (int i = 0; i < columns.size(); i++) {
|
||||
if (values.get(i) instanceof Constant<?>) {
|
||||
constantPaths.add(columns.get(i));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -449,9 +453,11 @@ public class SQLSerializer extends SerializerBase<SQLSerializer> {
|
||||
serialize(subQuery.getMetadata(), false);
|
||||
|
||||
} else {
|
||||
for (int i = 0; i < columns.size(); i++) {
|
||||
if (values.get(i) instanceof Constant<?>) {
|
||||
constantPaths.add(columns.get(i));
|
||||
if (!useLiterals) {
|
||||
for (int i = 0; i < columns.size(); i++) {
|
||||
if (values.get(i) instanceof Constant<?>) {
|
||||
constantPaths.add(columns.get(i));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -488,7 +494,7 @@ public class SQLSerializer extends SerializerBase<SQLSerializer> {
|
||||
}
|
||||
handle(update.getFirst());
|
||||
append(" = ");
|
||||
if (update.getSecond() instanceof Constant<?>) {
|
||||
if (!useLiterals && update.getSecond() instanceof Constant<?>) {
|
||||
constantPaths.add(update.getFirst());
|
||||
}
|
||||
handle(update.getSecond());
|
||||
@ -608,7 +614,22 @@ public class SQLSerializer extends SerializerBase<SQLSerializer> {
|
||||
|
||||
@Override
|
||||
public void visitConstant(Object constant) {
|
||||
if (constant instanceof Collection) {
|
||||
if (useLiterals) {
|
||||
if (constant instanceof Collection) {
|
||||
append("(");
|
||||
boolean first = true;
|
||||
for (Object o : ((Collection)constant)) {
|
||||
if (!first) {
|
||||
append(COMMA);
|
||||
}
|
||||
append(templates.asLiteral(o));
|
||||
first = false;
|
||||
}
|
||||
append(")");
|
||||
} else {
|
||||
append(templates.asLiteral(constant));
|
||||
}
|
||||
} else if (constant instanceof Collection) {
|
||||
append("(");
|
||||
boolean first = true;
|
||||
for (Object o : ((Collection)constant)) {
|
||||
@ -699,6 +720,7 @@ public class SQLSerializer extends SerializerBase<SQLSerializer> {
|
||||
@Override
|
||||
protected void visitOperation(Class<?> type, Operator<?> operator, List<? extends Expression<?>> args) {
|
||||
if (args.size() == 2
|
||||
&& !useLiterals
|
||||
&& args.get(0) instanceof Path<?>
|
||||
&& args.get(1) instanceof Constant<?>
|
||||
&& operator != Ops.NUMCAST) {
|
||||
@ -752,4 +774,8 @@ public class SQLSerializer extends SerializerBase<SQLSerializer> {
|
||||
}
|
||||
}
|
||||
|
||||
public void setUseLiterals(boolean useLiterals) {
|
||||
this.useLiterals = useLiterals;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -19,6 +19,9 @@ import java.util.HashMap;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
import org.joda.time.format.DateTimeFormat;
|
||||
import org.joda.time.format.DateTimeFormatter;
|
||||
|
||||
import com.google.common.primitives.Primitives;
|
||||
import com.mysema.query.JoinType;
|
||||
import com.mysema.query.QueryException;
|
||||
@ -38,6 +41,8 @@ import com.mysema.query.types.Templates;
|
||||
*/
|
||||
public class SQLTemplates extends Templates {
|
||||
|
||||
enum DateTimeType {DATE, TIME, DATETIME};
|
||||
|
||||
public static final Expression<?> RECURSIVE = TemplateExpressionImpl.create(Object.class, "");
|
||||
|
||||
public static final SQLTemplates DEFAULT = new SQLTemplates("\"",'\\',false);
|
||||
@ -81,6 +86,12 @@ public class SQLTemplates extends Templates {
|
||||
|
||||
}
|
||||
|
||||
private static final DateTimeFormatter dateFormatter = DateTimeFormat.forPattern("yyyy-MM-dd");
|
||||
|
||||
private static final DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
|
||||
|
||||
private static final DateTimeFormatter timeFormatter = DateTimeFormat.forPattern("HH:mm:ss");
|
||||
|
||||
private final Map<Class<?>, String> class2type = new HashMap<Class<?>, String>();
|
||||
|
||||
private final String quoteStr;
|
||||
@ -294,6 +305,37 @@ public class SQLTemplates extends Templates {
|
||||
class2type.put(java.sql.Timestamp.class, "timestamp");
|
||||
}
|
||||
|
||||
public String asLiteral(Object o) {
|
||||
if (o instanceof Character) {
|
||||
return "'" + o + "'"; // TODO proper escaping
|
||||
} else if (o instanceof String) {
|
||||
return "'" + o + "'"; // TODO proper escaping
|
||||
} else if (o instanceof java.util.Date) {
|
||||
DateTimeFormatter formatter = dateTimeFormatter;
|
||||
DateTimeType type = DateTimeType.DATETIME;
|
||||
if (o instanceof java.sql.Date) {
|
||||
formatter = dateFormatter;
|
||||
type = DateTimeType.DATE;
|
||||
} else if (o instanceof java.sql.Time) {
|
||||
formatter = timeFormatter;
|
||||
type = DateTimeType.TIME;
|
||||
}
|
||||
return asLiteral(o, type, formatter);
|
||||
} else {
|
||||
return o.toString();
|
||||
}
|
||||
}
|
||||
|
||||
public String asLiteral(Object o, DateTimeType type, DateTimeFormatter formatter) {
|
||||
String keyword = "ts";
|
||||
if (type == DateTimeType.DATE) {
|
||||
keyword = "d";
|
||||
} else if (type == DateTimeType.TIME) {
|
||||
keyword = "t";
|
||||
}
|
||||
return "{" + keyword + " '" + formatter.print(((java.util.Date)o).getTime()) + "'}";
|
||||
}
|
||||
|
||||
protected void addClass2TypeMappings(String type, Class<?>... classes) {
|
||||
for (Class<?> cl : classes) {
|
||||
class2type.put(cl, type);
|
||||
@ -444,7 +486,6 @@ public class SQLTemplates extends Templates {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public final String getUpdate() {
|
||||
return update;
|
||||
}
|
||||
@ -761,6 +802,4 @@ public class SQLTemplates extends Templates {
|
||||
this.nullsLast = nullsLast;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -13,6 +13,8 @@
|
||||
*/
|
||||
package com.mysema.query.sql;
|
||||
|
||||
import org.joda.time.format.DateTimeFormatter;
|
||||
|
||||
import com.mysema.query.types.Ops;
|
||||
|
||||
/**
|
||||
@ -86,4 +88,16 @@ public class SQLiteTemplates extends SQLTemplates {
|
||||
// add(Ops.StringOps.RPAD, "concat({0}, repeat(' ', {1} - length({0})))");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String asLiteral(Object o, DateTimeType type, DateTimeFormatter formatter) {
|
||||
long millis = ((java.util.Date)o).getTime();
|
||||
if (type == DateTimeType.DATE) {
|
||||
return "date(" + millis + ",'unixepoch')";
|
||||
} else if (type == DateTimeType.TIME) {
|
||||
return "time(" + millis + ",'unixepoch')";
|
||||
} else {
|
||||
return "datetime(" + millis + ",'unixepoch')";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
/*
|
||||
* Copyright 2011, Mysema Ltd
|
||||
*
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
@ -42,7 +42,7 @@ import com.mysema.query.sql.oracle.OracleQuery;
|
||||
public abstract class AbstractBaseTest {
|
||||
|
||||
protected final class TestQuery extends AbstractSQLQuery<TestQuery> implements SQLCommonQuery<TestQuery> {
|
||||
|
||||
|
||||
private TestQuery(Connection conn, Configuration configuration) {
|
||||
super(conn, configuration);
|
||||
}
|
||||
@ -50,7 +50,7 @@ public abstract class AbstractBaseTest {
|
||||
private TestQuery(Connection conn, Configuration configuration, QueryMetadata metadata) {
|
||||
super(conn, configuration, metadata);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected String buildQueryString(boolean countRow) {
|
||||
String rv = super.buildQueryString(countRow);
|
||||
@ -71,20 +71,20 @@ public abstract class AbstractBaseTest {
|
||||
}
|
||||
|
||||
private Connection connection = Connections.getConnection();
|
||||
|
||||
|
||||
private SQLTemplates templates = Connections.getTemplates();
|
||||
|
||||
private Configuration configuration = new Configuration(templates);
|
||||
|
||||
protected Configuration configuration = new Configuration(templates);
|
||||
|
||||
@Nullable
|
||||
protected String expectedQuery;
|
||||
|
||||
|
||||
@Rule
|
||||
public static MethodRule skipForQuotedRule = new SkipForQuotedRule();
|
||||
|
||||
|
||||
@Rule
|
||||
public static MethodRule targetRule = new TargetRule();
|
||||
|
||||
|
||||
protected SQLUpdateClause update(RelationalPath<?> e) {
|
||||
return new SQLUpdateClause(connection, configuration, e);
|
||||
}
|
||||
@ -96,7 +96,7 @@ public abstract class AbstractBaseTest {
|
||||
protected SQLInsertClause insert(RelationalPath<?> e, AbstractSQLSubQuery<?> sq) {
|
||||
return new SQLInsertClause(connection, configuration, e, sq);
|
||||
}
|
||||
|
||||
|
||||
protected SQLDeleteClause delete(RelationalPath<?> e) {
|
||||
return new SQLDeleteClause(connection, configuration, e);
|
||||
}
|
||||
@ -104,7 +104,7 @@ public abstract class AbstractBaseTest {
|
||||
protected SQLMergeClause merge(RelationalPath<?> e) {
|
||||
return new SQLMergeClause(connection, configuration, e);
|
||||
}
|
||||
|
||||
|
||||
protected ExtendedSQLQuery extQuery() {
|
||||
return new ExtendedSQLQuery(connection, configuration);
|
||||
}
|
||||
@ -112,20 +112,20 @@ public abstract class AbstractBaseTest {
|
||||
protected MySQLQuery mysqlQuery() {
|
||||
return new MySQLQuery(connection, configuration);
|
||||
}
|
||||
|
||||
|
||||
protected SQLInsertClause mysqlReplace(RelationalPath<?> path) {
|
||||
return new MySQLReplaceClause(connection, configuration, path);
|
||||
}
|
||||
|
||||
|
||||
protected TestQuery query() {
|
||||
return new TestQuery(connection, configuration);
|
||||
}
|
||||
|
||||
|
||||
protected TestQuery testQuery() {
|
||||
return new TestQuery(connection, configuration,
|
||||
return new TestQuery(connection, configuration,
|
||||
new DefaultQueryMetadata().noValidate());
|
||||
}
|
||||
|
||||
|
||||
protected OracleQuery oracleQuery() {
|
||||
return new OracleQuery(connection, configuration) {
|
||||
@Override
|
||||
|
||||
@ -32,7 +32,7 @@ import com.mysema.query.sql.domain.QEmployee;
|
||||
import com.mysema.testutil.ExcludeIn;
|
||||
|
||||
@ExcludeIn({CUBRID, DERBY, ORACLE, SQLSERVER, POSTGRES, SQLITE, TERADATA})
|
||||
public class BeanPopulationBase extends AbstractBaseTest{
|
||||
public class BeanPopulationBase extends AbstractBaseTest {
|
||||
|
||||
private final QEmployee e = new QEmployee("e");
|
||||
|
||||
|
||||
@ -1262,7 +1262,7 @@ public class SelectBase extends AbstractBaseTest{
|
||||
}
|
||||
|
||||
@Test
|
||||
@ExcludeIn(SQLITE)
|
||||
@ExcludeIn({POSTGRES, SQLITE})
|
||||
public void String_IndexOf() {
|
||||
StringExpression str = Expressions.stringTemplate("' abcd '");
|
||||
|
||||
|
||||
@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Copyright 2013, Mysema Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.mysema.query;
|
||||
|
||||
public class SelectBaseUseLiterals extends SelectBase {
|
||||
|
||||
public SelectBaseUseLiterals() {
|
||||
configuration.setUseLiterals(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void Limit_and_Offset2() {
|
||||
// not supported
|
||||
}
|
||||
|
||||
@Override
|
||||
public void Path_Alias() {
|
||||
// not supported
|
||||
}
|
||||
|
||||
}
|
||||
@ -221,4 +221,14 @@ public class SQLSerializerTest {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void UseLiterals() {
|
||||
SQLSerializer serializer = new SQLSerializer(Configuration.DEFAULT);
|
||||
serializer.setUseLiterals(true);
|
||||
|
||||
Expression<?> expr = SQLExpressions.datediff(DatePart.year, employee.datefield, new java.sql.Date(0));
|
||||
serializer.handle(expr);
|
||||
assertEquals("datediff('year',EMPLOYEE.DATEFIELD,'1968')", serializer.toString());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -16,8 +16,12 @@ package com.mysema.query.sql;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.sql.Date;
|
||||
import java.sql.Time;
|
||||
import java.sql.Timestamp;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.mysema.query.types.ConstantImpl;
|
||||
@ -39,6 +43,15 @@ public class SQLTemplatesTest {
|
||||
assertEquals("fetch first 5 rows only", serializer.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void AsLiteral() {
|
||||
SQLTemplates templates = SQLTemplates.DEFAULT;
|
||||
assertEquals("'1970-01-01'", templates.asLiteral(new Date(0)));
|
||||
assertEquals("'03:00:00'", templates.asLiteral(new Time(0)));
|
||||
assertEquals("'1970-01-01 03:00:00'", templates.asLiteral(new Timestamp(0)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void Quote() {
|
||||
Pattern pattern = Pattern.compile("[a-zA-Z0-9_\\-]+");
|
||||
|
||||
@ -9,6 +9,7 @@ import com.mysema.query.InsertBase;
|
||||
import com.mysema.query.LikeEscapeBase;
|
||||
import com.mysema.query.MergeBase;
|
||||
import com.mysema.query.SelectBase;
|
||||
import com.mysema.query.SelectBaseUseLiterals;
|
||||
import com.mysema.query.SubqueriesBase;
|
||||
import com.mysema.query.TypesBase;
|
||||
import com.mysema.query.UnionBase;
|
||||
@ -23,6 +24,7 @@ public class DerbySuiteTest extends AbstractSuite {
|
||||
public static class LikeEscape extends LikeEscapeBase {}
|
||||
public static class Merge extends MergeBase {}
|
||||
public static class Select extends SelectBase {}
|
||||
public static class SelectUseLiterals extends SelectBaseUseLiterals {}
|
||||
public static class Subqueries extends SubqueriesBase {}
|
||||
public static class Types extends TypesBase {}
|
||||
public static class Union extends UnionBase {}
|
||||
|
||||
@ -9,6 +9,7 @@ import com.mysema.query.InsertBase;
|
||||
import com.mysema.query.LikeEscapeBase;
|
||||
import com.mysema.query.MergeBase;
|
||||
import com.mysema.query.SelectBase;
|
||||
import com.mysema.query.SelectBaseUseLiterals;
|
||||
import com.mysema.query.SubqueriesBase;
|
||||
import com.mysema.query.TypesBase;
|
||||
import com.mysema.query.UnionBase;
|
||||
@ -23,6 +24,7 @@ public class H2SuiteTest extends AbstractSuite {
|
||||
public static class LikeEscape extends LikeEscapeBase {}
|
||||
public static class Merge extends MergeBase {}
|
||||
public static class Select extends SelectBase {}
|
||||
public static class SelectUseLiterals extends SelectBaseUseLiterals {}
|
||||
public static class Subqueries extends SubqueriesBase {}
|
||||
public static class Types extends TypesBase {}
|
||||
public static class Union extends UnionBase {}
|
||||
|
||||
@ -9,6 +9,7 @@ import com.mysema.query.InsertBase;
|
||||
import com.mysema.query.LikeEscapeBase;
|
||||
import com.mysema.query.MergeBase;
|
||||
import com.mysema.query.SelectBase;
|
||||
import com.mysema.query.SelectBaseUseLiterals;
|
||||
import com.mysema.query.SubqueriesBase;
|
||||
import com.mysema.query.TypesBase;
|
||||
import com.mysema.query.UnionBase;
|
||||
@ -23,6 +24,7 @@ public class H2WithQuotingTest extends AbstractSuite {
|
||||
public static class LikeEscape extends LikeEscapeBase {}
|
||||
public static class Merge extends MergeBase {}
|
||||
public static class Select extends SelectBase {}
|
||||
public static class SelectUseLiterals extends SelectBaseUseLiterals {}
|
||||
public static class Subqueries extends SubqueriesBase {}
|
||||
public static class Types extends TypesBase {}
|
||||
public static class Union extends UnionBase {}
|
||||
|
||||
@ -9,6 +9,7 @@ import com.mysema.query.InsertBase;
|
||||
import com.mysema.query.LikeEscapeBase;
|
||||
import com.mysema.query.MergeBase;
|
||||
import com.mysema.query.SelectBase;
|
||||
import com.mysema.query.SelectBaseUseLiterals;
|
||||
import com.mysema.query.SubqueriesBase;
|
||||
import com.mysema.query.TypesBase;
|
||||
import com.mysema.query.UnionBase;
|
||||
@ -23,6 +24,7 @@ public class H2WithSchemaTest extends AbstractSuite {
|
||||
public static class LikeEscape extends LikeEscapeBase {}
|
||||
public static class Merge extends MergeBase {}
|
||||
public static class Select extends SelectBase {}
|
||||
public static class SelectUseLiterals extends SelectBaseUseLiterals {}
|
||||
public static class Subqueries extends SubqueriesBase {}
|
||||
public static class Types extends TypesBase {}
|
||||
public static class Union extends UnionBase {}
|
||||
|
||||
@ -9,6 +9,7 @@ import com.mysema.query.InsertBase;
|
||||
import com.mysema.query.LikeEscapeBase;
|
||||
import com.mysema.query.MergeBase;
|
||||
import com.mysema.query.SelectBase;
|
||||
import com.mysema.query.SelectBaseUseLiterals;
|
||||
import com.mysema.query.SubqueriesBase;
|
||||
import com.mysema.query.TypesBase;
|
||||
import com.mysema.query.UnionBase;
|
||||
@ -23,6 +24,7 @@ public class HsqldbSuiteTest extends AbstractSuite {
|
||||
public static class LikeEscape extends LikeEscapeBase {}
|
||||
public static class Merge extends MergeBase {}
|
||||
public static class Select extends SelectBase {}
|
||||
public static class SelectUseLiterals extends SelectBaseUseLiterals {}
|
||||
public static class Subqueries extends SubqueriesBase {}
|
||||
public static class Types extends TypesBase {}
|
||||
public static class Union extends UnionBase {}
|
||||
|
||||
@ -10,6 +10,7 @@ import com.mysema.query.InsertBase;
|
||||
import com.mysema.query.LikeEscapeBase;
|
||||
import com.mysema.query.MergeBase;
|
||||
import com.mysema.query.SelectBase;
|
||||
import com.mysema.query.SelectBaseUseLiterals;
|
||||
import com.mysema.query.SubqueriesBase;
|
||||
import com.mysema.query.TypesBase;
|
||||
import com.mysema.query.UnionBase;
|
||||
@ -25,6 +26,7 @@ public class MSSQLSuiteTest extends AbstractSuite {
|
||||
public static class LikeEscape extends LikeEscapeBase {}
|
||||
public static class Merge extends MergeBase {}
|
||||
public static class Select extends SelectBase {}
|
||||
public static class SelectUseLiterals extends SelectBaseUseLiterals {}
|
||||
public static class Subqueries extends SubqueriesBase {}
|
||||
public static class Types extends TypesBase {}
|
||||
public static class Union extends UnionBase {}
|
||||
|
||||
@ -10,6 +10,7 @@ import com.mysema.query.InsertBase;
|
||||
import com.mysema.query.LikeEscapeBase;
|
||||
import com.mysema.query.MergeBase;
|
||||
import com.mysema.query.SelectBase;
|
||||
import com.mysema.query.SelectBaseUseLiterals;
|
||||
import com.mysema.query.SubqueriesBase;
|
||||
import com.mysema.query.TypesBase;
|
||||
import com.mysema.query.UnionBase;
|
||||
@ -26,6 +27,7 @@ public class MySQLSuiteTest extends AbstractSuite {
|
||||
public static class LikeEscape extends LikeEscapeBase {}
|
||||
public static class Merge extends MergeBase {}
|
||||
public static class Select extends SelectBase {}
|
||||
public static class SelectUseLiterals extends SelectBaseUseLiterals {}
|
||||
public static class Subqueries extends SubqueriesBase {}
|
||||
public static class Types extends TypesBase {}
|
||||
public static class Union extends UnionBase {}
|
||||
|
||||
@ -10,6 +10,7 @@ import com.mysema.query.InsertBase;
|
||||
import com.mysema.query.LikeEscapeBase;
|
||||
import com.mysema.query.MergeBase;
|
||||
import com.mysema.query.SelectBase;
|
||||
import com.mysema.query.SelectBaseUseLiterals;
|
||||
import com.mysema.query.SubqueriesBase;
|
||||
import com.mysema.query.TypesBase;
|
||||
import com.mysema.query.UnionBase;
|
||||
@ -26,6 +27,7 @@ public class MySQLWithQuotingTest extends AbstractSuite {
|
||||
public static class LikeEscape extends LikeEscapeBase {}
|
||||
public static class Merge extends MergeBase {}
|
||||
public static class Select extends SelectBase {}
|
||||
public static class SelectUseLiterals extends SelectBaseUseLiterals {}
|
||||
public static class Subqueries extends SubqueriesBase {}
|
||||
public static class Types extends TypesBase {}
|
||||
public static class Union extends UnionBase {}
|
||||
|
||||
@ -10,6 +10,7 @@ import com.mysema.query.InsertBase;
|
||||
import com.mysema.query.LikeEscapeBase;
|
||||
import com.mysema.query.MergeBase;
|
||||
import com.mysema.query.SelectBase;
|
||||
import com.mysema.query.SelectBaseUseLiterals;
|
||||
import com.mysema.query.SubqueriesBase;
|
||||
import com.mysema.query.TypesBase;
|
||||
import com.mysema.query.UnionBase;
|
||||
@ -26,6 +27,7 @@ public class OracleSuiteTest extends AbstractSuite {
|
||||
public static class LikeEscape extends LikeEscapeBase {}
|
||||
public static class Merge extends MergeBase {}
|
||||
public static class Select extends SelectBase {}
|
||||
public static class SelectUseLiterals extends SelectBaseUseLiterals {}
|
||||
public static class Subqueries extends SubqueriesBase {}
|
||||
public static class Types extends TypesBase {}
|
||||
public static class Union extends UnionBase {}
|
||||
|
||||
@ -10,6 +10,7 @@ import com.mysema.query.InsertBase;
|
||||
import com.mysema.query.LikeEscapeBase;
|
||||
import com.mysema.query.MergeBase;
|
||||
import com.mysema.query.SelectBase;
|
||||
import com.mysema.query.SelectBaseUseLiterals;
|
||||
import com.mysema.query.SubqueriesBase;
|
||||
import com.mysema.query.TypesBase;
|
||||
import com.mysema.query.UnionBase;
|
||||
@ -26,6 +27,7 @@ public class OracleWithQuotingTest extends AbstractSuite {
|
||||
public static class LikeEscape extends LikeEscapeBase {}
|
||||
public static class Merge extends MergeBase {}
|
||||
public static class Select extends SelectBase {}
|
||||
public static class SelectUseLiterals extends SelectBaseUseLiterals {}
|
||||
public static class Subqueries extends SubqueriesBase {}
|
||||
public static class Types extends TypesBase {}
|
||||
public static class Union extends UnionBase {}
|
||||
|
||||
@ -10,6 +10,7 @@ import com.mysema.query.InsertBase;
|
||||
import com.mysema.query.LikeEscapeBase;
|
||||
import com.mysema.query.MergeBase;
|
||||
import com.mysema.query.SelectBase;
|
||||
import com.mysema.query.SelectBaseUseLiterals;
|
||||
import com.mysema.query.SubqueriesBase;
|
||||
import com.mysema.query.TypesBase;
|
||||
import com.mysema.query.UnionBase;
|
||||
@ -26,6 +27,7 @@ public class PostgreSQLSuiteTest extends AbstractSuite {
|
||||
public static class LikeEscape extends LikeEscapeBase {}
|
||||
public static class Merge extends MergeBase {}
|
||||
public static class Select extends SelectBase {}
|
||||
public static class SelectUseLiterals extends SelectBaseUseLiterals {}
|
||||
public static class Subqueries extends SubqueriesBase {}
|
||||
public static class Types extends TypesBase {}
|
||||
public static class Union extends UnionBase {}
|
||||
|
||||
@ -9,6 +9,7 @@ import com.mysema.query.InsertBase;
|
||||
import com.mysema.query.LikeEscapeBase;
|
||||
import com.mysema.query.MergeBase;
|
||||
import com.mysema.query.SelectBase;
|
||||
import com.mysema.query.SelectBaseUseLiterals;
|
||||
import com.mysema.query.SubqueriesBase;
|
||||
import com.mysema.query.TypesBase;
|
||||
import com.mysema.query.UnionBase;
|
||||
@ -23,6 +24,7 @@ public class SQLiteSuiteTest extends AbstractSuite {
|
||||
public static class LikeEscape extends LikeEscapeBase {}
|
||||
public static class Merge extends MergeBase {}
|
||||
public static class Select extends SelectBase {}
|
||||
public static class SelectUseLiterals extends SelectBaseUseLiterals {}
|
||||
public static class Subqueries extends SubqueriesBase {}
|
||||
public static class Types extends TypesBase {}
|
||||
public static class Union extends UnionBase {}
|
||||
|
||||
@ -10,6 +10,7 @@ import com.mysema.query.InsertBase;
|
||||
import com.mysema.query.LikeEscapeBase;
|
||||
import com.mysema.query.MergeBase;
|
||||
import com.mysema.query.SelectBase;
|
||||
import com.mysema.query.SelectBaseUseLiterals;
|
||||
import com.mysema.query.SubqueriesBase;
|
||||
import com.mysema.query.TypesBase;
|
||||
import com.mysema.query.UnionBase;
|
||||
@ -26,6 +27,7 @@ public class TeradataSuiteTest extends AbstractSuite {
|
||||
public static class LikeEscape extends LikeEscapeBase {}
|
||||
public static class Merge extends MergeBase {}
|
||||
public static class Select extends SelectBase {}
|
||||
public static class SelectUseLiterals extends SelectBaseUseLiterals {}
|
||||
public static class Subqueries extends SubqueriesBase {}
|
||||
public static class Types extends TypesBase {}
|
||||
public static class Union extends UnionBase {}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user