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();
}
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);
}
}
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;
}
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"));
}
}
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);
}
}
Aggregations