Search in sources :

Example 1 with PlatformColumn

use of org.jumpmind.db.model.PlatformColumn in project symmetric-ds by JumpMind.

the class AbstractJdbcDdlReader method genericizeDefaultValuesAndUpdatePlatformColumn.

protected void genericizeDefaultValuesAndUpdatePlatformColumn(Column column) {
    PlatformColumn platformColumn = column.findPlatformColumn(platform.getName());
    platformColumn.setDefaultValue(column.getDefaultValue());
    /*
         * Translate from platform specific functions to ansi sql functions
         */
    if ("getdate()".equalsIgnoreCase(column.getDefaultValue())) {
        column.setDefaultValue("CURRENT_TIMESTAMP");
    }
}
Also used : PlatformColumn(org.jumpmind.db.model.PlatformColumn)

Example 2 with PlatformColumn

use of org.jumpmind.db.model.PlatformColumn in project symmetric-ds by JumpMind.

the class AbstractJdbcDdlReader method readColumn.

/*
     * Extracts a column definition from the result set.
     * 
     * @param metaData The database meta data
     * 
     * @param values The column meta data values as defined by {@link
     * #getColumnsForColumn()}
     * 
     * @return The column
     */
protected Column readColumn(DatabaseMetaDataWrapper metaData, Map<String, Object> values) throws SQLException {
    Column column = new Column();
    PlatformColumn platformColumn = new PlatformColumn();
    platformColumn.setName(platform.getName());
    column.setName((String) values.get("COLUMN_NAME"));
    String defaultValue = (String) values.get("COLUMN_DEF");
    if (defaultValue == null) {
        defaultValue = (String) values.get("COLUMN_DEFAULT");
    }
    if (defaultValue != null) {
        defaultValue = defaultValue.trim();
        column.setDefaultValue(defaultValue);
    }
    String typeName = (String) values.get("TYPE_NAME");
    column.setJdbcTypeName(typeName);
    Integer mappedType = mapUnknownJdbcTypeForColumn(values);
    if (mappedType != null) {
        column.setMappedTypeCode(mappedType);
    } else {
        column.setMappedTypeCode((Integer) values.get("DATA_TYPE"));
    }
    column.setJdbcTypeCode((Integer) values.get("DATA_TYPE"));
    column.setPrecisionRadix(((Integer) values.get("NUM_PREC_RADIX")).intValue());
    String columnSize = (String) values.get("COLUMN_SIZE");
    int decimalDigits = ((Integer) values.get("DECIMAL_DIGITS")).intValue();
    try {
        platformColumn.setType(typeName);
        if (isNotBlank(columnSize)) {
            platformColumn.setSize(Integer.parseInt(columnSize));
        }
        platformColumn.setDecimalDigits(decimalDigits);
        column.addPlatformColumn(platformColumn);
    } catch (Exception ex) {
        log.warn("", ex);
    }
    if (columnSize == null) {
        columnSize = (String) _defaultSizes.get(new Integer(column.getMappedTypeCode()));
    }
    // we're setting the size after the precision and radix in case
    // the database prefers to return them in the size value
    column.setSize(columnSize);
    if (decimalDigits != 0) {
        // if there is a scale value, set it after the size (which probably
        // did not contain
        // a scale specification)
        column.setScale(decimalDigits);
    }
    column.setRequired("NO".equalsIgnoreCase(((String) values.get("IS_NULLABLE")).trim()));
    column.setDescription((String) values.get("REMARKS"));
    return column;
}
Also used : Column(org.jumpmind.db.model.Column) IndexColumn(org.jumpmind.db.model.IndexColumn) PlatformColumn(org.jumpmind.db.model.PlatformColumn) SqlException(org.jumpmind.db.sql.SqlException) SQLException(java.sql.SQLException) PlatformColumn(org.jumpmind.db.model.PlatformColumn)

Example 3 with PlatformColumn

use of org.jumpmind.db.model.PlatformColumn in project symmetric-ds by JumpMind.

the class AbstractDdlTypesTest method testPlatformSpecificDdl.

@Test
public void testPlatformSpecificDdl() throws Exception {
    dropTable();
    createTable();
    Table fromDb1 = platform.readTableFromDatabase(null, null, tableName());
    assertNotNull(fromDb1);
    dropTable();
    Column[] columns1 = fromDb1.getColumns();
    for (Column column : columns1) {
        assertNotNull(column.findPlatformColumn(getName()));
    }
    String xml = DatabaseXmlUtil.toXml(fromDb1);
    log.info("XML generated for table:\n" + xml);
    StringReader reader = new StringReader(xml);
    Table fromXml = DatabaseXmlUtil.read(reader, false).getTable(0);
    for (Column column : fromXml.getColumns()) {
        assertNotNull("Expected " + getName() + " platform specific column information for " + column.getName(), column.findPlatformColumn(getName()));
    }
    assertNotNull(fromXml);
    platform.alterTables(false, fromXml);
    Table fromDb2 = platform.readTableFromDatabase(null, null, tableName());
    assertNotNull("Could not find " + tableName() + " in the database", fromDb2);
    for (Column column1 : columns1) {
        PlatformColumn pColumn1 = column1.findPlatformColumn(getName());
        Column column2 = fromDb2.findColumn(column1.getName());
        assertNotNull(column2);
        PlatformColumn pColumn2 = column2.findPlatformColumn(getName());
        assertNotNull(pColumn2);
        assertEquals("Column types not equals for column " + column1.getName(), pColumn1.getType(), pColumn2.getType());
        assertEquals(pColumn1.getSize(), pColumn2.getSize());
        assertEquals(pColumn1.getDecimalDigits(), pColumn2.getDecimalDigits());
    }
}
Also used : Table(org.jumpmind.db.model.Table) Column(org.jumpmind.db.model.Column) PlatformColumn(org.jumpmind.db.model.PlatformColumn) StringReader(java.io.StringReader) PlatformColumn(org.jumpmind.db.model.PlatformColumn) Test(org.junit.Test)

Example 4 with PlatformColumn

use of org.jumpmind.db.model.PlatformColumn in project symmetric-ds by JumpMind.

the class AbstractDdlBuilder method mergeOrRemovePlatformTypes.

/**
 * When the platform columns were added alters were not taken into
 * consideration. Therefore, we copy the desiredPlatformColumn to the
 * current platform column because the alter code ends up using the current
 * column to come up with the alter statements. This is a hack that depends
 * on the fact that none of the alter decision are based upon the current
 * column's platform column.
 */
protected void mergeOrRemovePlatformTypes(Database currentModel, Database desiredModel) {
    Table[] currentTables = currentModel.getTables();
    for (Table currentTable : currentTables) {
        /*
             * Warning - we don't currently support altering tables across
             * different catalogs and schemas, so we only need to look up by
             * name
             */
        Table desiredTable = desiredModel.findTable(currentTable.getName(), false);
        if (desiredTable != null) {
            Column[] currentColumns = currentTable.getColumns();
            for (Column currentColumn : currentColumns) {
                Column desiredColumn = desiredTable.getColumnWithName(currentColumn.getName());
                if (desiredColumn != null) {
                    currentColumn.removePlatformColumn(databaseName);
                    PlatformColumn desiredPlatformColumn = desiredColumn.findPlatformColumn(databaseName);
                    if (desiredPlatformColumn != null) {
                        currentColumn.addPlatformColumn(desiredPlatformColumn);
                    }
                }
            }
        }
    }
}
Also used : Table(org.jumpmind.db.model.Table) Column(org.jumpmind.db.model.Column) IndexColumn(org.jumpmind.db.model.IndexColumn) PlatformColumn(org.jumpmind.db.model.PlatformColumn) PlatformColumn(org.jumpmind.db.model.PlatformColumn)

Example 5 with PlatformColumn

use of org.jumpmind.db.model.PlatformColumn in project symmetric-ds by JumpMind.

the class AbstractDdlBuilder method getSqlType.

/**
 * Returns the full SQL type specification (including size and
 * precision/scale) for the given column.
 *
 * @param column
 *            The column
 * @return The full SQL type string including the size
 */
protected String getSqlType(Column column) {
    PlatformColumn platformColumn = column.findPlatformColumn(databaseName);
    String nativeType = getNativeType(column);
    if (platformColumn != null) {
        nativeType = platformColumn.getType();
    }
    int sizePos = nativeType.indexOf(SIZE_PLACEHOLDER);
    StringBuilder sqlType = new StringBuilder();
    sqlType.append(sizePos >= 0 ? nativeType.substring(0, sizePos) : nativeType);
    Integer size = column.getSizeAsInt();
    if (platformColumn != null) {
        size = platformColumn.getSize();
    }
    if ((size == null || size == 0) && platformColumn == null) {
        size = databaseInfo.getDefaultSize(column.getMappedTypeCode());
    }
    int scale = column.getScale();
    if (platformColumn != null) {
        scale = platformColumn.getDecimalDigits();
    }
    if (size != null && size >= 0) {
        if (databaseInfo.hasSize(column.getMappedTypeCode())) {
            if (size > 0) {
                sqlType.append("(");
                sqlType.append(size.toString());
                sqlType.append(")");
            }
        } else if (databaseInfo.hasPrecisionAndScale(column.getMappedTypeCode())) {
            StringBuilder precisionAndScale = new StringBuilder();
            precisionAndScale.append("(");
            precisionAndScale.append(size);
            if (scale >= 0) {
                precisionAndScale.append(",");
                precisionAndScale.append(scale);
            }
            precisionAndScale.append(")");
            if (!"(0,0)".equals(precisionAndScale.toString())) {
                sqlType.append(precisionAndScale);
            }
        }
    }
    sqlType.append(sizePos >= 0 ? nativeType.substring(sizePos + SIZE_PLACEHOLDER.length()) : "");
    filterColumnSqlType(sqlType);
    return sqlType.toString();
}
Also used : PlatformColumn(org.jumpmind.db.model.PlatformColumn)

Aggregations

PlatformColumn (org.jumpmind.db.model.PlatformColumn)11 Column (org.jumpmind.db.model.Column)7 IndexColumn (org.jumpmind.db.model.IndexColumn)4 Table (org.jumpmind.db.model.Table)4 IOException (java.io.IOException)2 ForeignKey (org.jumpmind.db.model.ForeignKey)2 IIndex (org.jumpmind.db.model.IIndex)2 Reference (org.jumpmind.db.model.Reference)2 IoException (org.jumpmind.exception.IoException)2 Test (org.junit.Test)2 StringReader (java.io.StringReader)1 SQLException (java.sql.SQLException)1 NonUniqueIndex (org.jumpmind.db.model.NonUniqueIndex)1 UniqueIndex (org.jumpmind.db.model.UniqueIndex)1 SqlException (org.jumpmind.db.sql.SqlException)1 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)1