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;
}
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;
}
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);
}
Aggregations