Search in sources :

Example 6 with DbImport

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));
}
Also used : IDatabasePlatform(org.jumpmind.db.platform.IDatabasePlatform) DbExport(org.jumpmind.symmetric.io.data.DbExport) ISqlTemplate(org.jumpmind.db.sql.ISqlTemplate) Table(org.jumpmind.db.model.Table) Database(org.jumpmind.db.model.Database) DbImport(org.jumpmind.symmetric.io.data.DbImport) Row(org.jumpmind.db.sql.Row) AbstractServiceTest(org.jumpmind.symmetric.service.impl.AbstractServiceTest) Test(org.junit.Test)

Example 7 with DbImport

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;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) DbImport(org.jumpmind.symmetric.io.data.DbImport) IDatabaseWriterFilter(org.jumpmind.symmetric.io.data.writer.IDatabaseWriterFilter) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 8 with DbImport

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"));
}
Also used : DbImport(org.jumpmind.symmetric.io.data.DbImport)

Example 9 with DbImport

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"));
    }
}
Also used : IDatabasePlatform(org.jumpmind.db.platform.IDatabasePlatform) Table(org.jumpmind.db.model.Table) Column(org.jumpmind.db.model.Column) DbImport(org.jumpmind.symmetric.io.data.DbImport) AbstractServiceTest(org.jumpmind.symmetric.service.impl.AbstractServiceTest) Test(org.junit.Test)

Example 10 with DbImport

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));
}
Also used : IDatabasePlatform(org.jumpmind.db.platform.IDatabasePlatform) DbExport(org.jumpmind.symmetric.io.data.DbExport) DbImport(org.jumpmind.symmetric.io.data.DbImport) File(java.io.File) AbstractServiceTest(org.jumpmind.symmetric.service.impl.AbstractServiceTest) Test(org.junit.Test)

Aggregations

DbImport (org.jumpmind.symmetric.io.data.DbImport)11 AbstractServiceTest (org.jumpmind.symmetric.service.impl.AbstractServiceTest)9 Test (org.junit.Test)9 IDatabasePlatform (org.jumpmind.db.platform.IDatabasePlatform)6 Table (org.jumpmind.db.model.Table)5 Database (org.jumpmind.db.model.Database)4 DbExport (org.jumpmind.symmetric.io.data.DbExport)4 File (java.io.File)2 ISqlTemplate (org.jumpmind.db.sql.ISqlTemplate)2 Row (org.jumpmind.db.sql.Row)2 SqlException (org.jumpmind.db.sql.SqlException)2 DbFill (org.jumpmind.symmetric.io.data.DbFill)2 BufferedInputStream (java.io.BufferedInputStream)1 FileInputStream (java.io.FileInputStream)1 Column (org.jumpmind.db.model.Column)1 ConflictException (org.jumpmind.symmetric.io.data.writer.ConflictException)1 IDatabaseWriterFilter (org.jumpmind.symmetric.io.data.writer.IDatabaseWriterFilter)1