use of org.jumpmind.db.model.Database in project symmetric-ds by JumpMind.
the class DbExportImportTest method importUniqueKeywordTable.
@Test
public void importUniqueKeywordTable() throws Exception {
ISymmetricEngine engine = getSymmetricEngine();
DbImport dbImport = new DbImport(engine.getDatabasePlatform());
dbImport.setFormat(DbImport.Format.XML);
dbImport.setDropIfExists(true);
dbImport.setAlterCaseToMatchDatabaseDefaultCase(true);
dbImport.importTables(getClass().getResourceAsStream("/test-dbimport-unique.xml"));
IDatabasePlatform platform = engine.getSymmetricDialect().getPlatform();
Database testTables = platform.readDatabaseFromXml("/test-dbimport-unique.xml", true);
Table table = testTables.findTable("test_db_import_unique", false);
Assert.assertEquals(0, platform.getSqlTemplate().queryForInt("select count(*) from " + table.getName()));
Assert.assertEquals(table.getColumnWithName("string_required_value").isUnique(), true);
DbImport importCsv = new DbImport(engine.getDatabasePlatform());
importCsv.setFormat(DbImport.Format.SQL);
importCsv.importTables(getClass().getResourceAsStream("/test-dbimport-unique-good.sql"));
Assert.assertEquals(5, platform.getSqlTemplate().queryForInt("select count(*) from " + table.getName()));
dbImport.importTables(getClass().getResourceAsStream("/test-dbimport-unique.xml"));
try {
importCsv.importTables(getClass().getResourceAsStream("/test-dbimport-unique-bad-line-2.sql"));
Assert.fail("Expected a sql exception");
} catch (SqlException ex) {
}
Assert.assertEquals(0, platform.getSqlTemplate().queryForInt("select count(*) from " + table.getName()));
importCsv.setCommitRate(1);
importCsv.setForceImport(true);
importCsv.importTables(getClass().getResourceAsStream("/test-dbimport-unique-bad-line-2.sql"));
Assert.assertEquals(4, platform.getSqlTemplate().queryForInt("select count(*) from " + table.getName()));
}
use of org.jumpmind.db.model.Database in project symmetric-ds by JumpMind.
the class DbExportImportTest method exportUniqueKeywordSqliteTable.
@Test
public void exportUniqueKeywordSqliteTable() throws Exception {
String dbName = engine.getDatabasePlatform().getName();
if (dbName.equals(DatabaseNamesConstants.SQLITE)) {
ISymmetricEngine engine = getSymmetricEngine();
DbImport dbImport = new DbImport(engine.getDatabasePlatform());
dbImport.setFormat(DbImport.Format.XML);
dbImport.setDropIfExists(true);
dbImport.setAlterCaseToMatchDatabaseDefaultCase(true);
dbImport.importTables(getClass().getResourceAsStream("/test-dbimport-unique.xml"));
IDatabasePlatform platform = engine.getSymmetricDialect().getPlatform();
Database testTables = platform.readDatabaseFromXml("/test-dbimport-unique.xml", true);
Table table = testTables.findTable("test_db_import_unique", false);
Assert.assertEquals(0, platform.getSqlTemplate().queryForInt("select count(*) from " + table.getName()));
Assert.assertEquals(table.getColumnWithName("string_required_value").isUnique(), true);
final int RECORD_COUNT = 100;
DbFill fill = new DbFill(platform);
fill.setRecordCount(RECORD_COUNT);
fill.fillTables(table.getName());
Assert.assertEquals(RECORD_COUNT, platform.getSqlTemplate().queryForInt("select count(*) from " + table.getName()));
DbExport export = new DbExport(platform);
export.setFormat(Format.XML);
export.setNoCreateInfo(false);
export.setNoData(true);
String xmlOutput = export.exportTables(new String[] { table.getName() });
dbImport.importTables(xmlOutput);
table = testTables.findTable("test_db_import_unique", false);
Assert.assertEquals(0, platform.getSqlTemplate().queryForInt("select count(*) from " + table.getName()));
Assert.assertEquals(table.getColumnWithName("string_required_value").isUnique(), true);
}
}
use of org.jumpmind.db.model.Database in project symmetric-ds by JumpMind.
the class DatabasePlatformTest method dropCreateAndThenReadTable.
protected Table dropCreateAndThenReadTable(Table table) {
Database database = new Database();
database.addTable(table);
platform.createDatabase(database, true, false);
return platform.getTableFromCache(table.getName(), true);
}
use of org.jumpmind.db.model.Database in project symmetric-ds by JumpMind.
the class DefaultDatabaseWriter method create.
@Override
protected boolean create(CsvData data) {
String xml = null;
try {
transaction.commit();
statistics.get(batch).startTimer(DataWriterStatisticConstants.DATABASEMILLIS);
xml = data.getParsedData(CsvData.ROW_DATA)[0];
log.info("About to create table using the following definition: {}", xml);
StringReader reader = new StringReader(xml);
Database db = DatabaseXmlUtil.read(reader, false);
if (writerSettings.isCreateTableAlterCaseToMatchDatabaseDefault()) {
platform.alterCaseToMatchDatabaseDefaultCase(db);
}
platform.makePlatformSpecific(db);
if (writerSettings.isAlterTable()) {
platform.alterDatabase(db, !writerSettings.isCreateTableFailOnError());
} else {
platform.createDatabase(db, writerSettings.isCreateTableDropFirst(), !writerSettings.isCreateTableFailOnError());
}
platform.resetCachedTableModel();
statistics.get(batch).increment(DataWriterStatisticConstants.CREATECOUNT);
return true;
} catch (RuntimeException ex) {
log.error("Failed to alter table using the following xml: {}", xml);
throw ex;
} finally {
statistics.get(batch).stopTimer(DataWriterStatisticConstants.DATABASEMILLIS);
}
}
use of org.jumpmind.db.model.Database in project symmetric-ds by JumpMind.
the class XmlDataReader method readNext.
protected void readNext() {
try {
Map<String, String> rowData = new LinkedHashMap<String, String>();
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) {
case XmlPullParser.TEXT:
if (columnName != null) {
rowData.put(columnName, parser.getText());
columnName = null;
}
break;
case XmlPullParser.START_TAG:
String name = parser.getName();
if ("row".equalsIgnoreCase(name)) {
data = new CsvData();
if (table != null) {
table.removeAllColumns();
}
data.setDataEventType(DataEventType.INSERT);
} else if ("field".equalsIgnoreCase(name)) {
boolean nullValue = false;
for (int i = 0; i < parser.getAttributeCount(); i++) {
String attributeName = parser.getAttributeName(i);
String attributeValue = parser.getAttributeValue(i);
if ("name".equalsIgnoreCase(attributeName)) {
columnName = attributeValue;
} else if ("xsi:nil".equalsIgnoreCase(attributeName)) {
nullValue = true;
}
}
if (nullValue) {
rowData.put(columnName, null);
columnName = null;
}
} else if ("table_data".equalsIgnoreCase(name)) {
Batch batch = new Batch();
batch.setBinaryEncoding(BinaryEncoding.BASE64);
next.add(batch);
table = new Table();
for (int i = 0; i < parser.getAttributeCount(); i++) {
String attributeName = parser.getAttributeName(i);
String attributeValue = parser.getAttributeValue(i);
if ("name".equalsIgnoreCase(attributeName)) {
table.setName(attributeValue);
}
}
next.add(table);
} else if ("table".equalsIgnoreCase(name)) {
Batch batch = new Batch();
batch.setBinaryEncoding(BinaryEncoding.BASE64);
next.add(batch);
table = DatabaseXmlUtil.nextTable(parser);
next.add(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;
case XmlPullParser.END_TAG:
name = parser.getName();
if ("row".equalsIgnoreCase(name)) {
String[] columnNames = rowData.keySet().toArray(new String[rowData.keySet().size()]);
for (String colName : columnNames) {
table.addColumn(new Column(colName));
}
String[] columnValues = rowData.values().toArray(new String[rowData.values().size()]);
data.putParsedData(CsvData.ROW_DATA, columnValues);
if (this.table == null || !this.table.equals(table)) {
next.add(table);
}
next.add(data);
rowData = new LinkedHashMap<String, String>();
} else if ("table_data".equalsIgnoreCase(name)) {
if (batch != null) {
batch.setComplete(true);
}
} else if ("field".equalsIgnoreCase(name)) {
columnName = null;
}
break;
}
eventType = parser.next();
}
} catch (IOException ex) {
throw new IoException(ex);
} catch (XmlPullParserException ex) {
throw new RuntimeException(ex);
}
}
Aggregations