Search in sources :

Example 11 with DefaultColumnDefinition

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

the class MySQLTableDefinition method getElements0.

@Override
public List<ColumnDefinition> getElements0() throws SQLException {
    List<ColumnDefinition> result = new ArrayList<>();
    for (Record record : create().select(COLUMNS.ORDINAL_POSITION, COLUMNS.COLUMN_NAME, COLUMNS.COLUMN_COMMENT, COLUMNS.COLUMN_TYPE, COLUMNS.DATA_TYPE, COLUMNS.IS_NULLABLE, COLUMNS.COLUMN_DEFAULT, COLUMNS.EXTRA, COLUMNS.GENERATION_EXPRESSION, COLUMNS.CHARACTER_MAXIMUM_LENGTH, // [#10856] Some older versions of MySQL 5.7 don't have the DATETIME_PRECISION column yet
    getDatabase().exists(COLUMNS.DATETIME_PRECISION) ? coalesce(COLUMNS.NUMERIC_PRECISION, COLUMNS.DATETIME_PRECISION).as(COLUMNS.NUMERIC_PRECISION) : COLUMNS.NUMERIC_PRECISION, COLUMNS.NUMERIC_SCALE, COLUMNS.EXTRA).from(COLUMNS).where(COLUMNS.TABLE_SCHEMA.in(getSchema().getName(), getSchema().getName())).and(COLUMNS.TABLE_NAME.equal(getName())).orderBy(COLUMNS.ORDINAL_POSITION)) {
        String dataType = record.get(COLUMNS.DATA_TYPE);
        // [#519] Some types have unsigned versions
        boolean unsigned = getDatabase().supportsUnsignedTypes();
        // [#7719]
        boolean displayWidths = getDatabase().integerDisplayWidths();
        // [#6492] MariaDB supports a standard IS_GENERATED, but MySQL doesn't (yet)
        boolean generated = record.get(COLUMNS.EXTRA) != null && record.get(COLUMNS.EXTRA).toUpperCase().contains("GENERATED");
        GenerationOption generationOption = "VIRTUAL GENERATED".equalsIgnoreCase(record.get(COLUMNS.EXTRA)) ? GenerationOption.VIRTUAL : "STORED GENERATED".equalsIgnoreCase(record.get(COLUMNS.EXTRA)) ? GenerationOption.STORED : null;
        columnTypeFix: if (unsigned || displayWidths) {
            if (asList("tinyint", "smallint", "mediumint", "int", "bigint").contains(dataType.toLowerCase())) {
                String columnType = record.get(COLUMNS.COLUMN_TYPE).toLowerCase();
                Matcher matcher = COLUMN_TYPE.matcher(columnType);
                if (matcher.find()) {
                    String mType = matcher.group(1);
                    String mPrecision = matcher.group(2);
                    String mUnsigned = matcher.group(3);
                    dataType = mType + (unsigned && mUnsigned != null ? mUnsigned : "") + (displayWidths && mPrecision != null ? mPrecision : "");
                }
            }
        }
        DataTypeDefinition type = new DefaultDataTypeDefinition(getDatabase(), getSchema(), dataType, record.get(COLUMNS.CHARACTER_MAXIMUM_LENGTH), record.get(COLUMNS.NUMERIC_PRECISION), record.get(COLUMNS.NUMERIC_SCALE), record.get(COLUMNS.IS_NULLABLE, boolean.class), generated ? null : record.get(COLUMNS.COLUMN_DEFAULT), name(getSchema().getName(), getName() + "_" + record.get(COLUMNS.COLUMN_NAME))).generatedAlwaysAs(generated ? record.get(COLUMNS.GENERATION_EXPRESSION) : null).generationOption(generationOption);
        result.add(new DefaultColumnDefinition(getDatabase().getTable(getSchema(), getName()), record.get(COLUMNS.COLUMN_NAME), result.size() + 1, type, "auto_increment".equalsIgnoreCase(record.get(COLUMNS.EXTRA)), record.get(COLUMNS.COLUMN_COMMENT)));
    }
    return result;
}
Also used : GenerationOption(org.jooq.impl.QOM.GenerationOption) Matcher(java.util.regex.Matcher) DefaultDataTypeDefinition(org.jooq.meta.DefaultDataTypeDefinition) DefaultColumnDefinition(org.jooq.meta.DefaultColumnDefinition) ArrayList(java.util.ArrayList) Record(org.jooq.Record) DefaultDataTypeDefinition(org.jooq.meta.DefaultDataTypeDefinition) DataTypeDefinition(org.jooq.meta.DataTypeDefinition) DefaultColumnDefinition(org.jooq.meta.DefaultColumnDefinition) ColumnDefinition(org.jooq.meta.ColumnDefinition)

Example 12 with DefaultColumnDefinition

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

the class FirebirdTableDefinition method getElements0.

@Override
protected List<ColumnDefinition> getElements0() throws SQLException {
    List<ColumnDefinition> result = new ArrayList<>();
    Rdb$relationFields r = RDB$RELATION_FIELDS.as("r");
    Rdb$fields f = RDB$FIELDS.as("f");
    // DatabaseMetaData implementation
    for (Record record : create().select(trim(r.RDB$FIELD_NAME).as(r.RDB$FIELD_NAME), r.RDB$DESCRIPTION, r.RDB$DEFAULT_VALUE, DSL.bitOr(r.RDB$NULL_FLAG.nvl(inline((short) 0)), f.RDB$NULL_FLAG.nvl(inline((short) 0))).as(r.RDB$NULL_FLAG), r.RDB$DEFAULT_SOURCE, f.RDB$COMPUTED_SOURCE, r.RDB$FIELD_POSITION, // [#3342] FIELD_LENGTH should be ignored for LOBs
    CHARACTER_LENGTH(f).as("CHAR_LEN"), f.RDB$FIELD_PRECISION, FIELD_SCALE(f).as("FIELD_SCALE"), FIELD_TYPE(f).as("FIELD_TYPE"), trim(f.RDB$FIELD_NAME).as("DOMAIN_NAME"), r.RDB$DESCRIPTION, (((FirebirdDatabase) getDatabase()).is30() ? r.RDB$IDENTITY_TYPE : inline((short) 0)).as(r.RDB$IDENTITY_TYPE)).from(r).leftOuterJoin(f).on(r.RDB$FIELD_SOURCE.eq(f.RDB$FIELD_NAME)).where(r.RDB$RELATION_NAME.eq(getName())).orderBy(r.RDB$FIELD_POSITION)) {
        // [#9411] Firebird reports the DEFAULT keyword in this column, which
        // we do not want to reproduce in generated code.
        String defaultValue = record.get(r.RDB$DEFAULT_SOURCE);
        boolean computed = record.get(f.RDB$COMPUTED_SOURCE) != null;
        if (defaultValue != null)
            defaultValue = P_DEFAULT.matcher(defaultValue).replaceFirst("");
        DefaultDataTypeDefinition type = new DefaultDataTypeDefinition(getDatabase(), getSchema(), record.get("FIELD_TYPE", String.class), record.get("CHAR_LEN", short.class), record.get(f.RDB$FIELD_PRECISION), record.get("FIELD_SCALE", Integer.class), record.get(r.RDB$NULL_FLAG) == 0, computed ? null : defaultValue, record.get("DOMAIN_NAME") == null ? null : DSL.name(record.get("DOMAIN_NAME", String.class))).generatedAlwaysAs(computed ? record.get(f.RDB$COMPUTED_SOURCE) : null);
        result.add(new DefaultColumnDefinition(getDatabase().getTable(getSchema(), getName()), record.get(r.RDB$FIELD_NAME), result.size() + 1, type, record.get(r.RDB$IDENTITY_TYPE, boolean.class), record.get(r.RDB$DESCRIPTION)));
    }
    return result;
}
Also used : Rdb$relationFields(org.jooq.meta.firebird.rdb.tables.Rdb$relationFields) DefaultDataTypeDefinition(org.jooq.meta.DefaultDataTypeDefinition) DefaultColumnDefinition(org.jooq.meta.DefaultColumnDefinition) Rdb$fields(org.jooq.meta.firebird.rdb.tables.Rdb$fields) ArrayList(java.util.ArrayList) Record(org.jooq.Record) DefaultColumnDefinition(org.jooq.meta.DefaultColumnDefinition) ColumnDefinition(org.jooq.meta.ColumnDefinition)

Example 13 with DefaultColumnDefinition

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

the class DerbyTableDefinition method getElements0.

@Override
public List<ColumnDefinition> getElements0() throws SQLException {
    List<ColumnDefinition> result = new ArrayList<>();
    for (Record record : create().select(SYSCOLUMNS.COLUMNNAME, SYSCOLUMNS.COLUMNNUMBER, SYSCOLUMNS.COLUMNDATATYPE, when(SYSCOLUMNS.AUTOINCREMENTINC.isNull(), SYSCOLUMNS.COLUMNDEFAULT).as(SYSCOLUMNS.COLUMNDEFAULT), SYSCOLUMNS.AUTOINCREMENTINC).from(SYSCOLUMNS).where(SYSCOLUMNS.REFERENCEID.cast(VARCHAR(32672)).equal(inline(tableid))).orderBy(SYSCOLUMNS.COLUMNNUMBER)) {
        String columnDataType = record.get(SYSCOLUMNS.COLUMNDATATYPE, String.class);
        String typeName = parseTypeName(columnDataType);
        // [#9945] Derby timestamps always have a precision of 9
        Number precision = "TIMESTAMP".equalsIgnoreCase(typeName) ? 9 : parsePrecision(columnDataType);
        Number scale = parseScale(columnDataType);
        String defaultValue = record.get(SYSCOLUMNS.COLUMNDEFAULT);
        boolean generated = defaultValue != null && defaultValue.toUpperCase().startsWith("GENERATED ALWAYS AS");
        DataTypeDefinition type = new DefaultDataTypeDefinition(getDatabase(), getSchema(), typeName, precision, precision, scale, !parseNotNull(columnDataType), generated ? null : defaultValue).generatedAlwaysAs(generated ? defaultValue.replaceAll("(?i:GENERATED\\s+ALWAYS\\s+AS\\s*\\(\\s*(.*?)\\s*\\)\\s*)", "$1") : null);
        result.add(new DefaultColumnDefinition(getDatabase().getTable(getSchema(), getName()), record.get(SYSCOLUMNS.COLUMNNAME), result.size() + 1, type, null != record.get(SYSCOLUMNS.AUTOINCREMENTINC), null));
    }
    return result;
}
Also used : DefaultDataTypeDefinition(org.jooq.meta.DefaultDataTypeDefinition) DefaultColumnDefinition(org.jooq.meta.DefaultColumnDefinition) ArrayList(java.util.ArrayList) Record(org.jooq.Record) DefaultDataTypeDefinition(org.jooq.meta.DefaultDataTypeDefinition) DataTypeDefinition(org.jooq.meta.DataTypeDefinition) DefaultColumnDefinition(org.jooq.meta.DefaultColumnDefinition) ColumnDefinition(org.jooq.meta.ColumnDefinition)

Example 14 with DefaultColumnDefinition

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

the class H2TableDefinition method getElements2_0.

public List<ColumnDefinition> getElements2_0() {
    List<ColumnDefinition> result = new ArrayList<>();
    H2Database db = (H2Database) getDatabase();
    for (Record record : create().select(COLUMNS.COLUMN_NAME, COLUMNS.ORDINAL_POSITION, // [#2230] [#11733] Translate INTERVAL_TYPE to supported types
    nvl(concat(ELEMENT_TYPES.DATA_TYPE, inline(" ARRAY")), when(COLUMNS.INTERVAL_TYPE.like(any(inline("%YEAR%"), inline("%MONTH%"))), inline("INTERVAL YEAR TO MONTH")).when(COLUMNS.INTERVAL_TYPE.like(any(inline("%DAY%"), inline("%HOUR%"), inline("%MINUTE%"), inline("%SECOND%"))), inline("INTERVAL DAY TO SECOND")).else_(Tables.COLUMNS.DATA_TYPE)).as(COLUMNS.TYPE_NAME), nvl(ELEMENT_TYPES.CHARACTER_MAXIMUM_LENGTH, COLUMNS.CHARACTER_MAXIMUM_LENGTH).as(COLUMNS.CHARACTER_MAXIMUM_LENGTH), coalesce(ELEMENT_TYPES.DATETIME_PRECISION, ELEMENT_TYPES.NUMERIC_PRECISION, COLUMNS.DATETIME_PRECISION, COLUMNS.NUMERIC_PRECISION).as(COLUMNS.NUMERIC_PRECISION), nvl(ELEMENT_TYPES.NUMERIC_SCALE, COLUMNS.NUMERIC_SCALE).as(COLUMNS.NUMERIC_SCALE), COLUMNS.IS_NULLABLE, field(Tables.COLUMNS.IS_GENERATED.eq(inline("ALWAYS"))).as(COLUMNS.IS_COMPUTED), Tables.COLUMNS.GENERATION_EXPRESSION, COLUMNS.COLUMN_DEFAULT, COLUMNS.REMARKS, field(Tables.COLUMNS.IS_IDENTITY.eq(inline("YES"))).as(Tables.COLUMNS.IS_IDENTITY), COLUMNS.DOMAIN_SCHEMA, COLUMNS.DOMAIN_NAME).from(COLUMNS).leftJoin(ELEMENT_TYPES).on(Tables.COLUMNS.TABLE_SCHEMA.equal(ELEMENT_TYPES.OBJECT_SCHEMA)).and(Tables.COLUMNS.TABLE_NAME.equal(ELEMENT_TYPES.OBJECT_NAME)).and(Tables.COLUMNS.DTD_IDENTIFIER.equal(ELEMENT_TYPES.COLLECTION_TYPE_IDENTIFIER)).where(COLUMNS.TABLE_SCHEMA.equal(getSchema().getName())).and(COLUMNS.TABLE_NAME.equal(getName())).and(!getDatabase().getIncludeInvisibleColumns() ? condition(COLUMNS.IS_VISIBLE.coerce(BOOLEAN)) : noCondition()).orderBy(COLUMNS.ORDINAL_POSITION)) {
        // [#5331] AUTO_INCREMENT (MySQL style)
        // [#5331] DEFAULT nextval('sequence') (PostgreSQL style)
        // [#6332] [#6339] system-generated defaults shouldn't produce a default clause
        boolean isIdentity = record.get(Tables.COLUMNS.IS_IDENTITY, boolean.class) || defaultString(record.get(COLUMNS.COLUMN_DEFAULT)).trim().toLowerCase().startsWith("nextval");
        boolean isComputed = record.get(COLUMNS.IS_COMPUTED, boolean.class);
        // [#681] Domain name if available
        Name userType = record.get(COLUMNS.DOMAIN_NAME) != null ? name(record.get(COLUMNS.DOMAIN_SCHEMA), record.get(COLUMNS.DOMAIN_NAME)) : name(getSchema().getName(), getName() + "_" + record.get(COLUMNS.COLUMN_NAME));
        SchemaDefinition typeSchema = record.get(COLUMNS.DOMAIN_NAME) != null ? getDatabase().getSchema(record.get(COLUMNS.DOMAIN_SCHEMA)) : getSchema();
        DataTypeDefinition type = new DefaultDataTypeDefinition(getDatabase(), typeSchema == null ? getSchema() : typeSchema, record.get(COLUMNS.TYPE_NAME), record.get(COLUMNS.CHARACTER_MAXIMUM_LENGTH), record.get(COLUMNS.NUMERIC_PRECISION), record.get(COLUMNS.NUMERIC_SCALE), record.get(COLUMNS.IS_NULLABLE, boolean.class), isIdentity || isComputed ? null : record.get(COLUMNS.COLUMN_DEFAULT), userType).generatedAlwaysAs(isComputed ? record.get(Tables.COLUMNS.GENERATION_EXPRESSION) : null);
        result.add(new DefaultColumnDefinition(getDatabase().getTable(getSchema(), getName()), record.get(COLUMNS.COLUMN_NAME), result.size() + 1, type, isIdentity, record.get(COLUMNS.REMARKS)));
    }
    return result;
}
Also used : SchemaDefinition(org.jooq.meta.SchemaDefinition) DefaultDataTypeDefinition(org.jooq.meta.DefaultDataTypeDefinition) DefaultColumnDefinition(org.jooq.meta.DefaultColumnDefinition) ArrayList(java.util.ArrayList) Record(org.jooq.Record) DefaultDataTypeDefinition(org.jooq.meta.DefaultDataTypeDefinition) DataTypeDefinition(org.jooq.meta.DataTypeDefinition) DefaultColumnDefinition(org.jooq.meta.DefaultColumnDefinition) ColumnDefinition(org.jooq.meta.ColumnDefinition) Name(org.jooq.Name)

Aggregations

ArrayList (java.util.ArrayList)14 ColumnDefinition (org.jooq.meta.ColumnDefinition)14 DefaultColumnDefinition (org.jooq.meta.DefaultColumnDefinition)14 DefaultDataTypeDefinition (org.jooq.meta.DefaultDataTypeDefinition)14 Record (org.jooq.Record)12 DataTypeDefinition (org.jooq.meta.DataTypeDefinition)11 SchemaDefinition (org.jooq.meta.SchemaDefinition)5 Name (org.jooq.Name)2 Rdb$fields (org.jooq.meta.firebird.rdb.tables.Rdb$fields)2 Columns (org.jooq.meta.postgres.information_schema.tables.Columns)2 PgNamespace (org.jooq.meta.postgres.pg_catalog.tables.PgNamespace)2 PgType (org.jooq.meta.postgres.pg_catalog.tables.PgType)2 StringUtils.defaultString (org.jooq.tools.StringUtils.defaultString)2 Matcher (java.util.regex.Matcher)1 Condition (org.jooq.Condition)1 Field (org.jooq.Field)1 GenerationOption (org.jooq.impl.QOM.GenerationOption)1 Rdb$procedureParameters (org.jooq.meta.firebird.rdb.tables.Rdb$procedureParameters)1 Rdb$relationFields (org.jooq.meta.firebird.rdb.tables.Rdb$relationFields)1 Parameters (org.jooq.meta.postgres.information_schema.tables.Parameters)1