/* * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package com.sun.rowset.internal; import java.sql.*; import java.io.*; import java.lang.*; import java.util.*; /** * A class that keeps track of a row's values. A Row object * maintains an array of current column values and an array of original * column values, and it provides methods for getting and setting the * value of a column. It also keeps track of which columns have * changed and whether the change was a delete, insert, or update. *

* Note that column numbers for rowsets start at 1, * whereas the first element of an array or bitset is 0. * The argument for the method getColumnUpdated refers to * the column number in the rowset (the first column is 1); * the argument for setColumnUpdated refers to the index * into the rowset's internal bitset (the first bit is 0). */ public class Row extends BaseRow implements Serializable, Cloneable { static final long serialVersionUID = 5047859032611314762L; /** * An array containing the current column values for this Row * object. * @serial */ private Object[] currentVals; /** * A BitSet object containing a flag for each column in * this Row object, with each flag indicating whether or * not the value in the column has been changed. * @serial */ private BitSet colsChanged; /** * A boolean indicating whether or not this Row * object has been deleted. true indicates that it has * been deleted; false indicates that it has not. * @serial */ private boolean deleted; /** * A boolean indicating whether or not this Row * object has been updated. true indicates that it has * been updated; false indicates that it has not. * @serial */ private boolean updated; /** * A boolean indicating whether or not this Row * object has been inserted. true indicates that it has * been inserted; false indicates that it has not. * @serial */ private boolean inserted; /** * The number of columns in this Row object. * @serial */ private int numCols; /** * Creates a new Row object with the given number of columns. * The newly-created row includes an array of original values, * an array for storing its current values, and a BitSet * object for keeping track of which column values have been changed. */ public Row(int numCols) { origVals = new Object[numCols]; currentVals = new Object[numCols]; colsChanged = new BitSet(numCols); this.numCols = numCols; } /** * Creates a new Row object with the given number of columns * and with its array of original values initialized to the given array. * The new Row object also has an array for storing its * current values and a BitSet object for keeping track * of which column values have been changed. */ public Row(int numCols, Object[] vals) { origVals = new Object[numCols]; System.arraycopy(vals, 0, origVals, 0, numCols); currentVals = new Object[numCols]; colsChanged = new BitSet(numCols); this.numCols = numCols; } /** * * This method is called internally by the CachedRowSet.populate * methods. * * @param idx the number of the column in this Row object * that is to be set; the index of the first column is * 1 * @param val the new value to be set */ public void initColumnObject(int idx, Object val) { origVals[idx - 1] = val; } /** * * This method is called internally by the CachedRowSet.updateXXX * methods. * * @param idx the number of the column in this Row object * that is to be set; the index of the first column is * 1 * @param val the new value to be set */ public void setColumnObject(int idx, Object val) { currentVals[idx - 1] = val; setColUpdated(idx - 1); } /** * Retrieves the column value stored in the designated column of this * Row object. * * @param columnIndex the index of the column value to be retrieved; * the index of the first column is 1 * @return an Object in the Java programming language that * represents the value stored in the designated column * @throws SQLException if there is a database access error */ public Object getColumnObject(int columnIndex) throws SQLException { if (getColUpdated(columnIndex - 1)) { return(currentVals[columnIndex - 1]); // maps to array!! } else { return(origVals[columnIndex - 1]); // maps to array!! } } /** * Indicates whether the designated column of this Row object * has been changed. * @param idx the index into the BitSet object maintained by * this Row object to keep track of which column * values have been modified; the index of the first bit is * 0 * @return true if the designated column value has been changed; * false otherwise * */ public boolean getColUpdated(int idx) { return colsChanged.get(idx); } /** * Sets this Row object's deleted field * to true. * * @see #getDeleted */ public void setDeleted() { // %%% was public deleted = true; } /** * Retrieves the value of this Row object's deleted field, * which will be true if one or more of its columns has been * deleted. * @return true if a column value has been deleted; false * otherwise * * @see #setDeleted */ public boolean getDeleted() { return(deleted); } /** * Sets the deleted field for this Row object to * false. */ public void clearDeleted() { deleted = false; } /** * Sets the value of this Row object's inserted field * to true. * * @see #getInserted */ public void setInserted() { inserted = true; } /** * Retrieves the value of this Row object's inserted field, * which will be true if this row has been inserted. * @return true if this row has been inserted; false * otherwise * * @see #setInserted */ public boolean getInserted() { return(inserted); } /** * Sets the inserted field for this Row object to * false. */ public void clearInserted() { // %%% was public inserted = false; } /** * Retrieves the value of this Row object's * updated field. * @return true if this Row object has been * updated; false if it has not * * @see #setUpdated */ public boolean getUpdated() { return(updated); } /** * Sets the updated field for this Row object to * true if one or more of its column values has been changed. * * @see #getUpdated */ public void setUpdated() { // only mark something as updated if one or // more of the columns has been changed. for (int i = 0; i < numCols; i++) { if (getColUpdated(i) == true) { updated = true; return; } } } /** * Sets the bit at the given index into this Row object's internal * BitSet object, indicating that the corresponding column value * (column idx + 1) has been changed. * * @param idx the index into the BitSet object maintained by * this Row object; the first bit is at index * 0 * */ private void setColUpdated(int idx) { colsChanged.set(idx); } /** * Sets the updated field for this Row object to * false, sets all the column values in this Row * object's internal array of current values to null, and clears * all of the bits in the BitSet object maintained by this * Row object. */ public void clearUpdated() { updated = false; for (int i = 0; i < numCols; i++) { currentVals[i] = null; colsChanged.clear(i); } } /** * Sets the column values in this Row object's internal * array of original values with the values in its internal array of * current values, sets all the values in this Row * object's internal array of current values to null, * clears all the bits in this Row object's internal bitset, * and sets its updated field to false. *

* This method is called internally by the CachedRowSet * method makeRowOriginal. */ public void moveCurrentToOrig() { for (int i = 0; i < numCols; i++) { if (getColUpdated(i) == true) { origVals[i] = currentVals[i]; currentVals[i] = null; colsChanged.clear(i); } } updated = false; } /** * Returns the row on which the cursor is positioned. * * @return the Row object on which the CachedRowSet * implementation objects's cursor is positioned */ public BaseRow getCurrentRow() { return null; } }