Path: blob/master/src/java.sql.rowset/share/classes/com/sun/rowset/internal/InsertRow.java
40948 views
/*1* Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.rowset.internal;2627import com.sun.rowset.JdbcRowSetResourceBundle;28import java.sql.*;29import javax.sql.*;30import java.io.*;31import java.util.*;3233/**34* A class used internally to manage a <code>CachedRowSet</code> object's35* insert row. This class keeps track of the number of columns in the36* insert row and which columns have had a value inserted. It provides37* methods for retrieving a column value, setting a column value, and finding38* out whether the insert row is complete.39*/40public class InsertRow extends BaseRow implements Serializable, Cloneable {4142/**43* An internal <code>BitSet</code> object used to keep track of the44* columns in this <code>InsertRow</code> object that have had a value45* inserted.46*/47private BitSet colsInserted;4849/**50* The number of columns in this <code>InsertRow</code> object.51*/52private int cols;5354private JdbcRowSetResourceBundle resBundle;5556/**57* Creates an <code>InsertRow</code> object initialized with the58* given number of columns, an array for keeping track of the59* original values in this insert row, and a60* <code>BitSet</code> object with the same number of bits as61* there are columns.62*63* @param numCols an <code>int</code> indicating the number of columns64* in this <code>InsertRow</code> object65*/66public InsertRow(int numCols) {67origVals = new Object[numCols];68colsInserted = new BitSet(numCols);69cols = numCols;70try {71resBundle = JdbcRowSetResourceBundle.getJdbcRowSetResourceBundle();72} catch(IOException ioe) {73throw new RuntimeException(ioe);74}75}7677/**78* Sets the bit in this <code>InsertRow</code> object's internal79* <code>BitSet</code> object that corresponds to the specified column80* in this <code>InsertRow</code> object. Setting a bit indicates81* that a value has been set.82*83* @param col the number of the column to be marked as inserted;84* the first column is <code>1</code>85*/86protected void markColInserted(int col) {87colsInserted.set(col);88}8990/**91* Indicates whether this <code>InsertRow</code> object has a value92* for every column that cannot be null.93* @param RowSetMD the <code>RowSetMetaData</code> object for the94* <code>CachedRowSet</code> object that maintains this95* <code>InsertRow</code> object96* @return <code>true</code> if this <code>InsertRow</code> object is97* complete; <code>false</code> otherwise98* @throws SQLException if there is an error accessing data99*/100public boolean isCompleteRow(RowSetMetaData RowSetMD) throws SQLException {101for (int i = 0; i < cols; i++) {102if (colsInserted.get(i) == false &&103RowSetMD.isNullable(i + 1) ==104ResultSetMetaData.columnNoNulls) {105return false;106}107108}109return true;110}111112/**113* Clears all the bits in the internal <code>BitSet</code> object114* maintained by this <code>InsertRow</code> object. Clearing all the bits115* indicates that none of the columns have had a value inserted.116*/117public void initInsertRow() {118for (int i = 0; i < cols; i++) {119colsInserted.clear(i);120}121}122123/**124* Retrieves the value of the designated column in this125* <code>InsertRow</code> object. If no value has been inserted126* into the designated column, this method throws an127* <code>SQLException</code>.128*129* @param idx the column number of the value to be retrieved;130* the first column is <code>1</code>131* @throws SQLException if no value has been inserted into132* the designated column133*/134public Object getColumnObject(int idx) throws SQLException {135if (colsInserted.get(idx - 1) == false) {136throw new SQLException(resBundle.handleGetObject("insertrow.novalue").toString());137}138return (origVals[idx - 1]);139}140141/**142* Sets the element in this <code>InsertRow</code> object's143* internal array of original values that corresponds to the144* designated column with the given value. If the third145* argument is <code>true</code>,146* which means that the cursor is on the insert row, this147* <code>InsertRow</code> object's internal <code>BitSet</code> object148* is set so that the bit corresponding to the column being set is149* turned on.150*151* @param idx the number of the column in the insert row to be set;152* the first column is <code>1</code>153* @param val the value to be set154*/155public void setColumnObject(int idx, Object val) {156origVals[idx - 1] = val;157markColInserted(idx - 1);158}159160/**161* This method re populates the resBundle162* during the deserialization process163*164*/165private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {166// Default state initialization happens here167ois.defaultReadObject();168// Initialization of transient Res Bundle happens here .169try {170resBundle = JdbcRowSetResourceBundle.getJdbcRowSetResourceBundle();171} catch(IOException ioe) {172throw new RuntimeException(ioe);173}174175}176177static final long serialVersionUID = 1066099658102869344L;178}179180181