Search in sources :

Example 11 with Database

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\""));
}
Also used : Table(org.jumpmind.db.model.Table) StringWriter(java.io.StringWriter) Database(org.jumpmind.db.model.Database) Test(org.junit.Test)

Example 12 with Database

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;
}
Also used : Table(org.jumpmind.db.model.Table) Database(org.jumpmind.db.model.Database)

Example 13 with 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);
    }
}
Also used : Table(org.jumpmind.db.model.Table) IOException(java.io.IOException) CsvData(org.jumpmind.symmetric.io.data.CsvData) LinkedHashMap(java.util.LinkedHashMap) Batch(org.jumpmind.symmetric.io.data.Batch) Column(org.jumpmind.db.model.Column) Database(org.jumpmind.db.model.Database) IoException(org.jumpmind.exception.IoException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException)

Example 14 with Database

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;
}
Also used : Table(org.jumpmind.db.model.Table) ArrayList(java.util.ArrayList) Database(org.jumpmind.db.model.Database) SymmetricException(org.jumpmind.symmetric.SymmetricException) ExecutionException(java.util.concurrent.ExecutionException) HashSet(java.util.HashSet)

Example 15 with Database

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);
    }
}
Also used : Database(org.jumpmind.db.model.Database) SqlException(org.jumpmind.db.sql.SqlException) IoException(org.jumpmind.exception.IoException) IOException(java.io.IOException)

Aggregations

Database (org.jumpmind.db.model.Database)37 Table (org.jumpmind.db.model.Table)21 IDatabasePlatform (org.jumpmind.db.platform.IDatabasePlatform)9 Test (org.junit.Test)7 IOException (java.io.IOException)6 IoException (org.jumpmind.exception.IoException)6 AbstractServiceTest (org.jumpmind.symmetric.service.impl.AbstractServiceTest)5 IDdlBuilder (org.jumpmind.db.platform.IDdlBuilder)4 SqlException (org.jumpmind.db.sql.SqlException)4 SqlScript (org.jumpmind.db.sql.SqlScript)4 DbExport (org.jumpmind.symmetric.io.data.DbExport)4 DbImport (org.jumpmind.symmetric.io.data.DbImport)4 File (java.io.File)3 ArrayList (java.util.ArrayList)3 InputStreamReader (java.io.InputStreamReader)2 LinkedHashMap (java.util.LinkedHashMap)2 AddTableChange (org.jumpmind.db.alter.AddTableChange)2 RemoveTableChange (org.jumpmind.db.alter.RemoveTableChange)2 TableChange (org.jumpmind.db.alter.TableChange)2 Column (org.jumpmind.db.model.Column)2