use of org.jumpmind.db.sql.DmlStatement in project symmetric-ds by JumpMind.
the class DbCompareDiffWriter method writeInsert.
public void writeInsert(DbCompareRow sourceCompareRow) {
if (stream == null) {
return;
}
Table targetTable = tables.getTargetTable();
DmlStatement statement = targetEngine.getDatabasePlatform().createDmlStatement(DmlType.INSERT, targetTable.getCatalog(), targetTable.getSchema(), targetTable.getName(), targetTable.getPrimaryKeyColumns(), targetTable.getColumns(), null, null);
Row row = new Row(targetTable.getColumnCount());
for (Column sourceColumn : tables.getSourceTable().getColumns()) {
Column targetColumn = tables.getColumnMapping().get(sourceColumn);
if (targetColumn == null) {
continue;
}
row.put(targetColumn.getName(), sourceCompareRow.getRowValues().get(sourceColumn.getName()));
}
String sql = statement.buildDynamicSql(BinaryEncoding.HEX, row, false, false);
writeLine(sql);
}
use of org.jumpmind.db.sql.DmlStatement in project symmetric-ds by JumpMind.
the class DbCompare method getComparisonSQL.
protected String getComparisonSQL(Table table, Column[] sortByColumns, IDatabasePlatform platform, String whereClause) {
DmlStatement statement = platform.createDmlStatement(DmlType.SELECT, table.getCatalog(), table.getSchema(), table.getName(), null, table.getColumns(), null, null);
StringBuilder sql = new StringBuilder(statement.getSql());
sql.append(whereClause).append(" ");
sql.append(buildOrderBy(table, sortByColumns, platform));
log.info("Comparison SQL: {}", sql);
return sql.toString();
}
use of org.jumpmind.db.sql.DmlStatement in project symmetric-ds by JumpMind.
the class DataService method getForeignTableRows.
protected List<TableRow> getForeignTableRows(List<TableRow> tableRows, Set<TableRow> visited) throws CloneNotSupportedException {
List<TableRow> fkDepList = new ArrayList<TableRow>();
for (TableRow tableRow : tableRows) {
if (!visited.contains(tableRow)) {
visited.add(tableRow);
for (ForeignKey fk : tableRow.getTable().getForeignKeys()) {
Table table = platform.getTableFromCache(fk.getForeignTableName(), false);
if (table == null) {
table = fk.getForeignTable();
if (table == null) {
table = platform.getTableFromCache(tableRow.getTable().getCatalog(), tableRow.getTable().getSchema(), fk.getForeignTableName(), false);
}
}
if (table != null) {
Table foreignTable = (Table) table.clone();
for (Column column : foreignTable.getColumns()) {
column.setPrimaryKey(false);
}
Row whereRow = new Row(fk.getReferenceCount());
String referenceColumnName = null;
boolean[] nullValues = new boolean[fk.getReferenceCount()];
int index = 0;
for (Reference ref : fk.getReferences()) {
Column foreignColumn = foreignTable.findColumn(ref.getForeignColumnName());
Object value = tableRow.getRow().get(ref.getLocalColumnName());
nullValues[index++] = value == null;
referenceColumnName = ref.getLocalColumnName();
whereRow.put(foreignColumn.getName(), value);
foreignColumn.setPrimaryKey(true);
}
boolean allNullValues = true;
for (boolean b : nullValues) {
if (!b) {
allNullValues = false;
break;
}
}
if (!allNullValues) {
DmlStatement whereSt = platform.createDmlStatement(DmlType.WHERE, foreignTable.getCatalog(), foreignTable.getSchema(), foreignTable.getName(), foreignTable.getPrimaryKeyColumns(), foreignTable.getColumns(), nullValues, null);
String whereSql = whereSt.buildDynamicSql(symmetricDialect.getBinaryEncoding(), whereRow, false, true, foreignTable.getPrimaryKeyColumns()).substring(6);
String delimiter = platform.getDatabaseInfo().getSqlCommandDelimiter();
if (delimiter != null && delimiter.length() > 0) {
whereSql = whereSql.substring(0, whereSql.length() - delimiter.length());
}
Row foreignRow = new Row(foreignTable.getColumnCount());
if (foreignTable.getForeignKeyCount() > 0) {
DmlStatement selectSt = platform.createDmlStatement(DmlType.SELECT, foreignTable, null);
Object[] keys = whereRow.toArray(foreignTable.getPrimaryKeyColumnNames());
Map<String, Object> values = sqlTemplate.queryForMap(selectSt.getSql(), keys);
if (values == null) {
log.warn("Unable to reload rows for missing foreign key data for table '{}', parent data not found. Using sql='{}' with keys '{}'", table.getName(), selectSt.getSql(), keys);
} else {
foreignRow.putAll(values);
}
}
TableRow foreignTableRow = new TableRow(foreignTable, foreignRow, whereSql, referenceColumnName, fk.getName());
fkDepList.add(foreignTableRow);
log.debug("Add foreign table reference '{}' whereSql='{}'", foreignTable.getName(), whereSql);
} else {
log.debug("The foreign table reference was null for {}", foreignTable.getName());
}
} else {
log.debug("Foreign table '{}' not found for foreign key '{}'", fk.getForeignTableName(), fk.getName());
}
if (fkDepList.size() > 0) {
fkDepList.addAll(getForeignTableRows(fkDepList, visited));
}
}
}
}
return fkDepList;
}
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(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;
}
}
}
use of org.jumpmind.db.sql.DmlStatement in project symmetric-ds by JumpMind.
the class DbFill method selectSpecificRow.
private Row selectSpecificRow(ISqlTransaction tran, Table table, Column[] keyColumns, Object[] values) {
Row row = null;
DmlStatement stmt = platform.createDmlStatement(DmlType.SELECT, table.getCatalog(), table.getSchema(), table.getName(), keyColumns, table.getColumns(), null, textColumnExpression);
if (verbose) {
StringBuilder sb = null;
for (int i = 0; i < keyColumns.length; i++) {
sb = (sb == null) ? new StringBuilder() : sb.append(", ");
sb.append(keyColumns[i].getName()).append("=").append(values[i]);
}
log.info("Selecting row from {} where {}", table.getName(), sb.toString());
}
List<Row> rows = queryForRows(tran, stmt.getSql(), values, stmt.getTypes());
if (rows.size() != 0) {
int rowNum = getRand().nextInt(rows.size());
row = rows.get(rowNum);
} else {
StringBuilder sb = null;
for (int i = 0; i < keyColumns.length; i++) {
sb = (sb == null) ? new StringBuilder() : sb.append(", ");
sb.append(keyColumns[i].getName()).append("=").append(values[i]);
}
log.warn("Unable to find row from {} where {}", table.getName(), sb.toString());
}
return row;
}
Aggregations