Search in sources :

Example 6 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)

Example 7 with PlatformColumn

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

the class DatabaseXmlUtil method write.

public static void write(Table table, Writer output) {
    try {
        output.write("\t<table name=\"" + StringEscapeUtils.escapeXml(table.getName()) + "\">\n");
        for (Column column : table.getColumns()) {
            output.write("\t\t<column name=\"" + StringEscapeUtils.escapeXml(column.getName()) + "\"");
            if (column.isPrimaryKey()) {
                output.write(" primaryKey=\"" + column.isPrimaryKey() + "\"");
            }
            if (column.isRequired()) {
                output.write(" required=\"" + column.isRequired() + "\"");
            }
            if (column.getMappedType() != null) {
                output.write(" type=\"" + column.getMappedType() + "\"");
            }
            if (column.getSize() != null) {
                output.write(" size=\"" + column.getSize() + "\"");
            }
            if (column.getDefaultValue() != null) {
                output.write(" default=\"" + StringEscapeUtils.escapeXml(column.getDefaultValue()) + "\"");
            }
            if (column.isAutoIncrement()) {
                output.write(" autoIncrement=\"" + column.isAutoIncrement() + "\"");
            }
            if (column.getJavaName() != null) {
                output.write(" javaName=\"" + column.getJavaName() + "\"");
            }
            if (column.isUnique()) {
                output.write(" unique=\"" + column.isUnique() + "\"");
            }
            if (column.getPlatformColumns() != null && column.getPlatformColumns().size() > 0) {
                Collection<PlatformColumn> platformColumns = column.getPlatformColumns().values();
                output.write(">\n");
                for (PlatformColumn platformColumn : platformColumns) {
                    output.write("\t\t\t<platform-column name=\"" + platformColumn.getName() + "\"");
                    output.write(" type=\"" + platformColumn.getType() + "\"");
                    if (platformColumn.getSize() > 0) {
                        output.write(" size=\"" + platformColumn.getSize() + "\"");
                    }
                    if (platformColumn.getDecimalDigits() > 0) {
                        output.write(" decimalDigits=\"" + platformColumn.getDecimalDigits() + "\"");
                    }
                    if (platformColumn.getDefaultValue() != null) {
                        output.write(" default=\"" + StringEscapeUtils.escapeXml(platformColumn.getDefaultValue()) + "\"");
                    }
                    output.write("/>\n");
                }
                output.write("\t\t</column>\n");
            } else {
                output.write("/>\n");
            }
        }
        for (ForeignKey fk : table.getForeignKeys()) {
            output.write("\t\t<foreign-key name=\"" + StringEscapeUtils.escapeXml(fk.getName()) + "\" foreignTable=\"" + StringEscapeUtils.escapeXml(fk.getForeignTableName()) + "\">\n");
            for (Reference ref : fk.getReferences()) {
                output.write("\t\t\t<reference local=\"" + StringEscapeUtils.escapeXml(ref.getLocalColumnName()) + "\" foreign=\"" + StringEscapeUtils.escapeXml(ref.getForeignColumnName()) + "\"/>\n");
            }
            output.write("\t\t</foreign-key>\n");
        }
        for (IIndex index : table.getIndices()) {
            if (index.isUnique()) {
                output.write("\t\t<unique name=\"" + StringEscapeUtils.escapeXml(index.getName()) + "\">\n");
                for (IndexColumn column : index.getColumns()) {
                    output.write("\t\t\t<unique-column name=\"" + StringEscapeUtils.escapeXml(column.getName()) + "\"/>\n");
                }
                output.write("\t\t</unique>\n");
            } else {
                output.write("\t\t<index name=\"" + StringEscapeUtils.escapeXml(index.getName()) + "\">\n");
                for (IndexColumn column : index.getColumns()) {
                    output.write("\t\t\t<index-column name=\"" + StringEscapeUtils.escapeXml(column.getName()) + "\"");
                    if (column.getSize() != null) {
                        output.write(" size=\"" + column.getSize() + "\"");
                    }
                    output.write("/>\n");
                }
                output.write("\t\t</index>\n");
            }
        }
        output.write("\t</table>\n");
    } catch (IOException e) {
        throw new IoException(e);
    }
}
Also used : IIndex(org.jumpmind.db.model.IIndex) Column(org.jumpmind.db.model.Column) IndexColumn(org.jumpmind.db.model.IndexColumn) PlatformColumn(org.jumpmind.db.model.PlatformColumn) Reference(org.jumpmind.db.model.Reference) IoException(org.jumpmind.exception.IoException) IOException(java.io.IOException) ForeignKey(org.jumpmind.db.model.ForeignKey) PlatformColumn(org.jumpmind.db.model.PlatformColumn) IndexColumn(org.jumpmind.db.model.IndexColumn)

Example 8 with PlatformColumn

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

the class AbstractDdlBuilder method getNativeDefaultValue.

/**
     * Returns the native default value for the column.
     *
     * @param column
     *            The column
     * @return The native default value
     */
protected String getNativeDefaultValue(Column column) {
    String defaultValue = column.getDefaultValue();
    PlatformColumn platformColumn = column.findPlatformColumn(databaseName);
    if (platformColumn != null) {
        defaultValue = platformColumn.getDefaultValue();
    }
    return defaultValue;
}
Also used : PlatformColumn(org.jumpmind.db.model.PlatformColumn)

Example 9 with PlatformColumn

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

the class AbstractDdlBuilderTest method testAlterTableWithNewVarcharSizeForPlatformWithPlatformColumn.

@Test
public void testAlterTableWithNewVarcharSizeForPlatformWithPlatformColumn() throws Exception {
    for (AbstractDdlBuilder ddlBuilder : ddlBuilders) {
        Table currentTable = new Table("Test", new Column("ID", true, Types.INTEGER, 50, 0), new Column("TXT", false, Types.VARCHAR, 50, 0));
        currentTable.getColumnWithName("TXT").addPlatformColumn(new PlatformColumn(DatabaseNamesConstants.H2, "VARCHAR2", 50, 0, null));
        Table desiredTable = new Table("Test", new Column("ID", true, Types.INTEGER, 50, 0), new Column("TXT", false, Types.VARCHAR, 255, 0));
        String alterSql = ddlBuilder.alterTable(currentTable, desiredTable);
        assertTrue("Failed to generate an appropriate alter for the following platform: " + ddlBuilder.databaseName, alterSql.contains("255"));
    }
}
Also used : Table(org.jumpmind.db.model.Table) Column(org.jumpmind.db.model.Column) PlatformColumn(org.jumpmind.db.model.PlatformColumn) PlatformColumn(org.jumpmind.db.model.PlatformColumn) Test(org.junit.Test)

Example 10 with PlatformColumn

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

the class MsSqlDdlReader method removePlatformSizeAndDecimal.

protected void removePlatformSizeAndDecimal(Column column) {
    for (PlatformColumn platformColumn : column.getPlatformColumns().values()) {
        platformColumn.setSize(-1);
        platformColumn.setDecimalDigits(-1);
    }
}
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