src/share/classes/com/sun/rowset/internal/CachedRowSetWriter.java

Print this page




 248  *     <code>CachedRowSet</code> object back to its underlying data source
 249  *   <UL>
 250  *      <LI>Iterates through each row of the <code>CachedRowSet</code> object
 251  *          to determine whether it has been updated, inserted, or deleted
 252  *      <LI>If the corresponding row in the data source has not been changed
 253  *          since the rowset last read its
 254  *          values, the writer will use the appropriate command to update,
 255  *          insert, or delete the row
 256  *      <LI>If any data in the data source does not match the original values
 257  *          for the <code>CachedRowSet</code> object, the writer will roll
 258  *          back any changes it has made to the row in the data source.
 259  *   </UL>
 260  * </OL>
 261  *
 262  * @return <code>true</code> if changes to the rowset were successfully
 263  *         written to the rowset's underlying data source;
 264  *         <code>false</code> otherwise
 265  */
 266     public boolean writeData(RowSetInternal caller) throws SQLException {
 267         boolean conflict = false;

 268         boolean showDel = false;
 269         PreparedStatement pstmtIns = null;
 270         iChangedValsInDbAndCRS = 0;
 271         iChangedValsinDbOnly = 0;
 272 
 273         // We assume caller is a CachedRowSet
 274         CachedRowSetImpl crs = (CachedRowSetImpl)caller;
 275         // crsResolve = new CachedRowSetImpl();
 276         this.crsResolve = new CachedRowSetImpl();;
 277 
 278         // The reader is registered with the writer at design time.
 279         // This is not required, in general.  The reader has logic
 280         // to get a JDBC connection, so call it.
 281 
 282         con = reader.connect(caller);
 283 
 284 
 285         if (con == null) {
 286             throw new SQLException(resBundle.handleGetObject("crswriter.connect").toString());
 287         }


 322 
 323         if (callerColumnCount < 1) {
 324             // No data, so return success.
 325             if (reader.getCloseConnection() == true)
 326                     con.close();
 327             return true;
 328         }
 329         // We need to see rows marked for deletion.
 330         showDel = crs.getShowDeleted();
 331         crs.setShowDeleted(true);
 332 
 333         // Look at all the rows.
 334         crs.beforeFirst();
 335 
 336         int rows =1;
 337         while (crs.next()) {
 338             if (crs.rowDeleted()) {
 339                 // The row has been deleted.
 340                 if (conflict = (deleteOriginalRow(crs, this.crsResolve)) == true) {
 341                        status.add(rows, SyncResolver.DELETE_ROW_CONFLICT);

 342                 } else {
 343                       // delete happened without any occurrence of conflicts
 344                       // so update status accordingly
 345                        status.add(rows, SyncResolver.NO_ROW_CONFLICT);
 346                 }
 347 
 348            } else if (crs.rowInserted()) {
 349                 // The row has been inserted.
 350 
 351                 pstmtIns = con.prepareStatement(insertCmd);
 352                 if ( (conflict = insertNewRow(crs, pstmtIns, this.crsResolve)) == true) {
 353                           status.add(rows, SyncResolver.INSERT_ROW_CONFLICT);

 354                 } else {
 355                       // insert happened without any occurrence of conflicts
 356                       // so update status accordingly
 357                        status.add(rows, SyncResolver.NO_ROW_CONFLICT);
 358                 }
 359             } else  if (crs.rowUpdated()) {
 360                   // The row has been updated.
 361                        if ( conflict = (updateOriginalRow(crs)) == true) {
 362                              status.add(rows, SyncResolver.UPDATE_ROW_CONFLICT);

 363                } else {
 364                       // update happened without any occurrence of conflicts
 365                       // so update status accordingly
 366                       status.add(rows, SyncResolver.NO_ROW_CONFLICT);
 367                }
 368 
 369             } else {
 370                /** The row is neither of inserted, updated or deleted.
 371                 *  So set nulls in the this.crsResolve for this row,
 372                 *  as nothing is to be done for such rows.
 373                 *  Also note that if such a row has been changed in database
 374                 *  and we have not changed(inserted, updated or deleted)
 375                 *  that is fine.
 376                 **/
 377                 int icolCount = crs.getMetaData().getColumnCount();
 378                 status.add(rows, SyncResolver.NO_ROW_CONFLICT);
 379 
 380                 this.crsResolve.moveToInsertRow();
 381                 for(int cols=0;cols<iColCount;cols++) {
 382                    this.crsResolve.updateNull(cols+1);


 392         // close the insert statement
 393         if(pstmtIns!=null)
 394         pstmtIns.close();
 395         // reset
 396         crs.setShowDeleted(showDel);
 397 
 398       boolean boolConf = false;
 399       for (int j=1;j<status.size();j++){
 400           // ignore status for index = 0 which is set to null
 401           if(! ((status.get(j)).equals(SyncResolver.NO_ROW_CONFLICT))) {
 402               // there is at least one conflict which needs to be resolved
 403               boolConf = true;
 404              break;
 405           }
 406       }
 407 
 408         crs.beforeFirst();
 409         this.crsResolve.beforeFirst();
 410 
 411     if(boolConf) {
 412         SyncProviderException spe = new SyncProviderException(status.size() - 1+resBundle.handleGetObject("crswriter.conflictsno").toString());


 413         //SyncResolver syncRes = spe.getSyncResolver();
 414 
 415          SyncResolverImpl syncResImpl = (SyncResolverImpl) spe.getSyncResolver();
 416 
 417          syncResImpl.setCachedRowSet(crs);
 418          syncResImpl.setCachedRowSetResolver(this.crsResolve);
 419 
 420          syncResImpl.setStatus(status);
 421          syncResImpl.setCachedRowSetWriter(this);
 422 
 423         throw spe;
 424     } else {
 425          return true;
 426     }
 427        /*
 428        if (conflict == true) {
 429             con.rollback();
 430             return false;
 431         } else {
 432             con.commit();




 248  *     <code>CachedRowSet</code> object back to its underlying data source
 249  *   <UL>
 250  *      <LI>Iterates through each row of the <code>CachedRowSet</code> object
 251  *          to determine whether it has been updated, inserted, or deleted
 252  *      <LI>If the corresponding row in the data source has not been changed
 253  *          since the rowset last read its
 254  *          values, the writer will use the appropriate command to update,
 255  *          insert, or delete the row
 256  *      <LI>If any data in the data source does not match the original values
 257  *          for the <code>CachedRowSet</code> object, the writer will roll
 258  *          back any changes it has made to the row in the data source.
 259  *   </UL>
 260  * </OL>
 261  *
 262  * @return <code>true</code> if changes to the rowset were successfully
 263  *         written to the rowset's underlying data source;
 264  *         <code>false</code> otherwise
 265  */
 266     public boolean writeData(RowSetInternal caller) throws SQLException {
 267         boolean conflict = false;
 268         int conflictNum = 0;
 269         boolean showDel = false;
 270         PreparedStatement pstmtIns = null;
 271         iChangedValsInDbAndCRS = 0;
 272         iChangedValsinDbOnly = 0;
 273 
 274         // We assume caller is a CachedRowSet
 275         CachedRowSetImpl crs = (CachedRowSetImpl)caller;
 276         // crsResolve = new CachedRowSetImpl();
 277         this.crsResolve = new CachedRowSetImpl();;
 278 
 279         // The reader is registered with the writer at design time.
 280         // This is not required, in general.  The reader has logic
 281         // to get a JDBC connection, so call it.
 282 
 283         con = reader.connect(caller);
 284 
 285 
 286         if (con == null) {
 287             throw new SQLException(resBundle.handleGetObject("crswriter.connect").toString());
 288         }


 323 
 324         if (callerColumnCount < 1) {
 325             // No data, so return success.
 326             if (reader.getCloseConnection() == true)
 327                     con.close();
 328             return true;
 329         }
 330         // We need to see rows marked for deletion.
 331         showDel = crs.getShowDeleted();
 332         crs.setShowDeleted(true);
 333 
 334         // Look at all the rows.
 335         crs.beforeFirst();
 336 
 337         int rows =1;
 338         while (crs.next()) {
 339             if (crs.rowDeleted()) {
 340                 // The row has been deleted.
 341                 if (conflict = (deleteOriginalRow(crs, this.crsResolve)) == true) {
 342                        status.add(rows, SyncResolver.DELETE_ROW_CONFLICT);
 343                     conflictNum++;
 344                 } else {
 345                       // delete happened without any occurrence of conflicts
 346                       // so update status accordingly
 347                        status.add(rows, SyncResolver.NO_ROW_CONFLICT);
 348                 }
 349 
 350            } else if (crs.rowInserted()) {
 351                 // The row has been inserted.
 352 
 353                 pstmtIns = con.prepareStatement(insertCmd);
 354                 if ( (conflict = insertNewRow(crs, pstmtIns, this.crsResolve)) == true) {
 355                           status.add(rows, SyncResolver.INSERT_ROW_CONFLICT);
 356                     conflictNum++;
 357                 } else {
 358                       // insert happened without any occurrence of conflicts
 359                       // so update status accordingly
 360                        status.add(rows, SyncResolver.NO_ROW_CONFLICT);
 361                 }
 362             } else  if (crs.rowUpdated()) {
 363                   // The row has been updated.
 364                        if ( conflict = (updateOriginalRow(crs)) == true) {
 365                              status.add(rows, SyncResolver.UPDATE_ROW_CONFLICT);
 366                 conflictNum++;
 367                } else {
 368                       // update happened without any occurrence of conflicts
 369                       // so update status accordingly
 370                       status.add(rows, SyncResolver.NO_ROW_CONFLICT);
 371                }
 372 
 373             } else {
 374                /** The row is neither of inserted, updated or deleted.
 375                 *  So set nulls in the this.crsResolve for this row,
 376                 *  as nothing is to be done for such rows.
 377                 *  Also note that if such a row has been changed in database
 378                 *  and we have not changed(inserted, updated or deleted)
 379                 *  that is fine.
 380                 **/
 381                 int icolCount = crs.getMetaData().getColumnCount();
 382                 status.add(rows, SyncResolver.NO_ROW_CONFLICT);
 383 
 384                 this.crsResolve.moveToInsertRow();
 385                 for(int cols=0;cols<iColCount;cols++) {
 386                    this.crsResolve.updateNull(cols+1);


 396         // close the insert statement
 397         if(pstmtIns!=null)
 398         pstmtIns.close();
 399         // reset
 400         crs.setShowDeleted(showDel);
 401 
 402       boolean boolConf = false;
 403       for (int j=1;j<status.size();j++){
 404           // ignore status for index = 0 which is set to null
 405           if(! ((status.get(j)).equals(SyncResolver.NO_ROW_CONFLICT))) {
 406               // there is at least one conflict which needs to be resolved
 407               boolConf = true;
 408              break;
 409           }
 410       }
 411 
 412         crs.beforeFirst();
 413         this.crsResolve.beforeFirst();
 414 
 415     if(boolConf) {
 416         SyncProviderException spe = new SyncProviderException(conflictNum + " "
 417             + resBundle.handleGetObject("crswriter.conflictsno").toString());
 418 
 419         //SyncResolver syncRes = spe.getSyncResolver();
 420 
 421          SyncResolverImpl syncResImpl = (SyncResolverImpl) spe.getSyncResolver();
 422 
 423          syncResImpl.setCachedRowSet(crs);
 424          syncResImpl.setCachedRowSetResolver(this.crsResolve);
 425 
 426          syncResImpl.setStatus(status);
 427          syncResImpl.setCachedRowSetWriter(this);
 428 
 429         throw spe;
 430     } else {
 431          return true;
 432     }
 433        /*
 434        if (conflict == true) {
 435             con.rollback();
 436             return false;
 437         } else {
 438             con.commit();