[jOOQ/jOOQ#681] Add interpreter support for DOMAINS (WIP)

This change includes:

- Support interpreting CREATE DOMAIN .. CHECK
- Support interpreting DROP DOMAIN
- Improved DomainImpl and added DomainDataType
This commit is contained in:
Lukas Eder 2020-05-28 16:50:47 +02:00
parent cf993c0b67
commit 8e15f27682
7 changed files with 73 additions and 10 deletions

View File

@ -114,6 +114,12 @@ public interface DataType<T> extends Serializable {
*/
Class<T> getType();
/**
* Get the defining DOMAIN type or <code>NULL</code> if there is no such
* type.
*/
Domain<T> getDomain();
/**
* Retrieve the Java type associated with ARRAYs of this data type.
*/

View File

@ -81,7 +81,7 @@ final class ArrayDataType<T> extends DefaultDataType<T[]> {
) {
super(t, precision, scale, length, nullability, collation, characterSet, identity, defaultValue);
this.elementType= elementType;
this.elementType = elementType;
}
@SuppressWarnings({ "unchecked", "rawtypes" })

View File

@ -87,10 +87,10 @@ import static org.jooq.SQLDialect.SQLITE;
// ...
import static org.jooq.impl.Keywords.K_CUBE;
import static org.jooq.impl.Keywords.K_GROUPING_SETS;
import static org.jooq.impl.Keywords.K_VALUE;
import static org.jooq.impl.Names.N_IF;
import static org.jooq.impl.Names.N_IIF;
import static org.jooq.impl.Names.N_SYSTEM_TIME;
import static org.jooq.impl.Names.N_VALUE;
import static org.jooq.impl.PositionalWindowFunction.PositionalFunctionType.FIRST_VALUE;
import static org.jooq.impl.PositionalWindowFunction.PositionalFunctionType.LAG;
import static org.jooq.impl.PositionalWindowFunction.PositionalFunctionType.LAST_VALUE;
@ -11942,7 +11942,7 @@ public class DSL {
*/
@Support({ H2, POSTGRES })
public static <T> Field<T> value(DataType<T> type) {
return field("{0}", type, K_VALUE);
return field("{0}", type, N_VALUE);
}
/**

View File

@ -74,6 +74,7 @@ import org.jooq.Collation;
import org.jooq.Configuration;
import org.jooq.Converter;
import org.jooq.DataType;
import org.jooq.Domain;
import org.jooq.EnumType;
import org.jooq.Field;
import org.jooq.JSON;
@ -769,6 +770,11 @@ public class DefaultDataType<T> implements DataType<T> {
return uType;
}
@Override
public /* non-final */ Domain<T> getDomain() {
return null;
}
@Override
public final Binding<?, T> getBinding() {
return binding;

View File

@ -42,8 +42,11 @@ import static java.util.Collections.unmodifiableList;
import java.util.List;
import org.jooq.Binding;
import org.jooq.Check;
import org.jooq.Configuration;
import org.jooq.Context;
import org.jooq.Converter;
import org.jooq.DataType;
import org.jooq.Domain;
import org.jooq.Name;
@ -52,7 +55,7 @@ import org.jooq.Schema;
/**
* @author Lukas Eder
*/
class DomainImpl<T> extends AbstractTypedNamed<T> implements Domain<T> {
class DomainImpl<T> extends AbstractNamed implements Domain<T> {
/**
* Generated UID
@ -60,12 +63,14 @@ class DomainImpl<T> extends AbstractTypedNamed<T> implements Domain<T> {
private static final long serialVersionUID = 162853300137140844L;
private final Schema schema;
private final Check<?>[] checks;
private final DataType<T> type;
DomainImpl(Schema schema, Name name, DataType<T> dataType, Check<?>... checks) {
super(qualify(schema, name), null, dataType);
DomainImpl(Schema schema, Name name, DataType<T> type, Check<?>... checks) {
super(qualify(schema, name), null);
this.schema = schema;
this.checks = checks;
this.type = new DomainDataType<>(this, type);
}
@Override
@ -78,6 +83,31 @@ class DomainImpl<T> extends AbstractTypedNamed<T> implements Domain<T> {
return unmodifiableList(asList(checks));
}
@Override
public final Converter<?, T> getConverter() {
return type.getConverter();
}
@Override
public final Binding<?, T> getBinding() {
return type.getBinding();
}
@Override
public final Class<T> getType() {
return type.getType();
}
@Override
public final DataType<T> getDataType() {
return type;
}
@Override
public final DataType<T> getDataType(Configuration configuration) {
return type.getDataType(configuration);
}
@Override
public final void accept(Context<?> ctx) {
ctx.visit(getUnqualifiedName());

View File

@ -209,7 +209,9 @@ final class Interpreter {
accept0((DropIndexImpl) query);
else if (query instanceof CreateDomainImpl)
accept0((CreateDomainImpl) query);
accept0((CreateDomainImpl<?>) query);
else if (query instanceof DropDomainImpl)
accept0((DropDomainImpl) query);
else if (query instanceof CommentOnImpl)
accept0((CommentOnImpl) query);
@ -997,7 +999,7 @@ final class Interpreter {
@SuppressWarnings({ "rawtypes", "unchecked" })
private final void accept0(CreateDomainImpl<?> query) {
Domain<?> domain = query.$domain();
MutableSchema schema = getSchema(domain.getSchema(), true);
MutableSchema schema = getSchema(domain.getSchema());
MutableDomain existing = schema.domain(domain);
if (existing != null) {
@ -1027,6 +1029,21 @@ final class Interpreter {
}
}
private final void accept0(DropDomainImpl query) {
Domain<?> domain = query.$domain();
MutableSchema schema = getSchema(domain.getSchema());
MutableDomain existing = schema.domain(domain);
if (existing == null) {
if (!query.$dropDomainIfExists())
throw notExists(domain);
return;
}
schema.domains.remove(existing);
}
private final void accept0(CommentOnImpl query) {
Table<?> table = query.$table();
Field<?> field = query.$field();
@ -2063,14 +2080,17 @@ final class Interpreter {
}
private final class MutableField extends MutableNamed {
MutableTable table;
DataType<?> type;
MutableTable table;
DataType<?> type;
MutableDomain domain;
MutableField(UnqualifiedName name, MutableTable table, DataType<?> type) {
super(name);
this.table = table;
this.type = type;
// TODO: Link type to domain
}
@Override

View File

@ -230,6 +230,7 @@ final class Names {
static final Name N_UCASE = unquotedName("ucase");
static final Name N_UPPER = unquotedName("upper");
static final Name N_USER = unquotedName("user");
static final Name N_VALUE = unquotedName("value");
static final Name N_VALUES = unquotedName("values");
static final Name N_WEEKDAY = unquotedName("weekday");
static final Name N_WIDTH_BUCKET = unquotedName("width_bucket");