Search in sources :

Example 1 with ColumnDefinition

use of org.jooq.util.ColumnDefinition 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 2 with ColumnDefinition

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

the class CUBRIDTableDefinition method getElements0.

@Override
public List<ColumnDefinition> getElements0() throws SQLException {
    List<ColumnDefinition> result = new ArrayList<ColumnDefinition>();
    for (Record record : create().select(DB_ATTRIBUTE.ATTR_NAME, DB_ATTRIBUTE.DEF_ORDER, DB_ATTRIBUTE.DATA_TYPE, DB_ATTRIBUTE.PREC, DB_ATTRIBUTE.SCALE, DB_ATTRIBUTE.IS_NULLABLE, DB_ATTRIBUTE.DEFAULT_VALUE, DB_SERIAL.NAME).from(DB_ATTRIBUTE).leftOuterJoin(DB_SERIAL).on(DB_ATTRIBUTE.ATTR_NAME.equal(DB_SERIAL.ATT_NAME).and(DB_ATTRIBUTE.CLASS_NAME.equal(DB_SERIAL.CLASS_NAME))).where(DB_ATTRIBUTE.CLASS_NAME.equal(getName())).orderBy(DB_ATTRIBUTE.DEF_ORDER).fetch()) {
        String dataType = record.get(DB_ATTRIBUTE.DATA_TYPE);
        DataTypeDefinition type = new DefaultDataTypeDefinition(getDatabase(), getSchema(), dataType, record.get(DB_ATTRIBUTE.PREC), record.get(DB_ATTRIBUTE.PREC), record.get(DB_ATTRIBUTE.SCALE), record.get(DB_ATTRIBUTE.IS_NULLABLE, boolean.class), record.get(DB_ATTRIBUTE.DEFAULT_VALUE), getName() + "_" + record.get(DB_ATTRIBUTE.ATTR_NAME));
        ColumnDefinition column = new DefaultColumnDefinition(getDatabase().getTable(getSchema(), getName()), record.get(DB_ATTRIBUTE.ATTR_NAME), record.get(DB_ATTRIBUTE.DEF_ORDER), type, record.get(DB_SERIAL.NAME) != null, null);
        result.add(column);
    }
    return result;
}
Also used : DefaultDataTypeDefinition(org.jooq.util.DefaultDataTypeDefinition) DefaultColumnDefinition(org.jooq.util.DefaultColumnDefinition) ArrayList(java.util.ArrayList) Record(org.jooq.Record) DataTypeDefinition(org.jooq.util.DataTypeDefinition) DefaultDataTypeDefinition(org.jooq.util.DefaultDataTypeDefinition) ColumnDefinition(org.jooq.util.ColumnDefinition) DefaultColumnDefinition(org.jooq.util.DefaultColumnDefinition)

Example 3 with ColumnDefinition

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

the class FirebirdDatabase method loadForeignKeys.

@Override
protected void loadForeignKeys(DefaultRelations relations) throws SQLException {
    Rdb$relationConstraints pk = RDB$RELATION_CONSTRAINTS.as("pk");
    Rdb$relationConstraints fk = RDB$RELATION_CONSTRAINTS.as("fk");
    Rdb$refConstraints rc = RDB$REF_CONSTRAINTS.as("rc");
    Rdb$indexSegments isp = RDB$INDEX_SEGMENTS.as("isp");
    Rdb$indexSegments isf = RDB$INDEX_SEGMENTS.as("isf");
    for (Record record : create().selectDistinct(fk.RDB$CONSTRAINT_NAME.trim().as("fk"), fk.RDB$RELATION_NAME.trim().as("fkTable"), isf.RDB$FIELD_NAME.trim().as("fkField"), pk.RDB$CONSTRAINT_NAME.trim().as("pk"), pk.RDB$RELATION_NAME.trim().as("pkTable")).from(fk).join(rc).on(fk.RDB$CONSTRAINT_NAME.eq(rc.RDB$CONSTRAINT_NAME)).join(pk).on(pk.RDB$CONSTRAINT_NAME.eq(rc.RDB$CONST_NAME_UQ)).join(isp).on(isp.RDB$INDEX_NAME.eq(pk.RDB$INDEX_NAME)).join(isf).on(isf.RDB$INDEX_NAME.eq(fk.RDB$INDEX_NAME)).where(isp.RDB$FIELD_POSITION.eq(isf.RDB$FIELD_POSITION)).orderBy(fk.RDB$CONSTRAINT_NAME.asc(), isf.RDB$FIELD_POSITION.asc()).fetch()) {
        String pkName = record.get("pk", String.class);
        String pkTable = record.get("pkTable", String.class);
        String fkName = record.get("fk", String.class);
        String fkTable = record.get("fkTable", String.class);
        String fkField = record.get("fkField", String.class);
        TableDefinition tdReferencing = getTable(getSchemata().get(0), fkTable, true);
        TableDefinition tdReferenced = getTable(getSchemata().get(0), pkTable, true);
        if (tdReferenced != null && tdReferencing != null) {
            ColumnDefinition referencingColumn = tdReferencing.getColumn(fkField);
            relations.addForeignKey(fkName, pkName, referencingColumn, getSchemata().get(0));
        }
    }
}
Also used : TableDefinition(org.jooq.util.TableDefinition) Record(org.jooq.Record) Rdb$relationConstraints(org.jooq.util.firebird.rdb.tables.Rdb$relationConstraints) Rdb$indexSegments(org.jooq.util.firebird.rdb.tables.Rdb$indexSegments) Rdb$refConstraints(org.jooq.util.firebird.rdb.tables.Rdb$refConstraints) ColumnDefinition(org.jooq.util.ColumnDefinition)

Example 4 with ColumnDefinition

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

the class FirebirdDatabase method loadPrimaryKeys.

@Override
protected void loadPrimaryKeys(DefaultRelations r) throws SQLException {
    for (Record record : fetchKeys("PRIMARY KEY")) {
        String tableName = record.get(RDB$RELATION_CONSTRAINTS.RDB$RELATION_NAME.trim());
        String fieldName = record.get(RDB$INDEX_SEGMENTS.RDB$FIELD_NAME.trim());
        String key = record.get(RDB$RELATION_CONSTRAINTS.RDB$CONSTRAINT_NAME.trim());
        TableDefinition td = getTable(this.getSchemata().get(0), tableName);
        if (td != null) {
            ColumnDefinition cd = td.getColumn(fieldName);
            r.addPrimaryKey(key, cd);
        }
    }
}
Also used : TableDefinition(org.jooq.util.TableDefinition) Record(org.jooq.Record) ColumnDefinition(org.jooq.util.ColumnDefinition)

Example 5 with ColumnDefinition

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

the class SQLiteDatabase method loadPrimaryKeys.

@Override
protected void loadPrimaryKeys(DefaultRelations relations) throws SQLException {
    for (String tableName : create().select(SQLiteMaster.NAME).from(SQLITE_MASTER).where(SQLiteMaster.TYPE.in("table")).orderBy(SQLiteMaster.NAME).fetch(SQLiteMaster.NAME)) {
        for (Record record : create().fetch("pragma table_info('" + tableName + "')")) {
            if (record.get("pk", int.class) > 0) {
                String columnName = record.get("name", String.class);
                // Generate a primary key name
                String key = "pk_" + tableName;
                TableDefinition table = getTable(getSchemata().get(0), tableName);
                if (table != null) {
                    ColumnDefinition column = table.getColumn(columnName);
                    relations.addPrimaryKey(key, column);
                }
            }
        }
    }
}
Also used : TableDefinition(org.jooq.util.TableDefinition) Record(org.jooq.Record) ColumnDefinition(org.jooq.util.ColumnDefinition)

Aggregations

ColumnDefinition (org.jooq.util.ColumnDefinition)27 Record (org.jooq.Record)23 ArrayList (java.util.ArrayList)15 TableDefinition (org.jooq.util.TableDefinition)14 DefaultColumnDefinition (org.jooq.util.DefaultColumnDefinition)13 DefaultDataTypeDefinition (org.jooq.util.DefaultDataTypeDefinition)13 SchemaDefinition (org.jooq.util.SchemaDefinition)12 DataTypeDefinition (org.jooq.util.DataTypeDefinition)10 StringUtils.defaultString (org.jooq.tools.StringUtils.defaultString)3 DefaultEnumDefinition (org.jooq.util.DefaultEnumDefinition)2 EnumDefinition (org.jooq.util.EnumDefinition)2 Rdb$fields (org.jooq.util.firebird.rdb.tables.Rdb$fields)2 Columns (org.jooq.util.postgres.information_schema.tables.Columns)2 PgNamespace (org.jooq.util.postgres.pg_catalog.tables.PgNamespace)2 StringReader (java.io.StringReader)1 HashMap (java.util.HashMap)1 Record5 (org.jooq.Record5)1 Schema (org.jooq.Schema)1 CSVReader (org.jooq.tools.csv.CSVReader)1 Rdb$indexSegments (org.jooq.util.firebird.rdb.tables.Rdb$indexSegments)1