Search in sources :

Example 1 with EnumDefinition

use of org.jooq.meta.EnumDefinition 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 EnumDefinition

use of org.jooq.meta.EnumDefinition 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 EnumDefinition

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

the class JavaGenerator method generateEnums.

protected void generateEnums(SchemaDefinition schema) {
    log.info("Generating ENUMs");
    for (EnumDefinition e : database.getEnums(schema)) {
        try {
            generateEnum(e);
        } catch (Exception ex) {
            log.error("Error while generating enum " + e, ex);
        }
    }
    watch.splitInfo("Enums generated");
}
Also used : EnumDefinition(org.jooq.meta.EnumDefinition) IOException(java.io.IOException) SQLDialectNotSupportedException(org.jooq.exception.SQLDialectNotSupportedException) ReflectException(org.jooq.tools.reflect.ReflectException)

Example 4 with EnumDefinition

use of org.jooq.meta.EnumDefinition 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)

Aggregations

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