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 com.sun.rowset.JdbcRowSetResourceBundle;
029: import java.sql.*;
030: import javax.sql.*;
031: import java.io.*;
032: import java.util.*;
033:
034: /**
035: * A class used internally to manage a <code>CachedRowSet</code> object's
036: * insert row. This class keeps track of the number of columns in the
037: * insert row and which columns have had a value inserted. It provides
038: * methods for retrieving a column value, setting a column value, and finding
039: * out whether the insert row is complete.
040: */
041: public class InsertRow extends BaseRow implements Serializable,
042: Cloneable {
043:
044: /**
045: * An internal <code>BitSet</code> object used to keep track of the
046: * columns in this <code>InsertRow</code> object that have had a value
047: * inserted.
048: */
049: private BitSet colsInserted;
050:
051: /**
052: * The number of columns in this <code>InsertRow</code> object.
053: */
054: private int cols;
055:
056: private JdbcRowSetResourceBundle resBundle;
057:
058: /**
059: * Creates an <code>InsertRow</code> object initialized with the
060: * given number of columns, an array for keeping track of the
061: * original values in this insert row, and a
062: * <code>BitSet</code> object with the same number of bits as
063: * there are columns.
064: *
065: * @param numCols an <code>int</code> indicating the number of columns
066: * in this <code>InsertRow</code> object
067: */
068: public InsertRow(int numCols) {
069: origVals = new Object[numCols];
070: colsInserted = new BitSet(numCols);
071: cols = numCols;
072: try {
073: resBundle = JdbcRowSetResourceBundle
074: .getJdbcRowSetResourceBundle();
075: } catch (IOException ioe) {
076: throw new RuntimeException(ioe);
077: }
078: }
079:
080: /**
081: * Sets the bit in this <code>InsertRow</code> object's internal
082: * <code>BitSet</code> object that corresponds to the specified column
083: * in this <code>InsertRow</code> object. Setting a bit indicates
084: * that a value has been set.
085: *
086: * @param col the number of the column to be marked as inserted;
087: * the first column is <code>1</code>
088: */
089: protected void markColInserted(int col) {
090: colsInserted.set(col);
091: }
092:
093: /**
094: * Indicates whether this <code>InsertRow</code> object has a value
095: * for every column that cannot be null.
096: * @param RowSetMD the <code>RowSetMetaData</code> object for the
097: * <code>CachedRowSet</code> object that maintains this
098: * <code>InsertRow</code> object
099: * @return <code>true</code> if this <code>InsertRow</code> object is
100: * complete; <code>false</code> otherwise
101: * @throws SQLException if there is an error accessing data
102: */
103: public boolean isCompleteRow(RowSetMetaData RowSetMD)
104: throws SQLException {
105: for (int i = 0; i < cols; i++) {
106: if (colsInserted.get(i) == false
107: && RowSetMD.isNullable(i + 1) == ResultSetMetaData.columnNoNulls) {
108: return false;
109: }
110:
111: }
112: return true;
113: }
114:
115: /**
116: * Clears all the bits in the internal <code>BitSet</code> object
117: * maintained by this <code>InsertRow</code> object. Clearing all the bits
118: * indicates that none of the columns have had a value inserted.
119: */
120: public void initInsertRow() {
121: for (int i = 0; i < cols; i++) {
122: colsInserted.clear(i);
123: }
124: }
125:
126: /**
127: * Retrieves the value of the designated column in this
128: * <code>InsertRow</code> object. If no value has been inserted
129: * into the designated column, this method throws an
130: * <code>SQLException</code>.
131: *
132: * @param idx the column number of the value to be retrieved;
133: * the first column is <code>1</code>
134: * @throws SQLException if no value has been inserted into
135: * the designated column
136: */
137: public Object getColumnObject(int idx) throws SQLException {
138: if (colsInserted.get(idx - 1) == false) {
139: throw new SQLException(resBundle.handleGetObject(
140: "insertrow.novalue").toString());
141: }
142: return (origVals[idx - 1]);
143: }
144:
145: /**
146: * Sets the element in this <code>InsertRow</code> object's
147: * internal array of original values that corresponds to the
148: * designated column with the given value. If the third
149: * argument is <code>true</code>,
150: * which means that the cursor is on the insert row, this
151: * <code>InsertRow</code> object's internal <code>BitSet</code> object
152: * is set so that the bit corresponding to the column being set is
153: * turned on.
154: *
155: * @param idx the number of the column in the insert row to be set;
156: * the first column is <code>1</code>
157: * @param val the value to be set
158: */
159: public void setColumnObject(int idx, Object val) {
160: origVals[idx - 1] = val;
161: markColInserted(idx - 1);
162: }
163: }
|