提交 818809d7 编写于 作者: L lancea

7145913: CachedRowSetSwriter.insertNewRow() throws SQLException

Reviewed-by: joehw, naoto, psandoz, forax
上级 a00f0039
/* /*
* Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -812,101 +812,119 @@ public class CachedRowSetWriter implements TransactionalWriter, Serializable { ...@@ -812,101 +812,119 @@ public class CachedRowSetWriter implements TransactionalWriter, Serializable {
} }
} }
/** /**
* Inserts a row that has been inserted into the given * Inserts a row that has been inserted into the given
* <code>CachedRowSet</code> object into the data source from which * <code>CachedRowSet</code> object into the data source from which
* the rowset is derived, returning <code>false</code> if the insertion * the rowset is derived, returning <code>false</code> if the insertion
* was successful. * was successful.
* *
* @param crs the <code>CachedRowSet</code> object that has had a row inserted * @param crs the <code>CachedRowSet</code> object that has had a row inserted
* and to whose underlying data source the row will be inserted * and to whose underlying data source the row will be inserted
* @param pstmt the <code>PreparedStatement</code> object that will be used * @param pstmt the <code>PreparedStatement</code> object that will be used
* to execute the insertion * to execute the insertion
* @return <code>false</code> to indicate that the insertion was successful; * @return <code>false</code> to indicate that the insertion was successful;
* <code>true</code> otherwise * <code>true</code> otherwise
* @throws SQLException if a database access error occurs * @throws SQLException if a database access error occurs
*/ */
private boolean insertNewRow(CachedRowSet crs, private boolean insertNewRow(CachedRowSet crs,
PreparedStatement pstmt, CachedRowSetImpl crsRes) throws SQLException { PreparedStatement pstmt, CachedRowSetImpl crsRes) throws SQLException {
int i = 0;
int icolCount = crs.getMetaData().getColumnCount(); boolean returnVal = false;
boolean returnVal = false; try (PreparedStatement pstmtSel = con.prepareStatement(selectCmd,
PreparedStatement pstmtSel = con.prepareStatement(selectCmd, ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); ResultSet.CONCUR_READ_ONLY);
ResultSet rs, rs2 = null; ResultSet rs = pstmtSel.executeQuery();
DatabaseMetaData dbmd = con.getMetaData(); ResultSet rs2 = con.getMetaData().getPrimaryKeys(null, null,
rs = pstmtSel.executeQuery(); crs.getTableName())
String table = crs.getTableName(); ) {
rs2 = dbmd.getPrimaryKeys(null, null, table);
String [] primaryKeys = new String[icolCount]; ResultSetMetaData rsmd = crs.getMetaData();
int k = 0; int icolCount = rsmd.getColumnCount();
while(rs2.next()) { String[] primaryKeys = new String[icolCount];
String pkcolname = rs2.getString("COLUMN_NAME"); int k = 0;
primaryKeys[k] = pkcolname; while (rs2.next()) {
k++; primaryKeys[k] = rs2.getString("COLUMN_NAME");
} k++;
}
if(rs.next()) {
for(int j=0;j<primaryKeys.length;j++) { if (rs.next()) {
if(primaryKeys[j] != null) { for (String pkName : primaryKeys) {
if(crs.getObject(primaryKeys[j]) == null){ if (!isPKNameValid(pkName, rsmd)) {
break;
} /* We came here as one of the the primary keys
String crsPK = (crs.getObject(primaryKeys[j])).toString(); * of the table is not present in the cached
String rsPK = (rs.getObject(primaryKeys[j])).toString(); * rowset object, it should be an autoincrement column
if(crsPK.equals(rsPK)) { * and not included while creating CachedRowSet
returnVal = true; * Object, proceed to check for other primary keys
this.crsResolve.moveToInsertRow(); */
for(i = 1; i <= icolCount; i++) { continue;
String colname = (rs.getMetaData()).getColumnName(i); }
if(colname.equals(primaryKeys[j]))
this.crsResolve.updateObject(i,rsPK); Object crsPK = crs.getObject(pkName);
else if (crsPK == null) {
this.crsResolve.updateNull(i); /*
} * It is possible that the PK is null on some databases
this.crsResolve.insertRow(); * and will be filled in at insert time (MySQL for example)
this.crsResolve.moveToCurrentRow(); */
} break;
} }
}
} String rsPK = rs.getObject(pkName).toString();
if(returnVal) if (crsPK.toString().equals(rsPK)) {
return returnVal; returnVal = true;
this.crsResolve.moveToInsertRow();
try { for (int i = 1; i <= icolCount; i++) {
for (i = 1; i <= icolCount; i++) { String colname = (rs.getMetaData()).getColumnName(i);
Object obj = crs.getObject(i); if (colname.equals(pkName))
if (obj != null) { this.crsResolve.updateObject(i,rsPK);
pstmt.setObject(i, obj); else
} else { this.crsResolve.updateNull(i);
pstmt.setNull(i,crs.getMetaData().getColumnType(i)); }
} this.crsResolve.insertRow();
} this.crsResolve.moveToCurrentRow();
}
i = pstmt.executeUpdate(); }
return false; }
} catch (SQLException ex) { if (returnVal) {
/** return returnVal;
* Cursor will come here if executeUpdate fails. }
* There can be many reasons why the insertion failed,
* one can be violation of primary key. try {
* Hence we cannot exactly identify why the insertion failed for (int i = 1; i <= icolCount; i++) {
* Present the current row as a null row to the user. Object obj = crs.getObject(i);
**/ if (obj != null) {
this.crsResolve.moveToInsertRow(); pstmt.setObject(i, obj);
} else {
pstmt.setNull(i,crs.getMetaData().getColumnType(i));
}
}
for(i = 1; i <= icolCount; i++) { pstmt.executeUpdate();
this.crsResolve.updateNull(i); return false;
}
} catch (SQLException ex) {
/*
* Cursor will come here if executeUpdate fails.
* There can be many reasons why the insertion failed,
* one can be violation of primary key.
* Hence we cannot exactly identify why the insertion failed,
* present the current row as a null row to the caller.
*/
this.crsResolve.moveToInsertRow();
for (int i = 1; i <= icolCount; i++) {
this.crsResolve.updateNull(i);
}
this.crsResolve.insertRow(); this.crsResolve.insertRow();
this.crsResolve.moveToCurrentRow(); this.crsResolve.moveToCurrentRow();
return true; return true;
} }
} }
}
/** /**
* Deletes the row in the underlying data source that corresponds to * Deletes the row in the underlying data source that corresponds to
...@@ -1437,4 +1455,25 @@ public class CachedRowSetWriter implements TransactionalWriter, Serializable { ...@@ -1437,4 +1455,25 @@ public class CachedRowSetWriter implements TransactionalWriter, Serializable {
} }
static final long serialVersionUID =-8506030970299413976L; static final long serialVersionUID =-8506030970299413976L;
/**
* Validate whether the Primary Key is known to the CachedRowSet. If it is
* not, it is an auto-generated key
* @param pk - Primary Key to validate
* @param rsmd - ResultSetMetadata for the RowSet
* @return true if found, false otherwise (auto generated key)
*/
private boolean isPKNameValid(String pk, ResultSetMetaData rsmd) throws SQLException {
boolean isValid = false;
int cols = rsmd.getColumnCount();
for(int i = 1; i<= cols; i++) {
String colName = rsmd.getColumnClassName(i);
if(colName.equalsIgnoreCase(pk)) {
isValid = true;
break;
}
}
return isValid;
}
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册