|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
|
|
package com.sun.rowset.internal; |
|
|
|
import java.sql.*; |
|
import javax.sql.*; |
|
import java.util.*; |
|
import java.io.*; |
|
import sun.reflect.misc.ReflectUtil; |
|
|
|
import com.sun.rowset.*; |
|
import java.text.MessageFormat; |
|
import javax.sql.rowset.*; |
|
import javax.sql.rowset.serial.SQLInputImpl; |
|
import javax.sql.rowset.serial.SerialArray; |
|
import javax.sql.rowset.serial.SerialBlob; |
|
import javax.sql.rowset.serial.SerialClob; |
|
import javax.sql.rowset.serial.SerialStruct; |
|
import javax.sql.rowset.spi.*; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public class CachedRowSetWriter implements TransactionalWriter, Serializable { |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
private transient Connection con; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
private String selectCmd; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
private String updateCmd; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
private String updateWhere; |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
private String deleteCmd; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
private String deleteWhere; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
private String insertCmd; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
private int[] keyCols; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
private Object[] params; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
private CachedRowSetReader reader; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
private ResultSetMetaData callerMd; |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
private int callerColumnCount; |
|
|
|
|
|
|
|
|
|
*/ |
|
private CachedRowSetImpl crsResolve; |
|
|
|
|
|
|
|
*/ |
|
private ArrayList<Integer> status; |
|
|
|
|
|
|
|
|
|
*/ |
|
private int iChangedValsInDbAndCRS; |
|
|
|
|
|
|
|
|
|
*/ |
|
private int iChangedValsinDbOnly ; |
|
|
|
private JdbcRowSetResourceBundle resBundle; |
|
|
|
public CachedRowSetWriter() { |
|
try { |
|
resBundle = JdbcRowSetResourceBundle.getJdbcRowSetResourceBundle(); |
|
} catch(IOException ioe) { |
|
throw new RuntimeException(ioe); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public boolean writeData(RowSetInternal caller) throws SQLException { |
|
long conflicts = 0; |
|
boolean showDel = false; |
|
PreparedStatement pstmtIns = null; |
|
iChangedValsInDbAndCRS = 0; |
|
iChangedValsinDbOnly = 0; |
|
|
|
|
|
CachedRowSetImpl crs = (CachedRowSetImpl)caller; |
|
|
|
this.crsResolve = new CachedRowSetImpl();; |
|
|
|
// The reader is registered with the writer at design time. |
|
// This is not required, in general. The reader has logic |
|
// to get a JDBC connection, so call it. |
|
|
|
con = reader.connect(caller); |
|
|
|
|
|
if (con == null) { |
|
throw new SQLException(resBundle.handleGetObject("crswriter.connect").toString()); |
|
} |
|
|
|
/* |
|
// Fix 6200646. |
|
// Don't change the connection or transaction properties. This will fail in a |
|
// J2EE container. |
|
if (con.getAutoCommit() == true) { |
|
con.setAutoCommit(false); |
|
} |
|
|
|
con.setTransactionIsolation(crs.getTransactionIsolation()); |
|
*/ |
|
|
|
initSQLStatements(crs); |
|
int iColCount; |
|
|
|
RowSetMetaDataImpl rsmdWrite = (RowSetMetaDataImpl)crs.getMetaData(); |
|
RowSetMetaDataImpl rsmdResolv = new RowSetMetaDataImpl(); |
|
|
|
iColCount = rsmdWrite.getColumnCount(); |
|
int sz= crs.size()+1; |
|
status = new ArrayList<>(sz); |
|
|
|
status.add(0,null); |
|
rsmdResolv.setColumnCount(iColCount); |
|
|
|
for(int i =1; i <= iColCount; i++) { |
|
rsmdResolv.setColumnType(i, rsmdWrite.getColumnType(i)); |
|
rsmdResolv.setColumnName(i, rsmdWrite.getColumnName(i)); |
|
rsmdResolv.setNullable(i, ResultSetMetaData.columnNullableUnknown); |
|
} |
|
this.crsResolve.setMetaData(rsmdResolv); |
|
|
|
// moved outside the insert inner loop |
|
//pstmtIns = con.prepareStatement(insertCmd); |
|
|
|
if (callerColumnCount < 1) { |
|
|
|
if (reader.getCloseConnection() == true) |
|
con.close(); |
|
return true; |
|
} |
|
|
|
showDel = crs.getShowDeleted(); |
|
crs.setShowDeleted(true); |
|
|
|
|
|
crs.beforeFirst(); |
|
|
|
int rows =1; |
|
while (crs.next()) { |
|
if (crs.rowDeleted()) { |
|
|
|
if (deleteOriginalRow(crs, this.crsResolve)) { |
|
status.add(rows, SyncResolver.DELETE_ROW_CONFLICT); |
|
conflicts++; |
|
} else { |
|
// delete happened without any occurrence of conflicts |
|
|
|
status.add(rows, SyncResolver.NO_ROW_CONFLICT); |
|
} |
|
|
|
} else if (crs.rowInserted()) { |
|
// The row has been inserted. |
|
|
|
pstmtIns = con.prepareStatement(insertCmd); |
|
if (insertNewRow(crs, pstmtIns, this.crsResolve)) { |
|
status.add(rows, SyncResolver.INSERT_ROW_CONFLICT); |
|
conflicts++; |
|
} else { |
|
// insert happened without any occurrence of conflicts |
|
|
|
status.add(rows, SyncResolver.NO_ROW_CONFLICT); |
|
} |
|
} else if (crs.rowUpdated()) { |
|
|
|
if (updateOriginalRow(crs)) { |
|
status.add(rows, SyncResolver.UPDATE_ROW_CONFLICT); |
|
conflicts++; |
|
} else { |
|
// update happened without any occurrence of conflicts |
|
|
|
status.add(rows, SyncResolver.NO_ROW_CONFLICT); |
|
} |
|
|
|
} else { |
|
|
|
|
|
|
|
|
|
|
|
|
|
**/ |
|
int icolCount = crs.getMetaData().getColumnCount(); |
|
status.add(rows, SyncResolver.NO_ROW_CONFLICT); |
|
|
|
this.crsResolve.moveToInsertRow(); |
|
for(int cols=0;cols<iColCount;cols++) { |
|
this.crsResolve.updateNull(cols+1); |
|
} //end for |
|
|
|
this.crsResolve.insertRow(); |
|
this.crsResolve.moveToCurrentRow(); |
|
|
|
} |
|
rows++; |
|
} //end while |
|
|
|
|
|
if(pstmtIns!=null) |
|
pstmtIns.close(); |
|
|
|
crs.setShowDeleted(showDel); |
|
|
|
crs.beforeFirst(); |
|
this.crsResolve.beforeFirst(); |
|
|
|
if(conflicts != 0) { |
|
SyncProviderException spe = new SyncProviderException(conflicts + " " + |
|
resBundle.handleGetObject("crswriter.conflictsno").toString()); |
|
//SyncResolver syncRes = spe.getSyncResolver(); |
|
|
|
SyncResolverImpl syncResImpl = (SyncResolverImpl) spe.getSyncResolver(); |
|
|
|
syncResImpl.setCachedRowSet(crs); |
|
syncResImpl.setCachedRowSetResolver(this.crsResolve); |
|
|
|
syncResImpl.setStatus(status); |
|
syncResImpl.setCachedRowSetWriter(this); |
|
|
|
throw spe; |
|
} else { |
|
return true; |
|
} |
|
/* |
|
if (conflict == true) { |
|
con.rollback(); |
|
return false; |
|
} else { |
|
con.commit(); |
|
if (reader.getCloseConnection() == true) { |
|
con.close(); |
|
} |
|
return true; |
|
} |
|
*/ |
|
|
|
} //end writeData |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
private boolean updateOriginalRow(CachedRowSet crs) |
|
throws SQLException { |
|
PreparedStatement pstmt; |
|
int i = 0; |
|
int idx = 0; |
|
|
|
|
|
ResultSet origVals = crs.getOriginalRow(); |
|
origVals.next(); |
|
|
|
try { |
|
updateWhere = buildWhereClause(updateWhere, origVals); |
|
|
|
|
|
/** |
|
* The following block of code is for checking a particular type of |
|
* query where in there is a where clause. Without this block, if a |
|
* SQL statement is built the "where" clause will appear twice hence |
|
* the DB errors out and a SQLException is thrown. This code also |
|
* considers that the where clause is in the right place as the |
|
* CachedRowSet object would already have been populated with this |
|
* query before coming to this point. |
|
**/ |
|
|
|
|
|
String tempselectCmd = selectCmd.toLowerCase(); |
|
|
|
int idxWhere = tempselectCmd.indexOf("where"); |
|
|
|
if(idxWhere != -1) |
|
{ |
|
String tempSelect = selectCmd.substring(0,idxWhere); |
|
selectCmd = tempSelect; |
|
} |
|
|
|
pstmt = con.prepareStatement(selectCmd + updateWhere, |
|
ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); |
|
|
|
for (i = 0; i < keyCols.length; i++) { |
|
if (params[i] != null) { |
|
pstmt.setObject(++idx, params[i]); |
|
} else { |
|
continue; |
|
} |
|
} |
|
|
|
try { |
|
pstmt.setMaxRows(crs.getMaxRows()); |
|
pstmt.setMaxFieldSize(crs.getMaxFieldSize()); |
|
pstmt.setEscapeProcessing(crs.getEscapeProcessing()); |
|
pstmt.setQueryTimeout(crs.getQueryTimeout()); |
|
} catch (Exception ex) { |
|
// Older driver don't support these operations. |
|
} |
|
|
|
ResultSet rs = null; |
|
rs = pstmt.executeQuery(); |
|
ResultSetMetaData rsmd = rs.getMetaData(); |
|
|
|
if (rs.next()) { |
|
if (rs.next()) { |
|
/** More than one row conflict. |
|
* If rs has only one row we are able to |
|
* uniquely identify the row where update |
|
* have to happen else if more than one |
|
* row implies we cannot uniquely identify the row |
|
* where we have to do updates. |
|
* crs.setKeyColumns needs to be set to |
|
* come out of this situation. |
|
*/ |
|
|
|
return true; |
|
} |
|
|
|
// don't close the rs |
|
// we require the record in rs to be used. |
|
// rs.close(); |
|
|
|
rs.first(); |
|
|
|
|
|
int colsNotChanged = 0; |
|
Vector<Integer> cols = new Vector<>(); |
|
String updateExec = updateCmd; |
|
Object orig; |
|
Object curr; |
|
Object rsval; |
|
boolean boolNull = true; |
|
Object objVal = null; |
|
|
|
// There's only one row and the cursor |
|
// needs to be on that row. |
|
|
|
boolean first = true; |
|
boolean flag = true; |
|
|
|
this.crsResolve.moveToInsertRow(); |
|
|
|
for (i = 1; i <= callerColumnCount; i++) { |
|
orig = origVals.getObject(i); |
|
curr = crs.getObject(i); |
|
rsval = rs.getObject(i); |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
Map<String, Class<?>> map = (crs.getTypeMap() == null)?con.getTypeMap():crs.getTypeMap(); |
|
if (rsval instanceof Struct) { |
|
|
|
Struct s = (Struct)rsval; |
|
|
|
|
|
Class<?> c = null; |
|
c = map.get(s.getSQLTypeName()); |
|
if (c != null) { |
|
|
|
SQLData obj = null; |
|
try { |
|
obj = (SQLData)ReflectUtil.newInstance(c); |
|
} catch (Exception ex) { |
|
throw new SQLException("Unable to Instantiate: ", ex); |
|
} |
|
|
|
Object attribs[] = s.getAttributes(map); |
|
|
|
SQLInputImpl sqlInput = new SQLInputImpl(attribs, map); |
|
|
|
obj.readSQL(sqlInput, s.getSQLTypeName()); |
|
rsval = obj; |
|
} |
|
} else if (rsval instanceof SQLData) { |
|
rsval = new SerialStruct((SQLData)rsval, map); |
|
} else if (rsval instanceof Blob) { |
|
rsval = new SerialBlob((Blob)rsval); |
|
} else if (rsval instanceof Clob) { |
|
rsval = new SerialClob((Clob)rsval); |
|
} else if (rsval instanceof java.sql.Array) { |
|
rsval = new SerialArray((java.sql.Array)rsval, map); |
|
} |
|
|
|
|
|
boolNull = true; |
|
|
|
/** This addtional checking has been added when the current value |
|
* in the DB is null, but the DB had a different value when the |
|
* data was actaully fetched into the CachedRowSet. |
|
**/ |
|
|
|
if(rsval == null && orig != null) { |
|
// value in db has changed |
|
// don't proceed with synchronization |
|
// get the value in db and pass it to the resolver. |
|
|
|
iChangedValsinDbOnly++; |
|
// Set the boolNull to false, |
|
|
|
boolNull = false; |
|
objVal = rsval; |
|
} |
|
|
|
/** Adding the checking for rsval to be "not" null or else |
|
* it would through a NullPointerException when the values |
|
* are compared. |
|
**/ |
|
|
|
else if(rsval != null && (!rsval.equals(orig))) |
|
{ |
|
// value in db has changed |
|
// don't proceed with synchronization |
|
// get the value in db and pass it to the resolver. |
|
|
|
iChangedValsinDbOnly++; |
|
// Set the boolNull to false, |
|
|
|
boolNull = false; |
|
objVal = rsval; |
|
} else if ( (orig == null || curr == null) ) { |
|
|
|
/** Adding the additonal condition of checking for "flag" |
|
* boolean variable, which would otherwise result in |
|
* building a invalid query, as the comma would not be |
|
* added to the query string. |
|
**/ |
|
|
|
if (first == false || flag == false) { |
|
updateExec += ", "; |
|
} |
|
updateExec += crs.getMetaData().getColumnName(i); |
|
cols.add(i); |
|
updateExec += " = ? "; |
|
first = false; |
|
|
|
/** Adding the extra condition for orig to be "not" null as the |
|
* condition for orig to be null is take prior to this, if this |
|
* is not added it will result in a NullPointerException when |
|
* the values are compared. |
|
**/ |
|
|
|
} else if (orig.equals(curr)) { |
|
colsNotChanged++; |
|
//nothing to update in this case since values are equal |
|
|
|
/** Adding the extra condition for orig to be "not" null as the |
|
* condition for orig to be null is take prior to this, if this |
|
* is not added it will result in a NullPointerException when |
|
* the values are compared. |
|
**/ |
|
|
|
} else if(orig.equals(curr) == false) { |
|
// When values from db and values in CachedRowSet are not equal, |
|
// if db value is same as before updation for each col in |
|
// the row before fetching into CachedRowSet, |
|
// only then we go ahead with updation, else we |
|
// throw SyncProviderException. |
|
|
|
// if value has changed in db after fetching from db |
|
// for some cols of the row and at the same time, some other cols |
|
// have changed in CachedRowSet, no synchronization happens |
|
|
|
// Synchronization happens only when data when fetching is |
|
// same or at most has changed in cachedrowset |
|
|
|
// check orig value with what is there in crs for a column |
|
// before updation in crs. |
|
|
|
if(crs.columnUpdated(i)) { |
|
if(rsval.equals(orig)) { |
|
// At this point we are sure that |
|
// the value updated in crs was from |
|
|
|
if (flag == false || first == false) { |
|
updateExec += ", "; |
|
} |
|
updateExec += crs.getMetaData().getColumnName(i); |
|
cols.add(i); |
|
updateExec += " = ? "; |
|
flag = false; |
|
} else { |
|
// Here the value has changed in the db after |
|
// data was fetched |
|
// Plus store this row from CachedRowSet and keep it |
|
|
|
boolNull= false; |
|
objVal = rsval; |
|
iChangedValsInDbAndCRS++; |
|
} |
|
} |
|
} |
|
|
|
if(!boolNull) { |
|
this.crsResolve.updateObject(i,objVal); |
|
} else { |
|
this.crsResolve.updateNull(i); |
|
} |
|
} //end for |
|
|
|
rs.close(); |
|
pstmt.close(); |
|
|
|
this.crsResolve.insertRow(); |
|
this.crsResolve.moveToCurrentRow(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
**/ |
|
if ( (first == false && cols.size() == 0) || |
|
colsNotChanged == callerColumnCount ) { |
|
return false; |
|
} |
|
|
|
if(iChangedValsInDbAndCRS != 0 || iChangedValsinDbOnly != 0) { |
|
return true; |
|
} |
|
|
|
|
|
updateExec += updateWhere; |
|
|
|
pstmt = con.prepareStatement(updateExec); |
|
|
|
|
|
for (i = 0; i < cols.size(); i++) { |
|
Object obj = crs.getObject(cols.get(i)); |
|
if (obj != null) |
|
pstmt.setObject(i + 1, obj); |
|
else |
|
pstmt.setNull(i + 1,crs.getMetaData().getColumnType(i + 1)); |
|
} |
|
idx = i; |
|
|
|
|
|
for (i = 0; i < keyCols.length; i++) { |
|
if (params[i] != null) { |
|
pstmt.setObject(++idx, params[i]); |
|
} else { |
|
continue; |
|
} |
|
} |
|
|
|
i = pstmt.executeUpdate(); |
|
|
|
/** |
|
* i should be equal to 1(row count), because we update |
|
* one row(returned as row count) at a time, if all goes well. |
|
* if 1 != 1, this implies we have not been able to |
|
* do updations properly i.e there is a conflict in database |
|
* versus what is in CachedRowSet for this particular row. |
|
**/ |
|
|
|
return false; |
|
|
|
} else { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
**/ |
|
return true; |
|
} |
|
} catch (SQLException ex) { |
|
ex.printStackTrace(); |
|
// if executeUpdate fails it will come here, |
|
|
|
this.crsResolve.moveToInsertRow(); |
|
|
|
for(i = 1; i <= callerColumnCount; i++) { |
|
this.crsResolve.updateNull(i); |
|
} |
|
|
|
this.crsResolve.insertRow(); |
|
this.crsResolve.moveToCurrentRow(); |
|
|
|
return true; |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
private boolean insertNewRow(CachedRowSet crs, |
|
PreparedStatement pstmt, CachedRowSetImpl crsRes) throws SQLException { |
|
|
|
boolean returnVal = false; |
|
|
|
try (PreparedStatement pstmtSel = con.prepareStatement(selectCmd, |
|
ResultSet.TYPE_SCROLL_SENSITIVE, |
|
ResultSet.CONCUR_READ_ONLY); |
|
ResultSet rs = pstmtSel.executeQuery(); |
|
ResultSet rs2 = con.getMetaData().getPrimaryKeys(null, null, |
|
crs.getTableName()) |
|
) { |
|
|
|
ResultSetMetaData rsmd = crs.getMetaData(); |
|
int icolCount = rsmd.getColumnCount(); |
|
String[] primaryKeys = new String[icolCount]; |
|
int k = 0; |
|
while (rs2.next()) { |
|
primaryKeys[k] = rs2.getString("COLUMN_NAME"); |
|
k++; |
|
} |
|
|
|
if (rs.next()) { |
|
for (String pkName : primaryKeys) { |
|
if (!isPKNameValid(pkName, rsmd)) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
continue; |
|
} |
|
|
|
Object crsPK = crs.getObject(pkName); |
|
if (crsPK == null) { |
|
|
|
|
|
|
|
*/ |
|
break; |
|
} |
|
|
|
String rsPK = rs.getObject(pkName).toString(); |
|
if (crsPK.toString().equals(rsPK)) { |
|
returnVal = true; |
|
this.crsResolve.moveToInsertRow(); |
|
for (int i = 1; i <= icolCount; i++) { |
|
String colname = (rs.getMetaData()).getColumnName(i); |
|
if (colname.equals(pkName)) |
|
this.crsResolve.updateObject(i,rsPK); |
|
else |
|
this.crsResolve.updateNull(i); |
|
} |
|
this.crsResolve.insertRow(); |
|
this.crsResolve.moveToCurrentRow(); |
|
} |
|
} |
|
} |
|
|
|
if (returnVal) { |
|
return returnVal; |
|
} |
|
|
|
try { |
|
for (int i = 1; i <= icolCount; i++) { |
|
Object obj = crs.getObject(i); |
|
if (obj != null) { |
|
pstmt.setObject(i, obj); |
|
} else { |
|
pstmt.setNull(i,crs.getMetaData().getColumnType(i)); |
|
} |
|
} |
|
|
|
pstmt.executeUpdate(); |
|
return false; |
|
|
|
} catch (SQLException ex) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
this.crsResolve.moveToInsertRow(); |
|
|
|
for (int i = 1; i <= icolCount; i++) { |
|
this.crsResolve.updateNull(i); |
|
} |
|
|
|
this.crsResolve.insertRow(); |
|
this.crsResolve.moveToCurrentRow(); |
|
|
|
return true; |
|
} |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
private boolean deleteOriginalRow(CachedRowSet crs, CachedRowSetImpl crsRes) throws SQLException { |
|
PreparedStatement pstmt; |
|
int i; |
|
int idx = 0; |
|
String strSelect; |
|
|
|
ResultSet origVals = crs.getOriginalRow(); |
|
origVals.next(); |
|
|
|
deleteWhere = buildWhereClause(deleteWhere, origVals); |
|
pstmt = con.prepareStatement(selectCmd + deleteWhere, |
|
ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); |
|
|
|
for (i = 0; i < keyCols.length; i++) { |
|
if (params[i] != null) { |
|
pstmt.setObject(++idx, params[i]); |
|
} else { |
|
continue; |
|
} |
|
} |
|
|
|
try { |
|
pstmt.setMaxRows(crs.getMaxRows()); |
|
pstmt.setMaxFieldSize(crs.getMaxFieldSize()); |
|
pstmt.setEscapeProcessing(crs.getEscapeProcessing()); |
|
pstmt.setQueryTimeout(crs.getQueryTimeout()); |
|
} catch (Exception ex) { |
|
|
|
|
|
*/ |
|
; |
|
} |
|
|
|
ResultSet rs = pstmt.executeQuery(); |
|
|
|
if (rs.next() == true) { |
|
if (rs.next()) { |
|
|
|
return true; |
|
} |
|
rs.first(); |
|
|
|
// Now check all the values in rs to be same in |
|
|
|
boolean boolChanged = false; |
|
|
|
crsRes.moveToInsertRow(); |
|
|
|
for (i = 1; i <= crs.getMetaData().getColumnCount(); i++) { |
|
|
|
Object original = origVals.getObject(i); |
|
Object changed = rs.getObject(i); |
|
|
|
if(original != null && changed != null ) { |
|
if(! (original.toString()).equals(changed.toString()) ) { |
|
boolChanged = true; |
|
crsRes.updateObject(i,origVals.getObject(i)); |
|
} |
|
} else { |
|
crsRes.updateNull(i); |
|
} |
|
} |
|
|
|
crsRes.insertRow(); |
|
crsRes.moveToCurrentRow(); |
|
|
|
if(boolChanged) { |
|
// do not delete as values in db have changed |
|
// deletion will not happen for this row from db |
|
|
|
return true; |
|
} else { |
|
// delete the row. |
|
// Go ahead with deleting, |
|
// don't do anything here |
|
} |
|
|
|
String cmd = deleteCmd + deleteWhere; |
|
pstmt = con.prepareStatement(cmd); |
|
|
|
idx = 0; |
|
for (i = 0; i < keyCols.length; i++) { |
|
if (params[i] != null) { |
|
pstmt.setObject(++idx, params[i]); |
|
} else { |
|
continue; |
|
} |
|
} |
|
|
|
if (pstmt.executeUpdate() != 1) { |
|
return true; |
|
} |
|
pstmt.close(); |
|
} else { |
|
|
|
return true; |
|
} |
|
|
|
|
|
return false; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void setReader(CachedRowSetReader reader) throws SQLException { |
|
this.reader = reader; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public CachedRowSetReader getReader() throws SQLException { |
|
return reader; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
private void initSQLStatements(CachedRowSet caller) throws SQLException { |
|
|
|
int i; |
|
|
|
callerMd = caller.getMetaData(); |
|
callerColumnCount = callerMd.getColumnCount(); |
|
if (callerColumnCount < 1) |
|
|
|
return; |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
String table = caller.getTableName(); |
|
if (table == null) { |
|
|
|
|
|
|
|
|
|
*/ |
|
table = callerMd.getTableName(1); |
|
if (table == null || table.length() == 0) { |
|
throw new SQLException(resBundle.handleGetObject("crswriter.tname").toString()); |
|
} |
|
} |
|
String catalog = callerMd.getCatalogName(1); |
|
String schema = callerMd.getSchemaName(1); |
|
DatabaseMetaData dbmd = con.getMetaData(); |
|
|
|
/* |
|
* Compose a SELECT statement. There are three parts. |
|
*/ |
|
|
|
|
|
selectCmd = "SELECT "; |
|
for (i=1; i <= callerColumnCount; i++) { |
|
selectCmd += callerMd.getColumnName(i); |
|
if ( i < callerMd.getColumnCount() ) |
|
selectCmd += ", "; |
|
else |
|
selectCmd += " "; |
|
} |
|
|
|
|
|
selectCmd += "FROM " + buildTableName(dbmd, catalog, schema, table); |
|
|
|
|
|
|
|
*/ |
|
updateCmd = "UPDATE " + buildTableName(dbmd, catalog, schema, table); |
|
|
|
|
|
/** |
|
* The following block of code is for checking a particular type of |
|
* query where in there is a where clause. Without this block, if a |
|
* SQL statement is built the "where" clause will appear twice hence |
|
* the DB errors out and a SQLException is thrown. This code also |
|
* considers that the where clause is in the right place as the |
|
* CachedRowSet object would already have been populated with this |
|
* query before coming to this point. |
|
**/ |
|
|
|
String tempupdCmd = updateCmd.toLowerCase(); |
|
|
|
int idxupWhere = tempupdCmd.indexOf("where"); |
|
|
|
if(idxupWhere != -1) |
|
{ |
|
updateCmd = updateCmd.substring(0,idxupWhere); |
|
} |
|
updateCmd += "SET "; |
|
|
|
|
|
|
|
*/ |
|
insertCmd = "INSERT INTO " + buildTableName(dbmd, catalog, schema, table); |
|
|
|
insertCmd += "("; |
|
for (i=1; i <= callerColumnCount; i++) { |
|
insertCmd += callerMd.getColumnName(i); |
|
if ( i < callerMd.getColumnCount() ) |
|
insertCmd += ", "; |
|
else |
|
insertCmd += ") VALUES ("; |
|
} |
|
for (i=1; i <= callerColumnCount; i++) { |
|
insertCmd += "?"; |
|
if (i < callerColumnCount) |
|
insertCmd += ", "; |
|
else |
|
insertCmd += ")"; |
|
} |
|
|
|
|
|
|
|
*/ |
|
deleteCmd = "DELETE FROM " + buildTableName(dbmd, catalog, schema, table); |
|
|
|
|
|
|
|
|
|
*/ |
|
buildKeyDesc(caller); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
private String buildTableName(DatabaseMetaData dbmd, |
|
String catalog, String schema, String table) throws SQLException { |
|
|
|
// trim all the leading and trailing whitespaces, |
|
// white spaces can never be catalog, schema or a table name. |
|
|
|
String cmd = ""; |
|
|
|
catalog = catalog.trim(); |
|
schema = schema.trim(); |
|
table = table.trim(); |
|
|
|
if (dbmd.isCatalogAtStart() == true) { |
|
if (catalog != null && catalog.length() > 0) { |
|
cmd += catalog + dbmd.getCatalogSeparator(); |
|
} |
|
if (schema != null && schema.length() > 0) { |
|
cmd += schema + "."; |
|
} |
|
cmd += table; |
|
} else { |
|
if (schema != null && schema.length() > 0) { |
|
cmd += schema + "."; |
|
} |
|
cmd += table; |
|
if (catalog != null && catalog.length() > 0) { |
|
cmd += dbmd.getCatalogSeparator() + catalog; |
|
} |
|
} |
|
cmd += " "; |
|
return cmd; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
private void buildKeyDesc(CachedRowSet crs) throws SQLException { |
|
|
|
keyCols = crs.getKeyColumns(); |
|
ResultSetMetaData resultsetmd = crs.getMetaData(); |
|
if (keyCols == null || keyCols.length == 0) { |
|
ArrayList<Integer> listKeys = new ArrayList<Integer>(); |
|
|
|
for (int i = 0; i < callerColumnCount; i++ ) { |
|
if(resultsetmd.getColumnType(i+1) != java.sql.Types.CLOB && |
|
resultsetmd.getColumnType(i+1) != java.sql.Types.STRUCT && |
|
resultsetmd.getColumnType(i+1) != java.sql.Types.SQLXML && |
|
resultsetmd.getColumnType(i+1) != java.sql.Types.BLOB && |
|
resultsetmd.getColumnType(i+1) != java.sql.Types.ARRAY && |
|
resultsetmd.getColumnType(i+1) != java.sql.Types.OTHER ) |
|
listKeys.add(i+1); |
|
} |
|
keyCols = new int[listKeys.size()]; |
|
for (int i = 0; i < listKeys.size(); i++ ) |
|
keyCols[i] = listKeys.get(i); |
|
} |
|
params = new Object[keyCols.length]; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
private String buildWhereClause(String whereClause, |
|
ResultSet rs) throws SQLException { |
|
whereClause = "WHERE "; |
|
|
|
for (int i = 0; i < keyCols.length; i++) { |
|
if (i > 0) { |
|
whereClause += "AND "; |
|
} |
|
whereClause += callerMd.getColumnName(keyCols[i]); |
|
params[i] = rs.getObject(keyCols[i]); |
|
if (rs.wasNull() == true) { |
|
whereClause += " IS NULL "; |
|
} else { |
|
whereClause += " = ? "; |
|
} |
|
} |
|
return whereClause; |
|
} |
|
|
|
void updateResolvedConflictToDB(CachedRowSet crs, Connection con) throws SQLException { |
|
|
|
PreparedStatement pStmt ; |
|
String strWhere = "WHERE " ; |
|
String strExec =" "; |
|
String strUpdate = "UPDATE "; |
|
int icolCount = crs.getMetaData().getColumnCount(); |
|
int keyColumns[] = crs.getKeyColumns(); |
|
Object param[]; |
|
String strSet=""; |
|
|
|
strWhere = buildWhereClause(strWhere, crs); |
|
|
|
if (keyColumns == null || keyColumns.length == 0) { |
|
keyColumns = new int[icolCount]; |
|
for (int i = 0; i < keyColumns.length; ) { |
|
keyColumns[i] = ++i; |
|
} |
|
} |
|
param = new Object[keyColumns.length]; |
|
|
|
strUpdate = "UPDATE " + buildTableName(con.getMetaData(), |
|
crs.getMetaData().getCatalogName(1), |
|
crs.getMetaData().getSchemaName(1), |
|
crs.getTableName()); |
|
|
|
// changed or updated values will become part of |
|
|
|
strUpdate += "SET "; |
|
|
|
boolean first = true; |
|
|
|
for (int i=1; i<=icolCount;i++) { |
|
if (crs.columnUpdated(i)) { |
|
if (first == false) { |
|
strSet += ", "; |
|
} |
|
strSet += crs.getMetaData().getColumnName(i); |
|
strSet += " = ? "; |
|
first = false; |
|
} //end if |
|
} //end for |
|
|
|
|
|
strUpdate += strSet; |
|
strWhere = "WHERE "; |
|
|
|
for (int i = 0; i < keyColumns.length; i++) { |
|
if (i > 0) { |
|
strWhere += "AND "; |
|
} |
|
strWhere += crs.getMetaData().getColumnName(keyColumns[i]); |
|
param[i] = crs.getObject(keyColumns[i]); |
|
if (crs.wasNull() == true) { |
|
strWhere += " IS NULL "; |
|
} else { |
|
strWhere += " = ? "; |
|
} |
|
} |
|
strUpdate += strWhere; |
|
|
|
pStmt = con.prepareStatement(strUpdate); |
|
|
|
int idx =0; |
|
for (int i = 0; i < icolCount; i++) { |
|
if(crs.columnUpdated(i+1)) { |
|
Object obj = crs.getObject(i+1); |
|
if (obj != null) { |
|
pStmt.setObject(++idx, obj); |
|
} else { |
|
pStmt.setNull(i + 1,crs.getMetaData().getColumnType(i + 1)); |
|
} //end if ..else |
|
} //end if crs.column... |
|
} //end for |
|
|
|
|
|
for (int i = 0; i < keyColumns.length; i++) { |
|
if (param[i] != null) { |
|
pStmt.setObject(++idx, param[i]); |
|
} |
|
} |
|
|
|
int id = pStmt.executeUpdate(); |
|
} |
|
|
|
|
|
|
|
|
|
*/ |
|
public void commit() throws SQLException { |
|
con.commit(); |
|
if (reader.getCloseConnection() == true) { |
|
con.close(); |
|
} |
|
} |
|
|
|
public void commit(CachedRowSetImpl crs, boolean updateRowset) throws SQLException { |
|
con.commit(); |
|
if(updateRowset) { |
|
if(crs.getCommand() != null) |
|
crs.execute(con); |
|
} |
|
|
|
if (reader.getCloseConnection() == true) { |
|
con.close(); |
|
} |
|
} |
|
|
|
|
|
|
|
*/ |
|
public void rollback() throws SQLException { |
|
con.rollback(); |
|
if (reader.getCloseConnection() == true) { |
|
con.close(); |
|
} |
|
} |
|
|
|
|
|
|
|
*/ |
|
public void rollback(Savepoint s) throws SQLException { |
|
con.rollback(s); |
|
if (reader.getCloseConnection() == true) { |
|
con.close(); |
|
} |
|
} |
|
|
|
private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException { |
|
|
|
ois.defaultReadObject(); |
|
|
|
try { |
|
resBundle = JdbcRowSetResourceBundle.getJdbcRowSetResourceBundle(); |
|
} catch(IOException ioe) { |
|
throw new RuntimeException(ioe); |
|
} |
|
|
|
} |
|
|
|
static final long serialVersionUID =-8506030970299413976L; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
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; |
|
} |
|
} |