use of org.jumpmind.db.model.Reference 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);
}
}
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.Reference in project symmetric-ds by JumpMind.
the class AbstractJdbcDdlReader method readForeignKey.
/*
* Reads the next foreign key spec from the result set.
*
* @param metaData The database meta data
*
* @param values The foreign key meta data as defined by {@link
* #getColumnsForFK()}
*
* @param knownFks The already read foreign keys for the current table
*/
protected void readForeignKey(DatabaseMetaDataWrapper metaData, Map<String, Object> values, Map<String, ForeignKey> knownFks) throws SQLException {
String fkName = (String) values.get("FK_NAME");
ForeignKey fk = (ForeignKey) knownFks.get(fkName);
if (fk == null) {
fk = new ForeignKey(fkName);
fk.setForeignTableName((String) values.get("PKTABLE_NAME"));
knownFks.put(fkName, fk);
}
Reference ref = new Reference();
ref.setForeignColumnName((String) values.get("PKCOLUMN_NAME"));
ref.setLocalColumnName((String) values.get("FKCOLUMN_NAME"));
if (values.containsKey("KEY_SEQ")) {
ref.setSequenceValue(((Short) values.get("KEY_SEQ")).intValue());
}
fk.addReference(ref);
}
use of org.jumpmind.db.model.Reference in project symmetric-ds by JumpMind.
the class SqliteDdlReader method readTable.
public Table readTable(String catalog, String schema, String tableName) {
Table table = null;
List<Column> columns = platform.getSqlTemplate().query("pragma table_info(" + tableName + ")", COLUMN_MAPPER);
checkForAutoIncrementColumn(columns, tableName);
if (columns != null && columns.size() > 0) {
table = new Table(tableName);
for (Column column : columns) {
table.addColumn(column);
}
List<IIndex> indexes = platform.getSqlTemplate().query("pragma index_list(" + tableName + ")", INDEX_MAPPER);
for (IIndex index : indexes) {
List<IndexColumn> indexColumns = platform.getSqlTemplate().query("pragma index_info(" + index.getName() + ")", INDEX_COLUMN_MAPPER);
for (IndexColumn indexColumn : indexColumns) {
/* Ignore auto index columns */
if (!indexColumn.getName().startsWith("sqlite_autoindex_")) {
index.addColumn(indexColumn);
indexColumn.setColumn(table.getColumnWithName(indexColumn.getName()));
}
}
if (!(index.hasAllPrimaryKeys() && index.getName().toLowerCase().contains("autoindex"))) {
table.addIndex(index);
}
}
Map<Integer, ForeignKey> keys = new HashMap<Integer, ForeignKey>();
List<Row> rows = platform.getSqlTemplate().query("pragma foreign_key_list(" + tableName + ")", new RowMapper());
for (Row row : rows) {
Integer id = row.getInt("id");
ForeignKey fk = keys.get(id);
if (fk == null) {
fk = new ForeignKey();
fk.setForeignTable(new Table(row.getString("table")));
keys.put(id, fk);
table.addForeignKey(fk);
}
fk.addReference(new Reference(new Column(row.getString("from")), new Column(row.getString("to"))));
}
}
return table;
}
use of org.jumpmind.db.model.Reference in project symmetric-ds by JumpMind.
the class AbstractDatabasePlatform method prefixForeignKeys.
protected void prefixForeignKeys(Table table, String tablePrefix, boolean storesUpperCaseIdentifiers) throws CloneNotSupportedException {
ForeignKey[] keys = table.getForeignKeys();
for (ForeignKey key : keys) {
String prefixedName = tablePrefix + key.getForeignTableName();
prefixedName = storesUpperCaseIdentifiers ? prefixedName.toUpperCase() : prefixedName.toLowerCase();
key.setForeignTableName(prefixedName);
String keyName = tablePrefix + key.getName();
keyName = storesUpperCaseIdentifiers ? keyName.toUpperCase() : keyName.toLowerCase();
key.setName(keyName);
Reference[] refs = key.getReferences();
for (Reference reference : refs) {
reference.setForeignColumnName(storesUpperCaseIdentifiers ? reference.getForeignColumnName().toUpperCase() : reference.getForeignColumnName().toLowerCase());
reference.setLocalColumnName(storesUpperCaseIdentifiers ? reference.getLocalColumnName().toUpperCase() : reference.getLocalColumnName().toLowerCase());
}
}
}
use of org.jumpmind.db.model.Reference 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