Search in sources :

Example 11 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(ISqlTransaction tran, 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();
    if (verbose) {
        log.info("Selecting row from " + table.getName());
    }
    List<Row> rows = queryForRows(tran, sql, null, null);
    if (rows.size() != 0) {
        int rowNum = getRand().nextInt(rows.size());
        row = rows.get(rowNum);
    } else {
        log.warn("Unable to find a row in table " + table.getName());
    }
    return row;
}
Also used : Row(org.jumpmind.db.sql.Row)

Example 12 with Row

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

the class DbFill method createDynamicRandomDeleteSql.

public String createDynamicRandomDeleteSql(Table table) {
    DmlStatement deleteStatement = createDeleteDmlStatement(table);
    Row row = selectRandomRow(null, table);
    return deleteStatement.buildDynamicDeleteSql(BinaryEncoding.HEX, row, false, true);
}
Also used : DmlStatement(org.jumpmind.db.sql.DmlStatement) Row(org.jumpmind.db.sql.Row)

Example 13 with Row

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

the class DbFill method selectRandomRecord.

private void selectRandomRecord(ISqlTransaction tran, Table table) {
    Row row = null;
    if (hasDependentColumns(table)) {
        Map<Column, Object> dependent = getDependentColumnValues(table);
        if (dependent.size() > 0) {
            row = selectSpecificRow(tran, table, dependent.keySet().toArray(new Column[dependent.size()]), dependent.values().toArray(new Object[dependent.size()]));
            saveDependentColumnValues(table, row);
        } else {
            row = selectRandomRow(tran, table);
            saveDependentColumnValues(table, row);
        }
    } else {
        row = selectRandomRow(tran, table);
    }
    currentRowValues.put(table.getName(), row);
}
Also used : Column(org.jumpmind.db.model.Column) Row(org.jumpmind.db.sql.Row)

Example 14 with Row

use of org.jumpmind.db.sql.Row 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(ISqlTransaction tran, Table table) {
    DmlStatement insertStatement = createInsertDmlStatement(table);
    Row row = createRandomInsertValues(insertStatement, table);
    try {
        tran.prepareAndExecute(insertStatement.getSql(), insertStatement.getValueArray(row.toArray(table.getColumnNames()), row.toArray(table.getPrimaryKeyColumnNames())));
    } catch (SqlException ex) {
        log.info("Failed to insert into {}: {}", table.getName(), ex.getMessage());
        if (continueOnError) {
            if (debug) {
                logRow(row);
                log.info("", ex);
            }
            selectRandomRecord(tran, table);
        } else {
            throw ex;
        }
    }
}
Also used : SqlException(org.jumpmind.db.sql.SqlException) DmlStatement(org.jumpmind.db.sql.DmlStatement) Row(org.jumpmind.db.sql.Row)

Example 15 with Row

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

the class DbFill method createRandomInsertValues.

private Row createRandomInsertValues(DmlStatement updStatement, Table table) {
    Column[] columns = updStatement.getMetaData();
    Row row = new Row(columns.length);
    for (int i = 0; i < columns.length; i++) {
        Object value = null;
        ForeignKeyReference fkr = foreignKeyReferences.get(table.getQualifiedColumnName(columns[i]));
        if (fkr != null) {
            Map<String, Object> foreignRowValues = currentRowValues.get(fkr.getForeignKey().getForeignTableName());
            if (foreignRowValues != null) {
                value = foreignRowValues.get(fkr.getReference().getForeignColumnName());
            }
        } else {
            value = generateRandomValueForColumn(columns[i]);
        }
        row.put(columns[i].getName(), value);
    }
    currentRowValues.put(table.getName(), row);
    return row;
}
Also used : Column(org.jumpmind.db.model.Column) 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