Connection connection = dbCon.getConnection();
// Set up a list of required tables and add extra entries to
// criteria if directed to delete all related records.
// StringStackBuffer.add() only adds element if it is unique.
StringStackBuffer tables = new StringStackBuffer();
Enumeration e = criteria.keys();
while(e.hasMoreElements())
{
String key = (String)e.nextElement();
Criteria.Criterion c = criteria.getCriterion(key);
String[] tableNames = c.getAllTables();
for (int i=0; i<tableNames.length; i++)
{
String tableName2 = criteria.getTableForAlias(tableNames[i]);
if ( tableName2 != null )
{
tables.add(
new StringBuffer(tableNames[i].length() +
tableName2.length() + 1)
.append(tableName2).append(' ').append(tableNames[i])
.toString() );
}
else
{
tables.add(tableNames[i]);
}
}
if ( criteria.isCascade() )
{
// This steps thru all the columns in the database.
TableMap[] tableMaps = dbMap.getTables();
for (int i=0; i<tableMaps.length; i++)
{
ColumnMap[] columnMaps = tableMaps[i].getColumns();
for (int j=0; j<columnMaps.length; j++)
{
// Only delete rows where the foreign key is
// also a primary key. Other rows need
// updateing, but that is not implemented.
if ( columnMaps[j].isForeignKey()
&& columnMaps[j].isPrimaryKey()
&& key.equals(columnMaps[j].getRelatedName()) )
{
tables.add(tableMaps[i].getName());
criteria.add(columnMaps[j].getFullyQualifiedName(),
criteria.getValue(key));
}
}
}
}
}
for (int i=0; i<tables.size(); i++)
{
KeyDef kd = new KeyDef();
StringStackBuffer whereClause = new StringStackBuffer();
ColumnMap[] columnMaps =
dbMap.getTable( tables.get(i) ).getColumns();
for (int j=0; j<columnMaps.length; j++)
{
ColumnMap colMap = columnMaps[j];
if ( colMap.isPrimaryKey() )
{
kd.addAttrib( colMap.getColumnName() );
}
String key = new StringBuffer(colMap.getTableName())
.append('.').append(colMap.getColumnName()).toString();
if ( criteria.containsKey(key) )
{
if ( criteria.getComparison(key).equals(Criteria.CUSTOM) )
{
whereClause.add( criteria.getString(key) );
}
else
{
whereClause.add( SqlExpression.build( colMap.getColumnName(),
criteria.getValue(key),
criteria.getComparison(key),
criteria.isIgnoreCase(),
db));
}
}
}
// Execute the statement.
TableDataSet tds = null;
try
{
tds = new TableDataSet(connection, tables.get(i), kd);
String sqlSnippet = whereClause.toString(" AND ");
TurbineLogging.getLogger(TurbineConstants.SQL_LOG_FACILITY)
.debug("BasePeer.doDelete: whereClause=" + sqlSnippet);
tds.where(sqlSnippet);
tds.fetchRecords();
if ( tds.size() > 1 && criteria.isSingleRecord() )