J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2003-2006 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | package com.sun.rowset.internal; |
| 27 | |
| 28 | import java.sql.*; |
| 29 | import java.io.*; |
| 30 | import java.math.*; |
| 31 | import java.lang.*; |
| 32 | import java.lang.reflect.*; |
| 33 | import java.util.*; |
| 34 | |
| 35 | /** |
| 36 | * A class that keeps track of a row's values. A <code>Row</code> object |
| 37 | * maintains an array of current column values and an array of original |
| 38 | * column values, and it provides methods for getting and setting the |
| 39 | * value of a column. It also keeps track of which columns have |
| 40 | * changed and whether the change was a delete, insert, or update. |
| 41 | * <P> |
| 42 | * Note that column numbers for rowsets start at <code>1</code>, |
| 43 | * whereas the first element of an array or bitset is <code>0</code>. |
| 44 | * The argument for the method <code>getColumnUpdated</code> refers to |
| 45 | * the column number in the rowset (the first column is <code>1</code>); |
| 46 | * the argument for <code>setColumnUpdated</code> refers to the index |
| 47 | * into the rowset's internal bitset (the first bit is <code>0</code>). |
| 48 | */ |
| 49 | public class Row extends BaseRow implements Serializable, Cloneable { |
| 50 | |
| 51 | /** |
| 52 | * An array containing the current column values for this <code>Row</code> |
| 53 | * object. |
| 54 | * @serial |
| 55 | */ |
| 56 | private Object[] currentVals; |
| 57 | |
| 58 | /** |
| 59 | * A <code>BitSet</code> object containing a flag for each column in |
| 60 | * this <code>Row</code> object, with each flag indicating whether or |
| 61 | * not the value in the column has been changed. |
| 62 | * @serial |
| 63 | */ |
| 64 | private BitSet colsChanged; |
| 65 | |
| 66 | /** |
| 67 | * A <code>boolean</code> indicating whether or not this <code>Row</code> |
| 68 | * object has been deleted. <code>true</code> indicates that it has |
| 69 | * been deleted; <code>false</code> indicates that it has not. |
| 70 | * @serial |
| 71 | */ |
| 72 | private boolean deleted; |
| 73 | |
| 74 | /** |
| 75 | * A <code>boolean</code> indicating whether or not this <code>Row</code> |
| 76 | * object has been updated. <code>true</code> indicates that it has |
| 77 | * been updated; <code>false</code> indicates that it has not. |
| 78 | * @serial |
| 79 | */ |
| 80 | private boolean updated; |
| 81 | |
| 82 | /** |
| 83 | * A <code>boolean</code> indicating whether or not this <code>Row</code> |
| 84 | * object has been inserted. <code>true</code> indicates that it has |
| 85 | * been inserted; <code>false</code> indicates that it has not. |
| 86 | * @serial |
| 87 | */ |
| 88 | private boolean inserted; |
| 89 | |
| 90 | /** |
| 91 | * The number of columns in this <code>Row</code> object. |
| 92 | * @serial |
| 93 | */ |
| 94 | private int numCols; |
| 95 | |
| 96 | /** |
| 97 | * Creates a new <code>Row</code> object with the given number of columns. |
| 98 | * The newly-created row includes an array of original values, |
| 99 | * an array for storing its current values, and a <code>BitSet</code> |
| 100 | * object for keeping track of which column values have been changed. |
| 101 | */ |
| 102 | public Row(int numCols) { |
| 103 | origVals = new Object[numCols]; |
| 104 | currentVals = new Object[numCols]; |
| 105 | colsChanged = new BitSet(numCols); |
| 106 | this.numCols = numCols; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Creates a new <code>Row</code> object with the given number of columns |
| 111 | * and with its array of original values initialized to the given array. |
| 112 | * The new <code>Row</code> object also has an array for storing its |
| 113 | * current values and a <code>BitSet</code> object for keeping track |
| 114 | * of which column values have been changed. |
| 115 | */ |
| 116 | public Row(int numCols, Object[] vals) { |
| 117 | origVals = new Object[numCols]; |
| 118 | for (int i=0; i < numCols; i++) { |
| 119 | origVals[i] = vals[i]; |
| 120 | } |
| 121 | currentVals = new Object[numCols]; |
| 122 | colsChanged = new BitSet(numCols); |
| 123 | this.numCols = numCols; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * |
| 128 | * This method is called internally by the <code>CachedRowSet.populate</code> |
| 129 | * methods. |
| 130 | * |
| 131 | * @param idx the number of the column in this <code>Row</code> object |
| 132 | * that is to be set; the index of the first column is |
| 133 | * <code>1</code> |
| 134 | * @param val the new value to be set |
| 135 | */ |
| 136 | public void initColumnObject(int idx, Object val) { |
| 137 | origVals[idx - 1] = val; |
| 138 | } |
| 139 | |
| 140 | |
| 141 | /** |
| 142 | * |
| 143 | * This method is called internally by the <code>CachedRowSet.updateXXX</code> |
| 144 | * methods. |
| 145 | * |
| 146 | * @param idx the number of the column in this <code>Row</code> object |
| 147 | * that is to be set; the index of the first column is |
| 148 | * <code>1</code> |
| 149 | * @param val the new value to be set |
| 150 | */ |
| 151 | public void setColumnObject(int idx, Object val) { |
| 152 | currentVals[idx - 1] = val; |
| 153 | setColUpdated(idx - 1); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Retrieves the column value stored in the designated column of this |
| 158 | * <code>Row</code> object. |
| 159 | * |
| 160 | * @param columnIndex the index of the column value to be retrieved; |
| 161 | * the index of the first column is <code>1</code> |
| 162 | * @return an <code>Object</code> in the Java programming language that |
| 163 | * represents the value stored in the designated column |
| 164 | * @throws SQLException if there is a database access error |
| 165 | */ |
| 166 | public Object getColumnObject(int columnIndex) throws SQLException { |
| 167 | if (getColUpdated(columnIndex - 1)) { |
| 168 | return(currentVals[columnIndex - 1]); // maps to array!! |
| 169 | } else { |
| 170 | return(origVals[columnIndex - 1]); // maps to array!! |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Indicates whether the designated column of this <code>Row</code> object |
| 176 | * has been changed. |
| 177 | * @param idx the index into the <code>BitSet</code> object maintained by |
| 178 | * this <code>Row</code> object to keep track of which column |
| 179 | * values have been modified; the index of the first bit is |
| 180 | * <code>0</code> |
| 181 | * @return <code>true</code> if the designated column value has been changed; |
| 182 | * <code>false</code> otherwise |
| 183 | * |
| 184 | */ |
| 185 | public boolean getColUpdated(int idx) { |
| 186 | return colsChanged.get(idx); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Sets this <code>Row</code> object's <code>deleted</code> field |
| 191 | * to <code>true</code>. |
| 192 | * |
| 193 | * @see #getDeleted |
| 194 | */ |
| 195 | public void setDeleted() { // %%% was public |
| 196 | deleted = true; |
| 197 | } |
| 198 | |
| 199 | |
| 200 | /** |
| 201 | * Retrieves the value of this <code>Row</code> object's <code>deleted</code> field, |
| 202 | * which will be <code>true</code> if one or more of its columns has been |
| 203 | * deleted. |
| 204 | * @return <code>true</code> if a column value has been deleted; <code>false</code> |
| 205 | * otherwise |
| 206 | * |
| 207 | * @see #setDeleted |
| 208 | */ |
| 209 | public boolean getDeleted() { |
| 210 | return(deleted); |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Sets the <code>deleted</code> field for this <code>Row</code> object to |
| 215 | * <code>false</code>. |
| 216 | */ |
| 217 | public void clearDeleted() { |
| 218 | deleted = false; |
| 219 | } |
| 220 | |
| 221 | |
| 222 | /** |
| 223 | * Sets the value of this <code>Row</code> object's <code>inserted</code> field |
| 224 | * to <code>true</code>. |
| 225 | * |
| 226 | * @see #getInserted |
| 227 | */ |
| 228 | public void setInserted() { |
| 229 | inserted = true; |
| 230 | } |
| 231 | |
| 232 | |
| 233 | /** |
| 234 | * Retrieves the value of this <code>Row</code> object's <code>inserted</code> field, |
| 235 | * which will be <code>true</code> if this row has been inserted. |
| 236 | * @return <code>true</code> if this row has been inserted; <code>false</code> |
| 237 | * otherwise |
| 238 | * |
| 239 | * @see #setInserted |
| 240 | */ |
| 241 | public boolean getInserted() { |
| 242 | return(inserted); |
| 243 | } |
| 244 | |
| 245 | |
| 246 | /** |
| 247 | * Sets the <code>inserted</code> field for this <code>Row</code> object to |
| 248 | * <code>false</code>. |
| 249 | */ |
| 250 | public void clearInserted() { // %%% was public |
| 251 | inserted = false; |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Retrieves the value of this <code>Row</code> object's |
| 256 | * <code>updated</code> field. |
| 257 | * @return <code>true</code> if this <code>Row</code> object has been |
| 258 | * updated; <code>false</code> if it has not |
| 259 | * |
| 260 | * @see #setUpdated |
| 261 | */ |
| 262 | public boolean getUpdated() { |
| 263 | return(updated); |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Sets the <code>updated</code> field for this <code>Row</code> object to |
| 268 | * <code>true</code> if one or more of its column values has been changed. |
| 269 | * |
| 270 | * @see #getUpdated |
| 271 | */ |
| 272 | public void setUpdated() { |
| 273 | // only mark something as updated if one or |
| 274 | // more of the columns has been changed. |
| 275 | for (int i = 0; i < numCols; i++) { |
| 276 | if (getColUpdated(i) == true) { |
| 277 | updated = true; |
| 278 | return; |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * Sets the bit at the given index into this <code>Row</code> object's internal |
| 285 | * <code>BitSet</code> object, indicating that the corresponding column value |
| 286 | * (column <code>idx</code> + 1) has been changed. |
| 287 | * |
| 288 | * @param idx the index into the <code>BitSet</code> object maintained by |
| 289 | * this <code>Row</code> object; the first bit is at index |
| 290 | * <code>0</code> |
| 291 | * |
| 292 | */ |
| 293 | private void setColUpdated(int idx) { |
| 294 | colsChanged.set(idx); |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * Sets the <code>updated</code> field for this <code>Row</code> object to |
| 299 | * <code>false</code>, sets all the column values in this <code>Row</code> |
| 300 | * object's internal array of current values to <code>null</code>, and clears |
| 301 | * all of the bits in the <code>BitSet</code> object maintained by this |
| 302 | * <code>Row</code> object. |
| 303 | */ |
| 304 | public void clearUpdated() { |
| 305 | updated = false; |
| 306 | for (int i = 0; i < numCols; i++) { |
| 307 | currentVals[i] = null; |
| 308 | colsChanged.clear(i); |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * Sets the column values in this <code>Row</code> object's internal |
| 314 | * array of original values with the values in its internal array of |
| 315 | * current values, sets all the values in this <code>Row</code> |
| 316 | * object's internal array of current values to <code>null</code>, |
| 317 | * clears all the bits in this <code>Row</code> object's internal bitset, |
| 318 | * and sets its <code>updated</code> field to <code>false</code>. |
| 319 | * <P> |
| 320 | * This method is called internally by the <code>CachedRowSet</code> |
| 321 | * method <code>makeRowOriginal</code>. |
| 322 | */ |
| 323 | public void moveCurrentToOrig() { |
| 324 | for (int i = 0; i < numCols; i++) { |
| 325 | if (getColUpdated(i) == true) { |
| 326 | origVals[i] = currentVals[i]; |
| 327 | currentVals[i] = null; |
| 328 | colsChanged.clear(i); |
| 329 | } |
| 330 | } |
| 331 | updated = false; |
| 332 | } |
| 333 | |
| 334 | /** |
| 335 | * Returns the row on which the cursor is positioned. |
| 336 | * |
| 337 | * @return the <code>Row</code> object on which the <code>CachedRowSet</code> |
| 338 | * implementation objects's cursor is positioned |
| 339 | */ |
| 340 | public BaseRow getCurrentRow() { |
| 341 | return null; |
| 342 | } |
| 343 | } |