From 195525392c8e4d846f7cad03d8074e79719c5889 Mon Sep 17 00:00:00 2001 From: Milosz Tylenda <mtylenda@apache.org> Date: Thu, 7 May 2009 19:57:34 +0000 Subject: [PATCH] OPENJPA-736: Change the way generated keys are retrieved so that the feature works also with databases other than DB2 git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@772756 13f79535-47bb-0310-9956-ffa450edef68 --- .../kernel/PreparedStatementManagerImpl.java | 23 +++++++++---------- .../apache/openjpa/jdbc/sql/DBDictionary.java | 2 +- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/PreparedStatementManagerImpl.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/PreparedStatementManagerImpl.java index 2c3265485..8a8f54306 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/PreparedStatementManagerImpl.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/PreparedStatementManagerImpl.java @@ -125,8 +125,7 @@ public class PreparedStatementManagerImpl sql).getMessage()); } if (autoAssignColNames != null) - populateAutoAssignCols(stmnt, autoAssign, autoAssignColNames, - row); + populateAutoAssignCols(stmnt, autoAssign, row); else { StateManagerImpl sm = (StateManagerImpl)row.getPrimaryKey(); if (sm != null) { @@ -168,19 +167,18 @@ public class PreparedStatementManagerImpl * sql to select the key will be issued from DBDictionary. */ protected List populateAutoAssignCols(PreparedStatement stmnt, - Column[] autoAssign, String[] autoAssignColNames, RowImpl row) + Column[] autoAssign, RowImpl row) throws SQLException { List vals = null; if (_dict.supportsGetGeneratedKeys) { // set auto assign values to id col - vals = getGeneratedKeys(stmnt, autoAssignColNames); + vals = getGeneratedKeys(stmnt, autoAssign); } - setObjectId(vals, autoAssign, autoAssignColNames, row); + setObjectId(vals, autoAssign, row); return vals; } - protected void setObjectId(List vals, Column[] autoAssign, - String[] autoAssignColNames, RowImpl row) + protected void setObjectId(List vals, Column[] autoAssign, RowImpl row) throws SQLException{ OpenJPAStateManager sm = row.getPrimaryKey(); ClassMapping mapping = (ClassMapping) sm.getMetaData(); @@ -203,13 +201,13 @@ public class PreparedStatementManagerImpl * getGeneratedKeys. */ protected List getGeneratedKeys(PreparedStatement stmnt, - String[] autoAssignColNames) + Column[] autoAssign) throws SQLException { ResultSet rs = stmnt.getGeneratedKeys(); - List vals = new ArrayList(); + List<Object> vals = new ArrayList<Object>(); while (rs.next()) { - for (int i = 0; i < autoAssignColNames.length; i++) - vals.add(rs.getObject(autoAssignColNames[i])); + for (int i = 0; i < autoAssign.length; i++) + vals.add(rs.getObject(i + 1)); } rs.close(); return vals; @@ -228,7 +226,8 @@ public class PreparedStatementManagerImpl && row.getPrimaryKey() != null) { autoAssignColNames = new String[autoAssign.length]; for (int i = 0; i < autoAssign.length; i++) - autoAssignColNames[i] = autoAssign[i].getName(); + autoAssignColNames[i] = + _dict.convertSchemaCase(autoAssign[i].getName()); } return autoAssignColNames; } 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 405e61e30..628f2c840 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 @@ -3990,7 +3990,7 @@ public class DBDictionary * Convert the specified schema name to a name that the database will * be able to understand. */ - protected String convertSchemaCase(String objectName) { + public String convertSchemaCase(String objectName) { if (objectName == null) return null;