From bd064a7be916dc74b03a326be7e9dcca73673227 Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Wed, 13 Apr 2022 14:35:59 +0200 Subject: [PATCH] [jOOQ/jOOQ#9879] Added GeneratorStatementType --- jOOQ/src/main/java/org/jooq/Generator.java | 5 ++ .../main/java/org/jooq/GeneratorContext.java | 17 +++- .../java/org/jooq/GeneratorStatementType.java | 82 +++++++++++++++++++ .../java/org/jooq/impl/AbstractDataType.java | 2 +- .../org/jooq/impl/DefaultConfiguration.java | 1 - .../jooq/impl/DefaultGeneratorContext.java | 12 ++- .../java/org/jooq/impl/FieldMapForUpdate.java | 1 + .../org/jooq/impl/FieldMapsForInsert.java | 1 + .../java/org/jooq/impl/InsertQueryImpl.java | 1 + .../java/org/jooq/impl/TableFieldImpl.java | 1 + 10 files changed, 119 insertions(+), 4 deletions(-) create mode 100644 jOOQ/src/main/java/org/jooq/GeneratorStatementType.java diff --git a/jOOQ/src/main/java/org/jooq/Generator.java b/jOOQ/src/main/java/org/jooq/Generator.java index 6306f076ae..137bfb5608 100644 --- a/jOOQ/src/main/java/org/jooq/Generator.java +++ b/jOOQ/src/main/java/org/jooq/Generator.java @@ -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. + *

+ * 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 extends Function>, Serializable {} diff --git a/jOOQ/src/main/java/org/jooq/GeneratorContext.java b/jOOQ/src/main/java/org/jooq/GeneratorContext.java index ab29b0a596..8b1a68a29c 100644 --- a/jOOQ/src/main/java/org/jooq/GeneratorContext.java +++ b/jOOQ/src/main/java/org/jooq/GeneratorContext.java @@ -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. + *

+ * 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 + * null when the statement type is unknown / not applicable. + */ + @Nullable + GeneratorStatementType statementType(); +} diff --git a/jOOQ/src/main/java/org/jooq/GeneratorStatementType.java b/jOOQ/src/main/java/org/jooq/GeneratorStatementType.java new file mode 100644 index 0000000000..ba735e19b6 --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/GeneratorStatementType.java @@ -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}. + *

+ * {@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. + *

+ * 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 INSERT clause context. + *

+ * 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 ON DUPLICATE KEY UPDATE clause, + * ON CONFLICT DO UPDATE clause context, or in a + * {@link Merge}'s UPDATE clause context. + *

+ * 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 + * RETURNING clause context. + *

+ * This applies to {@link GenerationOption#VIRTUAL} only. + */ + SELECT +} diff --git a/jOOQ/src/main/java/org/jooq/impl/AbstractDataType.java b/jOOQ/src/main/java/org/jooq/impl/AbstractDataType.java index 862955ba6f..0b27c38620 100644 --- a/jOOQ/src/main/java/org/jooq/impl/AbstractDataType.java +++ b/jOOQ/src/main/java/org/jooq/impl/AbstractDataType.java @@ -199,7 +199,7 @@ implements @Override public final Field generatedAlwaysAs() { Generator s = generatedAlwaysAsGenerator(); - return s == null ? null : s.apply(new DefaultGeneratorContext(CONFIG)); + return s == null ? null : s.apply(new DefaultGeneratorContext(CONFIG, null)); } @Override diff --git a/jOOQ/src/main/java/org/jooq/impl/DefaultConfiguration.java b/jOOQ/src/main/java/org/jooq/impl/DefaultConfiguration.java index a63822913a..23878a940b 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DefaultConfiguration.java +++ b/jOOQ/src/main/java/org/jooq/impl/DefaultConfiguration.java @@ -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; diff --git a/jOOQ/src/main/java/org/jooq/impl/DefaultGeneratorContext.java b/jOOQ/src/main/java/org/jooq/impl/DefaultGeneratorContext.java index 4ad51001c7..9f78b7641c 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DefaultGeneratorContext.java +++ b/jOOQ/src/main/java/org/jooq/impl/DefaultGeneratorContext.java @@ -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; } } diff --git a/jOOQ/src/main/java/org/jooq/impl/FieldMapForUpdate.java b/jOOQ/src/main/java/org/jooq/impl/FieldMapForUpdate.java index 6c404f4da7..d65d284e52 100644 --- a/jOOQ/src/main/java/org/jooq/impl/FieldMapForUpdate.java +++ b/jOOQ/src/main/java/org/jooq/impl/FieldMapForUpdate.java @@ -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; diff --git a/jOOQ/src/main/java/org/jooq/impl/FieldMapsForInsert.java b/jOOQ/src/main/java/org/jooq/impl/FieldMapsForInsert.java index 072bfe69a7..c1b1d7de95 100644 --- a/jOOQ/src/main/java/org/jooq/impl/FieldMapsForInsert.java +++ b/jOOQ/src/main/java/org/jooq/impl/FieldMapsForInsert.java @@ -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; diff --git a/jOOQ/src/main/java/org/jooq/impl/InsertQueryImpl.java b/jOOQ/src/main/java/org/jooq/impl/InsertQueryImpl.java index 08b9d38c69..f3dc7126c3 100644 --- a/jOOQ/src/main/java/org/jooq/impl/InsertQueryImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/InsertQueryImpl.java @@ -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; diff --git a/jOOQ/src/main/java/org/jooq/impl/TableFieldImpl.java b/jOOQ/src/main/java/org/jooq/impl/TableFieldImpl.java index 835e02ef58..64e90fcd20 100644 --- a/jOOQ/src/main/java/org/jooq/impl/TableFieldImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/TableFieldImpl.java @@ -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;