From 8f5399bc821351fa5029733d8aae99ea2ef51885 Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Thu, 23 Feb 2023 13:00:36 +0100 Subject: [PATCH] [jOOQ/jOOQ#14077] Add parser support --- .../main/java/org/jooq/AlterViewAsStep.java | 79 ------------------- .../src/main/java/org/jooq/AlterViewStep.java | 9 ++- jOOQ/src/main/java/org/jooq/DSLContext.java | 4 +- .../java/org/jooq/impl/AlterViewImpl.java | 1 - jOOQ/src/main/java/org/jooq/impl/DSL.java | 4 +- .../java/org/jooq/impl/DefaultDSLContext.java | 4 +- .../main/java/org/jooq/impl/ParserImpl.java | 24 +++++- 7 files changed, 36 insertions(+), 89 deletions(-) delete mode 100644 jOOQ/src/main/java/org/jooq/AlterViewAsStep.java diff --git a/jOOQ/src/main/java/org/jooq/AlterViewAsStep.java b/jOOQ/src/main/java/org/jooq/AlterViewAsStep.java deleted file mode 100644 index 53983536f9..0000000000 --- a/jOOQ/src/main/java/org/jooq/AlterViewAsStep.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * 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 - * ASL 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; - -import static org.jooq.SQLDialect.*; -import static org.jooq.impl.DSL.*; - -import java.util.*; - -import org.jooq.impl.DSL; - -import org.jetbrains.annotations.*; - -/** - * A step in the construction of the ALTER VIEW statement. - *

- *

Referencing XYZ*Step types directly from client code

- *

- * It is usually not recommended to reference any XYZ*Step types - * directly from client code, or assign them to local variables. When writing - * dynamic SQL, creating a statement's components dynamically, and passing them - * to the DSL API statically is usually a better choice. See the manual's - * section about dynamic SQL for details: https://www.jooq.org/doc/latest/manual/sql-building/dynamic-sql. - *

- * Drawbacks of referencing the XYZ*Step types directly: - *

- */ -@SuppressWarnings({ "unused" }) -public interface AlterViewAsStep { - - /** - * Add the AS clause to the ALTER VIEW statement. - */ - @Support({ FIREBIRD, H2, MARIADB, MYSQL, POSTGRES, YUGABYTEDB }) - @NotNull @CheckReturnValue - AlterViewFinalStep as(Select as); -} diff --git a/jOOQ/src/main/java/org/jooq/AlterViewStep.java b/jOOQ/src/main/java/org/jooq/AlterViewStep.java index 9b90590266..ac880e8beb 100644 --- a/jOOQ/src/main/java/org/jooq/AlterViewStep.java +++ b/jOOQ/src/main/java/org/jooq/AlterViewStep.java @@ -68,7 +68,7 @@ import org.jetbrains.annotations.*; * */ @SuppressWarnings({ "unused" }) -public interface AlterViewStep extends AlterViewAsStep { +public interface AlterViewStep { /** * Add the COMMENT clause to the ALTER VIEW statement. @@ -104,4 +104,11 @@ public interface AlterViewStep extends AlterViewAsStep { @Support({ H2, HSQLDB, POSTGRES, YUGABYTEDB }) @NotNull @CheckReturnValue AlterViewFinalStep renameTo(Table renameTo); + + /** + * Add the AS clause to the ALTER VIEW statement. + */ + @Support({ FIREBIRD, H2, MARIADB, MYSQL, POSTGRES, YUGABYTEDB }) + @NotNull @CheckReturnValue + AlterViewFinalStep as(Select as); } diff --git a/jOOQ/src/main/java/org/jooq/DSLContext.java b/jOOQ/src/main/java/org/jooq/DSLContext.java index 7e3a24dc84..d88b8a4a65 100644 --- a/jOOQ/src/main/java/org/jooq/DSLContext.java +++ b/jOOQ/src/main/java/org/jooq/DSLContext.java @@ -9633,7 +9633,7 @@ public interface DSLContext extends Scope { */ @NotNull @CheckReturnValue @Support({ FIREBIRD, H2, MARIADB, MYSQL, POSTGRES, YUGABYTEDB }) - AlterViewAsStep alterView(Table view, Field... fields); + AlterViewStep alterView(Table view, Field... fields); /** * The ALTER VIEW statement. @@ -9642,7 +9642,7 @@ public interface DSLContext extends Scope { */ @NotNull @CheckReturnValue @Support({ FIREBIRD, H2, MARIADB, MYSQL, POSTGRES, YUGABYTEDB }) - AlterViewAsStep alterView(Table view, Collection> fields); + AlterViewStep alterView(Table view, Collection> fields); /** * The COMMENT ON TABLE statement. diff --git a/jOOQ/src/main/java/org/jooq/impl/AlterViewImpl.java b/jOOQ/src/main/java/org/jooq/impl/AlterViewImpl.java index 2d1853c6db..263ee51ff6 100644 --- a/jOOQ/src/main/java/org/jooq/impl/AlterViewImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/AlterViewImpl.java @@ -71,7 +71,6 @@ extends implements QOM.AlterView, AlterViewStep, - AlterViewAsStep, AlterViewFinalStep { diff --git a/jOOQ/src/main/java/org/jooq/impl/DSL.java b/jOOQ/src/main/java/org/jooq/impl/DSL.java index 6d5b4b6d72..46dd9055b1 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DSL.java +++ b/jOOQ/src/main/java/org/jooq/impl/DSL.java @@ -7714,7 +7714,7 @@ public class DSL { */ @NotNull @CheckReturnValue @Support({ FIREBIRD, H2, MARIADB, MYSQL, POSTGRES, YUGABYTEDB }) - public static org.jooq.AlterViewAsStep alterView(Table view, Field... fields) { + public static org.jooq.AlterViewStep alterView(Table view, Field... fields) { return dsl().alterView(view, fields); } @@ -7729,7 +7729,7 @@ public class DSL { */ @NotNull @CheckReturnValue @Support({ FIREBIRD, H2, MARIADB, MYSQL, POSTGRES, YUGABYTEDB }) - public static org.jooq.AlterViewAsStep alterView(Table view, Collection> fields) { + public static org.jooq.AlterViewStep alterView(Table view, Collection> fields) { return dsl().alterView(view, fields); } diff --git a/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java b/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java index b3d5e48341..f7d7c40347 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java +++ b/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java @@ -3054,12 +3054,12 @@ public class DefaultDSLContext extends AbstractScope implements DSLContext, Seri } @Override - public org.jooq.AlterViewAsStep alterView(Table view, Field... fields) { + public org.jooq.AlterViewStep alterView(Table view, Field... fields) { return new AlterViewImpl(configuration(), view, Arrays.asList(fields), false); } @Override - public org.jooq.AlterViewAsStep alterView(Table view, Collection> fields) { + public org.jooq.AlterViewStep alterView(Table view, Collection> fields) { return new AlterViewImpl(configuration(), view, new QueryPartList<>(fields), false); } diff --git a/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java b/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java index 46c47ad184..02caafa586 100644 --- a/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java @@ -4219,8 +4219,28 @@ final class DefaultParseContext extends AbstractScope implements ParseContext { private final DDLQuery parseAlterView() { boolean ifExists = parseKeywordIf("IF EXISTS"); Table oldName = parseTableName(); + Field[] fields = EMPTY_FIELD; - if (parseKeywordIf("RENAME")) { + if (parseIf('(')) { + fields = parseList(',', c -> parseFieldName()).toArray(fields); + parse(')'); + } + + if (parseKeywordIf("AS")) { + Select select = parseWithOrSelect(); + int degree = Tools.degree(select); + + if (fields.length > 0 && fields.length != degree) + throw exception("Select list size (" + degree + ") must match declared field size (" + fields.length + ")"); + + if (fields.length == 0) + return dsl.alterView(oldName).as(select); + else + return dsl.alterView(oldName, fields).as(select); + } + else if (fields.length > 0) + throw expected("AS"); + else if (parseKeywordIf("RENAME")) { parseKeyword("AS", "TO"); Table newName = parseTableName(); @@ -4233,7 +4253,7 @@ final class DefaultParseContext extends AbstractScope implements ParseContext { else if (parseKeywordIf("SET")) return dsl.alterView(oldName).comment(parseOptionsDescription()); else - throw expected("OWNER TO", "RENAME", "SET"); + throw expected("AS", "OWNER TO", "RENAME", "SET"); } private final Comment parseOptionsDescription() {