Skip to content

Commit

Permalink
0000733: dbexport should use the default schema and default catalog i…
Browse files Browse the repository at this point in the history
…f they aren't specified
  • Loading branch information
chenson42 committed Jul 26, 2012
1 parent 46e09f7 commit e0fe16a
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 17 deletions.
Expand Up @@ -115,11 +115,13 @@ public String exportTables(Table[] tables) throws IOException {
}

public void exportTables(OutputStream output) throws IOException {
setDefaultSchemaAndCatalog();
Database database = platform.readDatabase(catalog, schema, new String[] {"TABLE"});
exportTables(output, database.getTables());
}

public void exportTables(OutputStream output, String[] tableNames) throws IOException {
setDefaultSchemaAndCatalog();
ArrayList<Table> tableList = new ArrayList<Table>();

for (String tableName : tableNames) {
Expand All @@ -136,25 +138,28 @@ public void exportTables(OutputStream output, String[] tableNames) throws IOExce
}

public void exportTables(OutputStream output, String tableName, String sql) throws IOException {
setDefaultSchemaAndCatalog();
Table table = platform.getDdlReader().readTable(catalog, schema, tableName, sql);
exportTables(output, new Table[] { table }, sql);
}

public void exportTables(OutputStream output, Table[] tables) throws IOException {
exportTables(output, tables, null);
}

public void exportTables(OutputStream output, Table[] tables, String sql) throws IOException {
final Writer writer = new OutputStreamWriter(output);
final CsvWriter csvWriter = new CsvWriter(writer, ',');

protected void setDefaultSchemaAndCatalog() {
if (StringUtils.isBlank(schema)) {
schema = platform.getDefaultSchema();
}

if (StringUtils.isBlank(catalog)) {
catalog = platform.getDefaultCatalog();
}
}

public void exportTables(OutputStream output, Table[] tables) throws IOException {
exportTables(output, tables, null);
}

public void exportTables(OutputStream output, Table[] tables, String sql) throws IOException {
final Writer writer = new OutputStreamWriter(output);
final CsvWriter csvWriter = new CsvWriter(writer, ',');

tables = Database.sortByForeignKeys(tables);

Expand Down
Expand Up @@ -7,14 +7,39 @@
import org.apache.commons.lang.StringUtils;
import org.jumpmind.db.model.Database;
import org.jumpmind.db.model.Table;
import org.jumpmind.db.platform.DatabaseNamesConstants;
import org.jumpmind.db.platform.IDatabasePlatform;
import org.jumpmind.db.sql.ISqlTemplate;
import org.jumpmind.symmetric.DbExport.Compatible;
import org.jumpmind.symmetric.DbExport.Format;
import org.jumpmind.symmetric.service.impl.AbstractServiceTest;
import org.junit.Test;

public class DbExportImportTest extends AbstractServiceTest {

@Test
public void testExportTableInSchemaOnH2() throws Exception {
if (getPlatform().getName().equals(DatabaseNamesConstants.H2)) {
ISymmetricEngine engine = getSymmetricEngine();
DataSource ds = engine.getDataSource();
ISqlTemplate template = getPlatform().getSqlTemplate();
template.update("CREATE SCHEMA IF NOT EXISTS A");
template.update("CREATE TABLE IF NOT EXISTS A.TEST (ID INT, NOTES VARCHAR(100), PRIMARY KEY (ID))");
template.update("DELETE FROM A.TEST");
template.update("INSERT INTO A.TEST VALUES(1,'test')");

DbExport export = new DbExport(ds);
export.setSchema("A");
export.setFormat(Format.SQL);
export.setNoCreateInfo(false);
export.setNoData(false);

String output = export.exportTables(new String[] {"TEST"}).toLowerCase();

// TODO validate
}
}

@Test
public void exportTestDatabaseSQL() throws Exception {
ISymmetricEngine engine = getSymmetricEngine();
Expand Down Expand Up @@ -61,9 +86,9 @@ public void exportThenImport() throws Exception {
.getDefaultSchema());
export.setCatalog(getSymmetricEngine().getSymmetricDialect().getPlatform()
.getDefaultCatalog());
String output = export.exportTables(new String[] {table.getName()});
//System.out.println(output);
String output = export.exportTables(new String[] { table.getName() });

// System.out.println(output);
// TODO validate

}
Expand Down
Expand Up @@ -156,8 +156,8 @@ protected List<MetaDataColumnDescriptor> initColumnsForTable() {

result.add(new MetaDataColumnDescriptor("TABLE_NAME", Types.VARCHAR));
result.add(new MetaDataColumnDescriptor("TABLE_TYPE", Types.VARCHAR, "UNKNOWN"));
result.add(new MetaDataColumnDescriptor("TABLE_CAT", Types.VARCHAR));
result.add(new MetaDataColumnDescriptor("TABLE_SCHEM", Types.VARCHAR));
result.add(new MetaDataColumnDescriptor(getResultSetCatalogName(), Types.VARCHAR));
result.add(new MetaDataColumnDescriptor(getResultSetSchemaName(), Types.VARCHAR));
result.add(new MetaDataColumnDescriptor("REMARKS", Types.VARCHAR));

return result;
Expand Down Expand Up @@ -422,6 +422,14 @@ protected List<MetaDataColumnDescriptor> getColumnsForIndex() {
public Database getDatabase(Connection connection) throws SQLException {
return readTables(null, null, null);
}

protected String getResultSetSchemaName() {
return "TABLE_SCHEM";
}

protected String getResultSetCatalogName() {
return "TABLE_CAT";
}

/*
* Reads the database model from the given connection.
Expand Down Expand Up @@ -536,6 +544,11 @@ public Table execute(Connection connection) throws SQLException {
try {
tableData = metaData.getTables(getTableNamePattern(table));
if (tableData != null && tableData.next()) {
ResultSetMetaData meta = tableData.getMetaData();
int count = meta.getColumnCount();
for (int i = 1 ; i <= count; i++) {
System.err.println(meta.getColumnName(i) + "=" + tableData.getObject(i));
}
Map<String, Object> values = readMetaData(tableData, initColumnsForTable());
return readTable(connection, metaData, values);
} else {
Expand Down Expand Up @@ -657,11 +670,11 @@ protected Table readTable(Connection connection, DatabaseMetaDataWrapper metaDat
table.setName(tableName);
table.setType(type);

String catalog = (String) values.get("TABLE_CAT");
String catalog = (String) values.get(getResultSetCatalogName());
table.setCatalog(catalog);
metaData.setCatalog(catalog);

String schema = (String) values.get("TABLE_SCHEM");
String schema = (String) values.get(getResultSetSchemaName());
table.setSchema(schema);
metaData.setSchemaPattern(schema);

Expand Down Expand Up @@ -1289,10 +1302,10 @@ public String determineSchemaOf(Connection connection, String schemaPattern, Tab

while (!found && tableData.next()) {
Map<String, Object> values = readMetaData(tableData, getColumnsForTable());
String tableName = (String) values.get("TABLE_NAME");
String tableName = (String) values.get(getResultSetCatalogName());

if ((tableName != null) && (tableName.length() > 0)) {
schema = (String) values.get("TABLE_SCHEM");
schema = (String) values.get(getResultSetSchemaName());
columnData = metaData.getColumns(tableName, getDefaultColumnPattern());
found = true;

Expand Down
Expand Up @@ -71,6 +71,16 @@ protected Column readColumn(DatabaseMetaDataWrapper metaData, Map<String, Object
}
return column;
}

@Override
protected String getResultSetSchemaName() {
return "TABLE_SCHEMA";
}

@Override
protected String getResultSetCatalogName() {
return "TABLE_CATALOG";
}

@Override
protected List<MetaDataColumnDescriptor> initColumnsForColumn() {
Expand Down

0 comments on commit e0fe16a

Please sign in to comment.