Search in sources :

Example 1 with Column

use of org.jumpmind.db.model.Column in project symmetric-ds by JumpMind.

the class DbCompare method getTargetComparisonSQL.

protected String getTargetComparisonSQL(DbCompareTables tables, IDatabasePlatform platform) {
    List<Column> mappedPkColumns = new ArrayList<Column>();
    for (Column sourcePkColumn : tables.getSourceTable().getPrimaryKeyColumns()) {
        Column targetColumn = tables.getColumnMapping().get(sourcePkColumn);
        if (targetColumn == null) {
            log.warn("No target column mapped to source PK column {}.  Dbcompare may be inaccurate for this table.", sourcePkColumn);
        } else {
            mappedPkColumns.add(targetColumn);
        }
    }
    String whereClause = config.getTargetWhereClause(tables.getTargetTable().getName());
    return getComparisonSQL(tables.getTargetTable(), tables.getTargetTable().getPrimaryKeyColumns(), platform, whereClause);
}
Also used : Column(org.jumpmind.db.model.Column) ArrayList(java.util.ArrayList)

Example 2 with Column

use of org.jumpmind.db.model.Column in project symmetric-ds by JumpMind.

the class DbCompareDiffWriter method writeInsert.

public void writeInsert(DbCompareRow sourceCompareRow) {
    if (stream == null) {
        return;
    }
    Table targetTable = tables.getTargetTable();
    DmlStatement statement = targetEngine.getDatabasePlatform().createDmlStatement(DmlType.INSERT, targetTable.getCatalog(), targetTable.getSchema(), targetTable.getName(), targetTable.getPrimaryKeyColumns(), targetTable.getColumns(), null, null);
    Row row = new Row(targetTable.getColumnCount());
    for (Column sourceColumn : tables.getSourceTable().getColumns()) {
        Column targetColumn = tables.getColumnMapping().get(sourceColumn);
        if (targetColumn == null) {
            continue;
        }
        row.put(targetColumn.getName(), sourceCompareRow.getRowValues().get(sourceColumn.getName()));
    }
    String sql = statement.buildDynamicSql(BinaryEncoding.HEX, row, false, false);
    writeLine(sql);
}
Also used : Table(org.jumpmind.db.model.Table) Column(org.jumpmind.db.model.Column) DmlStatement(org.jumpmind.db.sql.DmlStatement) Row(org.jumpmind.db.sql.Row)

Example 3 with Column

use of org.jumpmind.db.model.Column in project symmetric-ds by JumpMind.

the class DbCompare method mapPrimaryKey.

protected boolean mapPrimaryKey(DbCompareTables tables) {
    List<Column> mappedPkColumns = new ArrayList<Column>();
    for (Column sourcePkColumn : tables.getSourceTable().getPrimaryKeyColumns()) {
        Column targetColumn = tables.getColumnMapping().get(sourcePkColumn);
        if (targetColumn == null) {
            log.warn("No target column mapped to source PK column {}. Unable to perform dbcompare on table {}", sourcePkColumn, tables.getSourceTable());
            return false;
        } else {
            mappedPkColumns.add(targetColumn);
        }
    }
    Column[] targetColumns = tables.getTargetTable().getColumns();
    for (Column column : targetColumns) {
        column.setPrimaryKey(false);
    }
    List<Column> reorderedColumns = new ArrayList<Column>();
    for (Column mappedPkColumn : mappedPkColumns) {
        mappedPkColumn.setPrimaryKey(true);
        reorderedColumns.add(mappedPkColumn);
    }
    for (Column column : targetColumns) {
        if (!reorderedColumns.contains(column)) {
            reorderedColumns.add(column);
        }
    }
    tables.getTargetTable().removeAllColumns();
    tables.getTargetTable().addColumns(reorderedColumns);
    return true;
}
Also used : Column(org.jumpmind.db.model.Column) ArrayList(java.util.ArrayList)

Example 4 with Column

use of org.jumpmind.db.model.Column in project symmetric-ds by JumpMind.

the class DatabaseUpgradeListener method beforeUpgrade.

@Override
public String beforeUpgrade(ISymmetricDialect symmetricDialect, String tablePrefix, Database currentModel, Database desiredModel) throws IOException {
    StringBuilder sb = new StringBuilder();
    String monitorTableName = tablePrefix + "_" + TableConstants.SYM_MONITOR;
    String nodeTableName = tablePrefix + "_" + TableConstants.SYM_NODE;
    if (currentModel.findTable(nodeTableName) != null && currentModel.findTable(monitorTableName) == null && desiredModel.findTable(monitorTableName) != null) {
        log.info("Detected upgrade to version 3.8");
        isUpgradeTo38 = true;
    } else {
        isUpgradeTo38 = false;
    }
    if (isUpgradeTo38) {
        Table transformTable = currentModel.findTable(tablePrefix + "_" + TableConstants.SYM_TRANSFORM_TABLE);
        if (transformTable != null && transformTable.findColumn("update_action") != null) {
            engine.getSqlTemplate().update("update " + tablePrefix + "_" + TableConstants.SYM_TRANSFORM_TABLE + " set update_action = 'UPD_ROW' where update_action is null");
        }
        String dataGapTableName = tablePrefix + "_" + TableConstants.SYM_DATA_GAP;
        if (currentModel.findTable(dataGapTableName) != null) {
            engine.getSqlTemplate().update("delete from " + dataGapTableName);
        }
        String nodeCommunicationTable = tablePrefix + "_" + TableConstants.SYM_NODE_COMMUNICATION;
        if (currentModel.findTable(nodeCommunicationTable) != null) {
            engine.getSqlTemplate().update("delete from " + tablePrefix + "_" + TableConstants.SYM_NODE_COMMUNICATION);
        }
    }
    if (engine.getDatabasePlatform().getName().equals(DatabaseNamesConstants.FIREBIRD) || engine.getDatabasePlatform().getName().equals(DatabaseNamesConstants.FIREBIRD_DIALECT1)) {
        checkForDroppedColumns(currentModel, desiredModel);
    }
    if (engine.getDatabasePlatform().getName().equals(DatabaseNamesConstants.INFORMIX)) {
        Table triggerTable = desiredModel.findTable(tablePrefix + "_" + TableConstants.SYM_TRIGGER);
        if (triggerTable != null) {
            for (Column column : triggerTable.getColumns()) {
                if (column.getMappedTypeCode() == Types.LONGVARCHAR) {
                    column.setJdbcTypeCode(Types.VARCHAR);
                    column.setMappedType("VARCHAR");
                    column.setMappedTypeCode(Types.VARCHAR);
                    column.setSize("255");
                }
            }
        }
    }
    if (engine.getDatabasePlatform().getName().equals(DatabaseNamesConstants.MYSQL)) {
        String function = tablePrefix + "_transaction_id_post_5_7_6";
        String select = "select count(*) from information_schema.routines where routine_name='" + function + "' and routine_schema in (select database())";
        if (engine.getDatabasePlatform().getSqlTemplate().queryForInt(select) > 0) {
            String drop = "drop function " + function;
            engine.getDatabasePlatform().getSqlTemplate().update(drop);
            log.info("Just uninstalled {}", function);
        }
    }
    return sb.toString();
}
Also used : Table(org.jumpmind.db.model.Table) Column(org.jumpmind.db.model.Column)

Example 5 with Column

use of org.jumpmind.db.model.Column in project symmetric-ds by JumpMind.

the class DataService method getForeignTableRows.

protected List<TableRow> getForeignTableRows(List<TableRow> tableRows, Set<TableRow> visited) throws CloneNotSupportedException {
    List<TableRow> fkDepList = new ArrayList<TableRow>();
    for (TableRow tableRow : tableRows) {
        if (!visited.contains(tableRow)) {
            visited.add(tableRow);
            for (ForeignKey fk : tableRow.getTable().getForeignKeys()) {
                Table table = platform.getTableFromCache(fk.getForeignTableName(), false);
                if (table == null) {
                    table = fk.getForeignTable();
                    if (table == null) {
                        table = platform.getTableFromCache(tableRow.getTable().getCatalog(), tableRow.getTable().getSchema(), fk.getForeignTableName(), false);
                    }
                }
                if (table != null) {
                    Table foreignTable = (Table) table.clone();
                    for (Column column : foreignTable.getColumns()) {
                        column.setPrimaryKey(false);
                    }
                    Row whereRow = new Row(fk.getReferenceCount());
                    String referenceColumnName = null;
                    boolean[] nullValues = new boolean[fk.getReferenceCount()];
                    int index = 0;
                    for (Reference ref : fk.getReferences()) {
                        Column foreignColumn = foreignTable.findColumn(ref.getForeignColumnName());
                        Object value = tableRow.getRow().get(ref.getLocalColumnName());
                        nullValues[index++] = value == null;
                        referenceColumnName = ref.getLocalColumnName();
                        whereRow.put(foreignColumn.getName(), value);
                        foreignColumn.setPrimaryKey(true);
                    }
                    boolean allNullValues = true;
                    for (boolean b : nullValues) {
                        if (!b) {
                            allNullValues = false;
                            break;
                        }
                    }
                    if (!allNullValues) {
                        DmlStatement whereSt = platform.createDmlStatement(DmlType.WHERE, foreignTable.getCatalog(), foreignTable.getSchema(), foreignTable.getName(), foreignTable.getPrimaryKeyColumns(), foreignTable.getColumns(), nullValues, null);
                        String whereSql = whereSt.buildDynamicSql(symmetricDialect.getBinaryEncoding(), whereRow, false, true, foreignTable.getPrimaryKeyColumns()).substring(6);
                        String delimiter = platform.getDatabaseInfo().getSqlCommandDelimiter();
                        if (delimiter != null && delimiter.length() > 0) {
                            whereSql = whereSql.substring(0, whereSql.length() - delimiter.length());
                        }
                        Row foreignRow = new Row(foreignTable.getColumnCount());
                        if (foreignTable.getForeignKeyCount() > 0) {
                            DmlStatement selectSt = platform.createDmlStatement(DmlType.SELECT, foreignTable, null);
                            Object[] keys = whereRow.toArray(foreignTable.getPrimaryKeyColumnNames());
                            Map<String, Object> values = sqlTemplate.queryForMap(selectSt.getSql(), keys);
                            if (values == null) {
                                log.warn("Unable to reload rows for missing foreign key data for table '{}', parent data not found.  Using sql='{}' with keys '{}'", table.getName(), selectSt.getSql(), keys);
                            } else {
                                foreignRow.putAll(values);
                            }
                        }
                        TableRow foreignTableRow = new TableRow(foreignTable, foreignRow, whereSql, referenceColumnName, fk.getName());
                        fkDepList.add(foreignTableRow);
                        log.debug("Add foreign table reference '{}' whereSql='{}'", foreignTable.getName(), whereSql);
                    } else {
                        log.debug("The foreign table reference was null for {}", foreignTable.getName());
                    }
                } else {
                    log.debug("Foreign table '{}' not found for foreign key '{}'", fk.getForeignTableName(), fk.getName());
                }
                if (fkDepList.size() > 0) {
                    fkDepList.addAll(getForeignTableRows(fkDepList, visited));
                }
            }
        }
    }
    return fkDepList;
}
Also used : Table(org.jumpmind.db.model.Table) Reference(org.jumpmind.db.model.Reference) ArrayList(java.util.ArrayList) ForeignKey(org.jumpmind.db.model.ForeignKey) Column(org.jumpmind.db.model.Column) DmlStatement(org.jumpmind.db.sql.DmlStatement) Row(org.jumpmind.db.sql.Row)

Aggregations

Column (org.jumpmind.db.model.Column)179 Table (org.jumpmind.db.model.Table)78 ArrayList (java.util.ArrayList)34 IndexColumn (org.jumpmind.db.model.IndexColumn)23 PlatformColumn (org.jumpmind.db.model.PlatformColumn)21 Test (org.junit.Test)16 Row (org.jumpmind.db.sql.Row)15 LinkedHashMap (java.util.LinkedHashMap)12 ResultSet (java.sql.ResultSet)11 DmlStatement (org.jumpmind.db.sql.DmlStatement)10 SqlException (org.jumpmind.db.sql.SqlException)10 InvocationTargetException (java.lang.reflect.InvocationTargetException)9 IIndex (org.jumpmind.db.model.IIndex)9 HashMap (java.util.HashMap)8 ForeignKey (org.jumpmind.db.model.ForeignKey)8 CsvData (org.jumpmind.symmetric.io.data.CsvData)8 PreparedStatement (java.sql.PreparedStatement)7 IOException (java.io.IOException)6 SQLException (java.sql.SQLException)6 Reference (org.jumpmind.db.model.Reference)6