[#1432] Removed redundant SQLClause, merged
functionality into SQLField
This commit is contained in:
parent
6e3e800b68
commit
dca8c44162
@ -813,7 +813,7 @@ public class Factory implements FactoryOperations {
|
||||
* @return A field wrapping the plain SQL
|
||||
*/
|
||||
public static Field<Object> field(String sql, QueryPart... parts) {
|
||||
return new SQLClause<Object>(sql, SQLDataType.OTHER, parts);
|
||||
return new SQLField<Object>(sql, SQLDataType.OTHER, parts);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -847,7 +847,7 @@ public class Factory implements FactoryOperations {
|
||||
* @return A field wrapping the plain SQL
|
||||
*/
|
||||
public static <T> Field<T> field(String sql, Class<T> type, QueryPart... parts) {
|
||||
return new SQLClause<T>(sql, getDataType(type), parts);
|
||||
return new SQLField<T>(sql, getDataType(type), parts);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -881,7 +881,7 @@ public class Factory implements FactoryOperations {
|
||||
* @return A field wrapping the plain SQL
|
||||
*/
|
||||
public static <T> Field<T> field(String sql, DataType<T> type, QueryPart... parts) {
|
||||
return new SQLClause<T>(sql, type, parts);
|
||||
return new SQLField<T>(sql, type, parts);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -36,6 +36,7 @@
|
||||
|
||||
package org.jooq.impl;
|
||||
|
||||
import static org.jooq.impl.Factory.field;
|
||||
import static org.jooq.impl.Factory.function;
|
||||
|
||||
import org.jooq.Configuration;
|
||||
@ -77,7 +78,7 @@ class Position extends AbstractFunction<Integer> {
|
||||
return function("charindex", SQLDataType.INTEGER, search, in);
|
||||
|
||||
default:
|
||||
return new SQLClause<Integer>("{position}({0} {in} {1})", SQLDataType.INTEGER, search, in);
|
||||
return field("{position}({0} {in} {1})", SQLDataType.INTEGER, search, in);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,114 +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 java.util.List;
|
||||
|
||||
import org.jooq.Attachable;
|
||||
import org.jooq.BindContext;
|
||||
import org.jooq.DataType;
|
||||
import org.jooq.QueryPart;
|
||||
import org.jooq.RenderContext;
|
||||
import org.jooq.exception.DataAccessException;
|
||||
import org.jooq.tools.StringUtils;
|
||||
|
||||
/**
|
||||
* @author Lukas Eder
|
||||
*/
|
||||
class SQLClause<T> extends AbstractField<T> {
|
||||
|
||||
/**
|
||||
* Generated UID
|
||||
*/
|
||||
private static final long serialVersionUID = 3132876322089761785L;
|
||||
|
||||
private final String sql;
|
||||
private final QueryPart[] parts;
|
||||
|
||||
SQLClause(String sql, DataType<T> type, QueryPart... parts) {
|
||||
super(sql, type);
|
||||
|
||||
this.sql = sql;
|
||||
this.parts = parts;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
// Field API
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
@Override
|
||||
public final List<Attachable> getAttachables() {
|
||||
return getAttachables(parts);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void toSQL(RenderContext context) {
|
||||
// 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) {
|
||||
if (sqlPart.matches("\\{\\d+\\}")) {
|
||||
if (i == parts.length) {
|
||||
throw new DataAccessException("The number of placeholders must match the number of query parts : " + sql);
|
||||
}
|
||||
|
||||
context.sql(parts[i++]);
|
||||
}
|
||||
else if (sqlPart.matches("\\{[\\w\\s]+\\}")) {
|
||||
context.keyword(sqlPart.substring(1, sqlPart.length() - 1));
|
||||
}
|
||||
else {
|
||||
context.sql(sqlPart);
|
||||
}
|
||||
}
|
||||
|
||||
if (i != parts.length) {
|
||||
throw new DataAccessException("The number of placeholders must match the number of query parts : " + sql);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void bind(BindContext context) {
|
||||
context.bind(parts);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final boolean isNullLiteral() {
|
||||
return "null".equalsIgnoreCase(sql);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user