Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* Copyright (c) 2009-2014, Data Geekery GmbH (http://www.datageekery.com)
* All rights reserved.
*
* This work is dual-licensed
* - under the Apache Software License 2.0 (the "ASL")
* - under the jOOQ License and Maintenance Agreement (the "jOOQ License")
* =============================================================================
* You may choose which license applies to you:
*
* - If you're using this work with Open Source databases, you may choose
* either ASL or jOOQ License.
* - If you're using this work with at least one commercial database, you must
* choose jOOQ License
*
* For more information, please visit http://www.jooq.org/licenses
*
* Apache Software License 2.0:
* -----------------------------------------------------------------------------
* 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.
*
* jOOQ License and Maintenance Agreement:
* -----------------------------------------------------------------------------
* Data Geekery grants the Customer the non-exclusive, timely limited and
* non-transferable license to install and use the Software under the terms of
* the jOOQ License and Maintenance Agreement.
*
* This library is distributed with a LIMITED WARRANTY. See the jOOQ License
* and Maintenance Agreement for more details: http://www.jooq.org/licensing
*/
package org.jooq.impl;
import static org.jooq.Clause.FIELD;
import static org.jooq.Comparator.EQUALS;
import static org.jooq.Comparator.GREATER;
import static org.jooq.Comparator.GREATER_OR_EQUAL;
import static org.jooq.Comparator.IN;
import static org.jooq.Comparator.IS_DISTINCT_FROM;
import static org.jooq.Comparator.IS_NOT_DISTINCT_FROM;
import static org.jooq.Comparator.LESS;
import static org.jooq.Comparator.LESS_OR_EQUAL;
import static org.jooq.Comparator.LIKE;
import static org.jooq.Comparator.LIKE_IGNORE_CASE;
import static org.jooq.Comparator.NOT_EQUALS;
import static org.jooq.Comparator.NOT_IN;
import static org.jooq.Comparator.NOT_LIKE;
import static org.jooq.Comparator.NOT_LIKE_IGNORE_CASE;
import static org.jooq.impl.DSL.inline;
import static org.jooq.impl.DSL.nullSafe;
import static org.jooq.impl.DSL.val;
import static org.jooq.impl.ExpressionOperator.ADD;
import static org.jooq.impl.ExpressionOperator.DIVIDE;
import static org.jooq.impl.ExpressionOperator.MULTIPLY;
import static org.jooq.impl.ExpressionOperator.SUBTRACT;
import static org.jooq.tools.Convert.FALSE_VALUES;
import static org.jooq.tools.Convert.TRUE_VALUES;
import static org.jooq.tools.StringUtils.defaultString;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.jooq.BetweenAndStep;
import org.jooq.Binding;
import org.jooq.CaseValueStep;
import org.jooq.CaseWhenStep;
import org.jooq.Clause;
import org.jooq.Comparator;
import org.jooq.Condition;
import org.jooq.Configuration;
import org.jooq.Context;
import org.jooq.Converter;
import org.jooq.DataType;
import org.jooq.DatePart;
import org.jooq.Field;
import org.jooq.QuantifiedSelect;
import org.jooq.Record1;
import org.jooq.Result;
import org.jooq.Select;
import org.jooq.SortField;
import org.jooq.SortOrder;
import org.jooq.WindowIgnoreNullsStep;
import org.jooq.WindowPartitionByStep;
import org.jooq.tools.StringUtils;
/**
* @author Lukas Eder
*/
abstract class AbstractField extends AbstractQueryPart implements Field {
/**
* Generated UID
*/
private static final long serialVersionUID = 2884811923648354905L;
private static final Clause[] CLAUSES = { FIELD };
private final String name;
private final String comment;
private final DataType dataType;
private final Binding, T> binding;
AbstractField(String name, DataType type) {
this(name, type, null, null);
}
AbstractField(String name, DataType type, String comment, Binding, T> binding) {
super();
this.name = name;
this.comment = defaultString(comment);
this.dataType = type;
this.binding =
binding != null
? binding
: type instanceof ConvertedDataType
? ((ConvertedDataType, T>) type).binding()
: new DefaultBinding(new IdentityConverter(type.getType()), type.isLob());
}
// ------------------------------------------------------------------------
// XXX: API (not implemented)
// ------------------------------------------------------------------------
@Override
public abstract void accept(Context> ctx);
@Override
public Clause[] clauses(Context> ctx) {
return CLAUSES;
}
// ------------------------------------------------------------------------
// XXX: API
// ------------------------------------------------------------------------
@Override
public Field as(String alias) {
return new FieldAlias(this, alias);
}
@Override
public final Field as(Field> otherField) {
return as(otherField.getName());
}
@Override
public final String getName() {
return name;
}
@Override
public final String getComment() {
return comment;
}
@Override
public final Converter, T> getConverter() {
return binding.converter();
}
@Override
public final Binding, T> getBinding() {
return binding;
}
@Override
public final DataType getDataType() {
return dataType;
}
@Override
public final DataType getDataType(Configuration configuration) {
return dataType.getDataType(configuration);
}
@Override
public final Class getType() {
return dataType.getType();
}
// ------------------------------------------------------------------------
// XXX: Type casts
// ------------------------------------------------------------------------
@Override
public final Field cast(Field field) {
return cast(field.getDataType());
}
@SuppressWarnings("unchecked")
@Override
public final Field cast(DataType type) {
// [#473] Prevent unnecessary casts
if (getDataType().equals(type)) {
return (Field) this;
}
else {
return new Cast(this, type);
}
}
@SuppressWarnings("unchecked")
@Override
public final Field cast(Class type) {
// [#2597] Prevent unnecessary casts
if (getType() == type) {
return (Field) this;
}
else {
return cast(DefaultDataType.getDataType(null, type));
}
}
// ------------------------------------------------------------------------
// XXX: Type coercions
// ------------------------------------------------------------------------
@Override
public final Field coerce(Field field) {
return coerce(field.getDataType());
}
@SuppressWarnings("unchecked")
@Override
public final Field coerce(DataType type) {
// [#473] Prevent unnecessary coercions
if (getDataType().equals(type)) {
return (Field) this;
}
else {
return new Coerce(this, type);
}
}
@Override
public final Field coerce(Class type) {
return coerce(DefaultDataType.getDataType(null, type));
}
// ------------------------------------------------------------------------
// XXX: Conversion of field into a sort field
// ------------------------------------------------------------------------
@Override
public final SortField asc() {
return sort(SortOrder.ASC);
}
@Override
public final SortField desc() {
return sort(SortOrder.DESC);
}
@Override
public final SortField sort(SortOrder order) {
return new SortFieldImpl(this, order);
}
@Override
public final SortField sortAsc(Collection sortList) {
Map map = new LinkedHashMap();
int i = 0;
for (T value : sortList) {
map.put(value, i++);
}
return sort(map);
}
@Override
public final SortField sortAsc(T... sortList) {
return sortAsc(Arrays.asList(sortList));
}
@Override
public final SortField sortDesc(Collection sortList) {
Map map = new LinkedHashMap();
int i = 0;
for (T value : sortList) {
map.put(value, i--);
}
return sort(map);
}
@Override
public final SortField sortDesc(T... sortList) {
return sortDesc(Arrays.asList(sortList));
}
@Override
public final SortField sort(Map sortMap) {
CaseValueStep decode = DSL.decode().value(this);
CaseWhenStep result = null;
for (Entry entry : sortMap.entrySet()) {
if (result == null) {
result = decode.when(entry.getKey(), inline(entry.getValue()));
}
else {
result.when(entry.getKey(), inline(entry.getValue()));
}
}
if (result == null) {
return null;
}
else {
return result.asc();
}
}
// ------------------------------------------------------------------------
// XXX: Arithmetic operations
// ------------------------------------------------------------------------
@Override
public final Field neg() {
return new Neg(this, ExpressionOperator.SUBTRACT);
}
@Override
public final Field add(Number value) {
return add(Utils.field(value));
}
/*
* This default implementation is known to be overridden by
* Expression to generate neater expressions
*/
@Override
public Field add(Field> value) {
return new Expression(ADD, this, nullSafe(value));
}
@Override
public final Field sub(Number value) {
return sub(Utils.field(value));
}
@Override
public final Field sub(Field> value) {
return new Expression(SUBTRACT, this, nullSafe(value));
}
@Override
public final Field mul(Number value) {
return mul(Utils.field(value));
}
/**
* This default implementation is known to be overridden by
* Expression to generate neater expressions
*/
@Override
public Field mul(Field extends Number> value) {
return new Expression(MULTIPLY, this, nullSafe(value));
}
@Override
public final Field div(Number value) {
return div(Utils.field(value));
}
@Override
public final Field div(Field extends Number> value) {
return new Expression(DIVIDE, this, nullSafe(value));
}
@Override
public final Field mod(Number value) {
return mod(Utils.field(value));
}
@Override
public final Field mod(Field extends Number> value) {
return new Mod(this, nullSafe(value));
}
// ------------------------------------------------------------------------
// XXX: Arithmetic operation aliases
// ------------------------------------------------------------------------
@Override
public final Field plus(Number value) {
return add(value);
}
@Override
public final Field plus(Field> value) {
return add(value);
}
@Override
public final Field subtract(Number value) {
return sub(value);
}
@Override
public final Field subtract(Field> value) {
return sub(value);
}
@Override
public final Field minus(Number value) {
return sub(value);
}
@Override
public final Field minus(Field> value) {
return sub(value);
}
@Override
public final Field multiply(Number value) {
return mul(value);
}
@Override
public final Field multiply(Field extends Number> value) {
return mul(value);
}
@Override
public final Field divide(Number value) {
return div(value);
}
@Override
public final Field divide(Field extends Number> value) {
return div(value);
}
@Override
public final Field modulo(Number value) {
return mod(value);
}
@Override
public final Field modulo(Field extends Number> value) {
return mod(value);
}
// ------------------------------------------------------------------------
// XXX: Bitwise operations
// ------------------------------------------------------------------------
// Unsafe casting is needed here, as bitwise operations only work on
// numeric values...
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public final Field bitNot() {
return DSL.bitNot((Field) this);
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public final Field bitAnd(T value) {
return DSL.bitAnd((Field) this, (Field) val(value, this));
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public final Field bitAnd(Field value) {
return DSL.bitAnd((Field) this, (Field) value);
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public final Field bitNand(T value) {
return DSL.bitNand((Field) this, (Field) val(value, this));
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public final Field bitNand(Field value) {
return DSL.bitNand((Field) this, (Field) value);
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public final Field bitOr(T value) {
return DSL.bitOr((Field) this, (Field) val(value, this));
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public final Field bitOr(Field value) {
return DSL.bitOr((Field) this, (Field) value);
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public final Field bitNor(T value) {
return DSL.bitNor((Field) this, (Field) val(value, this));
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public final Field bitNor(Field value) {
return DSL.bitNor((Field) this, (Field) value);
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public final Field bitXor(T value) {
return DSL.bitXor((Field) this, (Field) val(value, this));
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public final Field bitXor(Field value) {
return DSL.bitXor((Field) this, (Field) value);
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public final Field bitXNor(T value) {
return DSL.bitXNor((Field) this, (Field) val(value, this));
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public final Field bitXNor(Field value) {
return DSL.bitXNor((Field) this, (Field) value);
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public final Field shl(T value) {
return DSL.shl((Field) this, (Field) val(value, this));
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public final Field shl(Field value) {
return DSL.shl((Field) this, (Field) value);
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public final Field shr(T value) {
return DSL.shr((Field) this, (Field) val(value, this));
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public final Field shr(Field value) {
return DSL.shr((Field) this, (Field) value);
}
// ------------------------------------------------------------------------
// XXX: Conditions created from this field
// ------------------------------------------------------------------------
@Override
public final Condition isNull() {
return new IsNull(this, true);
}
@Override
public final Condition isNotNull() {
return new IsNull(this, false);
}
@Override
public final Condition isDistinctFrom(T value) {
return isDistinctFrom(Utils.field(value, this));
}
@Override
public final Condition isDistinctFrom(Field field) {
return compare(IS_DISTINCT_FROM, field);
}
@Override
public final Condition isNotDistinctFrom(T value) {
return isNotDistinctFrom(Utils.field(value, this));
}
@Override
public final Condition isNotDistinctFrom(Field field) {
return compare(IS_NOT_DISTINCT_FROM, field);
}
@SuppressWarnings({ "unchecked" })
@Override
public final Condition isTrue() {
Class> type = getType();
if (type == String.class) {
return ((Field) this).in(Utils.inline(TRUE_VALUES.toArray(new String[TRUE_VALUES.size()])));
}
else if (Number.class.isAssignableFrom(type)) {
return ((Field) this).equal(inline((Number) getDataType().convert(1)));
}
else if (Boolean.class.isAssignableFrom(type)) {
return ((Field) this).equal(inline(true));
}
else {
return cast(String.class).in(TRUE_VALUES);
}
}
@SuppressWarnings({ "unchecked" })
@Override
public final Condition isFalse() {
Class> type = getType();
if (type == String.class) {
return ((Field) this).in(Utils.inline(FALSE_VALUES.toArray(new String[FALSE_VALUES.size()])));
}
else if (Number.class.isAssignableFrom(type)) {
return ((Field) this).equal(inline((Number) getDataType().convert(0)));
}
else if (Boolean.class.isAssignableFrom(type)) {
return ((Field) this).equal(inline(false));
}
else {
return cast(String.class).in(Utils.inline(FALSE_VALUES.toArray(new String[FALSE_VALUES.size()])));
}
}
@Override
public final Condition like(String value) {
return like(Utils.field(value, String.class));
}
@Override
public final Condition like(String value, char escape) {
return like(Utils.field(value, String.class), escape);
}
@Override
public final Condition like(Field field) {
return new CompareCondition(this, nullSafe(field), LIKE);
}
@Override
public final Condition like(Field field, char escape) {
return new CompareCondition(this, nullSafe(field), LIKE, escape);
}
@Override
public final Condition likeIgnoreCase(String value) {
return likeIgnoreCase(Utils.field(value, String.class));
}
@Override
public final Condition likeIgnoreCase(String value, char escape) {
return likeIgnoreCase(Utils.field(value, String.class), escape);
}
@Override
public final Condition likeIgnoreCase(Field field) {
return new CompareCondition(this, nullSafe(field), LIKE_IGNORE_CASE);
}
@Override
public final Condition likeIgnoreCase(Field field, char escape) {
return new CompareCondition(this, nullSafe(field), LIKE_IGNORE_CASE, escape);
}
@Override
public final Condition likeRegex(String pattern) {
return likeRegex(Utils.field(pattern, String.class));
}
@Override
public final Condition likeRegex(Field pattern) {
return new RegexpLike(this, nullSafe(pattern));
}
@Override
public final Condition notLike(String value) {
return notLike(Utils.field(value, String.class));
}
@Override
public final Condition notLike(String value, char escape) {
return notLike(Utils.field(value, String.class), escape);
}
@Override
public final Condition notLike(Field field) {
return new CompareCondition(this, nullSafe(field), NOT_LIKE);
}
@Override
public final Condition notLike(Field field, char escape) {
return new CompareCondition(this, nullSafe(field), NOT_LIKE, escape);
}
@Override
public final Condition notLikeIgnoreCase(String value) {
return notLikeIgnoreCase(Utils.field(value, String.class));
}
@Override
public final Condition notLikeIgnoreCase(String value, char escape) {
return notLikeIgnoreCase(Utils.field(value, String.class), escape);
}
@Override
public final Condition notLikeIgnoreCase(Field field) {
return new CompareCondition(this, nullSafe(field), NOT_LIKE_IGNORE_CASE);
}
@Override
public final Condition notLikeIgnoreCase(Field field, char escape) {
return new CompareCondition(this, nullSafe(field), NOT_LIKE_IGNORE_CASE, escape);
}
@Override
public final Condition notLikeRegex(String pattern) {
return likeRegex(pattern).not();
}
@Override
public final Condition notLikeRegex(Field pattern) {
return likeRegex(pattern).not();
}
@Override
public final Condition contains(T value) {
return new Contains(this, value);
}
@Override
public final Condition contains(Field value) {
return new Contains(this, value);
}
@Override
public final Condition startsWith(T value) {
Field concat = DSL.concat(Utils.escapeForLike(value), inline("%"));
return like(concat, Utils.ESCAPE);
}
@Override
public final Condition startsWith(Field value) {
Field concat = DSL.concat(Utils.escapeForLike(value), inline("%"));
return like(concat, Utils.ESCAPE);
}
@Override
public final Condition endsWith(T value) {
Field concat = DSL.concat(inline("%"), Utils.escapeForLike(value));
return like(concat, Utils.ESCAPE);
}
@Override
public final Condition endsWith(Field value) {
Field concat = DSL.concat(inline("%"), Utils.escapeForLike(value));
return like(concat, Utils.ESCAPE);
}
private final boolean isAccidentalSelect(T[] values) {
return (values != null && values.length == 1 && values[0] instanceof Select);
}
private final boolean isAccidentalCollection(T[] values) {
return (values != null && values.length == 1 && values[0] instanceof Collection);
}
@SuppressWarnings("unchecked")
@Override
public final Condition in(T... values) {
// [#3362] Prevent "rogue" API usage when using Field