Search in sources :

Example 21 with DmlStatement

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

the class DefaultDatabaseWriterConflictResolver method isVersionNewer.

protected boolean isVersionNewer(Conflict conflict, AbstractDatabaseWriter writer, CsvData data) {
    DefaultDatabaseWriter databaseWriter = (DefaultDatabaseWriter) writer;
    String columnName = conflict.getDetectExpression();
    Table targetTable = writer.getTargetTable();
    Table sourceTable = writer.getSourceTable();
    String[] pkData = data.getPkData(targetTable);
    Object[] objectValues = databaseWriter.getPlatform().getObjectValues(writer.getBatch().getBinaryEncoding(), pkData, targetTable.getPrimaryKeyColumns());
    DmlStatement stmt = databaseWriter.getPlatform().createDmlStatement(DmlType.FROM, targetTable, writer.getWriterSettings().getTextColumnExpression());
    String sql = stmt.getColumnsSql(new Column[] { targetTable.getColumnWithName(columnName) });
    Long existingVersion = databaseWriter.getTransaction().queryForObject(sql, Long.class, objectValues);
    if (existingVersion == null) {
        return true;
    } else {
        Map<String, String> newData = data.toColumnNameValuePairs(sourceTable.getColumnNames(), CsvData.ROW_DATA);
        Long loadingVersion = Long.valueOf(newData.get(columnName));
        return loadingVersion > existingVersion;
    }
}
Also used : Table(org.jumpmind.db.model.Table) DmlStatement(org.jumpmind.db.sql.DmlStatement)

Example 22 with DmlStatement

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

the class StructureDataWriter method buildSql.

protected String buildSql(DmlType dmlType, String[] values, Column[] columns) {
    // TODO we should try to reuse statements
    // TODO support primary key updates
    DmlStatement statement = DmlStatementFactory.createDmlStatement(targetDatabaseName, dmlType, currentTable, useQuotedIdentifiers);
    Object[] objects = platform.getObjectValues(binaryEncoding, values, columns, false, false);
    Row row = new Row(columns.length);
    for (int i = 0; i < columns.length; i++) {
        row.put(columns[i].getName(), objects[i]);
    }
    return statement.buildDynamicSql(binaryEncoding, row, false, useJdbcTimestampFormat);
}
Also used : DmlStatement(org.jumpmind.db.sql.DmlStatement) Row(org.jumpmind.db.sql.Row)

Example 23 with DmlStatement

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

the class DbFill method insertRandomRecord.

/**
 * Select a random row from the table and update all columns except for primary and foreign keys.
 *
 * @param sqlTemplate
 * @param table
 */
private void insertRandomRecord(Table table) {
    DmlStatement insertStatement = createInsertDmlStatement(table);
    Row row = createRandomInsertValues(insertStatement, table);
    try {
        platform.getSqlTemplate().update(insertStatement.getSql(), insertStatement.getValueArray(row.toArray(table.getColumnNames()), row.toArray(table.getPrimaryKeyColumnNames())));
        if (verbose) {
            log.info("Successful update in " + table.getName());
        }
    } catch (SqlException ex) {
        log.info("Failed to process {} with values of {}", insertStatement.getSql(), ArrayUtils.toString(row.toArray(table.getColumnNames())));
        if (continueOnError) {
            if (debug) {
                log.info("", ex);
            }
        } else {
            throw ex;
        }
    }
}
Also used : SqlException(org.jumpmind.db.sql.SqlException) DmlStatement(org.jumpmind.db.sql.DmlStatement) Row(org.jumpmind.db.sql.Row)

Example 24 with DmlStatement

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

the class DbFill method deleteRandomRecord.

/**
 * Delete a random row in the given table or delete all rows matching selectColumns
 * in the given table.
 *
 * @param table Table to delete from.
 * @param selectColumns If provided, the rows that match this criteria are deleted.
 */
private void deleteRandomRecord(Table table) {
    DmlStatement deleteStatement = createDeleteDmlStatement(table);
    Row row = selectRandomRow(table);
    try {
        platform.getSqlTemplate().update(deleteStatement.getSql(), row.toArray(table.getColumnNames()));
        if (verbose) {
            log.info("Successful update in " + table.getName());
        }
    } catch (SqlException ex) {
        log.info("Failed to process {} with values of {}", deleteStatement.getSql(), ArrayUtils.toString(row.toArray(table.getColumnNames())));
        if (continueOnError) {
            if (debug) {
                log.info("", ex);
            }
        } else {
            throw ex;
        }
    }
}
Also used : SqlException(org.jumpmind.db.sql.SqlException) DmlStatement(org.jumpmind.db.sql.DmlStatement) Row(org.jumpmind.db.sql.Row)

Example 25 with DmlStatement

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

the class AuditTableDataRouter method routeToNodes.

public Set<String> routeToNodes(SimpleRouterContext context, DataMetaData dataMetaData, Set<Node> nodes, boolean initialLoad, boolean initialLoadSelectUsed, TriggerRouter triggerRouter) {
    DataEventType eventType = dataMetaData.getData().getDataEventType();
    if (eventType == DataEventType.INSERT || eventType == DataEventType.UPDATE || eventType == DataEventType.DELETE) {
        IParameterService parameterService = engine.getParameterService();
        IDatabasePlatform platform = engine.getDatabasePlatform();
        TriggerHistory triggerHistory = dataMetaData.getTriggerHistory();
        Table table = dataMetaData.getTable().copyAndFilterColumns(triggerHistory.getParsedColumnNames(), triggerHistory.getParsedPkColumnNames(), true);
        String tableName = table.getFullyQualifiedTableName();
        Table auditTable = auditTables.get(tableName);
        if (auditTable == null) {
            auditTable = toAuditTable(table);
            auditTables.put(tableName, auditTable);
            if (parameterService.is(ParameterConstants.AUTO_CONFIGURE_DATABASE)) {
                platform.alterTables(true, auditTable);
            }
        }
        DatabaseInfo dbInfo = platform.getDatabaseInfo();
        String auditTableName = auditTable.getQualifiedTableName(dbInfo.getDelimiterToken(), dbInfo.getCatalogSeparator(), dbInfo.getSchemaSeparator());
        ISqlTemplate template = platform.getSqlTemplate();
        Map<String, Object> values = null;
        if (eventType != DataEventType.DELETE) {
            values = new HashMap<String, Object>(getNewDataAsObject(null, dataMetaData, engine.getSymmetricDialect(), false));
        } else {
            values = new HashMap<String, Object>(getOldDataAsObject(null, dataMetaData, engine.getSymmetricDialect(), false));
        }
        Long sequence = (Long) context.get(auditTableName);
        if (sequence == null) {
            sequence = 1l + template.queryForLong(String.format("select max(%s) from %s", auditTable.getColumnWithName(COLUMN_AUDIT_ID).getName(), auditTableName));
        } else {
            sequence = 1l + sequence;
        }
        context.put(auditTable.getName(), sequence);
        values.put(auditTable.getColumnWithName(COLUMN_AUDIT_ID).getName(), sequence);
        values.put(auditTable.getColumnWithName(COLUMN_AUDIT_TIME).getName(), new Date());
        values.put(auditTable.getColumnWithName(COLUMN_AUDIT_EVENT).getName(), eventType.getCode());
        DmlStatement statement = platform.createDmlStatement(DmlType.INSERT, auditTable, null);
        int[] types = statement.getTypes();
        Object[] args = statement.getValueArray(values);
        String sql = statement.getSql();
        template.update(sql, args, types);
    }
    return null;
}
Also used : IDatabasePlatform(org.jumpmind.db.platform.IDatabasePlatform) Table(org.jumpmind.db.model.Table) DatabaseInfo(org.jumpmind.db.platform.DatabaseInfo) DataEventType(org.jumpmind.symmetric.io.data.DataEventType) IParameterService(org.jumpmind.symmetric.service.IParameterService) Date(java.util.Date) ISqlTemplate(org.jumpmind.db.sql.ISqlTemplate) TriggerHistory(org.jumpmind.symmetric.model.TriggerHistory) DmlStatement(org.jumpmind.db.sql.DmlStatement)

Aggregations

DmlStatement (org.jumpmind.db.sql.DmlStatement)25 Row (org.jumpmind.db.sql.Row)19 Table (org.jumpmind.db.model.Table)12 Column (org.jumpmind.db.model.Column)10 SqlException (org.jumpmind.db.sql.SqlException)10 ArrayList (java.util.ArrayList)5 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 IDatabasePlatform (org.jumpmind.db.platform.IDatabasePlatform)3 Date (java.util.Date)2 ISqlTemplate (org.jumpmind.db.sql.ISqlTemplate)2 Timestamp (java.sql.Timestamp)1 LinkedHashMap (java.util.LinkedHashMap)1 ForeignKey (org.jumpmind.db.model.ForeignKey)1 Reference (org.jumpmind.db.model.Reference)1 DatabaseInfo (org.jumpmind.db.platform.DatabaseInfo)1 ParseException (org.jumpmind.exception.ParseException)1 DataEventType (org.jumpmind.symmetric.io.data.DataEventType)1 ConflictException (org.jumpmind.symmetric.io.data.writer.ConflictException)1 TriggerHistory (org.jumpmind.symmetric.model.TriggerHistory)1 IParameterService (org.jumpmind.symmetric.service.IParameterService)1