Search in sources :

Example 11 with ColumnDefinition

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

the class PostgresTableDefinition method getElements0.

@Override
public List<ColumnDefinition> getElements0() throws SQLException {
    List<ColumnDefinition> result = new ArrayList<>();
    PostgresDatabase database = (PostgresDatabase) getDatabase();
    Field<String> dataType = 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")).when(COLUMNS.DATA_TYPE.eq(inline("USER-DEFINED")).and(COLUMNS.UDT_NAME.eq(inline("geometry"))), inline("geometry")).else_(COLUMNS.DATA_TYPE);
    Field<String> udtSchema = COLUMNS.UDT_SCHEMA;
    Field<Integer> precision = nvl(COLUMNS.DATETIME_PRECISION, COLUMNS.NUMERIC_PRECISION);
    Field<String> serialColumnDefault = inline("nextval('%_seq'::regclass)");
    Field<String> generationExpression = COLUMNS.GENERATION_EXPRESSION;
    Field<String> attgenerated = database.is12() ? PG_ATTRIBUTE.ATTGENERATED : inline("s");
    Condition isSerial = lower(COLUMNS.COLUMN_DEFAULT).likeIgnoreCase(serialColumnDefault);
    Condition isIdentity10 = COLUMNS.IS_IDENTITY.eq(inline("YES"));
    // [#9200] only use COLUMN_DEFAULT for ColumnDefinition#isIdentity() if
    // table has no column with IS_IDENTITY = 'YES'
    Condition isIdentity = database.is10() ? isIdentity10.or(count().filterWhere(isIdentity10).over().eq(inline(0)).and(isSerial)) : isSerial;
    for (Record record : create().select(COLUMNS.COLUMN_NAME, COLUMNS.ORDINAL_POSITION, dataType.as(COLUMNS.DATA_TYPE), // [#8067] [#11658] A more robust / sophisticated decoding might be available
    nvl(COLUMNS.CHARACTER_MAXIMUM_LENGTH, when(COLUMNS.UDT_NAME.in(inline("_varchar"), inline("_bpchar"), inline("_char")), PG_ATTRIBUTE.ATTTYPMOD.sub(inline(4)))).as(COLUMNS.CHARACTER_MAXIMUM_LENGTH), precision.as(COLUMNS.NUMERIC_PRECISION), COLUMNS.NUMERIC_SCALE, (when(isIdentity, inline("YES"))).as(COLUMNS.IS_IDENTITY), COLUMNS.IS_NULLABLE, generationExpression.as(COLUMNS.GENERATION_EXPRESSION), attgenerated.as(PG_ATTRIBUTE.ATTGENERATED), (when(isIdentity, inline(null, String.class)).else_(COLUMNS.COLUMN_DEFAULT)).as(COLUMNS.COLUMN_DEFAULT), coalesce(COLUMNS.DOMAIN_SCHEMA, udtSchema).as(COLUMNS.UDT_SCHEMA), coalesce(COLUMNS.DOMAIN_NAME, COLUMNS.UDT_NAME).as(COLUMNS.UDT_NAME), PG_DESCRIPTION.DESCRIPTION).from(COLUMNS).join(PG_NAMESPACE).on(COLUMNS.TABLE_SCHEMA.eq(PG_NAMESPACE.NSPNAME)).join(PG_CLASS).on(PG_CLASS.RELNAME.eq(COLUMNS.TABLE_NAME)).and(PG_CLASS.RELNAMESPACE.eq(PG_NAMESPACE.OID)).join(PG_ATTRIBUTE).on(PG_ATTRIBUTE.ATTRELID.eq(PG_CLASS.OID)).and(PG_ATTRIBUTE.ATTNAME.eq(COLUMNS.COLUMN_NAME)).leftJoin(PG_DESCRIPTION).on(PG_DESCRIPTION.OBJOID.eq(PG_CLASS.OID)).and(PG_DESCRIPTION.OBJSUBID.eq(COLUMNS.ORDINAL_POSITION)).where(COLUMNS.TABLE_SCHEMA.equal(getSchema().getName())).and(COLUMNS.TABLE_NAME.equal(getName())).orderBy(COLUMNS.ORDINAL_POSITION)) {
        SchemaDefinition typeSchema = null;
        String schemaName = record.get(COLUMNS.UDT_SCHEMA);
        if (schemaName != null)
            typeSchema = getDatabase().getSchema(schemaName);
        DataTypeDefinition type = new DefaultDataTypeDefinition(getDatabase(), typeSchema, record.get(COLUMNS.DATA_TYPE), record.get(COLUMNS.CHARACTER_MAXIMUM_LENGTH), record.get(COLUMNS.NUMERIC_PRECISION), record.get(COLUMNS.NUMERIC_SCALE), record.get(COLUMNS.IS_NULLABLE, boolean.class), record.get(COLUMNS.COLUMN_DEFAULT), name(record.get(COLUMNS.UDT_SCHEMA), record.get(COLUMNS.UDT_NAME))).generatedAlwaysAs(record.get(COLUMNS.GENERATION_EXPRESSION)).generationOption("s".equals(record.get(PG_ATTRIBUTE.ATTGENERATED)) ? GenerationOption.STORED : "v".equals(record.get(PG_ATTRIBUTE.ATTGENERATED)) ? GenerationOption.VIRTUAL : null);
        ColumnDefinition column = new DefaultColumnDefinition(getDatabase().getTable(getSchema(), getName()), record.get(COLUMNS.COLUMN_NAME), record.get(COLUMNS.ORDINAL_POSITION, int.class), type, record.get(COLUMNS.IS_IDENTITY, boolean.class), record.get(PG_DESCRIPTION.DESCRIPTION));
        result.add(column);
    }
    return result;
}
Also used : Condition(org.jooq.Condition) SchemaDefinition(org.jooq.meta.SchemaDefinition) DefaultDataTypeDefinition(org.jooq.meta.DefaultDataTypeDefinition) ArrayList(java.util.ArrayList) DefaultDataTypeDefinition(org.jooq.meta.DefaultDataTypeDefinition) DataTypeDefinition(org.jooq.meta.DataTypeDefinition) DefaultColumnDefinition(org.jooq.meta.DefaultColumnDefinition) ColumnDefinition(org.jooq.meta.ColumnDefinition) DefaultColumnDefinition(org.jooq.meta.DefaultColumnDefinition) Record(org.jooq.Record)

Example 12 with ColumnDefinition

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

the class PostgresTableValuedFunction method getElements0.

@Override
public List<ColumnDefinition> getElements0() throws SQLException {
    List<ColumnDefinition> result = new ArrayList<>();
    Routines r = ROUTINES;
    Parameters p = PARAMETERS;
    PgNamespace pg_n = PG_NAMESPACE;
    PgProc pg_p = PG_PROC;
    Columns c = COLUMNS;
    PgType pg_t = PG_TYPE;
    Field<Integer> pPrecision = pNumericPrecision(p);
    Field<Integer> cPrecision = nvl(c.DATETIME_PRECISION, c.NUMERIC_PRECISION);
    Field<Integer> rPrecision = nvl(r.DATETIME_PRECISION, r.NUMERIC_PRECISION);
    for (Record record : create().select(p.PARAMETER_NAME, rowNumber().over(partitionBy(p.SPECIFIC_NAME).orderBy(p.ORDINAL_POSITION)).as(p.ORDINAL_POSITION), p.DATA_TYPE, p.CHARACTER_MAXIMUM_LENGTH, pPrecision.as(p.NUMERIC_PRECISION), p.NUMERIC_SCALE, inline("true").as(c.IS_NULLABLE), (((PostgresDatabase) getDatabase()).is94() ? PARAMETERS.PARAMETER_DEFAULT : inline((String) null)).as(c.COLUMN_DEFAULT), p.UDT_SCHEMA, p.UDT_NAME).from(r).join(p).on(row(r.SPECIFIC_CATALOG, r.SPECIFIC_SCHEMA, r.SPECIFIC_NAME).eq(p.SPECIFIC_CATALOG, p.SPECIFIC_SCHEMA, p.SPECIFIC_NAME)).join(pg_n).on(r.SPECIFIC_SCHEMA.eq(pg_n.NSPNAME)).join(pg_p).on(pg_p.PRONAMESPACE.eq(pg_n.OID)).and(pg_p.PRONAME.concat("_").concat(pg_p.OID).eq(r.SPECIFIC_NAME)).where(r.SPECIFIC_NAME.eq(specificName)).and(p.PARAMETER_MODE.ne("IN")).and(pg_p.PRORETSET).unionAll(// from INFORMATION_SCHEMA.TABLES
    select(coalesce(c.COLUMN_NAME, getName()).as(c.COLUMN_NAME), coalesce(c.ORDINAL_POSITION, inline(1)).as(c.ORDINAL_POSITION), coalesce(c.DATA_TYPE, r.DATA_TYPE).as(c.DATA_TYPE), coalesce(c.CHARACTER_MAXIMUM_LENGTH, r.CHARACTER_MAXIMUM_LENGTH).as(c.CHARACTER_MAXIMUM_LENGTH), coalesce(cPrecision, rPrecision).as(c.NUMERIC_PRECISION), coalesce(c.NUMERIC_SCALE, r.NUMERIC_SCALE).as(c.NUMERIC_SCALE), coalesce(c.IS_NULLABLE, "true").as(c.IS_NULLABLE), coalesce(c.COLUMN_DEFAULT, inline((String) null)).as(c.COLUMN_DEFAULT), coalesce(c.UDT_SCHEMA, inline((String) null)).as(c.UDT_SCHEMA), coalesce(c.UDT_NAME, r.UDT_NAME, field(select(pg_t.TYPNAME).from(pg_t).where(pg_t.OID.eq(pg_p.PRORETTYPE)))).as(c.UDT_NAME)).from(r).leftOuterJoin(c).on(row(r.TYPE_UDT_CATALOG, r.TYPE_UDT_SCHEMA, r.TYPE_UDT_NAME).eq(c.TABLE_CATALOG, c.TABLE_SCHEMA, c.TABLE_NAME)).join(pg_n).on(r.SPECIFIC_SCHEMA.eq(pg_n.NSPNAME)).join(pg_p).on(pg_p.PRONAMESPACE.eq(pg_n.OID)).and(pg_p.PRONAME.concat("_").concat(pg_p.OID).eq(r.SPECIFIC_NAME)).where(r.SPECIFIC_NAME.eq(specificName)).and(row(r.SPECIFIC_CATALOG, r.SPECIFIC_SCHEMA, r.SPECIFIC_NAME).notIn(select(p.SPECIFIC_CATALOG, p.SPECIFIC_SCHEMA, p.SPECIFIC_NAME).from(p).where(p.PARAMETER_MODE.eq("OUT")))).and(pg_p.PRORETSET)).orderBy(2)) {
        SchemaDefinition typeSchema = null;
        String schemaName = record.get(p.UDT_SCHEMA);
        if (schemaName != null)
            typeSchema = getDatabase().getSchema(schemaName);
        DataTypeDefinition type = new DefaultDataTypeDefinition(getDatabase(), typeSchema, record.get(p.DATA_TYPE), record.get(p.CHARACTER_MAXIMUM_LENGTH), record.get(p.NUMERIC_PRECISION), record.get(p.NUMERIC_SCALE), record.get(c.IS_NULLABLE, boolean.class), record.get(c.COLUMN_DEFAULT), name(record.get(p.UDT_SCHEMA), record.get(p.UDT_NAME)));
        result.add(new DefaultColumnDefinition(getDatabase().getTable(getSchema(), getName()), record.get(p.PARAMETER_NAME), result.size() + 1, type, defaultString(record.get(c.COLUMN_DEFAULT)).startsWith("nextval"), null));
    }
    return result;
}
Also used : Routines(org.jooq.meta.postgres.information_schema.tables.Routines) SchemaDefinition(org.jooq.meta.SchemaDefinition) Parameters(org.jooq.meta.postgres.information_schema.tables.Parameters) PgType(org.jooq.meta.postgres.pg_catalog.tables.PgType) DefaultDataTypeDefinition(org.jooq.meta.DefaultDataTypeDefinition) ArrayList(java.util.ArrayList) Columns(org.jooq.meta.postgres.information_schema.tables.Columns) StringUtils.defaultString(org.jooq.tools.StringUtils.defaultString) DefaultDataTypeDefinition(org.jooq.meta.DefaultDataTypeDefinition) DataTypeDefinition(org.jooq.meta.DataTypeDefinition) DefaultColumnDefinition(org.jooq.meta.DefaultColumnDefinition) ColumnDefinition(org.jooq.meta.ColumnDefinition) DefaultColumnDefinition(org.jooq.meta.DefaultColumnDefinition) PgProc(org.jooq.meta.postgres.pg_catalog.tables.PgProc) Record(org.jooq.Record) PgNamespace(org.jooq.meta.postgres.pg_catalog.tables.PgNamespace)

Example 13 with ColumnDefinition

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

the class XMLTableDefinition method getElements0.

@Override
protected List<ColumnDefinition> getElements0() throws SQLException {
    List<ColumnDefinition> result = new ArrayList<>();
    for (Column column : info.getColumns()) {
        if (StringUtils.equals(defaultIfNull(table.getTableCatalog(), ""), defaultIfNull(column.getTableCatalog(), "")) && StringUtils.equals(defaultIfNull(table.getTableSchema(), ""), defaultIfNull(column.getTableSchema(), "")) && StringUtils.equals(defaultIfNull(table.getTableName(), ""), defaultIfNull(column.getTableName(), ""))) {
            SchemaDefinition schema = getDatabase().getSchema(column.getTableSchema());
            DataTypeDefinition type = new DefaultDataTypeDefinition(getDatabase(), schema, column.getDataType(), unbox(column.getCharacterMaximumLength()), unbox(column.getNumericPrecision()), unbox(column.getNumericScale()), column.isIsNullable(), column.getColumnDefault()).generatedAlwaysAs(TRUE.equals(column.isIsGenerated()) ? column.getGenerationExpression() : null).generationOption(TRUE.equals(column.isIsGenerated()) ? "STORED".equalsIgnoreCase(column.getGenerationOption()) ? STORED : "VIRTUAL".equalsIgnoreCase(column.getGenerationOption()) ? VIRTUAL : null : null);
            result.add(new DefaultColumnDefinition(this, column.getColumnName(), unbox(column.getOrdinalPosition()), type, column.getIdentityGeneration() != null, TRUE.equals(column.isReadonly()), column.getComment()));
        }
    }
    return result;
}
Also used : SchemaDefinition(org.jooq.meta.SchemaDefinition) Column(org.jooq.util.xml.jaxb.Column) DefaultDataTypeDefinition(org.jooq.meta.DefaultDataTypeDefinition) DefaultColumnDefinition(org.jooq.meta.DefaultColumnDefinition) ArrayList(java.util.ArrayList) DefaultDataTypeDefinition(org.jooq.meta.DefaultDataTypeDefinition) DataTypeDefinition(org.jooq.meta.DataTypeDefinition) DefaultColumnDefinition(org.jooq.meta.DefaultColumnDefinition) ColumnDefinition(org.jooq.meta.ColumnDefinition)

Example 14 with ColumnDefinition

use of org.jooq.meta.ColumnDefinition 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 15 with ColumnDefinition

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

the class CUBRIDTableDefinition method getElements0.

@Override
public List<ColumnDefinition> getElements0() throws SQLException {
    List<ColumnDefinition> result = new ArrayList<>();
    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)) {
        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));
        result.add(new DefaultColumnDefinition(getDatabase().getTable(getSchema(), getName()), record.get(DB_ATTRIBUTE.ATTR_NAME), result.size() + 1, type, record.get(DB_SERIAL.NAME) != null, 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) ColumnDefinition(org.jooq.meta.ColumnDefinition) DefaultColumnDefinition(org.jooq.meta.DefaultColumnDefinition)

Aggregations

ColumnDefinition (org.jooq.meta.ColumnDefinition)26 ArrayList (java.util.ArrayList)21 DefaultDataTypeDefinition (org.jooq.meta.DefaultDataTypeDefinition)18 Record (org.jooq.Record)16 DataTypeDefinition (org.jooq.meta.DataTypeDefinition)16 SchemaDefinition (org.jooq.meta.SchemaDefinition)15 DefaultColumnDefinition (org.jooq.meta.DefaultColumnDefinition)14 IndexColumnDefinition (org.jooq.meta.IndexColumnDefinition)10 TableDefinition (org.jooq.meta.TableDefinition)10 IndexDefinition (org.jooq.meta.IndexDefinition)7 Name (org.jooq.Name)6 CatalogDefinition (org.jooq.meta.CatalogDefinition)6 EnumDefinition (org.jooq.meta.EnumDefinition)6 List (java.util.List)5 SortOrder (org.jooq.SortOrder)5 PackageDefinition (org.jooq.meta.PackageDefinition)5 RoutineDefinition (org.jooq.meta.RoutineDefinition)5 SequenceDefinition (org.jooq.meta.SequenceDefinition)5 UniqueKeyDefinition (org.jooq.meta.UniqueKeyDefinition)5 File (java.io.File)4