diff --git a/jOOQ-manual/src/main/resources/org/jooq/web/grammar-3.12.txt b/jOOQ-manual/src/main/resources/org/jooq/web/grammar-3.12.txt index 15a9aa01d5..22ce3e604f 100644 --- a/jOOQ-manual/src/main/resources/org/jooq/web/grammar-3.12.txt +++ b/jOOQ-manual/src/main/resources/org/jooq/web/grammar-3.12.txt @@ -15,6 +15,7 @@ ddlStatement = | alterViewStatement | commentStatement | createTableStatement +| createTypeStatement | createIndexStatement | createSchemaStatement | createSequenceStatement @@ -121,6 +122,9 @@ break [ 'COMMENT' [ '=' ] stringLiteral ] ; +createTypeStatement = 'CREATE TYPE' typeName 'AS ENUM' '(' [ stringLiteral { ',' stringLiteral } ] ')' +; + createIndexStatement = 'CREATE' [ 'UNIQUE' ] 'INDEX' [ 'IF NOT EXISTS' ] [ indexName ] break 'ON' tableName [ 'USING' 'BTREE' ] '(' sortFields ')' [ 'INCLUDE' '(' identifiers ')' ] @@ -883,6 +887,9 @@ schemaName = name tableName = name ; +typeName = name +; + indexName = name ; diff --git a/jOOQ/src/main/java/org/jooq/CreateTypeFinalStep.java b/jOOQ/src/main/java/org/jooq/CreateTypeFinalStep.java new file mode 100644 index 0000000000..63d542f95e --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/CreateTypeFinalStep.java @@ -0,0 +1,65 @@ +/* + * 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 {@link Query} that can create types. + *

+ *

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: + *

+ * + * @author Lukas Eder + */ +public interface CreateTypeFinalStep extends DDLQuery { + +} diff --git a/jOOQ/src/main/java/org/jooq/CreateTypeStep.java b/jOOQ/src/main/java/org/jooq/CreateTypeStep.java new file mode 100644 index 0000000000..d1d6586151 --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/CreateTypeStep.java @@ -0,0 +1,94 @@ +/* + * 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 static org.jooq.SQLDialect.H2; +import static org.jooq.SQLDialect.POSTGRES; + +import java.util.Collection; + +/** + * A {@link Query} that can create types. + *

+ *

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: + *

+ * + * @author Lukas Eder + */ +public interface CreateTypeStep { + + /** + * Add the AS ENUM clause to the CREATE TYPE statement. + */ + @Support({ H2, POSTGRES }) + CreateTypeFinalStep asEnum(); + + /** + * Add the AS ENUM clause to the CREATE TYPE statement. + */ + @Support({ H2, POSTGRES }) + CreateTypeFinalStep asEnum(String... values); + + /** + * Add the AS ENUM clause to the CREATE TYPE statement. + */ + @SuppressWarnings("unchecked") + @Support({ H2, POSTGRES }) + CreateTypeFinalStep asEnum(Field... values); + + /** + * Add the AS ENUM clause to the CREATE TYPE statement. + */ + @Support({ H2, POSTGRES }) + CreateTypeFinalStep asEnum(Collection values); +} diff --git a/jOOQ/src/main/java/org/jooq/DSLContext.java b/jOOQ/src/main/java/org/jooq/DSLContext.java index db8f04d609..42f30d28fb 100644 --- a/jOOQ/src/main/java/org/jooq/DSLContext.java +++ b/jOOQ/src/main/java/org/jooq/DSLContext.java @@ -8885,6 +8885,22 @@ public interface DSLContext extends Scope , AutoCloseable { CreateViewAsStep createViewIfNotExists(Table view, BiFunction, ? super Integer, ? extends Field> fieldNameFunction); + /** + * Create a new DSL CREATE TYPE statement. + * + * @see DSL#createType(String) + */ + @Support({ H2, POSTGRES }) + CreateTypeStep createType(String type); + + /** + * Create a new DSL CREATE TYPE statement. + * + * @see DSL#createType(Name) + */ + @Support({ H2, POSTGRES }) + CreateTypeStep createType(Name type); + /** * Create a new DSL CREATE INDEX statement. * diff --git a/jOOQ/src/main/java/org/jooq/impl/CreateTypeImpl.java b/jOOQ/src/main/java/org/jooq/impl/CreateTypeImpl.java new file mode 100644 index 0000000000..a843336e85 --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/impl/CreateTypeImpl.java @@ -0,0 +1,127 @@ +/* + * 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 static org.jooq.impl.Keywords.K_AS; +import static org.jooq.impl.Keywords.K_CREATE; +import static org.jooq.impl.Keywords.K_ENUM; +import static org.jooq.impl.Keywords.K_TYPE; +import static org.jooq.impl.SQLDataType.VARCHAR; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; + +import org.jooq.Clause; +import org.jooq.Configuration; +import org.jooq.Context; +import org.jooq.CreateTypeFinalStep; +import org.jooq.CreateTypeStep; +import org.jooq.Field; +import org.jooq.Name; +import org.jooq.conf.ParamType; + +/** + * @author Lukas Eder + */ +final class CreateTypeImpl extends AbstractQuery implements + + // Cascading interface implementations for CREATE TYPE behaviour + CreateTypeStep, + CreateTypeFinalStep { + + private static final long serialVersionUID = -5018375056147329888L; + private final Name type; + private QueryPartList values; + + CreateTypeImpl(Configuration configuration, Name type) { + super(configuration); + + this.type = type; + } + + // ------------------------------------------------------------------------ + // XXX: DSL API + // ------------------------------------------------------------------------ + + @Override + public final CreateTypeFinalStep asEnum() { + return asEnum(Collections.emptyList()); + } + + @Override + public final CreateTypeFinalStep asEnum(String... v) { + return asEnum(Tools.inline(v)); + } + + + @SafeVarargs + + @Override + public final CreateTypeFinalStep asEnum(Field... v) { + return asEnum(Arrays.asList(v)); + } + + @Override + public final CreateTypeFinalStep asEnum(Collection v) { + values = new QueryPartList>(Tools.fields(v, VARCHAR)); + return this; + } + + // ------------------------------------------------------------------------ + // XXX: QueryPart API + // ------------------------------------------------------------------------ + + @Override + public final void accept(Context ctx) { + ParamType previous = ctx.paramType(); + + ctx.visit(K_CREATE).sql(' ').visit(K_TYPE).sql(' ') + .visit(type).sql(' ') + .visit(K_AS).sql(' ').visit(K_ENUM).sql(" (") + .paramType(ParamType.INLINED) + .visit(values) + .sql(')') + .paramType(previous); + } + + @Override + public final Clause[] clauses(Context ctx) { + return null; + } +} diff --git a/jOOQ/src/main/java/org/jooq/impl/DSL.java b/jOOQ/src/main/java/org/jooq/impl/DSL.java index 94518a80b9..c34d40eb87 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DSL.java +++ b/jOOQ/src/main/java/org/jooq/impl/DSL.java @@ -160,6 +160,7 @@ import org.jooq.CreateIndexStep; import org.jooq.CreateSchemaFinalStep; import org.jooq.CreateSequenceFlagsStep; import org.jooq.CreateTableAsStep; +import org.jooq.CreateTypeStep; import org.jooq.CreateViewAsStep; import org.jooq.DSLContext; import org.jooq.DataType; @@ -6931,6 +6932,26 @@ public class DSL { } + /** + * Create a new DSL CREATE TYPE statement. + * + * @see DSLContext#createType(String) + */ + @Support({ H2, POSTGRES }) + public static CreateTypeStep createType(String type) { + return dsl().createType(type); + } + + /** + * Create a new DSL CREATE TYPE statement. + * + * @see DSLContext#createType(Name) + */ + @Support({ H2, POSTGRES }) + public static CreateTypeStep createType(Name type) { + return dsl().createType(type); + } + /** * Create a new DSL CREATE INDEX statement. * diff --git a/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java b/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java index dab186f069..8da90487cb 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java +++ b/jOOQ/src/main/java/org/jooq/impl/DefaultDSLContext.java @@ -114,6 +114,7 @@ import org.jooq.CreateIndexStep; import org.jooq.CreateSchemaFinalStep; import org.jooq.CreateSequenceFlagsStep; import org.jooq.CreateTableAsStep; +import org.jooq.CreateTypeStep; import org.jooq.CreateViewAsStep; import org.jooq.Cursor; import org.jooq.DDLFlag; @@ -3053,6 +3054,16 @@ public class DefaultDSLContext extends AbstractScope implements DSLContext, Seri return new CreateTableImpl(configuration(), table, true, false); } + @Override + public CreateTypeStep createType(String type) { + return createType(name(type)); + } + + @Override + public CreateTypeStep createType(Name type) { + return new CreateTypeImpl(configuration(), type); + } + @Override public CreateIndexStep createIndex() { return new CreateIndexImpl(configuration(), null, false, false); diff --git a/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java b/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java index a4511c460a..8f4a4122c6 100644 --- a/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java @@ -1918,6 +1918,8 @@ final class ParserImpl implements Parser { return parseCreateTable(ctx, false); else if (parseKeywordIf(ctx, "TEMPORARY TABLE")) return parseCreateTable(ctx, true); + else if (parseKeywordIf(ctx, "TYPE")) + return parseCreateType(ctx); else if (parseKeywordIf(ctx, "GENERATOR")) return parseCreateSequence(ctx); else if (parseKeywordIf(ctx, "GLOBAL TEMPORARY TABLE")) @@ -1951,6 +1953,7 @@ final class ParserImpl implements Parser { "SEQUENCE", "TABLE", "TEMPORARY TABLE", + "TYPE", "UNIQUE INDEX", "VIEW"); } @@ -2763,6 +2766,23 @@ final class ParserImpl implements Parser { return storageStep; } + private static final DDLQuery parseCreateType(ParserContext ctx) { + Name name = parseIdentifier(ctx); + parseKeyword(ctx, "AS ENUM"); + List values = new ArrayList(); + parse(ctx, '('); + + if (!parseIf(ctx, ')')) { + do { + values.add(parseStringLiteral(ctx)); + } + while (parseIf(ctx, ',')); + parse(ctx, ')'); + } + + return ctx.dsl.createType(name).asEnum(values); + } + private static final Index parseIndexSpecification(ParserContext ctx, Table table) { Name name = parseIdentifierIf(ctx); parse(ctx, '('); @@ -7371,7 +7391,7 @@ final class ParserImpl implements Parser { break; } - throw ctx.exception("Unknown data type"); + return new DefaultDataType(ctx.dsl.dialect(), Object.class, parseIdentifier(ctx).toString()); } private static final boolean parseKeywordOrIdentifierIf(ParserContext ctx, String keyword) { diff --git a/jOOQ/src/main/java/org/jooq/impl/Tools.java b/jOOQ/src/main/java/org/jooq/impl/Tools.java index cf50e848c4..689c84bec4 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Tools.java +++ b/jOOQ/src/main/java/org/jooq/impl/Tools.java @@ -1530,8 +1530,28 @@ final class Tools { List> result = new ArrayList>(values.length); - for (int i = 0; i < values.length; i++) - result.add(field(values[i], type)); + for (Object value : values) + result.add(field(value, type)); + + return result; + } + + /** + * Be sure that a given set of objects are fields. + * + * @param values The argument objects + * @param type The type to take the bind value types from + * @return The argument objects themselves, if they are {@link Field}s, or a bind + * values created from the argument objects. + */ + static final List> fields(Collection values, DataType type) { + if (values == null || type == null) + return new ArrayList>(); + + List> result = new ArrayList>(values.size()); + + for (Object value : values) + result.add(field(value, type)); return result; }