Search in sources :

Example 21 with DefaultDataTypeDefinition

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

the class MySQLRoutineDefinition method createParameter.

private ParameterDefinition createParameter(Matcher matcher, int group, int columnIndex, String paramName) {
    String paramType = matcher.group(group + 1);
    Number precision = 0;
    Number scale = 0;
    if (!StringUtils.isBlank(matcher.group(group + 2))) {
        precision = Integer.valueOf(matcher.group(group + 2));
    }
    if (!StringUtils.isBlank(matcher.group(group + 3))) {
        scale = Integer.valueOf(matcher.group(group + 3));
    }
    DataTypeDefinition type = new DefaultDataTypeDefinition(getDatabase(), getSchema(), paramType, precision, precision, scale, null, (String) null);
    return new DefaultParameterDefinition(this, paramName, columnIndex, type);
}
Also used : DefaultDataTypeDefinition(org.jooq.meta.DefaultDataTypeDefinition) DefaultParameterDefinition(org.jooq.meta.DefaultParameterDefinition) DefaultDataTypeDefinition(org.jooq.meta.DefaultDataTypeDefinition) DataTypeDefinition(org.jooq.meta.DataTypeDefinition)

Example 22 with DefaultDataTypeDefinition

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

the class SQLiteTableDefinition method getElements0.

@Override
public List<ColumnDefinition> getElements0() throws SQLException {
    List<ColumnDefinition> result = new ArrayList<>();
    Field<String> fName = field(name("name"), String.class);
    Field<String> fType = field(name("type"), String.class);
    Field<Boolean> fNotnull = field(name("notnull"), boolean.class);
    Field<String> fDefaultValue = field(name("dflt_value"), String.class);
    Field<Integer> fPk = field(name("pk"), int.class);
    Field<Integer> fHidden = field(name("hidden"), int.class);
    for (Record record : create().select(fName, fType, fNotnull, fDefaultValue, fPk, fHidden).from("pragma_table_xinfo({0})", inline(getName())).where("hidden in (0, 2, 3)")) {
        String name = record.get(fName);
        String dataType = record.get(fType).replaceAll("\\(\\d+(\\s*,\\s*\\d+)?\\)", "");
        Number precision = parsePrecision(record.get(fType));
        Number scale = parseScale(record.get(fType));
        // SQLite identities are primary keys whose tables are mentioned in
        // sqlite_sequence
        int pk = record.get(fPk);
        int hidden = record.get(fHidden);
        boolean identity = false;
        boolean generated = hidden == 2 || hidden == 3;
        String generator = null;
        // [#8278] [#11172] SQLite doesn't store the data type for all views or virtual tables
        if (isBlank(dataType) || "other".equals(dataType)) {
            Table<?> t = interpretedTable();
            if (t != null) {
                Field<?> f = t.field(name);
                if (f != null) {
                    dataType = f.getDataType().getName();
                    precision = f.getDataType().precision();
                    scale = f.getDataType().scale();
                }
            }
        }
        if (generated) {
            // SQLite's type affinity feature gets in the way of correctly
            // interpreting data types when the column is computed, see
            // https://sqlite.org/forum/forumpost/8ebb993012
            // This is ignoring typos, because things like "generaated aways"
            // also work in SQLite, as long as there's an "as" keyword...
            dataType = dataType.replaceAll("(?i:\\s*generated\\s+always\\s*)", "");
        }
        if (pk > 0) {
            // [#6854] sqlite_sequence only contains identity information once a table contains records.
            identity |= existsSqliteSequence() && create().fetchOne("select count(*) from sqlite_sequence where name = ?", getName()).get(0, Boolean.class);
            if (!identity && !create().fetchExists(selectOne().from("{0}", DSL.name(getName()))))
                identity = getSource().matches("(?s:.*\\b" + getName() + "\\b[^,]*(?i:\\bautoincrement\\b)[^,]*.*)");
        }
        DefaultDataTypeDefinition type = new DefaultDataTypeDefinition(getDatabase(), getSchema(), dataType, precision, precision, scale, !record.get(fNotnull), record.get(fDefaultValue)).generatedAlwaysAs(generator).generationOption(hidden == 2 ? VIRTUAL : hidden == 3 ? STORED : null);
        result.add(new DefaultColumnDefinition(getDatabase().getTable(getSchema(), getName()), name, result.size() + 1, type, identity, null));
    }
    return result;
}
Also used : DefaultDataTypeDefinition(org.jooq.meta.DefaultDataTypeDefinition) ArrayList(java.util.ArrayList) DefaultColumnDefinition(org.jooq.meta.DefaultColumnDefinition) ColumnDefinition(org.jooq.meta.ColumnDefinition) DefaultColumnDefinition(org.jooq.meta.DefaultColumnDefinition) Record(org.jooq.Record)

Example 23 with DefaultDataTypeDefinition

use of org.jooq.meta.DefaultDataTypeDefinition 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 24 with DefaultDataTypeDefinition

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

the class PostgresRoutineDefinition method init0.

@Override
protected void init0() throws SQLException {
    Parameters p = PARAMETERS;
    Field<Integer> count = count().filterWhere(p.PARAMETER_NAME.ne(inline(""))).over(partitionBy(p.SPECIFIC_NAME, p.PARAMETER_NAME));
    Field<Integer> c = count.as("c");
    for (Record record : create().select(p.PARAMETER_NAME, when(p.DATA_TYPE.eq(inline("USER-DEFINED")).and(p.UDT_NAME.eq(inline("geometry"))), inline("geometry")).else_(p.DATA_TYPE).as(p.DATA_TYPE), p.CHARACTER_MAXIMUM_LENGTH, pNumericPrecision(p).as(p.NUMERIC_PRECISION), p.NUMERIC_SCALE, p.UDT_SCHEMA, p.UDT_NAME, p.ORDINAL_POSITION, p.PARAMETER_MODE, ((PostgresDatabase) getDatabase()).is94() ? p.PARAMETER_DEFAULT : inline((String) null).as(p.PARAMETER_DEFAULT), c).from(p).where(p.SPECIFIC_SCHEMA.equal(getSchema().getName())).and(p.SPECIFIC_NAME.equal(specificName)).orderBy(p.ORDINAL_POSITION.asc())) {
        String parameterName = record.get(p.PARAMETER_NAME);
        String inOut = record.get(p.PARAMETER_MODE);
        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), null, record.get(p.PARAMETER_DEFAULT), name(record.get(p.UDT_SCHEMA), record.get(p.UDT_NAME)));
        ParameterDefinition parameter = new DefaultParameterDefinition(this, parameterName, record.get(p.ORDINAL_POSITION), type, record.get(p.PARAMETER_DEFAULT) != null, StringUtils.isBlank(parameterName), "", record.get(c) > 1 ? record.get(p.ORDINAL_POSITION, String.class) : null);
        addParameter(InOutDefinition.getFromString(inOut), parameter);
    }
}
Also used : SchemaDefinition(org.jooq.meta.SchemaDefinition) Parameters(org.jooq.meta.postgres.information_schema.tables.Parameters) DefaultDataTypeDefinition(org.jooq.meta.DefaultDataTypeDefinition) Record(org.jooq.Record) DefaultParameterDefinition(org.jooq.meta.DefaultParameterDefinition) DefaultDataTypeDefinition(org.jooq.meta.DefaultDataTypeDefinition) DataTypeDefinition(org.jooq.meta.DataTypeDefinition) ParameterDefinition(org.jooq.meta.ParameterDefinition) DefaultParameterDefinition(org.jooq.meta.DefaultParameterDefinition)

Example 25 with DefaultDataTypeDefinition

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

the class XMLDatabase method getSequences0.

@Override
protected List<SequenceDefinition> getSequences0() {
    List<SequenceDefinition> result = new ArrayList<>();
    for (Sequence sequence : info().getSequences()) {
        if (getInputSchemata().contains(sequence.getSequenceSchema())) {
            SchemaDefinition schema = getSchema(sequence.getSequenceSchema());
            DataTypeDefinition type = new DefaultDataTypeDefinition(this, schema, sequence.getDataType(), sequence.getCharacterMaximumLength(), sequence.getNumericPrecision(), sequence.getNumericScale(), false, (String) null);
            result.add(new DefaultSequenceDefinition(schema, sequence.getSequenceName(), type, sequence.getComment(), sequence.getStartValue(), sequence.getIncrement(), sequence.getMinimumValue(), sequence.getMaximumValue(), Boolean.TRUE.equals(sequence.isCycleOption()), sequence.getCache()));
        }
    }
    return result;
}
Also used : DefaultSequenceDefinition(org.jooq.meta.DefaultSequenceDefinition) SchemaDefinition(org.jooq.meta.SchemaDefinition) DefaultSequenceDefinition(org.jooq.meta.DefaultSequenceDefinition) SequenceDefinition(org.jooq.meta.SequenceDefinition) DefaultDataTypeDefinition(org.jooq.meta.DefaultDataTypeDefinition) ArrayList(java.util.ArrayList) Sequence(org.jooq.util.xml.jaxb.Sequence) DataTypeDefinition(org.jooq.meta.DataTypeDefinition) DefaultDataTypeDefinition(org.jooq.meta.DefaultDataTypeDefinition)

Aggregations

DefaultDataTypeDefinition (org.jooq.meta.DefaultDataTypeDefinition)36 Record (org.jooq.Record)30 DataTypeDefinition (org.jooq.meta.DataTypeDefinition)30 ArrayList (java.util.ArrayList)27 SchemaDefinition (org.jooq.meta.SchemaDefinition)19 ColumnDefinition (org.jooq.meta.ColumnDefinition)14 DefaultColumnDefinition (org.jooq.meta.DefaultColumnDefinition)14 DefaultParameterDefinition (org.jooq.meta.DefaultParameterDefinition)8 DefaultSequenceDefinition (org.jooq.meta.DefaultSequenceDefinition)7 ParameterDefinition (org.jooq.meta.ParameterDefinition)7 SequenceDefinition (org.jooq.meta.SequenceDefinition)7 DefaultDomainDefinition (org.jooq.meta.DefaultDomainDefinition)5 DomainDefinition (org.jooq.meta.DomainDefinition)5 Rdb$fields (org.jooq.meta.firebird.rdb.tables.Rdb$fields)4 Name (org.jooq.Name)3 PgNamespace (org.jooq.meta.postgres.pg_catalog.tables.PgNamespace)3 PgType (org.jooq.meta.postgres.pg_catalog.tables.PgType)3 Rdb$procedureParameters (org.jooq.meta.firebird.rdb.tables.Rdb$procedureParameters)2 DomainConstraints (org.jooq.meta.hsqldb.information_schema.tables.DomainConstraints)2 Columns (org.jooq.meta.postgres.information_schema.tables.Columns)2