Search in sources :

Example 36 with Row

use of org.jumpmind.db.sql.Row in project symmetric-ds by JumpMind.

the class AndroidSqlReadCursor method next.

public T next() {
    try {
        if (this.cursor.moveToNext()) {
            Row row = getMapForRow();
            rowNumber++;
            if (this.mapper != null) {
                return this.mapper.mapRow(row);
            }
        }
        return null;
    } catch (Exception ex) {
        throw this.sqlTemplate.translate(ex);
    }
}
Also used : Row(org.jumpmind.db.sql.Row)

Example 37 with Row

use of org.jumpmind.db.sql.Row in project symmetric-ds by JumpMind.

the class AndroidSqlReadCursor method getMapForRow.

protected Row getMapForRow() {
    int columnCount = this.cursor.getColumnCount();
    Row mapOfColValues = new Row(columnCount);
    for (int i = 0; i < columnCount; i++) {
        String name = this.cursor.getColumnName(i);
        mapOfColValues.put(name, this.cursor.getString(i));
    }
    return mapOfColValues;
}
Also used : Row(org.jumpmind.db.sql.Row)

Example 38 with Row

use of org.jumpmind.db.sql.Row in project symmetric-ds by JumpMind.

the class DbCompare method compareTables.

protected TableReport compareTables(DbCompareTables tables, OutputStream sqlDiffOutput) {
    String sourceSelect = getSourceComparisonSQL(tables, sourceEngine.getDatabasePlatform());
    String targetSelect = getTargetComparisonSQL(tables, targetEngine.getDatabasePlatform());
    CountingSqlReadCursor sourceCursor = new CountingSqlReadCursor(sourceEngine.getDatabasePlatform().getSqlTemplate().queryForCursor(sourceSelect, defaultRowMapper));
    CountingSqlReadCursor targetCursor = new CountingSqlReadCursor(targetEngine.getDatabasePlatform().getSqlTemplate().queryForCursor(targetSelect, defaultRowMapper));
    TableReport tableReport = new TableReport();
    tableReport.setSourceTable(tables.getSourceTable().getName());
    tableReport.setTargetTable(tables.getTargetTable().getName());
    Row sourceRow = sourceCursor.next();
    Row targetRow = targetCursor.next();
    int counter = 0;
    long startTime = System.currentTimeMillis();
    DbCompareDiffWriter diffWriter = null;
    OutputStream stream = null;
    if (sqlDiffOutput != null) {
        diffWriter = new DbCompareDiffWriter(targetEngine, tables, sqlDiffOutput);
    } else {
        stream = getSqlDiffOutputStream(tables);
        diffWriter = new DbCompareDiffWriter(targetEngine, tables, stream);
    }
    try {
        while (true) {
            if (sourceRow == null && targetRow == null) {
                break;
            }
            counter++;
            if ((counter % 50000) == 0) {
                long elapsed = System.currentTimeMillis() - startTime;
                log.info("{} rows processed for table {}. Elapsed time {}. ({} ms.) Current report status {}", counter, tables.getSourceTable().getName(), DurationFormatUtils.formatDurationWords((elapsed), true, true), elapsed, tableReport);
            }
            DbCompareRow sourceCompareRow = sourceRow != null ? new DbCompareRow(sourceEngine, dbValueComparator, tables.getSourceTable(), sourceRow) : null;
            DbCompareRow targetCompareRow = targetRow != null ? new DbCompareRow(targetEngine, dbValueComparator, tables.getTargetTable(), targetRow) : null;
            int comparePk = comparePk(tables, sourceCompareRow, targetCompareRow);
            if (comparePk == 0) {
                Map<Column, String> deltas = sourceCompareRow.compareTo(tables, targetCompareRow);
                if (deltas.isEmpty()) {
                    tableReport.countMatchedRow();
                } else {
                    diffWriter.writeUpdate(targetCompareRow, deltas);
                    tableReport.countDifferentRow();
                }
                sourceRow = sourceCursor.next();
                targetRow = targetCursor.next();
            } else if (comparePk < 0) {
                diffWriter.writeInsert(sourceCompareRow);
                tableReport.countMissingRow();
                sourceRow = sourceCursor.next();
            } else {
                diffWriter.writeDelete(targetCompareRow);
                tableReport.countExtraRow();
                targetRow = targetCursor.next();
            }
            tableReport.setSourceRows(sourceCursor.count);
            tableReport.setTargetRows(targetCursor.count);
        }
    } finally {
        if (stream != null) {
            IOUtils.closeQuietly(stream);
        }
        IOUtils.closeQuietly(sourceCursor);
        IOUtils.closeQuietly(targetCursor);
    }
    return tableReport;
}
Also used : TableReport(org.jumpmind.symmetric.io.DbCompareReport.TableReport) Column(org.jumpmind.db.model.Column) OutputStream(java.io.OutputStream) Row(org.jumpmind.db.sql.Row)

Example 39 with Row

use of org.jumpmind.db.sql.Row in project symmetric-ds by JumpMind.

the class DbCompareDiffWriter method writeDelete.

public void writeDelete(DbCompareRow targetCompareRow) {
    if (stream == null) {
        return;
    }
    Table table = targetCompareRow.getTable();
    DmlStatement statement = targetEngine.getDatabasePlatform().createDmlStatement(DmlType.DELETE, table.getCatalog(), table.getSchema(), table.getName(), table.getPrimaryKeyColumns(), null, null, null);
    Row row = new Row(targetCompareRow.getTable().getPrimaryKeyColumnCount());
    for (int i = 0; i < targetCompareRow.getTable().getPrimaryKeyColumnCount(); i++) {
        row.put(table.getColumn(i).getName(), targetCompareRow.getRowValues().get(targetCompareRow.getTable().getColumn(i).getName()));
    }
    String sql = statement.buildDynamicDeleteSql(BinaryEncoding.HEX, row, false, true);
    writeLine(sql);
}
Also used : Table(org.jumpmind.db.model.Table) DmlStatement(org.jumpmind.db.sql.DmlStatement) Row(org.jumpmind.db.sql.Row)

Example 40 with Row

use of org.jumpmind.db.sql.Row in project symmetric-ds by JumpMind.

the class DbCompareDiffWriter method writeUpdate.

public void writeUpdate(DbCompareRow targetCompareRow, Map<Column, String> deltas) {
    if (stream == null) {
        return;
    }
    Table table = targetCompareRow.getTable();
    Column[] changedColumns = deltas.keySet().toArray(new Column[deltas.keySet().size()]);
    DmlStatement statement = targetEngine.getDatabasePlatform().createDmlStatement(DmlType.UPDATE, table.getCatalog(), table.getSchema(), table.getName(), table.getPrimaryKeyColumns(), changedColumns, null, null);
    Row row = new Row(changedColumns.length + table.getPrimaryKeyColumnCount());
    for (Column changedColumn : deltas.keySet()) {
        String value = deltas.get(changedColumn);
        row.put(changedColumn.getName(), value);
    }
    for (String pkColumnName : table.getPrimaryKeyColumnNames()) {
        String value = targetCompareRow.getRow().getString(pkColumnName);
        row.put(pkColumnName, value);
    }
    String sql = statement.buildDynamicSql(BinaryEncoding.HEX, row, false, true);
    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)

Aggregations

Row (org.jumpmind.db.sql.Row)66 ArrayList (java.util.ArrayList)27 ISqlRowMapper (org.jumpmind.db.sql.ISqlRowMapper)23 Trigger (org.jumpmind.db.model.Trigger)19 JdbcSqlTemplate (org.jumpmind.db.sql.JdbcSqlTemplate)19 DmlStatement (org.jumpmind.db.sql.DmlStatement)13 Column (org.jumpmind.db.model.Column)11 Table (org.jumpmind.db.model.Table)9 Date (java.util.Date)8 HashMap (java.util.HashMap)7 ISqlTemplate (org.jumpmind.db.sql.ISqlTemplate)7 Test (org.junit.Test)5 HashSet (java.util.HashSet)3 IDatabasePlatform (org.jumpmind.db.platform.IDatabasePlatform)3 SqlException (org.jumpmind.db.sql.SqlException)3 NetworkedNode (org.jumpmind.symmetric.model.NetworkedNode)3 Node (org.jumpmind.symmetric.model.Node)3 Map (java.util.Map)2 NotImplementedException (org.apache.commons.lang.NotImplementedException)2 Database (org.jumpmind.db.model.Database)2