[jOOQ/jOOQ#9879] Added a GeneratorContext

Generators may require access to a Configuration, so they should be
Function<Generator, Field<T>>, not Supplier<Field<T>>

Fixed issues and added many more tests for virtual client side computed
columns.
This commit is contained in:
Lukas Eder 2022-04-13 12:02:28 +02:00
parent 779d67338a
commit fb99f8b05a
6 changed files with 133 additions and 7 deletions

View File

@ -38,11 +38,11 @@
package org.jooq;
import java.io.Serializable;
import java.util.function.Supplier;
import java.util.function.Function;
/**
* A generator can be used with {@link DataType#generatedAlwaysAs(Generator)} to
* implement dynamic, client side computed columns.
*/
@FunctionalInterface
public interface Generator<T> extends Supplier<Field<T>>, Serializable {}
public interface Generator<T> extends Function<GeneratorContext, Field<T>>, Serializable {}

View File

@ -0,0 +1,44 @@
/*
* 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;
/**
* A generator context is an argument object that is passed to a
* {@link Generator} when generating client side computed columns.
*/
public interface GeneratorContext extends Scope {}

View File

@ -42,7 +42,6 @@ import static org.jooq.SQLDialect.DERBY;
import static org.jooq.SQLDialect.FIREBIRD;
// ...
// ...
import static org.jooq.impl.DSL.unquotedName;
import static org.jooq.impl.Internal.arrayType;
import static org.jooq.impl.QOM.GenerationOption.STORED;
import static org.jooq.impl.QOM.GenerationOption.VIRTUAL;
@ -110,7 +109,6 @@ import org.jooq.impl.QOM.UEmpty;
import org.jooq.types.Interval;
import org.jooq.types.UNumber;
import jakarta.persistence.GenerationType;
// ...
/**
@ -192,7 +190,7 @@ implements
@Override
public final DataType<T> generatedAlwaysAs(Field<T> generatedAlwaysAsValue) {
return generatedAlwaysAs(() -> generatedAlwaysAsValue);
return generatedAlwaysAs(ctx -> generatedAlwaysAsValue);
}
@Override
@ -201,7 +199,7 @@ implements
@Override
public final Field<T> generatedAlwaysAs() {
Generator<T> s = generatedAlwaysAsGenerator();
return s == null ? null : s.get();
return s == null ? null : s.apply(new DefaultGeneratorContext(CONFIG));
}
@Override

View File

@ -0,0 +1,51 @@
/*
* 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 org.jooq.Configuration;
import org.jooq.GeneratorContext;
/**
* @author Lukas Eder
*/
final class DefaultGeneratorContext extends AbstractScope implements GeneratorContext {
DefaultGeneratorContext(Configuration configuration) {
super(configuration);
}
}

View File

@ -43,8 +43,10 @@ import java.util.function.Predicate;
import org.jooq.Clause;
import org.jooq.Context;
import org.jooq.DataType;
import org.jooq.Field;
import org.jooq.ForeignKey;
import org.jooq.Generator;
import org.jooq.Name;
import org.jooq.QueryPart;
import org.jooq.Record;
@ -54,6 +56,8 @@ import org.jooq.TableField;
// ...
import org.jooq.UniqueKey;
import org.jetbrains.annotations.NotNull;
/**
* @author Lukas Eder
*/
@ -88,12 +92,25 @@ final class TableAlias<R extends Record> extends AbstractTable<R> implements QOM
List<Field<?>> result = Tools.map(this.alias.wrapped().fieldsRow().fields(), (f, i) -> new TableFieldImpl(
alias.fieldAliases != null && alias.fieldAliases.length > i
? alias.fieldAliases[i]
: f.getUnqualifiedName(), f.getDataType(), this, f.getCommentPart(), f.getBinding()
: f.getUnqualifiedName(), removeGenerator(f.getDataType()), this, f.getCommentPart(), f.getBinding()
));
return new FieldsImpl<>(result);
}
static final <T> DataType<T> removeGenerator(DataType<T> dataType) {
return dataType;
}
/**
* Get the aliased table wrapped by this table.
*/

View File

@ -45,6 +45,7 @@ import static org.jooq.Clause.FIELD_REFERENCE;
import static org.jooq.impl.DefaultMetaProvider.meta;
import static org.jooq.impl.Tools.BooleanDataKey.DATA_OMIT_CLAUSE_EVENT_EMISSION;
import java.util.function.Function;
import java.util.stream.Stream;
import org.jooq.Binding;
@ -52,6 +53,8 @@ import org.jooq.Clause;
import org.jooq.Comment;
import org.jooq.Context;
import org.jooq.DataType;
import org.jooq.Field;
import org.jooq.Generator;
import org.jooq.Name;
import org.jooq.Record;
import org.jooq.RowId;
@ -60,6 +63,8 @@ import org.jooq.TableField;
import org.jooq.impl.QOM.UNotYetImplemented;
import org.jooq.tools.StringUtils;
import org.jetbrains.annotations.NotNull;
/**
* A common base type for table fields.
*
@ -95,6 +100,11 @@ class TableFieldImpl<R extends Record, T> extends AbstractField<T> implements Ta
// XXX: QueryPart API
// ------------------------------------------------------------------------
@Override
public boolean declaresFields() {
return super.declaresFields();
}
@Override
public final Clause[] clauses(Context<?> ctx) {
return CLAUSES;
@ -117,6 +127,12 @@ class TableFieldImpl<R extends Record, T> extends AbstractField<T> implements Ta