[#7518] [#7570] [#7776] Better support for enum types

- [#7518] [#7776] Add support for parsing custom data types
- [#7570] Add support for CREATE TYPE .. AS ENUM
This commit is contained in:
Lukas Eder 2018-08-17 14:26:39 +02:00
parent a4ccdb3848
commit 85e2b1c6ff
9 changed files with 384 additions and 3 deletions

View File

@ -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
;

View File

@ -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.
* <p>
* <h3>Referencing <code>XYZ*Step</code> types directly from client code</h3>
* <p>
* It is usually not recommended to reference any <code>XYZ*Step</code> 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: <a href=
* "https://www.jooq.org/doc/latest/manual/sql-building/dynamic-sql">https://www.jooq.org/doc/latest/manual/sql-building/dynamic-sql</a>.
* <p>
* Drawbacks of referencing the <code>XYZ*Step</code> types directly:
* <ul>
* <li>They're operating on mutable implementations (as of jOOQ 3.x)</li>
* <li>They're less composable and not easy to get right when dynamic SQL gets
* complex</li>
* <li>They're less readable</li>
* <li>They might have binary incompatible changes between minor releases</li>
* </ul>
*
* @author Lukas Eder
*/
public interface CreateTypeFinalStep extends DDLQuery {
}

View File

@ -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.
* <p>
* <h3>Referencing <code>XYZ*Step</code> types directly from client code</h3>
* <p>
* It is usually not recommended to reference any <code>XYZ*Step</code> 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: <a href=
* "https://www.jooq.org/doc/latest/manual/sql-building/dynamic-sql">https://www.jooq.org/doc/latest/manual/sql-building/dynamic-sql</a>.
* <p>
* Drawbacks of referencing the <code>XYZ*Step</code> types directly:
* <ul>
* <li>They're operating on mutable implementations (as of jOOQ 3.x)</li>
* <li>They're less composable and not easy to get right when dynamic SQL gets
* complex</li>
* <li>They're less readable</li>
* <li>They might have binary incompatible changes between minor releases</li>
* </ul>
*
* @author Lukas Eder
*/
public interface CreateTypeStep {
/**
* Add the <code>AS ENUM</code> clause to the <code>CREATE TYPE</code> statement.
*/
@Support({ H2, POSTGRES })
CreateTypeFinalStep asEnum();
/**
* Add the <code>AS ENUM</code> clause to the <code>CREATE TYPE</code> statement.
*/
@Support({ H2, POSTGRES })
CreateTypeFinalStep asEnum(String... values);
/**
* Add the <code>AS ENUM</code> clause to the <code>CREATE TYPE</code> statement.
*/
@SuppressWarnings("unchecked")
@Support({ H2, POSTGRES })
CreateTypeFinalStep asEnum(Field<String>... values);
/**
* Add the <code>AS ENUM</code> clause to the <code>CREATE TYPE</code> statement.
*/
@Support({ H2, POSTGRES })
CreateTypeFinalStep asEnum(Collection<?> values);
}

View File

@ -8885,6 +8885,22 @@ public interface DSLContext extends Scope , AutoCloseable {
CreateViewAsStep<Record> createViewIfNotExists(Table<?> view, BiFunction<? super Field<?>, ? super Integer, ? extends Field<?>> fieldNameFunction);
/**
* Create a new DSL <code>CREATE TYPE</code> statement.
*
* @see DSL#createType(String)
*/
@Support({ H2, POSTGRES })
CreateTypeStep createType(String type);
/**
* Create a new DSL <code>CREATE TYPE</code> statement.
*
* @see DSL#createType(Name)
*/
@Support({ H2, POSTGRES })
CreateTypeStep createType(Name type);
/**
* Create a new DSL <code>CREATE INDEX</code> statement.
*

View File

@ -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<String>... v) {
return asEnum(Arrays.asList(v));
}
@Override
public final CreateTypeFinalStep asEnum(Collection<?> v) {
values = new QueryPartList<Field<?>>(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;
}
}

View File

@ -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 <code>CREATE TYPE</code> statement.
*
* @see DSLContext#createType(String)
*/
@Support({ H2, POSTGRES })
public static CreateTypeStep createType(String type) {
return dsl().createType(type);
}
/**
* Create a new DSL <code>CREATE TYPE</code> statement.
*
* @see DSLContext#createType(Name)
*/
@Support({ H2, POSTGRES })
public static CreateTypeStep createType(Name type) {
return dsl().createType(type);
}
/**
* Create a new DSL <code>CREATE INDEX</code> statement.
*

View File

@ -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<Record>(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);

View File

@ -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<String> values = new ArrayList<String>();
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) {

View File

@ -1530,8 +1530,28 @@ final class Tools {
List<Field<?>> result = new ArrayList<Field<?>>(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<Field<?>> fields(Collection<?> values, DataType<?> type) {
if (values == null || type == null)
return new ArrayList<Field<?>>();
List<Field<?>> result = new ArrayList<Field<?>>(values.size());
for (Object value : values)
result.add(field(value, type));
return result;
}