Skip to content

Commit

Permalink
0001240: No way to send schema changes in schema other, than public
Browse files Browse the repository at this point in the history
  • Loading branch information
erilong committed Apr 18, 2014
1 parent 34d1a7f commit 90015c5
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 10 deletions.
Expand Up @@ -387,6 +387,12 @@ public String getCreateTableXML(TriggerHistory triggerHistory, TriggerRouter tri
Database db = new Database();
setDatabaseName(triggerRouter, db);
db.addTable(table);
if (table.getCatalog() != null && !table.getCatalog().equals(platform.getDefaultCatalog())) {
db.setCatalog(table.getCatalog());
}
if (table.getSchema() != null && !table.getSchema().equals(platform.getDefaultSchema())) {
db.setSchema(table.getSchema());
}
StringWriter buffer = new StringWriter();
DatabaseXmlUtil.write(db, buffer);
// TODO: remove when these bugs are fixed in DdlUtils
Expand Down
Expand Up @@ -141,10 +141,14 @@ public static Database read(Reader reader, boolean validate) {
String attributeValue = parser.getAttributeValue(i);
if (attributeName.equalsIgnoreCase("name")) {
database.setName(attributeValue);
} else if (attributeName.equalsIgnoreCase("catalog")) {
database.setCatalog(attributeValue);
} else if (attributeName.equalsIgnoreCase("schema")) {
database.setSchema(attributeValue);
}
}
} else if (name.equalsIgnoreCase("table")) {
Table table = nextTable(parser);
Table table = nextTable(parser, database.getCatalog(), database.getSchema());
if (table != null) {
database.addTable(table);
}
Expand Down Expand Up @@ -172,6 +176,10 @@ public static Database read(Reader reader, boolean validate) {
}

public static Table nextTable(XmlPullParser parser) {
return nextTable(parser, null, null);
}

public static Table nextTable(XmlPullParser parser, String catalog, String schema) {
try {
Table table = null;
ForeignKey fk = null;
Expand All @@ -184,6 +192,8 @@ public static Table nextTable(XmlPullParser parser) {
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);
Expand Down Expand Up @@ -383,6 +393,12 @@ public static void write(Database model, Writer output) {
output.write("<?xml version=\"1.0\"?>\n<!DOCTYPE database SYSTEM \"" + DTD_PREFIX
+ "\">\n");
output.write("<database name=\"" + model.getName() + "\"");
if (model.getCatalog() != null) {
output.write(" catalog=\"" + model.getCatalog() + "\"");
}
if (model.getSchema() != null) {
output.write(" schema=\"" + model.getSchema() + "\"");
}
if (model.getIdMethod() != null) {
output.write(" defaultIdMethod=\"" + model.getIdMethod() + "\"");
}
Expand All @@ -403,6 +419,12 @@ public static String toXml(Table table) {
return writer.toString();
}

public static String toXml(Database db) {
StringWriter writer = new StringWriter();
write(db, writer);
return writer.toString();
}

public static void write(Table table, Writer output) {

try {
Expand Down
36 changes: 29 additions & 7 deletions symmetric-db/src/main/java/org/jumpmind/db/model/Database.java
Expand Up @@ -49,6 +49,10 @@ public class Database implements Serializable, Cloneable {
/** The name of the database model. */
private String name;

private String catalog;

private String schema;

/** The method for generating primary keys (currently ignored). */
private String idMethod;

Expand Down Expand Up @@ -168,6 +172,22 @@ public void setName(String name) {
this.name = name;
}

public String getCatalog() {
return catalog;
}

public void setCatalog(String catalog) {
this.catalog = catalog;
}

public String getSchema() {
return schema;
}

public void setSchema(String schema) {
this.schema = schema;
}

/**
* Returns the version of this database model.
*
Expand Down Expand Up @@ -578,6 +598,8 @@ public Database copy() {
public Object clone() throws CloneNotSupportedException {
Database result = (Database) super.clone();
result.name = name;
result.catalog = catalog;
result.schema = schema;
result.idMethod = idMethod;
result.version = version;
result.tables = new ArrayList<Table>(tables.size());
Expand All @@ -596,8 +618,8 @@ public boolean equals(Object obj) {
Database other = (Database) obj;

// Note that this compares case sensitive
return new EqualsBuilder().append(name, other.name).append(tables, other.tables)
.isEquals();
return new EqualsBuilder().append(name, other.name).append(catalog, other.catalog)
.append(schema, other.schema).append(tables, other.tables).isEquals();
} else {
return false;
}
Expand All @@ -616,11 +638,11 @@ public int hashCode() {
public String toString() {
StringBuffer result = new StringBuffer();

result.append("Database [name=");
result.append(getName());
result.append("; ");
result.append(getTableCount());
result.append(" tables]");
result.append("Database [name=").append(name);
result.append("; catalog=").append(catalog);
result.append("; schema=").append(catalog);
result.append("; tableCount=").append(getTableCount());
result.append("]");

return result.toString();
}
Expand Down
Expand Up @@ -1668,6 +1668,13 @@ protected void writeTableAlterStmt(Table table, StringBuilder ddl) {
*/
protected void writeTableCreationStmt(Table table, StringBuilder ddl) {
ddl.append("CREATE TABLE ");
// TODO: use getDelimitedIdentifier() around catalog/schema, but we'll need to get the case right
if (table.getCatalog() != null) {
ddl.append(table.getCatalog()).append(".");
}
if (table.getSchema() != null) {
ddl.append(table.getSchema()).append(".");
}
printlnIdentifier(getTableName(table.getName()), ddl);
println("(", ddl);

Expand Down
Expand Up @@ -426,7 +426,14 @@ protected void startTable(Table table) {
if (format == Format.SYM_XML) {
write("<batch xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n");
} else if (format == Format.XML) {
write("<database xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" name=\"dbexport\">\n");
write("<database xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" name=\"dbexport\"");
if (catalog != null && !catalog.equals(platform.getDefaultCatalog())) {
write(" catalog=\"" + catalog + "\"");
}
if (schema != null && !schema.equals(platform.getDefaultSchema())) {
write(" schema=\"" + schema + "\"");
}
write(">\n");
}
startedWriting = true;
}
Expand Down
Expand Up @@ -32,6 +32,7 @@
import org.apache.commons.io.IOUtils;
import org.jumpmind.db.io.DatabaseXmlUtil;
import org.jumpmind.db.model.Column;
import org.jumpmind.db.model.Database;
import org.jumpmind.db.model.Table;
import org.jumpmind.db.util.BinaryEncoding;
import org.jumpmind.exception.IoException;
Expand Down Expand Up @@ -85,6 +86,8 @@ protected void readNext() {
String columnName = null;
CsvData data = null;
Table table = null;
String catalog = null;
String schema = null;
int eventType = parser.next();
while (eventType != XmlPullParser.END_DOCUMENT) {
switch (eventType) {
Expand Down Expand Up @@ -138,10 +141,25 @@ protected void readNext() {
next.add(batch);
table = DatabaseXmlUtil.nextTable(parser);
next.add(table);
String xml = DatabaseXmlUtil.toXml(table);
Database db = new Database();
db.setName("dbimport");
db.setCatalog(catalog);
db.setSchema(schema);
db.addTable(table);
String xml = DatabaseXmlUtil.toXml(db);
data = new CsvData(DataEventType.CREATE);
data.putCsvData(CsvData.ROW_DATA, CsvUtils.escapeCsvData(xml));
next.add(data);
} else if ("database".equalsIgnoreCase(name)) {
for (int i = 0; i < parser.getAttributeCount(); i++) {
String attributeName = parser.getAttributeName(i);
String attributeValue = parser.getAttributeValue(i);
if ("catalog".equalsIgnoreCase(attributeName)) {
catalog = attributeValue;
} else if ("schema".equalsIgnoreCase(attributeName)) {
schema = attributeValue;
}
}
}
break;

Expand Down
Expand Up @@ -459,6 +459,8 @@ public Database readTables(final String catalog, final String schema, final Stri
public Database execute(Connection connection) throws SQLException {
Database db = new Database();
db.setName(Table.getQualifiedTablePrefix(catalog, schema));
db.setCatalog(catalog);
db.setSchema(schema);
db.addTables(readTables(connection, catalog, schema, tableTypes));
db.initialize();
return db;
Expand Down

0 comments on commit 90015c5

Please sign in to comment.