[jOOQ/jOOQ#9879] Added GeneratorStatementType

This commit is contained in:
Lukas Eder 2022-04-13 14:35:59 +02:00
parent 69d2708080
commit bd064a7be9
10 changed files with 119 additions and 4 deletions

View File

@ -43,6 +43,11 @@ import java.util.function.Function;
/**
* A generator can be used with {@link DataType#generatedAlwaysAs(Generator)} to
* implement dynamic, client side computed columns.
* <p>
* This API is part of a commercial only feature. To use this feature, please
* use the jOOQ Professional Edition or the jOOQ Enterprise Edition.
*
* @author Lukas Eder
*/
@FunctionalInterface
public interface Generator<T> extends Function<GeneratorContext, Field<T>>, Serializable {}

View File

@ -37,8 +37,23 @@
*/
package org.jooq;
import org.jetbrains.annotations.Nullable;
/**
* A generator context is an argument object that is passed to a
* {@link Generator} when generating client side computed columns.
* <p>
* This API is part of a commercial only feature. To use this feature, please
* use the jOOQ Professional Edition or the jOOQ Enterprise Edition.
*
* @author Lukas Eder
*/
public interface GeneratorContext extends Scope {}
public interface GeneratorContext extends Scope {
/**
* The statement type in which the {@link Generator} is being invoked, or
* <code>null</code> when the statement type is unknown / not applicable.
*/
@Nullable
GeneratorStatementType statementType();
}

View File

@ -0,0 +1,82 @@
/*
* 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;
import org.jooq.impl.QOM.GenerationOption;
/**
* The statement type of a {@link GeneratorContext}.
* <p>
* {@link Generator} implementations may choose to execute different logic based
* on the statement type. One example are {@link AuditProvider} fields, which
* can distinguish between audit information on inserted or updated records.
* <p>
* This API is part of a commercial only feature. To use this feature, please
* use the jOOQ Professional Edition or the jOOQ Enterprise Edition.
*
* @author Lukas Eder
*/
public enum GeneratorStatementType {
/**
* The {@link Generator} is being invoked in an {@link Insert} context, or
* in a {@link Merge}'s <code>INSERT</code> clause context.
* <p>
* This applies to {@link GenerationOption#STORED} only.
*/
INSERT,
/**
* The {@link Generator} is being invoked in an {@link Update} context, or
* in an {@link Insert}'s <code>ON DUPLICATE KEY UPDATE</code> clause,
* <code>ON CONFLICT DO UPDATE</code> clause context, or in a
* {@link Merge}'s <code>UPDATE</code> clause context.
* <p>
* This applies to {@link GenerationOption#STORED} only.
*/
UPDATE,
/**
* The {@link Generator} is being invoked in a {@link Select} context, or an
* {@link Insert}'s, {@link Update}'s, or {@link Delete}'s
* <code>RETURNING</code> clause context.
* <p>
* This applies to {@link GenerationOption#VIRTUAL} only.
*/
SELECT
}

View File

@ -199,7 +199,7 @@ implements
@Override
public final Field<T> generatedAlwaysAs() {
Generator<T> s = generatedAlwaysAsGenerator();
return s == null ? null : s.apply(new DefaultGeneratorContext(CONFIG));
return s == null ? null : s.apply(new DefaultGeneratorContext(CONFIG, null));
}
@Override

View File

@ -62,7 +62,6 @@ import org.jooq.Configuration;
import org.jooq.ConnectionProvider;
import org.jooq.ConverterProvider;
import org.jooq.DSLContext;
import org.jooq.DefaultAuditProvider;
import org.jooq.DiagnosticsListener;
import org.jooq.DiagnosticsListenerProvider;
import org.jooq.ExecuteListener;

View File

@ -39,13 +39,23 @@ package org.jooq.impl;
import org.jooq.Configuration;
import org.jooq.GeneratorContext;
import org.jooq.GeneratorStatementType;
/**
* @author Lukas Eder
*/
final class DefaultGeneratorContext extends AbstractScope implements GeneratorContext {
DefaultGeneratorContext(Configuration configuration) {
final GeneratorStatementType statementType;
DefaultGeneratorContext(Configuration configuration, GeneratorStatementType statementType) {
super(configuration);
this.statementType = statementType;
}
@Override
public final GeneratorStatementType statementType() {
return statementType;
}
}

View File

@ -104,6 +104,7 @@ import org.jooq.Context;
import org.jooq.Field;
import org.jooq.FieldOrRow;
import org.jooq.FieldOrRowOrSelect;
import org.jooq.GeneratorStatementType;
// ...
import org.jooq.RenderContext.CastMode;
import org.jooq.Row;

View File

@ -80,6 +80,7 @@ import org.jooq.Context;
import org.jooq.DataType;
import org.jooq.Field;
import org.jooq.FieldOrRow;
import org.jooq.GeneratorStatementType;
import org.jooq.Param;
// ...
import org.jooq.Record;

View File

@ -110,6 +110,7 @@ import org.jooq.Configuration;
import org.jooq.Constraint;
import org.jooq.Context;
import org.jooq.Field;
import org.jooq.GeneratorStatementType;
import org.jooq.Identity;
import org.jooq.InsertQuery;
import org.jooq.MergeNotMatchedStep;

View File

@ -55,6 +55,7 @@ import org.jooq.Context;
import org.jooq.DataType;
import org.jooq.Field;
import org.jooq.Generator;
import org.jooq.GeneratorStatementType;
import org.jooq.Name;
import org.jooq.Record;
import org.jooq.RowId;