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
This commit is contained in:
Milosz Tylenda 2009-05-07 19:57:34 +00:00
parent eb359de6a5
commit 195525392c
2 changed files with 12 additions and 13 deletions

View File

@ -125,8 +125,7 @@ public class PreparedStatementManagerImpl
sql).getMessage()); sql).getMessage());
} }
if (autoAssignColNames != null) if (autoAssignColNames != null)
populateAutoAssignCols(stmnt, autoAssign, autoAssignColNames, populateAutoAssignCols(stmnt, autoAssign, row);
row);
else { else {
StateManagerImpl sm = (StateManagerImpl)row.getPrimaryKey(); StateManagerImpl sm = (StateManagerImpl)row.getPrimaryKey();
if (sm != null) { if (sm != null) {
@ -168,19 +167,18 @@ public class PreparedStatementManagerImpl
* sql to select the key will be issued from DBDictionary. * sql to select the key will be issued from DBDictionary.
*/ */
protected List populateAutoAssignCols(PreparedStatement stmnt, protected List populateAutoAssignCols(PreparedStatement stmnt,
Column[] autoAssign, String[] autoAssignColNames, RowImpl row) Column[] autoAssign, RowImpl row)
throws SQLException { throws SQLException {
List vals = null; List vals = null;
if (_dict.supportsGetGeneratedKeys) { if (_dict.supportsGetGeneratedKeys) {
// set auto assign values to id col // 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; return vals;
} }
protected void setObjectId(List vals, Column[] autoAssign, protected void setObjectId(List vals, Column[] autoAssign, RowImpl row)
String[] autoAssignColNames, RowImpl row)
throws SQLException{ throws SQLException{
OpenJPAStateManager sm = row.getPrimaryKey(); OpenJPAStateManager sm = row.getPrimaryKey();
ClassMapping mapping = (ClassMapping) sm.getMetaData(); ClassMapping mapping = (ClassMapping) sm.getMetaData();
@ -203,13 +201,13 @@ public class PreparedStatementManagerImpl
* getGeneratedKeys. * getGeneratedKeys.
*/ */
protected List getGeneratedKeys(PreparedStatement stmnt, protected List getGeneratedKeys(PreparedStatement stmnt,
String[] autoAssignColNames) Column[] autoAssign)
throws SQLException { throws SQLException {
ResultSet rs = stmnt.getGeneratedKeys(); ResultSet rs = stmnt.getGeneratedKeys();
List vals = new ArrayList(); List<Object> vals = new ArrayList<Object>();
while (rs.next()) { while (rs.next()) {
for (int i = 0; i < autoAssignColNames.length; i++) for (int i = 0; i < autoAssign.length; i++)
vals.add(rs.getObject(autoAssignColNames[i])); vals.add(rs.getObject(i + 1));
} }
rs.close(); rs.close();
return vals; return vals;
@ -228,7 +226,8 @@ public class PreparedStatementManagerImpl
&& row.getPrimaryKey() != null) { && row.getPrimaryKey() != null) {
autoAssignColNames = new String[autoAssign.length]; autoAssignColNames = new String[autoAssign.length];
for (int i = 0; i < autoAssign.length; i++) for (int i = 0; i < autoAssign.length; i++)
autoAssignColNames[i] = autoAssign[i].getName(); autoAssignColNames[i] =
_dict.convertSchemaCase(autoAssign[i].getName());
} }
return autoAssignColNames; return autoAssignColNames;
} }

View File

@ -3990,7 +3990,7 @@ public class DBDictionary
* Convert the specified schema name to a name that the database will * Convert the specified schema name to a name that the database will
* be able to understand. * be able to understand.
*/ */
protected String convertSchemaCase(String objectName) { public String convertSchemaCase(String objectName) {
if (objectName == null) if (objectName == null)
return null; return null;