Search in sources :

Example 1 with DefaultEnumDefinition

use of org.jooq.meta.DefaultEnumDefinition in project jOOQ by jOOQ.

the class PostgresDatabase method getEnums0.

@Override
protected List<EnumDefinition> getEnums0() throws SQLException {
    List<EnumDefinition> result = new ArrayList<>();
    // [#2736] This table is unavailable in Amazon Redshift
    if (exists(PG_ENUM)) {
        // cross-version compatible enum literal ordering
        for (Identifier type : create().select(PG_TYPE.pgNamespace().NSPNAME, PG_TYPE.TYPNAME).from(PG_TYPE).where(PG_TYPE.pgNamespace().NSPNAME.in(getInputSchemata())).and(PG_TYPE.OID.in(select(PG_ENUM.ENUMTYPID).from(PG_ENUM))).orderBy(PG_TYPE.pgNamespace().NSPNAME, PG_TYPE.TYPNAME).fetch(mapping(Identifier::new))) {
            DefaultEnumDefinition definition = null;
            for (String label : enumLabels(type.schema, type.name)) {
                SchemaDefinition schema = getSchema(type.schema);
                String typeName = String.valueOf(type.name);
                if (definition == null || !definition.getName().equals(typeName)) {
                    definition = new DefaultEnumDefinition(schema, typeName, null);
                    result.add(definition);
                }
                definition.addLiteral(label);
            }
        }
    }
    return result;
}
Also used : SchemaDefinition(org.jooq.meta.SchemaDefinition) ArrayList(java.util.ArrayList) DefaultEnumDefinition(org.jooq.meta.DefaultEnumDefinition) DefaultEnumDefinition(org.jooq.meta.DefaultEnumDefinition) EnumDefinition(org.jooq.meta.EnumDefinition)

Example 2 with DefaultEnumDefinition

use of org.jooq.meta.DefaultEnumDefinition in project jOOQ by jOOQ.

the class MySQLDatabase method getEnums0.

@Override
protected List<EnumDefinition> getEnums0() throws SQLException {
    List<EnumDefinition> result = new ArrayList<>();
    for (ColumnRecord r : create().select(COLUMNS.TABLE_SCHEMA, COLUMNS.TABLE_NAME, COLUMNS.COLUMN_NAME, COLUMNS.COLUMN_TYPE, COLUMNS.COLUMN_COMMENT).from(COLUMNS).where(COLUMNS.COLUMN_TYPE.like("enum(%)").and(COLUMNS.TABLE_SCHEMA.in(workaroundFor5213(getInputSchemata())))).orderBy(COLUMNS.TABLE_SCHEMA.asc(), COLUMNS.TABLE_NAME.asc(), COLUMNS.COLUMN_NAME.asc()).fetch(mapping(ColumnRecord::new))) {
        SchemaDefinition schema = getSchema(r.schema);
        String name = r.table + "_" + r.column;
        // [#1237] Don't generate enum classes for columns in MySQL tables
        // that are excluded from code generation
        TableDefinition tableDefinition = getTable(schema, r.table);
        if (tableDefinition != null) {
            ColumnDefinition columnDefinition = tableDefinition.getColumn(r.column);
            if (columnDefinition != null) {
                // are explicitly forced to another type
                if (getConfiguredForcedType(columnDefinition, columnDefinition.getType()) == null) {
                    DefaultEnumDefinition definition = new DefaultEnumDefinition(schema, name, r.comment);
                    CSVReader reader = new CSVReader(new StringReader(r.type.replaceAll("(^enum\\()|(\\)$)", "")), // Separator
                    ',', // Quote character
                    '\'', // Strict quotes
                    true);
                    for (String string : reader.next()) definition.addLiteral(string);
                    result.add(definition);
                }
            }
        }
    }
    return result;
}
Also used : SchemaDefinition(org.jooq.meta.SchemaDefinition) CSVReader(org.jooq.tools.csv.CSVReader) ArrayList(java.util.ArrayList) StringReader(java.io.StringReader) TableDefinition(org.jooq.meta.TableDefinition) DefaultEnumDefinition(org.jooq.meta.DefaultEnumDefinition) DefaultEnumDefinition(org.jooq.meta.DefaultEnumDefinition) EnumDefinition(org.jooq.meta.EnumDefinition) DefaultIndexColumnDefinition(org.jooq.meta.DefaultIndexColumnDefinition) ColumnDefinition(org.jooq.meta.ColumnDefinition) IndexColumnDefinition(org.jooq.meta.IndexColumnDefinition)

Example 3 with DefaultEnumDefinition

use of org.jooq.meta.DefaultEnumDefinition in project jOOQ by jOOQ.

the class CUBRIDDatabase method getEnums0.

@Override
protected List<EnumDefinition> getEnums0() throws SQLException {
    List<EnumDefinition> result = new ArrayList<>();
    for (TableDefinition tableDefinition : getTables(getSchemata().get(0))) {
        for (Record record : create().fetch("SHOW COLUMNS FROM {0} WHERE TYPE LIKE 'ENUM(%)'", field(name(tableDefinition.getInputName())))) {
            String table = tableDefinition.getInputName();
            String column = record.get("Field", String.class);
            String columnType = record.get("Type", String.class);
            String name = table + "_" + column;
            ColumnDefinition columnDefinition = tableDefinition.getColumn(column);
            // are explicitly forced to another type
            if (getConfiguredForcedType(columnDefinition) == null) {
                DefaultEnumDefinition definition = new DefaultEnumDefinition(getSchemata().get(0), name, "");
                for (String string : columnType.replaceAll("ENUM\\(|\\)", "").split(",")) {
                    definition.addLiteral(string.trim().replaceAll("'", ""));
                }
                result.add(definition);
            }
        }
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) TableDefinition(org.jooq.meta.TableDefinition) DefaultEnumDefinition(org.jooq.meta.DefaultEnumDefinition) Record(org.jooq.Record) DefaultEnumDefinition(org.jooq.meta.DefaultEnumDefinition) EnumDefinition(org.jooq.meta.EnumDefinition) ColumnDefinition(org.jooq.meta.ColumnDefinition)

Example 4 with DefaultEnumDefinition

use of org.jooq.meta.DefaultEnumDefinition in project jOOQ by jOOQ.

the class H2Database method getDomainEnums2_0.

private void getDomainEnums2_0(List<EnumDefinition> result) {
    // TODO: Re-generate the H2 schema and use generated code to run this query
    create().resultQuery(("" + "select object_schema, object_name, array_agg(ev.value_name order by ev.VALUE_ORDINAL)\n" + "from INFORMATION_SCHEMA.ENUM_VALUES ev\n" + "where ev.OBJECT_TYPE = 'DOMAIN'\n" + "group by object_schema, object_name\n" + "order by 1, 2\n" + "")).coerce(COLUMNS.TABLE_SCHEMA, COLUMNS.TABLE_NAME, arrayAgg(COLUMNS.COLUMN_NAME)).forEach(r -> {
        SchemaDefinition schema = getSchema(r.value1());
        if (schema == null)
            return;
        DefaultEnumDefinition definition = new DefaultEnumDefinition(schema, r.value2(), "");
        for (String string : r.value3()) definition.addLiteral(string);
        result.add(definition);
    });
}
Also used : SchemaDefinition(org.jooq.meta.SchemaDefinition) DefaultEnumDefinition(org.jooq.meta.DefaultEnumDefinition)

Example 5 with DefaultEnumDefinition

use of org.jooq.meta.DefaultEnumDefinition in project jOOQ by jOOQ.

the class H2Database method getInlineEnums2_0.

private void getInlineEnums2_0(List<EnumDefinition> result) {
    // TODO: Re-generate the H2 schema and use generated code to run this query
    create().resultQuery(("" + "select c.TABLE_SCHEMA , c.TABLE_NAME , c.COLUMN_NAME , array_agg(ev.value_name order by ev.VALUE_ORDINAL)\n" + "from INFORMATION_SCHEMA.ENUM_VALUES ev\n" + "join information_schema.columns c\n" + "on ev.OBJECT_SCHEMA = c.TABLE_SCHEMA\n" + "and ev.OBJECT_NAME = c.TABLE_NAME\n" + "and ev.ENUM_IDENTIFIER = c.DTD_IDENTIFIER\n" + "where ev.OBJECT_TYPE = 'TABLE'\n" + "and c.DOMAIN_NAME IS NULL\n" + "group by c.TABLE_SCHEMA , c.TABLE_NAME , c.COLUMN_NAME\n" + "order by 1, 2, 3\n" + "")).coerce(COLUMNS.TABLE_SCHEMA, COLUMNS.TABLE_NAME, COLUMNS.COLUMN_NAME, arrayAgg(COLUMNS.COLUMN_NAME)).forEach(r -> {
        SchemaDefinition schema = getSchema(r.value1());
        if (schema == null)
            return;
        // [#1237] Don't generate enum classes for columns in MySQL tables
        // that are excluded from code generation
        TableDefinition tableDefinition = getTable(schema, r.value2());
        if (tableDefinition != null) {
            ColumnDefinition columnDefinition = tableDefinition.getColumn(r.value3());
            if (columnDefinition != null) {
                // are explicitly forced to another type
                if (getConfiguredForcedType(columnDefinition, columnDefinition.getType()) == null) {
                    String name = r.value2() + "_" + r.value3();
                    DefaultEnumDefinition definition = new DefaultEnumDefinition(schema, name, "");
                    for (String string : r.value4()) definition.addLiteral(string);
                    result.add(definition);
                }
            }
        }
    });
}
Also used : SchemaDefinition(org.jooq.meta.SchemaDefinition) TableDefinition(org.jooq.meta.TableDefinition) DefaultEnumDefinition(org.jooq.meta.DefaultEnumDefinition) DefaultIndexColumnDefinition(org.jooq.meta.DefaultIndexColumnDefinition) ColumnDefinition(org.jooq.meta.ColumnDefinition) IndexColumnDefinition(org.jooq.meta.IndexColumnDefinition)

Aggregations

DefaultEnumDefinition (org.jooq.meta.DefaultEnumDefinition)5 SchemaDefinition (org.jooq.meta.SchemaDefinition)4 ArrayList (java.util.ArrayList)3 ColumnDefinition (org.jooq.meta.ColumnDefinition)3 EnumDefinition (org.jooq.meta.EnumDefinition)3 TableDefinition (org.jooq.meta.TableDefinition)3 DefaultIndexColumnDefinition (org.jooq.meta.DefaultIndexColumnDefinition)2 IndexColumnDefinition (org.jooq.meta.IndexColumnDefinition)2 StringReader (java.io.StringReader)1 Record (org.jooq.Record)1 CSVReader (org.jooq.tools.csv.CSVReader)1