[#1268] Add Factory.field(String, QueryPart...) to generate custom clauses - Some simplifications

This commit is contained in:
Lukas Eder 2012-04-06 17:05:10 +00:00
parent 6e6fcbcf78
commit 7d78b0a23a
12 changed files with 50 additions and 378 deletions

View File

@ -61,20 +61,21 @@ class CurrentDate extends AbstractFunction<Date> {
final Field<Date> getFunction0(Configuration configuration) {
switch (configuration.getDialect()) {
case ORACLE:
return function("sysdate", SQLDataType.DATE);
return field("sysdate", SQLDataType.DATE);
case DERBY: // No break
case HSQLDB: // No break
case INGRES: // No break
case POSTGRES: // No break
case SQLITE: // No break
return field("current_date", Date.class);
case DB2:
case DERBY:
case HSQLDB:
case INGRES:
case POSTGRES:
case SQLITE:
return field("current_date", SQLDataType.DATE);
case SQLSERVER:
return field("convert(date, current_timestamp)", Date.class);
return field("convert(date, current_timestamp)", SQLDataType.DATE);
case SYBASE:
return field("current date", Date.class);
return field("current date", SQLDataType.DATE);
}
return function("current_date", SQLDataType.DATE);

View File

@ -61,20 +61,21 @@ class CurrentTime extends AbstractFunction<Time> {
final Field<Time> getFunction0(Configuration configuration) {
switch (configuration.getDialect()) {
case ORACLE:
return function("sysdate", SQLDataType.TIME);
return field("sysdate", SQLDataType.TIME);
case DERBY: // No break
case HSQLDB: // No break
case INGRES: // No break
case POSTGRES: // No break
case SQLITE: // No break
return field("current_time", Time.class);
case DB2:
case DERBY:
case HSQLDB:
case INGRES:
case POSTGRES:
case SQLITE:
return field("current_time", SQLDataType.TIME);
case SQLSERVER:
return field("convert(time, current_timestamp)", Time.class);
return field("convert(time, current_timestamp)", SQLDataType.TIME);
case SYBASE:
return field("current time", Time.class);
return field("current time", SQLDataType.TIME);
}
return function("current_time", SQLDataType.TIME);

View File

@ -64,18 +64,19 @@ class CurrentTimestamp extends AbstractFunction<Timestamp> {
return function("current_bigdatetime", SQLDataType.TIMESTAMP);
case ORACLE:
return function("sysdate", SQLDataType.TIMESTAMP);
return field("sysdate", SQLDataType.TIMESTAMP);
case DERBY: // No break
case HSQLDB: // No break
case INGRES: // No break
case POSTGRES: // No break
case SQLITE: // No break
case DB2:
case DERBY:
case HSQLDB:
case INGRES:
case POSTGRES:
case SQLITE:
case SQLSERVER:
return field("current_timestamp", Timestamp.class);
return field("current_timestamp", SQLDataType.TIMESTAMP);
case SYBASE:
return field("current timestamp", Timestamp.class);
return field("current timestamp", SQLDataType.TIMESTAMP);
}
return function("current_timestamp", SQLDataType.TIMESTAMP);

View File

@ -59,17 +59,16 @@ class CurrentUser extends AbstractFunction<String> {
final Field<String> getFunction0(Configuration configuration) {
switch (configuration.getDialect()) {
case ASE:
case ORACLE:
return field("user", SQLDataType.VARCHAR);
case ORACLE:
return function("user", SQLDataType.VARCHAR);
case DERBY: // No break
case HSQLDB: // No break
case INGRES: // No break
case POSTGRES: // No break
case SQLSERVER: // No break
case SQLITE: // No break
case DB2:
case DERBY:
case HSQLDB:
case INGRES:
case POSTGRES:
case SQLSERVER:
case SQLITE:
case SYBASE:
return field("current_user", String.class);
}

View File

@ -1,104 +0,0 @@
/**
* Copyright (c) 2009-2012, Lukas Eder, lukas.eder@gmail.com
* All rights reserved.
*
* This software is licensed to you under the Apache License, Version 2.0
* (the "License"); You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* . Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* . Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* . Neither the name "jOOQ" nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package org.jooq.impl;
import static org.jooq.impl.Factory.field;
import static org.jooq.impl.Factory.function;
import static org.jooq.impl.Factory.literal;
import static org.jooq.impl.Factory.val;
import java.math.BigDecimal;
import org.jooq.Configuration;
import org.jooq.Field;
/**
* @author Lukas Eder
* @deprecated - This implementation is no longer needed when date time
* arithmetic is implemented completely
*/
@Deprecated
class DateAdd<T> extends AbstractFunction<T> {
/**
* Generated UID
*/
private static final long serialVersionUID = -4070594108194592245L;
private final Field<T> field;
private final Number value;
DateAdd(Field<T> field, Number value) {
super("+", field.getDataType(), field);
this.field = field;
this.value = value;
}
@Override
final Field<T> getFunction0(Configuration configuration) {
switch (configuration.getDialect()) {
case ASE:
return function("dateadd", getDataType(), literal("day"), val(value), field);
case CUBRID:
return function("adddate", getDataType(), field, val(value));
case DB2:
case HSQLDB:
return field.add(field("? day", BigDecimal.class, value));
case DERBY:
return new FnPrefixFunction<T>("timestampadd", getDataType(), field("SQL_TSI_DAY"),
val(value.intValue()), field);
case INGRES:
return field.add(field("date('" + value + " days')", BigDecimal.class));
case MYSQL:
return function("timestampadd", getDataType(), field("day"), val(value), field);
case POSTGRES:
return field.add(field("interval '" + value + " days'", BigDecimal.class));
case SQLITE:
return function("datetime", getDataType(), field, val("+" + value + " day"));
default:
return field.add(val(value));
}
}
}

View File

@ -80,8 +80,7 @@ class DateDiff extends AbstractFunction<Integer> {
function("days", getDataType(), date2));
case DERBY:
return new FnPrefixFunction<Integer>("timestampdiff",
getDataType(), field("SQL_TSI_DAY"), date2, date1);
return field("{fn {timestampdiff}({sql_tsi_day}, {0}, {1}) }", getDataType(), date2, date1);
case H2:
case HSQLDB:

View File

@ -1,104 +0,0 @@
/**
* Copyright (c) 2009-2012, Lukas Eder, lukas.eder@gmail.com
* All rights reserved.
*
* This software is licensed to you under the Apache License, Version 2.0
* (the "License"); You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* . Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* . Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* . Neither the name "jOOQ" nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package org.jooq.impl;
import static org.jooq.impl.Factory.field;
import static org.jooq.impl.Factory.function;
import static org.jooq.impl.Factory.literal;
import static org.jooq.impl.Factory.val;
import java.math.BigDecimal;
import org.jooq.Configuration;
import org.jooq.Field;
/**
* @author Lukas Eder
* @deprecated - This implementation is no longer needed when date time
* arithmetic is implemented completely
*/
@Deprecated
class DateSub<T> extends AbstractFunction<T> {
/**
* Generated UID
*/
private static final long serialVersionUID = -4070594108194592245L;
private final Field<T> field;
private final Number value;
DateSub(Field<T> field, Number value) {
super("-", field.getDataType(), field);
this.field = field;
this.value = value;
}
@Override
final Field<T> getFunction0(Configuration configuration) {
switch (configuration.getDialect()) {
case ASE:
return function("dateadd", getDataType(), literal("day"), val(-value.intValue()), field);
case CUBRID:
return function("subdate", getDataType(), field, val(value));
case DB2:
case HSQLDB:
return field.sub(field("? day", BigDecimal.class, value));
case DERBY:
return new FnPrefixFunction<T>("timestampadd", getDataType(), field("SQL_TSI_DAY"),
val(-value.intValue()), field);
case INGRES:
return field.sub(field("date('" + value + " days')", BigDecimal.class));
case MYSQL:
return function("timestampadd", getDataType(), field("day"), val(-value.intValue()), field);
case POSTGRES:
return field.sub(field("interval '" + value + " days'", BigDecimal.class));
case SQLITE:
return function("datetime", getDataType(), field, val("-" + value + " day"));
default:
return field.sub(val(value));
}
}
}

View File

@ -301,24 +301,20 @@ class Expression<T> extends AbstractFunction<T> {
YearToMonth interval = ((Param<YearToMonth>) rhs.get(0)).getValue();
if (operator == ADD) {
return new FnPrefixFunction<T>("timestampadd", getDataType(), field("SQL_TSI_MONTH"),
val(interval.intValue()), lhs);
return field("{fn {timestampadd}({sql_tsi_month}, {0}, {1}) }", getDataType(), val(interval.intValue()), lhs);
}
else {
return new FnPrefixFunction<T>("timestampadd", getDataType(), field("SQL_TSI_MONTH"),
val(-interval.intValue()), lhs);
return field("{fn {timestampadd}({sql_tsi_month}, {0}, {1}) }", getDataType(), val(-interval.intValue()), lhs);
}
}
else {
DayToSecond interval = ((Param<DayToSecond>) rhs.get(0)).getValue();
if (operator == ADD) {
return new FnPrefixFunction<T>("timestampadd", getDataType(), field("SQL_TSI_SECOND"),
val((long) interval.getTotalSeconds()), lhs);
return field("{fn {timestampadd}({sql_tsi_second}, {0}, {1}) }", getDataType(), val((long) interval.getTotalSeconds()), lhs);
}
else {
return new FnPrefixFunction<T>("timestampadd", getDataType(), field("SQL_TSI_SECOND"),
val((long) -interval.getTotalSeconds()), lhs);
return field("{fn {timestampadd}({sql_tsi_second}, {0}, {1}) }", getDataType(), val((long) -interval.getTotalSeconds()), lhs);
}
}
}
@ -393,12 +389,10 @@ class Expression<T> extends AbstractFunction<T> {
case DERBY: {
if (operator == ADD) {
return new FnPrefixFunction<T>("timestampadd", getDataType(), field("SQL_TSI_DAY"),
rhsAsNumber(), lhs);
return field("{fn {timestampadd}({sql_tsi_day}, {0}, {1}) }", getDataType(), rhsAsNumber(), lhs);
}
else {
return new FnPrefixFunction<T>("timestampadd", getDataType(), field("SQL_TSI_DAY"),
rhsAsNumber().neg(), lhs);
return field("{fn {timestampadd}({sql_tsi_day}, {0}, {1}) }", getDataType(), rhsAsNumber().neg(), lhs);
}
}

View File

@ -1,70 +0,0 @@
/**
* Copyright (c) 2009-2012, Lukas Eder, lukas.eder@gmail.com
* All rights reserved.
*
* This software is licensed to you under the Apache License, Version 2.0
* (the "License"); You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* . Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* . Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* . Neither the name "jOOQ" nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package org.jooq.impl;
import org.jooq.DataType;
import org.jooq.Field;
import org.jooq.SQLDialect;
/**
* A special type of function that has a {fn xxx()} syntax.
* <p>
* This is specifically used in the {@link SQLDialect#DERBY} dialect
*
* @author Lukas Eder
* @see <a href="http://db.apache.org/derby/docs/10.7/ref/rrefjdbc88908.html">http://db.apache.org/derby/docs/10.7/ref/rrefjdbc88908.html</a>
*/
class FnPrefixFunction<T> extends Function<T> {
/**
* Generated UID
*/
private static final long serialVersionUID = 732969685820300959L;
FnPrefixFunction(String name, DataType<T> type, Field<?>... arguments) {
super(name, type, arguments);
}
@Override
protected final String getFNPrefix() {
return "{fn ";
}
@Override
protected final String getFNSuffix() {
return "}";
}
}

View File

@ -40,7 +40,6 @@ import java.util.List;
import org.jooq.Attachable;
import org.jooq.BindContext;
import org.jooq.Configuration;
import org.jooq.DataType;
import org.jooq.QueryPart;
import org.jooq.RenderContext;
@ -77,9 +76,8 @@ class Function<T> extends AbstractField<T> {
@Override
public final void toSQL(RenderContext context) {
context.sql(getFNPrefix());
context.sql(getFNName(context.getDialect()));
context.sql(getArgumentListDelimiter(context, "("));
context.sql("(");
String separator = "";
for (QueryPart field : arguments) {
@ -89,8 +87,7 @@ class Function<T> extends AbstractField<T> {
separator = ", ";
}
context.sql(getArgumentListDelimiter(context, ")"));
context.sql(getFNSuffix());
context.sql(")");
toSQLSuffix(context);
}
@ -107,49 +104,6 @@ class Function<T> extends AbstractField<T> {
return term;
}
/**
* Subclasses may override this method to add an additional prefix in the
* function SQL string
*/
protected String getFNPrefix() {
return "";
}
/**
* Subclasses may override this method to add an additional suffix in the
* function SQL string
*/
protected String getFNSuffix() {
return "";
}
private final String getArgumentListDelimiter(Configuration configuration, String delimiter) {
switch (configuration.getDialect()) {
case ORACLE: // No break
case DB2: // No break
// Empty argument lists do not have parentheses ()
if (arguments.length == 0) {
return "";
} else {
return delimiter;
}
case SQLITE: // No break
case DERBY: // No break
case H2: // No break
case HSQLDB: // No break
case INGRES: // No break
case MYSQL: // No break
case POSTGRES: // No break
case SQLSERVER:// No break
case SYBASE:
// Default behaviour is needed for hashCode() and toString();
default:
return delimiter;
}
}
/**
* Render the argument field. This renders the field directly, by default.

View File

@ -76,7 +76,9 @@ class SQLClause<T> extends AbstractField<T> {
@Override
public final void toSQL(RenderContext context) {
String[] sqlParts = StringUtils.split("\\{[\\w\\s]+\\}", sql);
// Tokenise {keywords} and placeholders {1} {2}.
// Avoid tokenising JDBC escape syntax, e.g. {fn xx()}
String[] sqlParts = StringUtils.split("\\{(?!(fn|d|t|ts)\\b)[\\w\\s]+\\}", sql);
int i = 0;
for (String sqlPart : sqlParts) {

View File

@ -89,8 +89,7 @@ class TimestampDiff extends AbstractFunction<DayToSecond> {
function("midnight_seconds", SQLDataType.INTEGER, timestamp2)).div(literal(new DayToSecond(1).getTotalSeconds())));
case DERBY:
return (Field) new FnPrefixFunction<Integer>("timestampdiff",
SQLDataType.INTEGER, field("SQL_TSI_SECOND"), timestamp2, timestamp1).div(literal(new DayToSecond(1).getTotalSeconds()));
return (Field) field("{fn {timestampdiff}({sql_tsi_second}, {0}, {1}) }", SQLDataType.INTEGER, timestamp2, timestamp1).div(literal(new DayToSecond(1).getTotalSeconds()));
case H2:
case HSQLDB: