Search in sources :

Example 76 with Row

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

the class DbFill method selectRandomRow.

/**
 * Select a random row from the table in the connected database. Return null if there are no rows.
 *
 * TODO: Cache rows.
 *
 * @param sqlTemplate
 * @param table The table to select a row from.
 * @return A random row from the table. Null if there are no rows.
 */
private Row selectRandomRow(Table table) {
    Row row = null;
    // Select all rows and return the primary key columns.
    String sql = platform.createDmlStatement(DmlType.SELECT_ALL, table.getCatalog(), table.getSchema(), table.getName(), table.getPrimaryKeyColumns(), table.getColumns(), null, textColumnExpression).getSql();
    final List<Row> rows = new ArrayList<Row>();
    platform.getSqlTemplate().query(sql, RANDOM_SELECT_SIZE, new ISqlRowMapper<Object>() {

        public Object mapRow(Row row) {
            rows.add(row);
            return Boolean.TRUE;
        }
    }, null, null);
    if (rows.size() != 0) {
        int rowNum = getRand().nextInt(rows.size());
        row = rows.get(rowNum);
    }
    return row;
}
Also used : ArrayList(java.util.ArrayList) Row(org.jumpmind.db.sql.Row)

Example 77 with Row

use of org.jumpmind.db.sql.Row 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)

Example 78 with Row

use of org.jumpmind.db.sql.Row 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)

Aggregations

Row (org.jumpmind.db.sql.Row)78 ArrayList (java.util.ArrayList)31 ISqlRowMapper (org.jumpmind.db.sql.ISqlRowMapper)23 Trigger (org.jumpmind.db.model.Trigger)19 DmlStatement (org.jumpmind.db.sql.DmlStatement)19 JdbcSqlTemplate (org.jumpmind.db.sql.JdbcSqlTemplate)19 Column (org.jumpmind.db.model.Column)15 Table (org.jumpmind.db.model.Table)12 Date (java.util.Date)11 SqlException (org.jumpmind.db.sql.SqlException)9 ISqlTemplate (org.jumpmind.db.sql.ISqlTemplate)8 Test (org.junit.Test)8 HashMap (java.util.HashMap)7 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 HashSet (java.util.HashSet)3 Map (java.util.Map)3 IDatabasePlatform (org.jumpmind.db.platform.IDatabasePlatform)3 IoException (org.jumpmind.exception.IoException)3 NetworkedNode (org.jumpmind.symmetric.model.NetworkedNode)3 Node (org.jumpmind.symmetric.model.Node)3