From 629977fb1c2a25ba7ce2dd38fe0062866f73b48f Mon Sep 17 00:00:00 2001 From: Patrick Linskey Date: Wed, 11 Jun 2008 23:55:46 +0000 Subject: [PATCH] OPENJPA-597. Merge from ../branches/1.1.x. svn merge -c 655600 ../branches/1.1.x git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@666915 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/openjpa/jdbc/sql/DBDictionary.java | 39 ++++++++++++++++++- .../openjpa/jdbc/sql/MySQLDictionary.java | 1 + 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java index 2eb839fe7..ffd766a13 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java @@ -232,6 +232,7 @@ public class DBDictionary public boolean supportsSelectEndIndex = false; public int rangePosition = RANGE_POST_SELECT; public boolean requiresAliasForSubselect = false; + public boolean requiresTargetForDelete = false; public boolean allowsAliasInBulkClause = true; public boolean supportsMultipleNontransactionalResultSets = true; public String searchStringEscape = "\\"; @@ -1896,8 +1897,16 @@ public class DBDictionary protected SQLBuffer toBulkOperation(ClassMapping mapping, Select sel, JDBCStore store, Object[] params, Map updateParams) { SQLBuffer sql = new SQLBuffer(this); - if (updateParams == null) + if (updateParams == null) { + if (requiresTargetForDelete) { + sql.append("DELETE "); + SQLBuffer deleteTargets = getDeleteTargets(sel); + sql.append(deleteTargets); + sql.append(" FROM "); + } else { sql.append("DELETE FROM "); + } + } else sql.append("UPDATE "); sel.addJoinClassConditions(); @@ -1993,6 +2002,28 @@ public class DBDictionary return sql; } + protected SQLBuffer getDeleteTargets(Select sel) { + SQLBuffer deleteTargets = new SQLBuffer(this); + Collection aliases = sel.getTableAliases(); + // Assumes aliases are of the form "TABLENAME t0" + for (Iterator itr = aliases.iterator(); itr.hasNext();) { + String tableAlias = itr.next().toString(); + int spaceIndex = tableAlias.indexOf(' '); + if (spaceIndex > 0 && spaceIndex < tableAlias.length() - 1) { + if (allowsAliasInBulkClause) { + deleteTargets.append(tableAlias.substring(spaceIndex + 1)); + } else { + deleteTargets.append(tableAlias.substring(0, spaceIndex)); + } + } else { + deleteTargets.append(tableAlias); + } + if (itr.hasNext()) + deleteTargets.append(", "); + } + return deleteTargets; + } + protected void appendUpdates(Select sel, JDBCStore store, SQLBuffer sql, Object[] params, Map updateParams, boolean allowAlias) { if (updateParams == null || updateParams.size() == 0) @@ -2020,7 +2051,11 @@ public class DBDictionary Val val = (Val) next.getValue(); Column col = fmd.getColumns()[0]; - sql.append(col.getName()); + if (allowAlias) { + sql.append(sel.getColumnAlias(col)); + } else { + sql.append(col.getName()); + } sql.append(" = "); ExpState state = val.initialize(sel, ctx, 0); diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/MySQLDictionary.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/MySQLDictionary.java index 9b01fc66f..d3e98abb4 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/MySQLDictionary.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/MySQLDictionary.java @@ -73,6 +73,7 @@ public class MySQLDictionary constraintNameMode = CONS_NAME_MID; supportsMultipleNontransactionalResultSets = false; requiresAliasForSubselect = true; // new versions + requiresTargetForDelete = true; supportsSelectStartIndex = true; supportsSelectEndIndex = true;