use of org.jumpmind.db.model.Database in project symmetric-ds by JumpMind.
the class DatabaseXmlUtilTest method testWriteXml.
@Test
public void testWriteXml() {
Database database = DatabaseXmlUtil.read(getClass().getResourceAsStream("/testDatabaseIO.xml"));
Table tableWithAmp = database.getTable(1);
StringWriter writer = new StringWriter();
DatabaseXmlUtil.write(tableWithAmp, writer);
String xml = writer.getBuffer().toString();
assertTrue(xml.contains("\"testColumnWith&\""));
assertTrue(xml.contains("\"&Amp\""));
}
use of org.jumpmind.db.model.Database in project symmetric-ds by JumpMind.
the class SqliteDdlReader method readTables.
public Database readTables(String catalog, String schema, String[] tableTypes) {
List<String> tableNames = getTableNames(catalog, schema, tableTypes);
Database database = new Database();
for (String tableName : tableNames) {
Table table = readTable(catalog, schema, tableName);
if (table != null) {
database.addTable(table);
}
}
return database;
}
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);
}
}
use of org.jumpmind.db.model.Database in project symmetric-ds by JumpMind.
the class TriggerRouterService method getTablesForTrigger.
protected Set<Table> getTablesForTrigger(Trigger trigger, List<Trigger> triggers, boolean useTableCache) {
Set<Table> tables = new HashSet<Table>();
try {
boolean ignoreCase = this.parameterService.is(ParameterConstants.DB_METADATA_IGNORE_CASE);
List<String> catalogNames = new ArrayList<String>();
if (trigger.isSourceCatalogNameWildCarded()) {
List<String> all = platform.getDdlReader().getCatalogNames();
for (String catalogName : all) {
if (trigger.matchesCatalogName(catalogName, ignoreCase)) {
catalogNames.add(catalogName);
}
}
if (catalogNames.size() == 0) {
catalogNames.add(null);
}
} else {
if (isBlank(trigger.getSourceCatalogName())) {
catalogNames.add(platform.getDefaultCatalog());
} else {
catalogNames.add(trigger.getSourceCatalogName());
}
}
for (String catalogName : catalogNames) {
List<String> schemaNames = new ArrayList<String>();
if (trigger.isSourceSchemaNameWildCarded()) {
List<String> all = platform.getDdlReader().getSchemaNames(catalogName);
for (String schemaName : all) {
if (trigger.matchesSchemaName(schemaName, ignoreCase)) {
schemaNames.add(schemaName);
}
}
if (schemaNames.size() == 0) {
schemaNames.add(null);
}
} else {
if (isBlank(trigger.getSourceSchemaName())) {
schemaNames.add(platform.getDefaultSchema());
} else {
schemaNames.add(trigger.getSourceSchemaName());
}
}
for (String schemaName : schemaNames) {
if (trigger.isSourceTableNameWildCarded()) {
Database database = symmetricDialect.getPlatform().readDatabase(catalogName, schemaName, new String[] { "TABLE" });
Table[] tableArray = database.getTables();
for (Table table : tableArray) {
if (trigger.matches(table, catalogName, schemaName, ignoreCase) && !containsExactMatchForSourceTableName(table, triggers, ignoreCase) && !table.getName().toLowerCase().startsWith(tablePrefix)) {
tables.add(table);
}
}
} else {
Table table = symmetricDialect.getPlatform().getTableFromCache(catalogName, schemaName, trigger.getSourceTableName(), !useTableCache);
if (table != null) {
tables.add(table);
}
}
}
}
} catch (Exception ex) {
log.error(String.format("Failed to retrieve tables for trigger with id of %s", trigger.getTriggerId()), ex);
}
return tables;
}
use of org.jumpmind.db.model.Database in project symmetric-ds by JumpMind.
the class AbstractSymmetricDialect method readSymmetricSchemaFromXml.
public Database readSymmetricSchemaFromXml() {
try {
Database database = merge(readDatabaseFromXml("/symmetric-schema.xml"), readDatabaseFromXml("/console-schema.xml"));
prefixConfigDatabase(database);
String extraTablesXml = parameterService.getString(ParameterConstants.AUTO_CONFIGURE_EXTRA_TABLES);
if (StringUtils.isNotBlank(extraTablesXml)) {
try {
database = merge(database, readDatabaseFromXml(extraTablesXml));
} catch (Exception ex) {
log.error("", ex);
}
}
return database;
} catch (RuntimeException ex) {
throw ex;
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
Aggregations