Search in sources :

Example 1 with EnumDefinition

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

the class MySQLDatabase method getEnums0.

@Override
protected List<EnumDefinition> getEnums0() throws SQLException {
    List<EnumDefinition> result = new ArrayList<EnumDefinition>();
    Result<Record5<String, String, String, String, String>> records = create().select(Columns.TABLE_SCHEMA, Columns.COLUMN_COMMENT, Columns.TABLE_NAME, Columns.COLUMN_NAME, Columns.COLUMN_TYPE).from(COLUMNS).where(Columns.COLUMN_TYPE.like("enum(%)").and(Columns.TABLE_SCHEMA.in(getInputSchemata()))).orderBy(Columns.TABLE_SCHEMA.asc(), Columns.TABLE_NAME.asc(), Columns.COLUMN_NAME.asc()).fetch();
    for (Record record : records) {
        SchemaDefinition schema = getSchema(record.get(Columns.TABLE_SCHEMA));
        String comment = record.get(Columns.COLUMN_COMMENT);
        String table = record.get(Columns.TABLE_NAME);
        String column = record.get(Columns.COLUMN_NAME);
        String name = table + "_" + column;
        String columnType = record.get(Columns.COLUMN_TYPE);
        // [#1237] Don't generate enum classes for columns in MySQL tables
        // that are excluded from code generation
        TableDefinition tableDefinition = getTable(schema, table);
        if (tableDefinition != null) {
            ColumnDefinition columnDefinition = tableDefinition.getColumn(column);
            if (columnDefinition != null) {
                // are explicitly forced to another type
                if (getConfiguredForcedType(columnDefinition, columnDefinition.getType()) == null) {
                    DefaultEnumDefinition definition = new DefaultEnumDefinition(schema, name, comment);
                    CSVReader reader = new CSVReader(new StringReader(columnType.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.util.SchemaDefinition) CSVReader(org.jooq.tools.csv.CSVReader) ArrayList(java.util.ArrayList) DefaultEnumDefinition(org.jooq.util.DefaultEnumDefinition) EnumDefinition(org.jooq.util.EnumDefinition) DefaultEnumDefinition(org.jooq.util.DefaultEnumDefinition) ColumnDefinition(org.jooq.util.ColumnDefinition) StringReader(java.io.StringReader) TableDefinition(org.jooq.util.TableDefinition) Record(org.jooq.Record) Record5(org.jooq.Record5)

Example 2 with EnumDefinition

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

the class CUBRIDDatabase method getEnums0.

@Override
protected List<EnumDefinition> getEnums0() throws SQLException {
    List<EnumDefinition> result = new ArrayList<EnumDefinition>();
    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.util.TableDefinition) DefaultEnumDefinition(org.jooq.util.DefaultEnumDefinition) Record(org.jooq.Record) EnumDefinition(org.jooq.util.EnumDefinition) DefaultEnumDefinition(org.jooq.util.DefaultEnumDefinition) ColumnDefinition(org.jooq.util.ColumnDefinition)

Example 3 with EnumDefinition

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

the class PostgresDatabase method getEnums0.

@Override
protected List<EnumDefinition> getEnums0() throws SQLException {
    List<EnumDefinition> result = new ArrayList<EnumDefinition>();
    // [#2736] This table is unavailable in Amazon Redshift
    if (exists(PG_ENUM)) {
        // [#2707] Fetch all enum type names first, in order to be able to
        // perform enumlabel::[typname] casts in the subsequent query for
        // cross-version compatible enum literal ordering
        Result<Record2<String, String>> types = create().select(PG_NAMESPACE.NSPNAME, PG_TYPE.TYPNAME).from(PG_TYPE).join(PG_NAMESPACE).on(PG_TYPE.TYPNAMESPACE.eq(oid(PG_NAMESPACE))).where(PG_NAMESPACE.NSPNAME.in(getInputSchemata())).and(oid(PG_TYPE).in(select(PG_ENUM.ENUMTYPID).from(PG_ENUM))).orderBy(PG_NAMESPACE.NSPNAME, PG_TYPE.TYPNAME).fetch();
        for (Record2<String, String> type : types) {
            String nspname = type.get(PG_NAMESPACE.NSPNAME);
            String typname = type.get(PG_TYPE.TYPNAME);
            DefaultEnumDefinition definition = null;
            for (String label : enumLabels(nspname, typname)) {
                SchemaDefinition schema = getSchema(nspname);
                String typeName = String.valueOf(typname);
                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.util.SchemaDefinition) ArrayList(java.util.ArrayList) DefaultEnumDefinition(org.jooq.util.DefaultEnumDefinition) EnumDefinition(org.jooq.util.EnumDefinition) DefaultEnumDefinition(org.jooq.util.DefaultEnumDefinition) Record2(org.jooq.Record2)

Aggregations

ArrayList (java.util.ArrayList)3 DefaultEnumDefinition (org.jooq.util.DefaultEnumDefinition)3 EnumDefinition (org.jooq.util.EnumDefinition)3 Record (org.jooq.Record)2 ColumnDefinition (org.jooq.util.ColumnDefinition)2 SchemaDefinition (org.jooq.util.SchemaDefinition)2 TableDefinition (org.jooq.util.TableDefinition)2 StringReader (java.io.StringReader)1 Record2 (org.jooq.Record2)1 Record5 (org.jooq.Record5)1 CSVReader (org.jooq.tools.csv.CSVReader)1