use of org.jumpmind.db.model.IndexColumn in project symmetric-ds by JumpMind.
the class VoltDbDdlBuilder method alignIndexColumns.
protected void alignIndexColumns(IIndex currentIndex, IIndex desiredIndex) {
List<IndexColumn> currentColumns = new ArrayList<IndexColumn>(Arrays.asList(currentIndex.getColumns()));
while (currentIndex.getColumnCount() > 0) {
currentIndex.removeColumn(0);
}
int oridinalPosition = 1;
for (IndexColumn desiredColumn : desiredIndex.getColumns()) {
for (int i = 0; i < currentColumns.size(); i++) {
IndexColumn currentColumn = currentColumns.get(i);
if (desiredColumn.getName().equalsIgnoreCase(currentColumn.getName())) {
currentColumn.setOrdinalPosition(oridinalPosition++);
currentIndex.addColumn(currentColumn);
currentColumns.remove(i);
i--;
}
}
}
for (IndexColumn currentColumn : currentColumns) {
currentIndex.addColumn(currentColumn);
}
}
use of org.jumpmind.db.model.IndexColumn in project symmetric-ds by JumpMind.
the class AbstractDdlBuilder method writeEmbeddedIndexCreateStmt.
/**
* Writes the given embedded index of the table.
*/
protected void writeEmbeddedIndexCreateStmt(Table table, IIndex index, StringBuilder ddl) {
if ((index.getName() != null) && (index.getName().length() > 0)) {
ddl.append(" CONSTRAINT ");
printIdentifier(getIndexName(index), ddl);
}
if (index.isUnique()) {
ddl.append(" UNIQUE");
} else {
ddl.append(" INDEX ");
}
ddl.append(" (");
for (int idx = 0; idx < index.getColumnCount(); idx++) {
IndexColumn idxColumn = index.getColumn(idx);
Column col = table.findColumn(idxColumn.getName());
if (col == null) {
// exception
throw new ModelException("Invalid column '" + idxColumn.getName() + "' on index " + index.getName() + " for table " + table.getName());
}
if (idx > 0) {
ddl.append(", ");
}
printIdentifier(getColumnName(col), ddl);
}
ddl.append(")");
}
use of org.jumpmind.db.model.IndexColumn in project symmetric-ds by JumpMind.
the class DatabaseXmlUtil method nextTable.
public static Table nextTable(XmlPullParser parser, String catalog, String schema) {
try {
Table table = null;
ForeignKey fk = null;
IIndex index = null;
boolean done = false;
int eventType = parser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT && !done) {
switch(eventType) {
case XmlPullParser.START_TAG:
String name = parser.getName();
if (name.equalsIgnoreCase("table")) {
table = new Table();
table.setCatalog(catalog);
table.setSchema(schema);
for (int i = 0; i < parser.getAttributeCount(); i++) {
String attributeName = parser.getAttributeName(i);
String attributeValue = parser.getAttributeValue(i);
if (attributeName.equalsIgnoreCase("name")) {
table.setName(attributeValue);
} else if (attributeName.equalsIgnoreCase("description")) {
table.setDescription(attributeValue);
}
}
} else if (name.equalsIgnoreCase("column")) {
Column column = new Column();
for (int i = 0; i < parser.getAttributeCount(); i++) {
String attributeName = parser.getAttributeName(i);
String attributeValue = parser.getAttributeValue(i);
if (attributeName.equalsIgnoreCase("name")) {
column.setName(attributeValue);
} else if (attributeName.equalsIgnoreCase("primaryKey")) {
column.setPrimaryKey(FormatUtils.toBoolean(attributeValue));
} else if (attributeName.equalsIgnoreCase("required")) {
column.setRequired(FormatUtils.toBoolean(attributeValue));
} else if (attributeName.equalsIgnoreCase("type")) {
column.setMappedType(attributeValue);
} else if (attributeName.equalsIgnoreCase("size")) {
column.setSize(attributeValue);
} else if (attributeName.equalsIgnoreCase("default")) {
if (StringUtils.isNotBlank(attributeValue)) {
column.setDefaultValue(attributeValue);
}
} else if (attributeName.equalsIgnoreCase("autoIncrement")) {
column.setAutoIncrement(FormatUtils.toBoolean(attributeValue));
} else if (attributeName.equalsIgnoreCase("javaName")) {
column.setJavaName(attributeValue);
} else if (attributeName.equalsIgnoreCase("description")) {
column.setDescription(attributeValue);
} else if (attributeName.equalsIgnoreCase("unique")) {
column.setUnique(FormatUtils.toBoolean(attributeValue));
}
}
if (table != null) {
table.addColumn(column);
}
} else if (name.equalsIgnoreCase("platform-column")) {
PlatformColumn platformColumn = new PlatformColumn();
for (int i = 0; i < parser.getAttributeCount(); i++) {
String attributeName = parser.getAttributeName(i);
String attributeValue = parser.getAttributeValue(i);
if (attributeName.equalsIgnoreCase("name")) {
platformColumn.setName(attributeValue);
} else if (attributeName.equalsIgnoreCase("type")) {
platformColumn.setType(attributeValue);
} else if (attributeName.equalsIgnoreCase("default")) {
platformColumn.setDefaultValue(attributeValue);
} else if (attributeName.equalsIgnoreCase("size")) {
if (isNotBlank(attributeValue)) {
platformColumn.setSize(Integer.parseInt(attributeValue));
}
} else if (attributeName.equalsIgnoreCase("decimalDigits")) {
if (isNotBlank(attributeValue)) {
platformColumn.setDecimalDigits(Integer.parseInt(attributeValue));
}
}
}
if (table != null && table.getColumnCount() > 0) {
table.getColumn(table.getColumnCount() - 1).addPlatformColumn(platformColumn);
}
} else if (name.equalsIgnoreCase("foreign-key")) {
fk = new ForeignKey();
for (int i = 0; i < parser.getAttributeCount(); i++) {
String attributeName = parser.getAttributeName(i);
String attributeValue = parser.getAttributeValue(i);
if (attributeName.equalsIgnoreCase("name")) {
fk.setName(attributeValue);
} else if (attributeName.equalsIgnoreCase("foreignTable")) {
fk.setForeignTableName(attributeValue);
}
}
table.addForeignKey(fk);
} else if (name.equalsIgnoreCase("reference")) {
Reference ref = new Reference();
for (int i = 0; i < parser.getAttributeCount(); i++) {
String attributeName = parser.getAttributeName(i);
String attributeValue = parser.getAttributeValue(i);
if (attributeName.equalsIgnoreCase("local")) {
ref.setLocalColumnName(attributeValue);
} else if (attributeName.equalsIgnoreCase("foreign")) {
ref.setForeignColumnName(attributeValue);
}
}
fk.addReference(ref);
} else if (name.equalsIgnoreCase("index") || name.equalsIgnoreCase("unique")) {
if (name.equalsIgnoreCase("index")) {
index = new NonUniqueIndex();
} else {
index = new UniqueIndex();
}
for (int i = 0; i < parser.getAttributeCount(); i++) {
String attributeName = parser.getAttributeName(i);
String attributeValue = parser.getAttributeValue(i);
if (attributeName.equalsIgnoreCase("name")) {
index.setName(attributeValue);
}
}
table.addIndex(index);
} else if (name.equalsIgnoreCase("index-column") || name.equalsIgnoreCase("unique-column")) {
IndexColumn indexColumn = new IndexColumn();
for (int i = 0; i < parser.getAttributeCount(); i++) {
String attributeName = parser.getAttributeName(i);
String attributeValue = parser.getAttributeValue(i);
if (attributeName.equalsIgnoreCase("name")) {
indexColumn.setName(attributeValue);
} else if (attributeName.equalsIgnoreCase("size")) {
indexColumn.setSize(attributeValue);
}
}
indexColumn.setColumn(table.getColumnWithName(indexColumn.getName()));
if (index != null) {
index.addColumn(indexColumn);
}
}
break;
case XmlPullParser.END_TAG:
name = parser.getName();
if (name.equalsIgnoreCase("table")) {
done = true;
} else if (name.equalsIgnoreCase("index") || name.equalsIgnoreCase("unique")) {
index = null;
} else if (name.equalsIgnoreCase("table")) {
table = null;
} else if (name.equalsIgnoreCase("foreign-key")) {
fk = null;
}
break;
}
if (!done) {
eventType = parser.next();
}
}
return table;
} catch (XmlPullParserException e) {
throw new IoException(e);
} catch (IOException e) {
throw new IoException(e);
}
}
use of org.jumpmind.db.model.IndexColumn 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.IndexColumn in project symmetric-ds by JumpMind.
the class AbstractDatabasePlatform method alterCaseToMatchDatabaseDefaultCase.
public void alterCaseToMatchDatabaseDefaultCase(Table table) {
table.setName(alterCaseToMatchDatabaseDefaultCase(table.getName()));
Column[] columns = table.getColumns();
for (Column column : columns) {
column.setName(alterCaseToMatchDatabaseDefaultCase(column.getName()));
}
IIndex[] indexes = table.getIndices();
for (IIndex index : indexes) {
index.setName(alterCaseToMatchDatabaseDefaultCase(index.getName()));
IndexColumn[] indexColumns = index.getColumns();
for (IndexColumn indexColumn : indexColumns) {
indexColumn.setName(alterCaseToMatchDatabaseDefaultCase(indexColumn.getName()));
}
}
ForeignKey[] fks = table.getForeignKeys();
for (ForeignKey foreignKey : fks) {
foreignKey.setName(alterCaseToMatchDatabaseDefaultCase(foreignKey.getName()));
foreignKey.setForeignTableName(alterCaseToMatchDatabaseDefaultCase(foreignKey.getForeignTableName()));
Reference[] references = foreignKey.getReferences();
for (Reference reference : references) {
reference.setForeignColumnName(alterCaseToMatchDatabaseDefaultCase(reference.getForeignColumnName()));
reference.setLocalColumnName(alterCaseToMatchDatabaseDefaultCase(reference.getLocalColumnName()));
}
}
}
Aggregations