org.jooq.impl.Internal Maven / Gradle / Ivy
/*
* 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
*
* https://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
* Apache-2.0 and offer limited warranties, support, maintenance, and commercial
* database integrations.
*
* For more information, please visit: https://www.jooq.org/legal/licensing
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package org.jooq.impl;
import static org.jooq.impl.Tools.CONFIG;
import static org.jooq.impl.Tools.CTX;
import static org.jooq.impl.Tools.configuration;
import static org.jooq.impl.Tools.nullSafe;
import static org.jooq.tools.StringUtils.isBlank;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Supplier;
import org.jooq.Binding;
import org.jooq.Check;
import org.jooq.Comment;
import org.jooq.Converter;
import org.jooq.ConverterContext;
import org.jooq.DDLExportConfiguration;
import org.jooq.DataType;
import org.jooq.Domain;
import org.jooq.EmbeddableRecord;
import org.jooq.Field;
import org.jooq.ForeignKey;
import org.jooq.Identity;
import org.jooq.Index;
import org.jooq.Name;
import org.jooq.OrderField;
import org.jooq.ParamMode;
import org.jooq.Parameter;
// ...
import org.jooq.Queries;
import org.jooq.Query;
import org.jooq.QueryPart;
import org.jooq.Record;
// ...
// ...
import org.jooq.Result;
import org.jooq.Row;
import org.jooq.Schema;
import org.jooq.Sequence;
import org.jooq.Statement;
import org.jooq.Support;
import org.jooq.Table;
import org.jooq.TableElement;
import org.jooq.TableField;
import org.jooq.UDT;
import org.jooq.UDTRecord;
import org.jooq.UniqueKey;
import org.jooq.exception.DataAccessException;
import org.jooq.exception.DataTypeException;
import org.jooq.impl.QOM.CreateTable;
import org.jooq.tools.reflect.Reflect;
import org.jooq.tools.reflect.ReflectException;
// ...
// ...
import org.jetbrains.annotations.NotNull;
import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;
/**
* A utility class that grants access to internal API, to be used only by
* generated code.
*
* This type is for JOOQ INTERNAL USE only. Do not reference directly.
*
* @author Lukas Eder
*/
@org.jooq.Internal
public final class Internal {
/**
* Factory method for embeddable types.
*/
@SafeVarargs
@NotNull
public static final > TableField createEmbeddable(Name name, Class recordType, Table table, TableField... fields) {
return createEmbeddable(name, recordType, false, table, fields);
}
/**
* Factory method for embeddable types.
*/
@SafeVarargs
@NotNull
public static final > TableField createEmbeddable(Name name, Class recordType, boolean replacesFields, Table table, TableField... fields) {
return new EmbeddableTableField<>(name, recordType, replacesFields, table, fields);
}
/**
* Factory method for indexes.
*/
@NotNull
public static final Index createIndex(Name name, Table> table, OrderField>[] sortFields, boolean unique) {
return new IndexImpl(name, table, sortFields, null, unique);
}
/**
* Factory method for identities.
*/
@NotNull
public static final Identity createIdentity(Table table, TableField field) {
return new IdentityImpl<>(table, field);
}
/**
* Factory method for unique keys.
*/
@NotNull
@SafeVarargs
public static final UniqueKey createUniqueKey(Table table, TableField... fields) {
return createUniqueKey(table, (Name) null, fields, true);
}
/**
* Factory method for unique keys.
*/
@NotNull
@SafeVarargs
public static final UniqueKey createUniqueKey(Table table, Name name, TableField... fields) {
return createUniqueKey(table, name, fields, true);
}
/**
* Factory method for unique keys.
*/
@NotNull
public static final UniqueKey createUniqueKey(Table table, Name name, TableField[] fields, boolean enforced) {
return new UniqueKeyImpl<>(table, name, fields, enforced);
}
/**
* Factory method for unique keys.
*/
@NotNull
public static final > UniqueKey createUniqueKey(Table table, Name name, TableField embeddableField, boolean enforced) {
return createUniqueKey(table, name, fields(embeddableField), enforced);
}
/**
* Factory method for foreign keys.
*
* @deprecated - 3.14.0 - [#9404] - Please re-generate your code.
*/
@Deprecated
@NotNull
@SafeVarargs
public static final ForeignKey createForeignKey(UniqueKey key, Table table, TableField... fields) {
return createForeignKey(table, (Name) null, fields, key, key.getFieldsArray(), true);
}
/**
* Factory method for foreign keys.
*/
@NotNull
public static final ForeignKey createForeignKey(Table table, Name name, TableField[] fkFields, UniqueKey uk, TableField[] ukFields, boolean enforced) {
ForeignKey result = new ReferenceImpl<>(table, name, fkFields, uk, ukFields == null ? uk.getFieldsArray() : ukFields, enforced);
if (uk instanceof UniqueKeyImpl u)
u.references.add(result);
return result;
}
/**
* Factory method for foreign keys.
*/
@NotNull
public static final > ForeignKey createForeignKey(Table table, Name name, TableField fkEmbeddableField, UniqueKey uk, TableField ukEmbeddableField, boolean enforced) {
return createForeignKey(table, name, fields(fkEmbeddableField), uk, fields(ukEmbeddableField), enforced);
}
/**
* Factory method for sequences.
*/
@NotNull
public static final Sequence createSequence(String name, Schema schema, DataType type) {
return new SequenceImpl<>(name, schema, type, false);
}
/**
* Factory method for sequences.
*/
@NotNull
public static final Sequence createSequence(String name, Schema schema, DataType type, Number startWith, Number incrementBy, Number minvalue, Number maxvalue, boolean cycle, Number cache) {
return new SequenceImpl<>(
DSL.name(name),
schema,
type,
false,
startWith != null ? Tools.field(startWith, type) : null,
incrementBy != null ? Tools.field(incrementBy, type) : null,
minvalue != null ? Tools.field(minvalue, type) : null,
maxvalue != null ? Tools.field(maxvalue, type) : null,
cycle,
cache != null ? Tools.field(cache, type) : null
);
}
/**
* Factory method for check constraints.
*/
@NotNull
public static final Check createCheck(Table table, Name name, String condition) {
return createCheck(table, name, condition, true);
}
/**
* Factory method for check constraints.
*/
@NotNull
public static final Check createCheck(Table table, Name name, String condition, boolean enforced) {
return new CheckImpl<>(table, name, DSL.condition(condition), enforced);
}
/**
* Factory method for domain specifications.
*/
@NotNull
public static final Domain createDomain(Schema schema, Name name, DataType type, Check>... checks) {
return createDomain(schema, name, type, null, null, checks);
}
/**
* Factory method for domain specifications.
*/
@NotNull
public static final Domain createDomain(Schema schema, Name name, DataType type, Converter converter, Check>... checks) {
return createDomain(schema, name, type, converter, null, checks);
}
/**
* Factory method for domain specifications.
*/
@NotNull
public static final Domain createDomain(Schema schema, Name name, DataType type, Binding binding, Check>... checks) {
return createDomain(schema, name, type, null, binding, checks);
}
/**
* Factory method for domain specifications.
*/
@NotNull
public static final Domain createDomain(Schema schema, Name name, DataType type, Converter converter, Binding binding, Check>... checks) {
Binding actualBinding = DefaultBinding.newBinding(converter, type, binding);
DataType actualType =
converter == null && binding == null
? (DataType) type
: type.asConvertedDataType(actualBinding);
return new DomainImpl<>(schema, name, actualType, checks);
}
/**
* Factory method for path aliases.
*/
@NotNull
public static final Name createPathAlias(Table> child, ForeignKey, ?> path) {
Name name = DSL.name(path.getName());
if (child instanceof TableImpl> t) {
Table> ancestor = t.child;
if (ancestor != null)
name = createPathAlias(ancestor, t.childPath).append(name);
else
name = child.getQualifiedName().append(name);
}
return DSL.name("alias_" + hash(name));
}
/**
* Factory method for parameters.
*/
@NotNull
public static final Parameter createParameter(String name, DataType type, boolean isDefaulted, boolean isUnnamed) {
return createParameter(name, type, isDefaulted, isUnnamed, null, null);
}
/**
* Factory method for parameters.
*/
@NotNull
public static final Parameter createParameter(String name, DataType type, boolean isDefaulted, boolean isUnnamed, Converter converter) {
return createParameter(name, type, isDefaulted, isUnnamed, converter, null);
}
/**
* Factory method for parameters.
*/
@NotNull
public static final Parameter createParameter(String name, DataType type, boolean isDefaulted, boolean isUnnamed, Binding binding) {
return createParameter(name, type, isDefaulted, isUnnamed, null, binding);
}
/**
* Factory method for parameters.
*/
@NotNull
@SuppressWarnings("unchecked")
public static final Parameter createParameter(String name, DataType type, boolean isDefaulted, boolean isUnnamed, Converter converter, Binding binding) {
final Binding actualBinding = DefaultBinding.newBinding(converter, type, binding);
final DataType actualType = converter == null && binding == null
? (DataType) type
: type.asConvertedDataType(actualBinding);
// TODO: [#11327] Get the ParamMode right
return new ParameterImpl<>(ParamMode.IN, DSL.name(name), actualType, isDefaulted, isUnnamed);
}
private Internal() {}
/**
* Factory method for indexes.
*
* @deprecated - 3.14.0 - [#9404] - Please re-generate your code.
*/
@NotNull
@Deprecated(since = "3.14", forRemoval = true)
public static final Index createIndex(String name, Table> table, OrderField>[] sortFields, boolean unique) {
return createIndex(DSL.name(name), table, sortFields, unique);
}
/**
* Factory method for unique keys.
*
* @deprecated - 3.14.0 - [#9404] - Please re-generate your code.
*/
@NotNull
@SafeVarargs
@Deprecated(since = "3.14", forRemoval = true)
public static final UniqueKey createUniqueKey(Table table, String name, TableField... fields) {
return createUniqueKey(table, name, fields, true);
}
/**
* Factory method for unique keys.
*
* @deprecated - 3.14.0 - [#9404] - Please re-generate your code.
*/
@NotNull
@Deprecated(since = "3.14", forRemoval = true)
public static final UniqueKey createUniqueKey(Table table, String name, TableField[] fields, boolean enforced) {
return createUniqueKey(table, DSL.name(name), fields, enforced);
}
/**
* Factory method for foreign keys.
*
* @deprecated - 3.14.0 - [#9404] - Please re-generate your code.
*/
@NotNull
@SafeVarargs
@Deprecated(since = "3.14", forRemoval = true)
public static final ForeignKey createForeignKey(UniqueKey key, Table table, String name, TableField... fields) {
return createForeignKey(key, table, name, fields, true);
}
/**
* Factory method for foreign keys.
*
* @deprecated - 3.14.0 - [#9404] - Please re-generate your code.
*/
@NotNull
@Deprecated(since = "3.14", forRemoval = true)
public static final ForeignKey createForeignKey(UniqueKey key, Table table, String name, TableField[] fields, boolean enforced) {
return createForeignKey(table, DSL.name(name), fields, key, key.getFieldsArray(), enforced);
}
/**
* Get the fields of an embeddable type.
*
* @deprecated - [#11058] - 3.14.5 - Please re-generate your code.
*/
@NotNull
@Deprecated(since = "3.14", forRemoval = true)
public static final > TableField[] fields(TableField embeddableField) {
return ((EmbeddableTableField) embeddableField).fields;
}
/**
* Get the fields row of an embeddable type.
*
* @deprecated - [#12238] - 3.16.0 - Please re-generate your code.
*/
@NotNull
@Deprecated(since = "3.16", forRemoval = true)
public static final > Row fieldsRow(TableField embeddableField) {
return (@NotNull Row) embeddableField.getDataType().getRow();
}
@Support
static final Field ineg(Field field) {
return new Neg<>(field, true);
}
@SuppressWarnings("unchecked")
@Support
static final Field iadd(Field lhs, Field> rhs) {
return new IAdd<>(lhs, (Field) nullSafe(rhs, lhs.getDataType()));
}
@SuppressWarnings("unchecked")
@Support
static final Field isub(Field lhs, Field> rhs) {
return new ISub<>(lhs, (Field) nullSafe(rhs, lhs.getDataType()));
}
@SuppressWarnings("unchecked")
@Support
static final Field imul(Field lhs, Field> rhs) {
return new IMul<>(lhs, (Field) nullSafe(rhs, lhs.getDataType()));
}
@SuppressWarnings("unchecked")
@Support
static final Field idiv(Field lhs, Field> rhs) {
return new IDiv<>(lhs, (Field) nullSafe(rhs, lhs.getDataType()));
}
/**
* Create a {@link Subscriber} from a set of lambdas.
*
* This is used for internal purposes and thus subject for change.
*/
public static final Subscriber subscriber(
Consumer super Subscription> subscription,
Consumer super T> onNext,
Consumer super Throwable> onError,
Runnable onComplete
) {
return new Subscriber() {
@Override
public void onSubscribe(Subscription s) {
subscription.accept(s);
}
@Override
public void onNext(T t) {
onNext.accept(t);
}
@Override
public void onError(Throwable t) {
onError.accept(t);
}
@Override
public void onComplete() {
onComplete.run();
}
};
}
/**
* JDK agnostic abstraction over {@link Class#arrayType()} and
* {@link Array#newInstance(Class, int)}.
*/
@SuppressWarnings({ "unchecked", "unused" })
public static final Class arrayType(Class type) {
if (true)
return (Class) type.arrayType();
else
return (Class) Array.newInstance(type, 0).getClass();
}
/**
* Create an empty result from a {@link Record} using its row type.
*/
public static final Result result(R record) {
return new ResultImpl<>(Tools.configuration(record), ((AbstractRecord) record).fields);
}
/**
* Whether this is a commercial edition of jOOQ.
*/
public static final boolean commercial() {
return CONFIG.get().commercial();
}
/**
* Whether this is a commercial edition of jOOQ, logging a warning message,
* if not.
*/
public static final boolean commercial(Supplier logMessage) {
return CONFIG.get().commercial(logMessage);
}
/**
* Whether this is a commercial edition of jOOQ, throwing an exception with
* a message, if not.
*/
public static final void requireCommercial(Supplier logMessage) throws DataAccessException {
CONFIG.get().requireCommercial(logMessage);
}
/**
* Return a non-negative hash code for a {@link QueryPart}, taking into
* account FindBugs' RV_ABSOLUTE_VALUE_OF_HASHCODE
pattern
*/
public static final int hash(QueryPart part) {
return hash0(CTX.get().render(part));
}
static final int hash0(Object object) {
if (object == null)
return 0;
// [#6025] Prevent unstable alias generation for derived tables due to
// inlined bind variables in hashCode() calculation
// [#6175] TODO: Speed this up with a faster way to calculate a hash code
else
return 0x7FFFFFF & object.hashCode();
}
private static final Lazy CONVERTER_SCOPE = Lazy.of(() -> new DefaultConverterContext(CONFIG.get()));
public static final ConverterContext converterContext() {
return CONVERTER_SCOPE.get();
}
private static final Lazy JAVA_VERSION = Lazy.of(() -> {
try {
return Reflect.onClass(Runtime.class)
// Since Java 9
.call("version")
// Since Java 10
.call("feature")
.get();
}
catch (ReflectException e) {
return 8;
}
});
/**
* Get the Java version (relevant to jOOQ) as an int.
*
* Supported versions are:
*
* - 8
* - 11
* - 17
* - 21
*
*/
public static final int javaVersion() {
return JAVA_VERSION.get();
}
/**
* [#11898] [#16044] This method just acts as a bridge to internal API from
* the deprecated-for-removal {@link org.jooq.tools.Convert} utility. Do not
* reuse these methods.
*/
@Deprecated(forRemoval = true)
public static final Object[] convert(Object[] values, Field>[] fields) {
return Convert.convert(values, fields);
}
/**
* [#11898] [#16044] This method just acts as a bridge to internal API from
* the deprecated-for-removal {@link org.jooq.tools.Convert} utility. Do not
* reuse these methods.
*/
@Deprecated(forRemoval = true)
public static final Object[] convert(Object[] values, Class>[] types) {
return Convert.convert(values, types);
}
/**
* [#11898] [#16044] This method just acts as a bridge to internal API from
* the deprecated-for-removal {@link org.jooq.tools.Convert} utility. Do not
* reuse these methods.
*/
@Deprecated(forRemoval = true)
public static final U[] convertArray(Object[] from, Converter, ? extends U> converter) throws DataTypeException {
return Convert.convertArray(from, converter);
}
/**
* [#11898] [#16044] This method just acts as a bridge to internal API from
* the deprecated-for-removal {@link org.jooq.tools.Convert} utility. Do not
* reuse these methods.
*/
@Deprecated(forRemoval = true)
public static final Object[] convertArray(Object[] from, Class> toClass) throws DataTypeException {
return Convert.convertArray(from, toClass);
}
/**
* [#11898] [#16044] This method just acts as a bridge to internal API from
* the deprecated-for-removal {@link org.jooq.tools.Convert} utility. Do not
* reuse these methods.
*/
@Deprecated(forRemoval = true)
public static final U[] convertCollection(Collection from, Class extends U[]> to) {
return Convert.convertCollection(from, to);
}
/**
* [#11898] [#16044] This method just acts as a bridge to internal API from
* the deprecated-for-removal {@link org.jooq.tools.Convert} utility. Do not
* reuse these methods.
*/
@Deprecated(forRemoval = true)
public static final U convert(Object from, Converter, ? extends U> converter) throws DataTypeException {
return Convert.convert(from, converter);
}
/**
* [#11898] [#16044] This method just acts as a bridge to internal API from
* the deprecated-for-removal {@link org.jooq.tools.Convert} utility. Do not
* reuse these methods.
*/
@Deprecated(forRemoval = true)
public static final T convert(Object from, Class extends T> toClass) throws DataTypeException {
return Convert.convert(from, toClass);
}
/**
* [#11898] [#16044] This method just acts as a bridge to internal API from
* the deprecated-for-removal {@link org.jooq.tools.Convert} utility. Do not
* reuse these methods.
*/
@Deprecated(forRemoval = true)
public static final List convert(Collection> collection, Class extends T> type) throws DataTypeException {
return Convert.convert(collection, type);
}
/**
* [#11898] [#16044] This method just acts as a bridge to internal API from
* the deprecated-for-removal {@link org.jooq.tools.Convert} utility. Do not
* reuse these methods.
*/
@Deprecated(forRemoval = true)
public static final List convert(Collection> collection, Converter, ? extends U> converter) throws DataTypeException {
return Convert.convert(collection, converter);
}
}