Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
0000869: dbexport to csv needs to either prevent or support multiple …
…table export
  • Loading branch information
chenson42 committed Oct 26, 2012
1 parent 5b5ae6a commit 819c773
Show file tree
Hide file tree
Showing 14 changed files with 553 additions and 279 deletions.
444 changes: 291 additions & 153 deletions symmetric-client/src/main/java/org/jumpmind/symmetric/DbExport.java

Large diffs are not rendered by default.

Expand Up @@ -21,8 +21,12 @@

package org.jumpmind.symmetric;

import java.io.File;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.lang.StringUtils;
import org.jumpmind.symmetric.DbExport.Compatible;
import org.jumpmind.symmetric.DbExport.Format;

Expand Down Expand Up @@ -54,6 +58,8 @@ public class DbExportCommand extends AbstractCommandLauncher {
private static final String OPTION_SCHEMA = "schema";

private static final String OPTION_CATALOG = "catalog";

private static final String OPTION_DIR = "dir";

public DbExportCommand() {
super("dbexport", "[tablename...]", "DbExport.Option.");
Expand Down Expand Up @@ -84,6 +90,7 @@ protected void printHelp(CommandLine cmd, Options options) {
protected void buildOptions(Options options) {
super.buildOptions(options);
addOption(options, null, OPTION_FORMAT, true);
addOption(options, null, OPTION_DIR, true);
addOption(options, null, OPTION_COMPATIBLE, true);
addOption(options, null, OPTION_SCHEMA, true);
addOption(options, null, OPTION_CATALOG, true);
Expand All @@ -101,9 +108,24 @@ protected void buildOptions(Options options) {
protected boolean executeWithOptions(CommandLine line) throws Exception {
DbExport dbExport = new DbExport(getDatabasePlatform(false));

if (line.hasOption(OPTION_DIR)) {
String dir = line.getOptionValue(OPTION_DIR);
if (new File(dir).exists()) {
dbExport.setDir(line.getOptionValue(OPTION_DIR));
} else {
throw new ParseException(String.format("The directory you chose, {}, does not exist", dir));
}
}

if (line.hasOption(OPTION_FORMAT)) {
dbExport.setFormat(Format.valueOf(line.getOptionValue(OPTION_FORMAT).toUpperCase()));
if (dbExport.getFormat() == Format.CSV && line.getArgs().length > 1
&& StringUtils.isBlank(dbExport.getDir())) {
throw new ParseException(
"When exporting multiple tables to CSV format you must designate a directory where the files will be written");
}
}

if (line.hasOption(OPTION_COMPATIBLE)) {
try {
dbExport.setCompatible(Compatible.valueOf(line.getOptionValue(OPTION_COMPATIBLE).toUpperCase()));
Expand Down
@@ -1,12 +1,14 @@
package org.jumpmind.symmetric;

import java.io.File;
import java.util.List;
import java.util.Set;

import javax.sql.DataSource;

import junit.framework.Assert;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
import org.jumpmind.db.model.Database;
Expand Down Expand Up @@ -262,7 +264,7 @@ public void exportThenImportCsv() throws Exception {
}

@Test
public void testOracleExportTimestampWithTimezone() throws Exception {
public void testExportTimestampWithTimezone() throws Exception {
ISymmetricEngine engine = getSymmetricEngine();
IDatabasePlatform platform = engine.getSymmetricDialect().getPlatform();
DataSource ds = engine.getDataSource();
Expand Down Expand Up @@ -304,6 +306,58 @@ public void testOracleExportTimestampWithTimezone() throws Exception {
}
}
}

@Test
public void testExportCsvToDirectory() throws Exception {
ISymmetricEngine engine = getSymmetricEngine();
DataSource ds = engine.getDataSource();

DbImport importXml = new DbImport(ds);
importXml.setFormat(DbImport.Format.XML);
importXml.importTables(getClass().getResourceAsStream("/test-dbexportimport-3-tables.xml"));

File dir = new File("target/test");
FileUtils.deleteDirectory(dir);
Assert.assertFalse(dir.exists());

DbExport exportCsv = new DbExport(ds);
exportCsv.setComments(true);
exportCsv.setFormat(Format.CSV);
exportCsv.setDir(dir.getAbsolutePath());
exportCsv.exportTables(new String[] {"a", "b", "c"});

Assert.assertTrue(dir.exists());
Assert.assertTrue(dir.isDirectory());

File a = new File(dir, "a.csv");
Assert.assertTrue(a.exists());
Assert.assertTrue(a.isFile());
List<String> lines = FileUtils.readLines(a);
Assert.assertEquals(9, lines.size());
Assert.assertEquals("id,string_value", lines.get(5));
Assert.assertEquals("1,This is a test of a", lines.get(6));
Assert.assertEquals("2,This is a test of a", lines.get(7));

File b = new File(dir, "b.csv");
Assert.assertTrue(b.exists());
Assert.assertTrue(b.isFile());
lines = FileUtils.readLines(b);
Assert.assertEquals(10, lines.size());
Assert.assertEquals("id,string_value", lines.get(5));
Assert.assertEquals("1,This is a test of b", lines.get(6));
Assert.assertEquals("2,This is a test of b", lines.get(7));
Assert.assertEquals("3,This is line 3 of b", lines.get(8));

File c = new File(dir, "c.csv");
Assert.assertTrue(c.exists());
Assert.assertTrue(c.isFile());
lines = FileUtils.readLines(c);
Assert.assertEquals(9, lines.size());
Assert.assertEquals("id,string_value", lines.get(5));
Assert.assertEquals("1,This is a test of c", lines.get(6));
Assert.assertEquals("2,This is a test of c", lines.get(7));

}

protected void compareRows(List<Row> one, List<Row> two) {
if (one.size() != two.size()) {
Expand Down
@@ -0,0 +1,49 @@
<?xml version="1.0"?>
<database name="testdb">
<table name="a">
<column name="id" type="INTEGER" required="true" primaryKey="true" />
<column name="string_value" type="VARCHAR" size="50" />
</table>
<table_data name="a">
<row>
<field name="id">1</field>
<field name="string_value">This is a test of a</field>
</row>
<row>
<field name="id">2</field>
<field name="string_value">This is a test of a</field>
</row>
</table_data>
<table name="b">
<column name="id" type="INTEGER" required="true" primaryKey="true" />
<column name="string_value" type="VARCHAR" size="50" />
</table>
<table_data name="b">
<row>
<field name="id">1</field>
<field name="string_value">This is a test of b</field>
</row>
<row>
<field name="id">2</field>
<field name="string_value">This is a test of b</field>
</row>
<row>
<field name="id">3</field>
<field name="string_value">This is line 3 of b</field>
</row>
</table_data>
<table name="c">
<column name="id" type="INTEGER" required="true" primaryKey="true" />
<column name="string_value" type="VARCHAR" size="50" />
</table>
<table_data name="c">
<row>
<field name="id">1</field>
<field name="string_value">This is a test of c</field>
</row>
<row>
<field name="id">2</field>
<field name="string_value">This is a test of c</field>
</row>
</table_data>
</database>
Expand Up @@ -12,7 +12,7 @@
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.jumpmind.db.io.DatabaseIO;
import org.jumpmind.db.io.DatabaseXmlUtil;
import org.jumpmind.db.model.Database;
import org.jumpmind.db.model.Table;
import org.jumpmind.db.platform.IDatabasePlatform;
Expand Down Expand Up @@ -394,7 +394,7 @@ protected boolean buildTablesFromDdlUtilXmlIfProvided() {
if (fileUrl != null) {
try {
log.info("Building database schema from: {}", xml);
Database database = new DatabaseIO().read(new InputStreamReader(fileUrl
Database database = DatabaseXmlUtil.read(new InputStreamReader(fileUrl
.openStream()));
IDatabasePlatform platform = symmetricDialect.getPlatform();
platform.createDatabase(database, true, true);
Expand Down
Expand Up @@ -28,7 +28,7 @@
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.jumpmind.db.io.DatabaseIO;
import org.jumpmind.db.io.DatabaseXmlUtil;
import org.jumpmind.db.model.Column;
import org.jumpmind.db.model.Database;
import org.jumpmind.db.model.ForeignKey;
Expand Down Expand Up @@ -344,8 +344,7 @@ public String getCreateTableXML(TriggerHistory triggerHistory, TriggerRouter tri
setDatabaseName(triggerRouter, db);
db.addTable(table);
StringWriter buffer = new StringWriter();
DatabaseIO xmlWriter = new DatabaseIO();
xmlWriter.write(db, buffer);
DatabaseXmlUtil.write(db, buffer);
// TODO: remove when these bugs are fixed in DdlUtils
String xml = buffer.toString().replaceAll("&apos;", "");
xml = xml.replaceAll("default=\"empty_blob\\(\\) *\"", "");
Expand Down
Expand Up @@ -244,6 +244,6 @@ protected void checkForOpenResources() {
SqlUtils.logOpenResources();
Assert.assertEquals("There should be no open cursors", 0, SqlUtils.getOpenSqlReadCursors().size());
Assert.assertEquals("There should be no open transactions", 0, SqlUtils.getOpenTransactions().size());
}
}

}

0 comments on commit 819c773

Please sign in to comment.