use of org.jumpmind.symmetric.io.data.DbImport in project symmetric-ds by JumpMind.
the class DbExportImportTest method exportThenImportCsvWithBackslashes.
@Test
public void exportThenImportCsvWithBackslashes() throws Exception {
ISymmetricEngine engine = getSymmetricEngine();
IDatabasePlatform platform = engine.getSymmetricDialect().getPlatform();
Database testTables = platform.readDatabaseFromXml("/test-dbimport.xml", true);
Table table = testTables.findTable("test_db_import_1", false);
recreateImportTable();
DbImport importCsv = new DbImport(platform);
importCsv.setFormat(DbImport.Format.SQL);
importCsv.importTables(getClass().getResourceAsStream("/test-dbimport-1-backslashes.sql"));
assertCountDbImportTableRecords(1);
DbExport export = new DbExport(platform);
export.setFormat(Format.CSV);
export.setNoCreateInfo(true);
export.setNoData(false);
String csvOutput = export.exportTables(new String[] { table.getName() });
ISqlTemplate sqlTemplate = platform.getSqlTemplate();
List<Row> rowsBeforeImport = sqlTemplate.query(SELECT_FROM_TEST_DB_IMPORT_1_ORDER_BY_ID);
recreateImportTable();
importCsv.setFormat(DbImport.Format.CSV);
importCsv.importTables(csvOutput, table.getName());
compareRows(table, rowsBeforeImport, sqlTemplate.query(SELECT_FROM_TEST_DB_IMPORT_1_ORDER_BY_ID));
}
use of org.jumpmind.symmetric.io.data.DbImport in project symmetric-ds by JumpMind.
the class DbImportCommand method executeWithOptions.
@Override
protected boolean executeWithOptions(CommandLine line) throws Exception {
DbImport dbImport = new DbImport(getDatabasePlatform(true));
if (line.hasOption(OPTION_FORMAT)) {
dbImport.setFormat(Format.valueOf(line.getOptionValue(OPTION_FORMAT).toUpperCase()));
}
if (line.hasOption(OPTION_CATALOG)) {
dbImport.setCatalog(line.getOptionValue(OPTION_CATALOG));
}
if (line.hasOption(OPTION_SCHEMA)) {
dbImport.setSchema(line.getOptionValue(OPTION_SCHEMA));
}
if (line.hasOption(OPTION_USE_VARIABLE_DATES)) {
dbImport.setUseVariableForDates(true);
}
if (line.hasOption(OPTION_COMMIT)) {
dbImport.setCommitRate(Long.parseLong(line.getOptionValue(OPTION_COMMIT)));
}
if (line.hasOption(OPTION_INTERVAL)) {
dbImport.setInterval((Integer.parseInt(line.getOptionValue(OPTION_INTERVAL))));
}
if (line.hasOption(OPTION_ALTER_CASE)) {
dbImport.setAlterCaseToMatchDatabaseDefaultCase(true);
}
if (line.hasOption(OPTION_DROP_IF_EXISTS)) {
dbImport.setDropIfExists(true);
}
if (line.hasOption(OPTION_ALTER)) {
dbImport.setAlterTables(true);
}
if (line.hasOption(OPTION_FILTER_CLASSES)) {
String filters = line.getOptionValue(OPTION_FILTER_CLASSES);
if (StringUtils.isNotBlank(filters)) {
String[] clazzes = filters.split(",");
for (String clazz : clazzes) {
if (StringUtils.isNotBlank(clazz)) {
IDatabaseWriterFilter databaseWriterFilter = (IDatabaseWriterFilter) Class.forName(clazz.trim()).newInstance();
dbImport.addDatabaseWriterFilter(databaseWriterFilter);
}
}
}
}
if (line.hasOption(OPTION_FORCE)) {
dbImport.setForceImport(true);
}
if (line.hasOption(OPTION_REPLACE)) {
dbImport.setReplaceRows(true);
}
if (line.hasOption(OPTION_IGNORE)) {
dbImport.setIgnoreCollisions(true);
}
String[] args = line.getArgs();
if (args.length == 0) {
dbImport.importTables(System.in, line.getOptionValue(OPTION_TABLE));
} else {
for (String fileName : args) {
if (!new File(fileName).exists()) {
throw new RuntimeException("Cannot find file " + fileName);
}
}
for (String fileName : args) {
BufferedInputStream in = new BufferedInputStream(new FileInputStream(fileName));
dbImport.importTables(in, line.getOptionValue(OPTION_TABLE));
in.close();
}
}
return true;
}
use of org.jumpmind.symmetric.io.data.DbImport in project symmetric-ds by JumpMind.
the class DbExportImportTest method recreateImportTable.
protected void recreateImportTable() {
ISymmetricEngine engine = getSymmetricEngine();
DbImport reCreateTablesImport = new DbImport(engine.getDatabasePlatform());
reCreateTablesImport.setFormat(DbImport.Format.XML);
reCreateTablesImport.setDropIfExists(true);
reCreateTablesImport.setAlterCaseToMatchDatabaseDefaultCase(true);
reCreateTablesImport.importTables(getClass().getResourceAsStream("/test-dbimport.xml"));
}
use of org.jumpmind.symmetric.io.data.DbImport in project symmetric-ds by JumpMind.
the class DbExportImportTest method testInsertBigIntIntoOracleIntField.
@Test
public void testInsertBigIntIntoOracleIntField() {
if (getPlatform().getName().equals(DatabaseNamesConstants.ORACLE)) {
ISymmetricEngine engine = getSymmetricEngine();
IDatabasePlatform platform = engine.getDatabasePlatform();
Table table = new Table("TEST_ORACLE_INTEGER");
table.addColumn(new Column("A", false, Types.INTEGER, -1, -1));
platform.alterCaseToMatchDatabaseDefaultCase(table);
platform.createTables(true, false, table);
DbImport importer = new DbImport(platform);
importer.setFormat(DbImport.Format.CSV);
importer.importTables("\"A\"\n1149140000100490", table.getName());
Assert.assertEquals(1149140000100490l, platform.getSqlTemplate().queryForLong("select A from TEST_ORACLE_INTEGER"));
}
}
use of org.jumpmind.symmetric.io.data.DbImport in project symmetric-ds by JumpMind.
the class DbExportImportTest method testExportCsvToDirectory.
@Test
public void testExportCsvToDirectory() throws Exception {
ISymmetricEngine engine = getSymmetricEngine();
IDatabasePlatform platform = engine.getSymmetricDialect().getPlatform();
DbImport importXml = new DbImport(platform);
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(platform);
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, platform.getTableFromCache("a", false).getName() + ".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, platform.getTableFromCache("b", false).getName() + ".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, platform.getTableFromCache("c", false).getName() + ".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));
}
Aggregations