Search in sources :

Example 16 with DmlStatement

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

the class DbFill method updateRandomRecord.

private void updateRandomRecord(ISqlTransaction tran, Table table) {
    DmlStatement updStatement = createUpdateDmlStatement(table);
    Row row = createRandomUpdateValues(tran, updStatement, table);
    Object[] values = new Object[table.getColumnCount()];
    int i = 0;
    for (Column column : table.getColumns()) {
        if (!column.isPrimaryKey()) {
            values[i++] = row.get(column.getName());
        }
    }
    for (Column column : table.getPrimaryKeyColumns()) {
        values[i++] = row.get(column.getName());
    }
    try {
        tran.prepareAndExecute(updStatement.getSql(), values);
    } catch (SqlException ex) {
        log.info("Failed to update {}: {}", table.getName(), ex.getMessage());
        if (continueOnError) {
            if (debug) {
                logRow(row);
                log.info("", ex);
            }
        } else {
            throw ex;
        }
    }
}
Also used : Column(org.jumpmind.db.model.Column) SqlException(org.jumpmind.db.sql.SqlException) DmlStatement(org.jumpmind.db.sql.DmlStatement) Row(org.jumpmind.db.sql.Row)

Example 17 with DmlStatement

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

the class JdbcPersistenceManager method find.

@Override
public <T> List<T> find(Class<T> clazz, String catalogName, String schemaName, String tableName) {
    try {
        Table table = findTable(catalogName, schemaName, tableName);
        T object = clazz.newInstance();
        LinkedHashMap<String, Column> objectToTableMapping = mapObjectToTable(object, table);
        Column[] columns = objectToTableMapping.values().toArray(new Column[objectToTableMapping.size()]);
        DmlStatement statement = databasePlatform.createDmlStatement(DmlType.SELECT_ALL, table.getCatalog(), table.getSchema(), table.getName(), null, columns, null, null);
        String sql = statement.getSql();
        List<Row> rows = databasePlatform.getSqlTemplate().query(sql);
        List<T> objects = new ArrayList<T>();
        for (Row row : rows) {
            object = clazz.newInstance();
            Set<String> propertyNames = objectToTableMapping.keySet();
            for (String propertyName : propertyNames) {
                Object value = row.get(objectToTableMapping.get(propertyName).getName());
                BeanUtils.copyProperty(object, propertyName, value);
            }
            objects.add(object);
        }
        return objects;
    } catch (Exception e) {
        throw toRuntimeException(e);
    }
}
Also used : Table(org.jumpmind.db.model.Table) ArrayList(java.util.ArrayList) SqlException(org.jumpmind.db.sql.SqlException) InvocationTargetException(java.lang.reflect.InvocationTargetException) Column(org.jumpmind.db.model.Column) DmlStatement(org.jumpmind.db.sql.DmlStatement) Row(org.jumpmind.db.sql.Row)

Example 18 with DmlStatement

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

the class JdbcPersistenceManager method refresh.

@Override
public void refresh(Object object, String catalogName, String schemaName, String tableName) {
    try {
        Table table = findTable(catalogName, schemaName, tableName);
        LinkedHashMap<String, Column> objectToTableMapping = mapObjectToTable(object, table);
        LinkedHashMap<String, Object> objectValuesByColumnName = getObjectValuesByColumnName(object, objectToTableMapping);
        Column[] columns = objectToTableMapping.values().toArray(new Column[objectToTableMapping.size()]);
        List<Column> keys = new ArrayList<Column>(1);
        for (Column column : columns) {
            if (column.isPrimaryKey()) {
                keys.add(column);
            }
        }
        DmlStatement statement = databasePlatform.createDmlStatement(DmlType.SELECT, table.getCatalog(), table.getSchema(), table.getName(), keys.toArray(new Column[keys.size()]), columns, null, null);
        String sql = statement.getSql();
        Object[] values = statement.getValueArray(objectValuesByColumnName);
        Row row = databasePlatform.getSqlTemplate().queryForRow(sql, values);
        if (row != null) {
            Set<String> propertyNames = objectToTableMapping.keySet();
            for (String propertyName : propertyNames) {
                Object value = row.get(objectToTableMapping.get(propertyName).getName());
                BeanUtils.copyProperty(object, propertyName, value);
            }
        }
    } catch (Exception e) {
        throw toRuntimeException(e);
    }
}
Also used : Table(org.jumpmind.db.model.Table) ArrayList(java.util.ArrayList) SqlException(org.jumpmind.db.sql.SqlException) InvocationTargetException(java.lang.reflect.InvocationTargetException) Column(org.jumpmind.db.model.Column) DmlStatement(org.jumpmind.db.sql.DmlStatement) Row(org.jumpmind.db.sql.Row)

Example 19 with DmlStatement

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

the class JdbcPersistenceManager method excecuteDml.

protected int excecuteDml(DmlType type, Object object, String catalogName, String schemaName, String tableName) {
    Table table = findTable(catalogName, schemaName, tableName);
    LinkedHashMap<String, Column> objectToTableMapping = mapObjectToTable(object, table);
    LinkedHashMap<String, Object> objectValuesByColumnName = getObjectValuesByColumnName(object, objectToTableMapping);
    Column[] columns = objectToTableMapping.values().toArray(new Column[objectToTableMapping.size()]);
    List<Column> keys = new ArrayList<Column>(1);
    for (Column column : columns) {
        if (column.isPrimaryKey()) {
            keys.add(column);
        }
    }
    boolean[] nullKeyValues = new boolean[keys.size()];
    int i = 0;
    for (Column column : keys) {
        nullKeyValues[i++] = objectValuesByColumnName.get(column.getName()) == null;
    }
    DmlStatement statement = databasePlatform.createDmlStatement(type, table.getCatalog(), table.getSchema(), table.getName(), keys.toArray(new Column[keys.size()]), columns, nullKeyValues, null);
    String sql = statement.getSql();
    Object[] values = statement.getValueArray(objectValuesByColumnName);
    int[] types = statement.getTypes();
    return databasePlatform.getSqlTemplate().update(sql, values, types);
}
Also used : Table(org.jumpmind.db.model.Table) ArrayList(java.util.ArrayList) Column(org.jumpmind.db.model.Column) DmlStatement(org.jumpmind.db.sql.DmlStatement)

Example 20 with DmlStatement

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

the class DefaultDatabaseWriter method getCurData.

protected String getCurData(ISqlTransaction transaction) {
    String curVal = null;
    if (writerSettings.isSaveCurrentValueOnError()) {
        String[] keyNames = Table.getArrayColumns(context.getTable().getPrimaryKeyColumns());
        String[] columnNames = Table.getArrayColumns(context.getTable().getColumns());
        org.jumpmind.db.model.Table targetTable = platform.getTableFromCache(context.getTable().getCatalog(), context.getTable().getSchema(), context.getTable().getName(), false);
        targetTable = targetTable.copyAndFilterColumns(columnNames, keyNames, true);
        String[] data = context.getData().getParsedData(CsvData.OLD_DATA);
        if (data == null) {
            data = context.getData().getParsedData(CsvData.ROW_DATA);
        }
        Column[] columns = targetTable.getColumns();
        Object[] objectValues = platform.getObjectValues(context.getBatch().getBinaryEncoding(), data, columns);
        Map<String, Object> columnDataMap = CollectionUtils.toMap(columnNames, objectValues);
        Column[] pkColumns = targetTable.getPrimaryKeyColumns();
        Object[] args = new Object[pkColumns.length];
        for (int i = 0; i < pkColumns.length; i++) {
            args[i] = columnDataMap.get(pkColumns[i].getName());
        }
        DmlStatement sqlStatement = platform.createDmlStatement(DmlType.SELECT, targetTable, writerSettings.getTextColumnExpression());
        Row row = null;
        List<Row> list = transaction.query(sqlStatement.getSql(), new ISqlRowMapper<Row>() {

            public Row mapRow(Row row) {
                return row;
            }
        }, args, null);
        if (list != null && list.size() > 0) {
            row = list.get(0);
        }
        if (row != null) {
            String[] existData = platform.getStringValues(context.getBatch().getBinaryEncoding(), columns, row, false, false);
            if (existData != null) {
                curVal = CsvUtils.escapeCsvData(existData);
            }
        }
    }
    return curVal;
}
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

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