org.jooq.impl.AbstractField Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of payment-retries-plugin Show documentation
Show all versions of payment-retries-plugin Show documentation
Kill Bill Payment Retries plugin
The newest version!
/*
* Copyright (c) 2009-2016, Data Geekery GmbH (http://www.datageekery.com)
* All rights reserved.
*
* 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.
*
* Other licenses:
* -----------------------------------------------------------------------------
* Commercial licenses for this work are available. These replace the above
* ASL 2.0 and offer limited warranties, support, maintenance, and commercial
* database integrations.
*
* For more information, please visit: http://www.jooq.org/licenses
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
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.impl.Tools.EMPTY_FIELD;
import static org.jooq.impl.Tools.EMPTY_STRING;
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 java.util.function.Function;
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.LikeEscapeStep;
import org.jooq.QuantifiedSelect;
import org.jooq.Record;
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;
AbstractField(String name, DataType type) {
this(name, type, null, type.getBinding());
}
@SuppressWarnings("unchecked")
AbstractField(String name, DataType type, String comment, Binding, T> binding) {
super();
this.name = name;
this.comment = defaultString(comment);
this.dataType = type.asConvertedDataType((Binding) binding);
}
// ------------------------------------------------------------------------
// XXX: API (not implemented)
// ------------------------------------------------------------------------
@Override
public abstract void accept(Context> ctx);
@Override
public Clause[] clauses(Context> ctx) {
return CLAUSES;
}
// ------------------------------------------------------------------------
// [#5518] Record method inversions, e.g. for use as method references
// ------------------------------------------------------------------------
@Override
public final Field field(Record record) {
return record.field(this);
}
@Override
public final T get(Record record) {
return record.get(this);
}
@Override
public final T getValue(Record record) {
return record.getValue(this);
}
@Override
public final T original(Record record) {
return record.original(this);
}
@Override
public final boolean changed(Record record) {
return record.changed(this);
}
@Override
public final void reset(Record record) {
record.reset(this);
}
@Override
public final Record1 from(Record record) {
return record.into(this);
}
// ------------------------------------------------------------------------
// 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 Field as(Function super Field, ? extends String> aliasFunction) {
return as(aliasFunction.apply(this));
}
@Override
public final String getName() {
return name;
}
@Override
public final String getComment() {
return comment;
}
@Override
public final Converter, T> getConverter() {
return getBinding().converter();
}
@Override
public final Binding, T> getBinding() {
return dataType.getBinding();
}
@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
@SafeVarargs
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
@SafeVarargs
public final SortField sortDesc(T... sortList) {
return sortDesc(Arrays.asList(sortList));
}
@Override
public final SortField sort(Map sortMap) {
CaseValueStep decode = DSL.choose(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(Tools.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, getDataType()));
}
@Override
public final Field sub(Number value) {
return sub(Tools.field(value));
}
@Override
public final Field sub(Field> value) {
return new Expression(SUBTRACT, this, nullSafe(value, getDataType()));
}
@Override
public final Field mul(Number value) {
return mul(Tools.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, getDataType()));
}
@Override
public final Field div(Number value) {
return div(Tools.field(value));
}
@Override
public final Field div(Field extends Number> value) {
return new Expression(DIVIDE, this, nullSafe(value, getDataType()));
}
@Override
public final Field mod(Number value) {
return mod(Tools.field(value));
}
@Override
public final Field mod(Field extends Number> value) {
return new Mod(this, nullSafe(value, getDataType()));
}
// ------------------------------------------------------------------------
// 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() {
// Workaround assignment for https://bugs.eclipse.org/bugs/show_bug.cgi?id=473657
final Field result = DSL.bitNot((Field) this);
return result;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public final Field bitAnd(T value) {
// Workaround assignment for https://bugs.eclipse.org/bugs/show_bug.cgi?id=473657
final Field result = DSL.bitAnd((Field) this, (Field) val(value, this));
return result;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public final Field bitAnd(Field value) {
// Workaround assignment for https://bugs.eclipse.org/bugs/show_bug.cgi?id=473657
final Field result = DSL.bitAnd((Field) this, (Field) value);
return result;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public final Field bitNand(T value) {
// Workaround assignment for https://bugs.eclipse.org/bugs/show_bug.cgi?id=473657
final Field result = DSL.bitNand((Field) this, (Field) val(value, this));
return result;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public final Field bitNand(Field value) {
// Workaround assignment for https://bugs.eclipse.org/bugs/show_bug.cgi?id=473657
final Field result = DSL.bitNand((Field) this, (Field) value);
return result;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public final Field bitOr(T value) {
// Workaround assignment for https://bugs.eclipse.org/bugs/show_bug.cgi?id=473657
final Field result = DSL.bitOr((Field) this, (Field) val(value, this));
return result;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public final Field bitOr(Field value) {
// Workaround assignment for https://bugs.eclipse.org/bugs/show_bug.cgi?id=473657
final Field result = DSL.bitOr((Field) this, (Field) value);
return result;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public final Field bitNor(T value) {
// Workaround assignment for https://bugs.eclipse.org/bugs/show_bug.cgi?id=473657
final Field result = DSL.bitNor((Field) this, (Field) val(value, this));
return result;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public final Field bitNor(Field value) {
// Workaround assignment for https://bugs.eclipse.org/bugs/show_bug.cgi?id=473657
final Field result = DSL.bitNor((Field) this, (Field) value);
return result;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public final Field bitXor(T value) {
// Workaround assignment for https://bugs.eclipse.org/bugs/show_bug.cgi?id=473657
final Field result = DSL.bitXor((Field) this, (Field) val(value, this));
return result;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public final Field bitXor(Field value) {
// Workaround assignment for https://bugs.eclipse.org/bugs/show_bug.cgi?id=473657
final Field result = DSL.bitXor((Field) this, (Field) value);
return result;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public final Field bitXNor(T value) {
// Workaround assignment for https://bugs.eclipse.org/bugs/show_bug.cgi?id=473657
final Field result = DSL.bitXNor((Field) this, (Field) val(value, this));
return result;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public final Field bitXNor(Field value) {
// Workaround assignment for https://bugs.eclipse.org/bugs/show_bug.cgi?id=473657
final Field result = DSL.bitXNor((Field) this, (Field) value);
return result;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public final Field shl(Number value) {
// Workaround assignment for https://bugs.eclipse.org/bugs/show_bug.cgi?id=473657
final Field result = DSL.shl((Field) this, (Field) Tools.field(value));
return result;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public final Field shl(Field extends Number> value) {
// Workaround assignment for https://bugs.eclipse.org/bugs/show_bug.cgi?id=473657
final Field result = DSL.shl((Field) this, value);
return result;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public final Field shr(Number value) {
// Workaround assignment for https://bugs.eclipse.org/bugs/show_bug.cgi?id=473657
final Field result = DSL.shr((Field) this, (Field) Tools.field(value));
return result;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public final Field shr(Field extends Number> value) {
// Workaround assignment for https://bugs.eclipse.org/bugs/show_bug.cgi?id=473657
final Field result = DSL.shr((Field) this, (Field) value);
return result;
}
// ------------------------------------------------------------------------
// 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(Tools.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(Tools.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(Tools.inline(TRUE_VALUES.toArray(EMPTY_STRING)));
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(Tools.inline(FALSE_VALUES.toArray(EMPTY_STRING)));
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(Tools.inline(FALSE_VALUES.toArray(EMPTY_STRING)));
}
@Override
public final LikeEscapeStep like(String value) {
return like(Tools.field(value, String.class));
}
@Override
public final Condition like(String value, char escape) {
return like(Tools.field(value, String.class), escape);
}
@Override
public final LikeEscapeStep like(Field field) {
return new CompareCondition(this, nullSafe(field, getDataType()), LIKE);
}
@Override
public final Condition like(Field field, char escape) {
return like(field).escape(escape);
}
@Override
public final LikeEscapeStep likeIgnoreCase(String value) {
return likeIgnoreCase(Tools.field(value, String.class));
}
@Override
public final Condition likeIgnoreCase(String value, char escape) {
return likeIgnoreCase(Tools.field(value, String.class), escape);
}
@Override
public final LikeEscapeStep likeIgnoreCase(Field field) {
return new CompareCondition(this, nullSafe(field, getDataType()), LIKE_IGNORE_CASE);
}
@Override
public final Condition likeIgnoreCase(Field field, char escape) {
return likeIgnoreCase(field).escape(escape);
}
@Override
public final Condition likeRegex(String pattern) {
return likeRegex(Tools.field(pattern, String.class));
}
@Override
public final Condition likeRegex(Field pattern) {
return new RegexpLike(this, nullSafe(pattern, getDataType()));
}
@Override
public final LikeEscapeStep notLike(String value) {
return notLike(Tools.field(value, String.class));
}
@Override
public final Condition notLike(String value, char escape) {
return notLike(Tools.field(value, String.class), escape);
}
@Override
public final LikeEscapeStep notLike(Field field) {
return new CompareCondition(this, nullSafe(field, getDataType()), NOT_LIKE);
}
@Override
public final Condition notLike(Field field, char escape) {
return notLike(field).escape(escape);
}
@Override
public final LikeEscapeStep notLikeIgnoreCase(String value) {
return notLikeIgnoreCase(Tools.field(value, String.class));
}
@Override
public final Condition notLikeIgnoreCase(String value, char escape) {
return notLikeIgnoreCase(Tools.field(value, String.class), escape);
}
@Override
public final LikeEscapeStep notLikeIgnoreCase(Field field) {
return new CompareCondition(this, nullSafe(field, getDataType()), NOT_LIKE_IGNORE_CASE);
}
@Override
public final Condition notLikeIgnoreCase(Field field, char escape) {
return notLikeIgnoreCase(field).escape(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(Tools.escapeForLike(value), inline("%"));
return like(concat, Tools.ESCAPE);
}
@Override
public final Condition startsWith(Field value) {
Field concat = DSL.concat(Tools.escapeForLike(value), inline("%"));
return like(concat, Tools.ESCAPE);
}
@Override
public final Condition endsWith(T value) {
Field concat = DSL.concat(inline("%"), Tools.escapeForLike(value));
return like(concat, Tools.ESCAPE);
}
@Override
public final Condition endsWith(Field value) {
Field concat = DSL.concat(inline("%"), Tools.escapeForLike(value));
return like(concat, Tools.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