mirror of
https://github.com/querydsl/querydsl.git
synced 2026-07-03 21:07:49 +08:00
Merge pull request #718 from querydsl/i714
Extend JDBC Type to provide literal serialization
This commit is contained in:
commit
fe41423c9d
@ -232,4 +232,4 @@
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
</project>
|
||||
</project>
|
||||
@ -73,6 +73,21 @@ public final class Configuration {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the literal representation of the given constant
|
||||
*
|
||||
* @param o
|
||||
* @return
|
||||
*/
|
||||
public String asLiteral(Object o) {
|
||||
Type type = javaTypeMapping.getType(o.getClass());
|
||||
if (type != null) {
|
||||
return templates.serialize(type.getLiteral(o), type.getSQLTypes()[0]);
|
||||
} else {
|
||||
throw new IllegalArgumentException("Unsupported literal type " + o.getClass().getName());
|
||||
}
|
||||
}
|
||||
|
||||
public SQLTemplates getTemplates() {
|
||||
return templates;
|
||||
}
|
||||
|
||||
@ -17,6 +17,8 @@ import com.mysema.query.QueryMetadata;
|
||||
import com.mysema.query.QueryModifiers;
|
||||
import com.mysema.query.types.Ops;
|
||||
|
||||
import java.sql.Types;
|
||||
|
||||
/**
|
||||
* DerbyTemplates is an SQL dialect for Derby
|
||||
*
|
||||
@ -107,15 +109,16 @@ public class DerbyTemplates extends SQLTemplates {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String asLiteral(DateTimeType type, String literal) {
|
||||
// JDBC escape syntax
|
||||
String keyword = "ts";
|
||||
if (type == DateTimeType.DATE) {
|
||||
keyword = "d";
|
||||
} else if (type == DateTimeType.TIME) {
|
||||
keyword = "t";
|
||||
public String serialize(String literal, int jdbcType) {
|
||||
if (jdbcType == Types.TIMESTAMP) {
|
||||
return "{ts '" + literal + "'}";
|
||||
} else if (jdbcType == Types.DATE) {
|
||||
return "{d '" + literal + "'}";
|
||||
} else if (jdbcType == Types.TIME) {
|
||||
return "{t '" + literal + "'}";
|
||||
} else {
|
||||
return super.serialize(literal, jdbcType);
|
||||
}
|
||||
return "{" + keyword + " '" + literal + "'}";
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@ -13,9 +13,6 @@
|
||||
*/
|
||||
package com.mysema.query.sql;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.util.List;
|
||||
|
||||
import com.mysema.commons.lang.Pair;
|
||||
import com.mysema.query.QueryFlag.Position;
|
||||
import com.mysema.query.QueryMetadata;
|
||||
@ -24,6 +21,10 @@ import com.mysema.query.types.Expression;
|
||||
import com.mysema.query.types.Ops;
|
||||
import com.mysema.query.types.Path;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.sql.Types;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* OracleTemplates is an SQL dialect for Oracle
|
||||
*
|
||||
@ -134,12 +135,11 @@ public class OracleTemplates extends SQLTemplates {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String asLiteral(DateTimeType type, String literal) {
|
||||
if (type == DateTimeType.TIME) {
|
||||
// Oracle doesn't support the time type
|
||||
return "(timestamp '1970-01-01 " + literal + "')";
|
||||
public String serialize(String literal, int jdbcType) {
|
||||
if (jdbcType == Types.TIME) {
|
||||
return "(timestamp '1970-01-01 " + literal + "'}";
|
||||
} else {
|
||||
return super.asLiteral(type, literal);
|
||||
return super.serialize(literal, jdbcType);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -13,15 +13,6 @@
|
||||
*/
|
||||
package com.mysema.query.sql;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import com.google.common.base.Strings;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.Lists;
|
||||
@ -32,24 +23,11 @@ import com.mysema.query.QueryFlag;
|
||||
import com.mysema.query.QueryFlag.Position;
|
||||
import com.mysema.query.QueryMetadata;
|
||||
import com.mysema.query.support.SerializerBase;
|
||||
import com.mysema.query.types.Constant;
|
||||
import com.mysema.query.types.ConstantImpl;
|
||||
import com.mysema.query.types.Expression;
|
||||
import com.mysema.query.types.ExpressionUtils;
|
||||
import com.mysema.query.types.FactoryExpression;
|
||||
import com.mysema.query.types.Operator;
|
||||
import com.mysema.query.types.Ops;
|
||||
import com.mysema.query.types.Order;
|
||||
import com.mysema.query.types.OrderSpecifier;
|
||||
import com.mysema.query.types.ParamExpression;
|
||||
import com.mysema.query.types.Path;
|
||||
import com.mysema.query.types.PathMetadata;
|
||||
import com.mysema.query.types.Predicate;
|
||||
import com.mysema.query.types.SubQueryExpression;
|
||||
import com.mysema.query.types.Template;
|
||||
import com.mysema.query.types.*;
|
||||
import com.mysema.query.types.Template.Element;
|
||||
import com.mysema.query.types.TemplateExpression;
|
||||
import com.mysema.query.types.TemplateFactory;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* SqlSerializer serializes Querydsl queries into SQL
|
||||
@ -705,12 +683,12 @@ public class SQLSerializer extends SerializerBase<SQLSerializer> {
|
||||
if (!first) {
|
||||
append(COMMA);
|
||||
}
|
||||
append(templates.asLiteral(o));
|
||||
append(configuration.asLiteral(o));
|
||||
first = false;
|
||||
}
|
||||
append(")");
|
||||
} else {
|
||||
append(templates.asLiteral(constant));
|
||||
append(configuration.asLiteral(constant));
|
||||
}
|
||||
} else if (constant instanceof Collection) {
|
||||
append("(");
|
||||
|
||||
@ -20,6 +20,8 @@ import com.mysema.query.QueryModifiers;
|
||||
import com.mysema.query.support.Expressions;
|
||||
import com.mysema.query.types.Ops;
|
||||
|
||||
import java.sql.Types;
|
||||
|
||||
/**
|
||||
* SQLServerTemplates is an SQL dialect for Microsoft SQL Server
|
||||
*
|
||||
@ -115,15 +117,16 @@ public class SQLServerTemplates extends SQLTemplates {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String asLiteral(DateTimeType type, String literal) {
|
||||
// JDBC escape syntax
|
||||
String keyword = "ts";
|
||||
if (type == DateTimeType.DATE) {
|
||||
keyword = "d";
|
||||
} else if (type == DateTimeType.TIME) {
|
||||
keyword = "t";
|
||||
public String serialize(String literal, int jdbcType) {
|
||||
if (jdbcType == Types.TIMESTAMP) {
|
||||
return "{ts '" + literal + "'}";
|
||||
} else if (jdbcType == Types.DATE) {
|
||||
return "{d '" + literal + "'}";
|
||||
} else if (jdbcType == Types.TIME) {
|
||||
return "{t '" + literal + "'}";
|
||||
} else {
|
||||
return super.serialize(literal, jdbcType);
|
||||
}
|
||||
return "{" + keyword + " '" + literal + "'}";
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@ -13,20 +13,6 @@
|
||||
*/
|
||||
package com.mysema.query.sql;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
import org.joda.time.LocalDate;
|
||||
import org.joda.time.LocalTime;
|
||||
import org.joda.time.ReadableInstant;
|
||||
import org.joda.time.ReadablePartial;
|
||||
import org.joda.time.format.DateTimeFormat;
|
||||
import org.joda.time.format.DateTimeFormatter;
|
||||
|
||||
import com.google.common.primitives.Primitives;
|
||||
import com.mysema.commons.lang.Pair;
|
||||
import com.mysema.query.JoinType;
|
||||
@ -34,12 +20,11 @@ import com.mysema.query.QueryException;
|
||||
import com.mysema.query.QueryFlag.Position;
|
||||
import com.mysema.query.QueryMetadata;
|
||||
import com.mysema.query.QueryModifiers;
|
||||
import com.mysema.query.types.Expression;
|
||||
import com.mysema.query.types.Ops;
|
||||
import com.mysema.query.types.Path;
|
||||
import com.mysema.query.types.SubQueryExpression;
|
||||
import com.mysema.query.types.TemplateExpressionImpl;
|
||||
import com.mysema.query.types.Templates;
|
||||
import com.mysema.query.types.*;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.sql.Types;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* SQLTemplates extends Templates to provides SQL specific extensions
|
||||
@ -49,8 +34,6 @@ 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);
|
||||
@ -94,12 +77,6 @@ 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;
|
||||
@ -365,35 +342,25 @@ public class SQLTemplates extends Templates {
|
||||
class2type.put(java.sql.Timestamp.class, "timestamp");
|
||||
}
|
||||
|
||||
public String asLiteral(Object o) {
|
||||
if (o instanceof Character) {
|
||||
return "'" + escapeLiteral(o.toString()) + "'";
|
||||
} else if (o instanceof String) {
|
||||
return "'" + escapeLiteral(o.toString()) + "'";
|
||||
// java.util.Date
|
||||
} else if (o instanceof java.util.Date) {
|
||||
java.util.Date date = (java.util.Date)o;
|
||||
if (o instanceof java.sql.Date) {
|
||||
return asLiteral(DateTimeType.DATE, dateFormatter.print(date.getTime()));
|
||||
} else if (o instanceof java.sql.Time) {
|
||||
return asLiteral(DateTimeType.TIME, timeFormatter.print(date.getTime()));
|
||||
} else {
|
||||
return asLiteral(DateTimeType.DATETIME, dateTimeFormatter.print(date.getTime()));
|
||||
}
|
||||
// Joda time
|
||||
} else if (o instanceof ReadablePartial) {
|
||||
ReadablePartial partial = (ReadablePartial)o;
|
||||
if (o instanceof LocalDate) {
|
||||
return asLiteral(DateTimeType.DATE, dateFormatter.print(partial));
|
||||
} else if (o instanceof LocalTime) {
|
||||
return asLiteral(DateTimeType.TIME, timeFormatter.print(partial));
|
||||
} else {
|
||||
return asLiteral(DateTimeType.DATETIME, dateTimeFormatter.print(partial));
|
||||
}
|
||||
} else if (o instanceof ReadableInstant) {
|
||||
return asLiteral(DateTimeType.DATETIME, dateTimeFormatter.print((ReadableInstant)o));
|
||||
} else {
|
||||
return o.toString();
|
||||
public String serialize(String literal, int jdbcType) {
|
||||
switch (jdbcType) {
|
||||
case Types.TIMESTAMP:
|
||||
return "(timestamp '" + literal + "')";
|
||||
case Types.DATE:
|
||||
return "(date '" + literal + "')";
|
||||
case Types.TIME:
|
||||
return "(time '" + literal + "')";
|
||||
case Types.CHAR:
|
||||
case Types.CLOB:
|
||||
case Types.LONGNVARCHAR:
|
||||
case Types.LONGVARCHAR:
|
||||
case Types.NCHAR:
|
||||
case Types.NCLOB:
|
||||
case Types.NVARCHAR:
|
||||
case Types.VARCHAR:
|
||||
return "'" + escapeLiteral(literal) + "'";
|
||||
default:
|
||||
return literal;
|
||||
}
|
||||
}
|
||||
|
||||
@ -417,17 +384,6 @@ public class SQLTemplates extends Templates {
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
public String asLiteral(DateTimeType type, String literal) {
|
||||
// SQL 92 standard
|
||||
String keyword = "timestamp";
|
||||
if (type == DateTimeType.DATE) {
|
||||
keyword = "date";
|
||||
} else if (type == DateTimeType.TIME) {
|
||||
keyword = "time";
|
||||
}
|
||||
return "(" + keyword + " '" + literal + "')";
|
||||
}
|
||||
|
||||
protected void addClass2TypeMappings(String type, Class<?>... classes) {
|
||||
for (Class<?> cl : classes) {
|
||||
class2type.put(cl, type);
|
||||
|
||||
@ -14,7 +14,10 @@
|
||||
package com.mysema.query.sql;
|
||||
|
||||
import com.mysema.query.types.Ops;
|
||||
import org.joda.time.*;
|
||||
import org.joda.time.format.DateTimeFormat;
|
||||
import org.joda.time.format.DateTimeFormatter;
|
||||
|
||||
import java.sql.Types;
|
||||
|
||||
/**
|
||||
* SQLiteTemplates is a SQL dialect for SQLite
|
||||
@ -24,6 +27,12 @@ import org.joda.time.*;
|
||||
*/
|
||||
public class SQLiteTemplates extends SQLTemplates {
|
||||
|
||||
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");
|
||||
|
||||
public static Builder builder() {
|
||||
return new Builder() {
|
||||
@Override
|
||||
@ -87,19 +96,16 @@ public class SQLiteTemplates extends SQLTemplates {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String asLiteral(Object o) {
|
||||
if (o instanceof java.util.Date) {
|
||||
return String.valueOf(((java.util.Date)o).getTime());
|
||||
} else if (o instanceof ReadableInstant) {
|
||||
return String.valueOf(((ReadableInstant) o).getMillis());
|
||||
} else if (o instanceof LocalDate) {
|
||||
return String.valueOf(((LocalDate) o).toDateTimeAtStartOfDay(DateTimeZone.UTC).getMillis());
|
||||
} else if (o instanceof LocalDateTime) {
|
||||
return String.valueOf(((LocalDateTime) o).toDateTime(DateTimeZone.UTC).getMillis());
|
||||
} else if (o instanceof LocalTime) {
|
||||
return String.valueOf(((LocalTime) o).getMillisOfDay());
|
||||
public String serialize(String literal, int jdbcType) {
|
||||
// XXX doesn't work with LocalDate, LocalDateTime and LocalTime
|
||||
if (jdbcType == Types.TIMESTAMP) {
|
||||
return String.valueOf(dateTimeFormatter.parseDateTime(literal).getMillis());
|
||||
} else if (jdbcType == Types.DATE) {
|
||||
return String.valueOf(dateFormatter.parseDateTime(literal).getMillis());
|
||||
} else if (jdbcType == Types.TIME) {
|
||||
return String.valueOf(timeFormatter.parseDateTime(literal).getMillis());
|
||||
} else {
|
||||
return super.asLiteral(o);
|
||||
return super.serialize(literal, jdbcType);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -13,12 +13,6 @@
|
||||
*/
|
||||
package com.mysema.query.sql.teradata;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.Maps;
|
||||
import com.mysema.query.sql.Configuration;
|
||||
@ -26,6 +20,12 @@ import com.mysema.query.sql.SQLBindings;
|
||||
import com.mysema.query.sql.SQLTemplates;
|
||||
import com.mysema.query.sql.dml.AbstractSQLClause;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* SetQueryBandClause provides support for Teradata specific set query_band executions.
|
||||
*
|
||||
@ -116,9 +116,9 @@ public class SetQueryBandClause extends AbstractSQLClause<SetQueryBandClause> {
|
||||
builder.append(";");
|
||||
}
|
||||
if (configuration.getUseLiterals() || forSession) {
|
||||
queryString = "set query_band="
|
||||
+ configuration.getTemplates().asLiteral(builder.toString())
|
||||
+ (forSession ? " for session" : " for transaction");
|
||||
queryString = "set query_band='"
|
||||
+ configuration.getTemplates().escapeLiteral(builder.toString())
|
||||
+ (forSession ? "' for session" : "' for transaction");
|
||||
parameter = null;
|
||||
} else {
|
||||
queryString = "set query_band=?" + (forSession ? " for session" : " for transaction");
|
||||
|
||||
@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright 2014, 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.sql.types;
|
||||
|
||||
import org.joda.time.format.DateTimeFormat;
|
||||
import org.joda.time.format.DateTimeFormatter;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.TimeZone;
|
||||
|
||||
/**
|
||||
* Common abstract superclass for Type implementations
|
||||
*
|
||||
* @author tiwe
|
||||
*
|
||||
* @param <T>
|
||||
*/
|
||||
public abstract class AbstractDateTimeType<T> extends AbstractType<T> {
|
||||
|
||||
private static final Calendar UTC = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
|
||||
|
||||
static {
|
||||
UTC.setTimeInMillis(0);
|
||||
}
|
||||
|
||||
protected static Calendar utc() {
|
||||
return (Calendar) UTC.clone();
|
||||
}
|
||||
|
||||
protected static final DateTimeFormatter dateFormatter = DateTimeFormat.forPattern("yyyy-MM-dd");
|
||||
|
||||
protected static final DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
|
||||
|
||||
protected static final DateTimeFormatter timeFormatter = DateTimeFormat.forPattern("HH:mm:ss");
|
||||
|
||||
public AbstractDateTimeType(int type) {
|
||||
super(type);
|
||||
}
|
||||
|
||||
}
|
||||
@ -13,9 +13,6 @@
|
||||
*/
|
||||
package com.mysema.query.sql.types;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.TimeZone;
|
||||
|
||||
/**
|
||||
* Common abstract superclass for Type implementations
|
||||
*
|
||||
@ -25,16 +22,6 @@ import java.util.TimeZone;
|
||||
*/
|
||||
public abstract class AbstractType<T> implements Type<T> {
|
||||
|
||||
private static final Calendar UTC = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
|
||||
|
||||
static {
|
||||
UTC.setTimeInMillis(0);
|
||||
}
|
||||
|
||||
protected static Calendar utc() {
|
||||
return (Calendar) UTC.clone();
|
||||
}
|
||||
|
||||
private final int type;
|
||||
|
||||
public AbstractType(int type) {
|
||||
@ -46,4 +33,8 @@ public abstract class AbstractType<T> implements Type<T> {
|
||||
return new int[]{type};
|
||||
}
|
||||
|
||||
public String getLiteral(T value) {
|
||||
return value.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -26,7 +26,7 @@ import java.util.Calendar;
|
||||
* @author tiwe
|
||||
*
|
||||
*/
|
||||
public class CalendarType extends AbstractType<Calendar> {
|
||||
public class CalendarType extends AbstractDateTimeType<Calendar> {
|
||||
|
||||
public CalendarType() {
|
||||
super(Types.TIMESTAMP);
|
||||
@ -36,6 +36,11 @@ public class CalendarType extends AbstractType<Calendar> {
|
||||
super(type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLiteral(Calendar value) {
|
||||
return dateTimeFormatter.print(value.getTimeInMillis());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Calendar getValue(ResultSet rs, int startIndex) throws SQLException {
|
||||
Timestamp ts = rs.getTimestamp(startIndex);
|
||||
|
||||
@ -13,21 +13,17 @@
|
||||
*/
|
||||
package com.mysema.query.sql.types;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Timestamp;
|
||||
import java.sql.Types;
|
||||
|
||||
import org.joda.time.DateTime;
|
||||
|
||||
import java.sql.*;
|
||||
|
||||
/**
|
||||
* DateTimeType maps DateTime to Timestamp on the JDBC level
|
||||
*
|
||||
* @author tiwe
|
||||
*
|
||||
*/
|
||||
public class DateTimeType extends AbstractType<DateTime> {
|
||||
public class DateTimeType extends AbstractDateTimeType<DateTime> {
|
||||
|
||||
public DateTimeType() {
|
||||
super(Types.TIMESTAMP);
|
||||
@ -37,6 +33,11 @@ public class DateTimeType extends AbstractType<DateTime> {
|
||||
super(type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLiteral(DateTime value) {
|
||||
return dateTimeFormatter.print(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<DateTime> getReturnedClass() {
|
||||
return DateTime.class;
|
||||
|
||||
@ -13,11 +13,7 @@
|
||||
*/
|
||||
package com.mysema.query.sql.types;
|
||||
|
||||
import java.sql.Date;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Types;
|
||||
import java.sql.*;
|
||||
|
||||
/**
|
||||
* DateType maps Date to Date on the JDBC level
|
||||
@ -25,7 +21,7 @@ import java.sql.Types;
|
||||
* @author tiwe
|
||||
*
|
||||
*/
|
||||
public class DateType extends AbstractType<Date> {
|
||||
public class DateType extends AbstractDateTimeType<Date> {
|
||||
|
||||
public DateType() {
|
||||
super(Types.DATE);
|
||||
@ -35,6 +31,11 @@ public class DateType extends AbstractType<Date> {
|
||||
super(type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLiteral(Date value) {
|
||||
return dateFormatter.print(value.getTime());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Date getValue(ResultSet rs, int startIndex) throws SQLException {
|
||||
return rs.getDate(startIndex);
|
||||
|
||||
@ -13,23 +13,19 @@
|
||||
*/
|
||||
package com.mysema.query.sql.types;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Timestamp;
|
||||
import java.sql.Types;
|
||||
|
||||
import org.joda.time.DateTime;
|
||||
import org.joda.time.DateTimeZone;
|
||||
import org.joda.time.LocalDateTime;
|
||||
|
||||
import java.sql.*;
|
||||
|
||||
/**
|
||||
* LocalDateTimeType maps LocalDateTime to Timestamp on the JDBC level
|
||||
*
|
||||
* @author tiwe
|
||||
*
|
||||
*/
|
||||
public class LocalDateTimeType extends AbstractType<LocalDateTime> {
|
||||
public class LocalDateTimeType extends AbstractDateTimeType<LocalDateTime> {
|
||||
|
||||
public LocalDateTimeType() {
|
||||
super(Types.TIMESTAMP);
|
||||
@ -39,6 +35,11 @@ public class LocalDateTimeType extends AbstractType<LocalDateTime> {
|
||||
super(type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLiteral(LocalDateTime value) {
|
||||
return dateTimeFormatter.print(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<LocalDateTime> getReturnedClass() {
|
||||
return LocalDateTime.class;
|
||||
|
||||
@ -13,22 +13,18 @@
|
||||
*/
|
||||
package com.mysema.query.sql.types;
|
||||
|
||||
import java.sql.Date;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Types;
|
||||
|
||||
import org.joda.time.DateTimeZone;
|
||||
import org.joda.time.LocalDate;
|
||||
|
||||
import java.sql.*;
|
||||
|
||||
/**
|
||||
* LocalDateType maps LocalDate to Date on the JDBC level
|
||||
*
|
||||
* @author tiwe
|
||||
*
|
||||
*/
|
||||
public class LocalDateType extends AbstractType<LocalDate> {
|
||||
public class LocalDateType extends AbstractDateTimeType<LocalDate> {
|
||||
|
||||
public LocalDateType() {
|
||||
super(Types.DATE);
|
||||
@ -38,6 +34,11 @@ public class LocalDateType extends AbstractType<LocalDate> {
|
||||
super(type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLiteral(LocalDate value) {
|
||||
return dateFormatter.print(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<LocalDate> getReturnedClass() {
|
||||
return LocalDate.class;
|
||||
|
||||
@ -13,22 +13,18 @@
|
||||
*/
|
||||
package com.mysema.query.sql.types;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Time;
|
||||
import java.sql.Types;
|
||||
|
||||
import org.joda.time.DateTimeZone;
|
||||
import org.joda.time.LocalTime;
|
||||
|
||||
import java.sql.*;
|
||||
|
||||
/**
|
||||
* LocalTimeType maps LocalTime to Time on the JDBC level
|
||||
*
|
||||
* @author tiwe
|
||||
*
|
||||
*/
|
||||
public class LocalTimeType extends AbstractType<LocalTime> {
|
||||
public class LocalTimeType extends AbstractDateTimeType<LocalTime> {
|
||||
|
||||
public LocalTimeType() {
|
||||
super(Types.TIME);
|
||||
@ -38,6 +34,11 @@ public class LocalTimeType extends AbstractType<LocalTime> {
|
||||
super(type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLiteral(LocalTime value) {
|
||||
return timeFormatter.print(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<LocalTime> getReturnedClass() {
|
||||
return LocalTime.class;
|
||||
|
||||
@ -13,11 +13,7 @@
|
||||
*/
|
||||
package com.mysema.query.sql.types;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Time;
|
||||
import java.sql.Types;
|
||||
import java.sql.*;
|
||||
|
||||
/**
|
||||
* TimeType maps Time to Time on the JDBC level
|
||||
@ -25,7 +21,7 @@ import java.sql.Types;
|
||||
* @author tiwe
|
||||
*
|
||||
*/
|
||||
public class TimeType extends AbstractType<Time> {
|
||||
public class TimeType extends AbstractDateTimeType<Time> {
|
||||
|
||||
public TimeType() {
|
||||
super(Types.TIME);
|
||||
@ -35,6 +31,11 @@ public class TimeType extends AbstractType<Time> {
|
||||
super(type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLiteral(Time value) {
|
||||
return timeFormatter.print(value.getTime());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Time getValue(ResultSet rs, int startIndex) throws SQLException {
|
||||
return rs.getTime(startIndex);
|
||||
|
||||
@ -13,11 +13,7 @@
|
||||
*/
|
||||
package com.mysema.query.sql.types;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Timestamp;
|
||||
import java.sql.Types;
|
||||
import java.sql.*;
|
||||
|
||||
/**
|
||||
* TimestampType maps Timestamp to Timestamp on the JDBC level
|
||||
@ -25,7 +21,7 @@ import java.sql.Types;
|
||||
* @author tiwe
|
||||
*
|
||||
*/
|
||||
public class TimestampType extends AbstractType<Timestamp> {
|
||||
public class TimestampType extends AbstractDateTimeType<Timestamp> {
|
||||
|
||||
public TimestampType() {
|
||||
super(Types.TIMESTAMP);
|
||||
@ -35,6 +31,11 @@ public class TimestampType extends AbstractType<Timestamp> {
|
||||
super(type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLiteral(Timestamp value) {
|
||||
return dateTimeFormatter.print(value.getTime());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Timestamp getValue(ResultSet rs, int startIndex) throws SQLException {
|
||||
return rs.getTimestamp(startIndex);
|
||||
|
||||
@ -13,12 +13,11 @@
|
||||
*/
|
||||
package com.mysema.query.sql.types;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
/**
|
||||
* Defines the de/serialization of a typed Java object from a ResultSet or to a PreparedStatement
|
||||
*
|
||||
@ -46,6 +45,14 @@ public interface Type<T> {
|
||||
*/
|
||||
Class<T> getReturnedClass();
|
||||
|
||||
/**
|
||||
* Get the literal representation
|
||||
*
|
||||
* @param value
|
||||
* @return
|
||||
*/
|
||||
String getLiteral(T value);
|
||||
|
||||
/**
|
||||
* Get the object from the result set
|
||||
*
|
||||
|
||||
@ -25,7 +25,7 @@ import java.util.Date;
|
||||
* @author tiwe
|
||||
*
|
||||
*/
|
||||
public class UtilDateType extends AbstractType<Date> {
|
||||
public class UtilDateType extends AbstractDateTimeType<Date> {
|
||||
|
||||
public UtilDateType() {
|
||||
super(Types.TIMESTAMP);
|
||||
@ -35,6 +35,11 @@ public class UtilDateType extends AbstractType<Date> {
|
||||
super(type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLiteral(Date value) {
|
||||
return dateTimeFormatter.print(value.getTime());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Date getValue(ResultSet rs, int startIndex) throws SQLException {
|
||||
return rs.getTimestamp(startIndex);
|
||||
|
||||
@ -320,7 +320,7 @@ public class SelectBase extends AbstractBaseTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@IncludeIn({H2, SQLSERVER, MYSQL, ORACLE, SQLITE, TERADATA}) // TODO fix postgres
|
||||
@IncludeIn({H2, SQLSERVER, MYSQL, ORACLE, TERADATA}) // TODO fix postgres
|
||||
public void Dates() {
|
||||
long ts = ((long)Math.floor(System.currentTimeMillis() / 1000)) * 1000;
|
||||
long tsDate = new org.joda.time.LocalDate(ts).toDateMidnight().getMillis();
|
||||
|
||||
@ -13,27 +13,22 @@
|
||||
*/
|
||||
package com.mysema.query.sql;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import com.mysema.query.support.Expressions;
|
||||
import com.mysema.query.types.*;
|
||||
import com.mysema.query.types.path.NumberPath;
|
||||
import com.mysema.query.types.template.SimpleTemplate;
|
||||
import org.joda.time.DateTime;
|
||||
import org.joda.time.LocalDate;
|
||||
import org.joda.time.LocalTime;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.sql.Date;
|
||||
import java.sql.Time;
|
||||
import java.sql.Timestamp;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import com.mysema.query.support.Expressions;
|
||||
import com.mysema.query.types.path.NumberPath;
|
||||
import org.joda.time.DateTime;
|
||||
import org.joda.time.LocalDate;
|
||||
import org.joda.time.LocalTime;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.mysema.query.types.ConstantImpl;
|
||||
import com.mysema.query.types.Operation;
|
||||
import com.mysema.query.types.OperationImpl;
|
||||
import com.mysema.query.types.Template;
|
||||
import com.mysema.query.types.TemplateFactory;
|
||||
import com.mysema.query.types.template.SimpleTemplate;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class SQLTemplatesTest {
|
||||
|
||||
@ -58,17 +53,19 @@ public class SQLTemplatesTest {
|
||||
@Test
|
||||
public void AsLiteral() {
|
||||
SQLTemplates templates = SQLTemplates.DEFAULT;
|
||||
assertMatches(DATE, templates.asLiteral(new Date(0)));
|
||||
assertMatches(TIME, templates.asLiteral(new Time(0)));
|
||||
assertMatches(DATETIME, templates.asLiteral(new Timestamp(0)));
|
||||
Configuration conf = new Configuration(templates);
|
||||
assertMatches(DATE, conf.asLiteral(new Date(0)));
|
||||
assertMatches(TIME, conf.asLiteral(new Time(0)));
|
||||
assertMatches(DATETIME, conf.asLiteral(new Timestamp(0)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void AsLiteral_JodaTime() {
|
||||
SQLTemplates templates = SQLTemplates.DEFAULT;
|
||||
assertMatches(DATE, templates.asLiteral(new LocalDate(0)));
|
||||
assertMatches(TIME, templates.asLiteral(new LocalTime(0)));
|
||||
assertMatches(DATETIME, templates.asLiteral(new DateTime(0)));
|
||||
Configuration conf = new Configuration(templates);
|
||||
assertMatches(DATE, conf.asLiteral(new LocalDate(0)));
|
||||
assertMatches(TIME, conf.asLiteral(new LocalTime(0)));
|
||||
assertMatches(DATETIME, conf.asLiteral(new DateTime(0)));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Loading…
Reference in New Issue
Block a user