001: /*
002: * Copyright 2003-2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.rowset.internal;
027:
028: import java.sql.*;
029: import java.io.*;
030: import java.math.*;
031: import java.lang.*;
032: import java.lang.reflect.*;
033: import java.util.*;
034:
035: /**
036: * A class that keeps track of a row's values. A <code>Row</code> object
037: * maintains an array of current column values and an array of original
038: * column values, and it provides methods for getting and setting the
039: * value of a column. It also keeps track of which columns have
040: * changed and whether the change was a delete, insert, or update.
041: * <P>
042: * Note that column numbers for rowsets start at <code>1</code>,
043: * whereas the first element of an array or bitset is <code>0</code>.
044: * The argument for the method <code>getColumnUpdated</code> refers to
045: * the column number in the rowset (the first column is <code>1</code>);
046: * the argument for <code>setColumnUpdated</code> refers to the index
047: * into the rowset's internal bitset (the first bit is <code>0</code>).
048: */
049: public class Row extends BaseRow implements Serializable, Cloneable {
050:
051: /**
052: * An array containing the current column values for this <code>Row</code>
053: * object.
054: * @serial
055: */
056: private Object[] currentVals;
057:
058: /**
059: * A <code>BitSet</code> object containing a flag for each column in
060: * this <code>Row</code> object, with each flag indicating whether or
061: * not the value in the column has been changed.
062: * @serial
063: */
064: private BitSet colsChanged;
065:
066: /**
067: * A <code>boolean</code> indicating whether or not this <code>Row</code>
068: * object has been deleted. <code>true</code> indicates that it has
069: * been deleted; <code>false</code> indicates that it has not.
070: * @serial
071: */
072: private boolean deleted;
073:
074: /**
075: * A <code>boolean</code> indicating whether or not this <code>Row</code>
076: * object has been updated. <code>true</code> indicates that it has
077: * been updated; <code>false</code> indicates that it has not.
078: * @serial
079: */
080: private boolean updated;
081:
082: /**
083: * A <code>boolean</code> indicating whether or not this <code>Row</code>
084: * object has been inserted. <code>true</code> indicates that it has
085: * been inserted; <code>false</code> indicates that it has not.
086: * @serial
087: */
088: private boolean inserted;
089:
090: /**
091: * The number of columns in this <code>Row</code> object.
092: * @serial
093: */
094: private int numCols;
095:
096: /**
097: * Creates a new <code>Row</code> object with the given number of columns.
098: * The newly-created row includes an array of original values,
099: * 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: * This method is called internally by the <code>CachedRowSet.updateXXX</code>
143: * methods.
144: *
145: * @param idx the number of the column in this <code>Row</code> object
146: * that is to be set; the index of the first column is
147: * <code>1</code>
148: * @param val the new value to be set
149: */
150: public void setColumnObject(int idx, Object val) {
151: currentVals[idx - 1] = val;
152: setColUpdated(idx - 1);
153: }
154:
155: /**
156: * Retrieves the column value stored in the designated column of this
157: * <code>Row</code> object.
158: *
159: * @param columnIndex the index of the column value to be retrieved;
160: * the index of the first column is <code>1</code>
161: * @return an <code>Object</code> in the Java programming language that
162: * represents the value stored in the designated column
163: * @throws SQLException if there is a database access error
164: */
165: public Object getColumnObject(int columnIndex) throws SQLException {
166: if (getColUpdated(columnIndex - 1)) {
167: return (currentVals[columnIndex - 1]); // maps to array!!
168: } else {
169: return (origVals[columnIndex - 1]); // maps to array!!
170: }
171: }
172:
173: /**
174: * Indicates whether the designated column of this <code>Row</code> object
175: * has been changed.
176: * @param idx the index into the <code>BitSet</code> object maintained by
177: * this <code>Row</code> object to keep track of which column
178: * values have been modified; the index of the first bit is
179: * <code>0</code>
180: * @return <code>true</code> if the designated column value has been changed;
181: * <code>false</code> otherwise
182: *
183: */
184: public boolean getColUpdated(int idx) {
185: return colsChanged.get(idx);
186: }
187:
188: /**
189: * Sets this <code>Row</code> object's <code>deleted</code> field
190: * to <code>true</code>.
191: *
192: * @see #getDeleted
193: */
194: public void setDeleted() { // %%% was public
195: deleted = true;
196: }
197:
198: /**
199: * Retrieves the value of this <code>Row</code> object's <code>deleted</code> field,
200: * which will be <code>true</code> if one or more of its columns has been
201: * deleted.
202: * @return <code>true</code> if a column value has been deleted; <code>false</code>
203: * otherwise
204: *
205: * @see #setDeleted
206: */
207: public boolean getDeleted() {
208: return (deleted);
209: }
210:
211: /**
212: * Sets the <code>deleted</code> field for this <code>Row</code> object to
213: * <code>false</code>.
214: */
215: public void clearDeleted() {
216: deleted = false;
217: }
218:
219: /**
220: * Sets the value of this <code>Row</code> object's <code>inserted</code> field
221: * to <code>true</code>.
222: *
223: * @see #getInserted
224: */
225: public void setInserted() {
226: inserted = true;
227: }
228:
229: /**
230: * Retrieves the value of this <code>Row</code> object's <code>inserted</code> field,
231: * which will be <code>true</code> if this row has been inserted.
232: * @return <code>true</code> if this row has been inserted; <code>false</code>
233: * otherwise
234: *
235: * @see #setInserted
236: */
237: public boolean getInserted() {
238: return (inserted);
239: }
240:
241: /**
242: * Sets the <code>inserted</code> field for this <code>Row</code> object to
243: * <code>false</code>.
244: */
245: public void clearInserted() { // %%% was public
246: inserted = false;
247: }
248:
249: /**
250: * Retrieves the value of this <code>Row</code> object's
251: * <code>updated</code> field.
252: * @return <code>true</code> if this <code>Row</code> object has been
253: * updated; <code>false</code> if it has not
254: *
255: * @see #setUpdated
256: */
257: public boolean getUpdated() {
258: return (updated);
259: }
260:
261: /**
262: * Sets the <code>updated</code> field for this <code>Row</code> object to
263: * <code>true</code> if one or more of its column values has been changed.
264: *
265: * @see #getUpdated
266: */
267: public void setUpdated() {
268: // only mark something as updated if one or
269: // more of the columns has been changed.
270: for (int i = 0; i < numCols; i++) {
271: if (getColUpdated(i) == true) {
272: updated = true;
273: return;
274: }
275: }
276: }
277:
278: /**
279: * Sets the bit at the given index into this <code>Row</code> object's internal
280: * <code>BitSet</code> object, indicating that the corresponding column value
281: * (column <code>idx</code> + 1) has been changed.
282: *
283: * @param idx the index into the <code>BitSet</code> object maintained by
284: * this <code>Row</code> object; the first bit is at index
285: * <code>0</code>
286: *
287: */
288: private void setColUpdated(int idx) {
289: colsChanged.set(idx);
290: }
291:
292: /**
293: * Sets the <code>updated</code> field for this <code>Row</code> object to
294: * <code>false</code>, sets all the column values in this <code>Row</code>
295: * object's internal array of current values to <code>null</code>, and clears
296: * all of the bits in the <code>BitSet</code> object maintained by this
297: * <code>Row</code> object.
298: */
299: public void clearUpdated() {
300: updated = false;
301: for (int i = 0; i < numCols; i++) {
302: currentVals[i] = null;
303: colsChanged.clear(i);
304: }
305: }
306:
307: /**
308: * Sets the column values in this <code>Row</code> object's internal
309: * array of original values with the values in its internal array of
310: * current values, sets all the values in this <code>Row</code>
311: * object's internal array of current values to <code>null</code>,
312: * clears all the bits in this <code>Row</code> object's internal bitset,
313: * and sets its <code>updated</code> field to <code>false</code>.
314: * <P>
315: * This method is called internally by the <code>CachedRowSet</code>
316: * method <code>makeRowOriginal</code>.
317: */
318: public void moveCurrentToOrig() {
319: for (int i = 0; i < numCols; i++) {
320: if (getColUpdated(i) == true) {
321: origVals[i] = currentVals[i];
322: currentVals[i] = null;
323: colsChanged.clear(i);
324: }
325: }
326: updated = false;
327: }
328:
329: /**
330: * Returns the row on which the cursor is positioned.
331: *
332: * @return the <code>Row</code> object on which the <code>CachedRowSet</code>
333: * implementation objects's cursor is positioned
334: */
335: public BaseRow getCurrentRow() {
336: return null;
337: }
338: }
|