001: package com.quadcap.sql;
002:
003: /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.io.Externalizable;
042: import java.io.IOException;
043: import java.io.ObjectInput;
044: import java.io.ObjectOutput;
045:
046: import java.util.ArrayList;
047:
048: import java.sql.SQLException;
049:
050: import com.quadcap.sql.types.Type;
051: import com.quadcap.sql.types.Value;
052: import com.quadcap.sql.types.ValueBlob;
053:
054: import com.quadcap.util.Debug;
055:
056: /**
057: * Essentially, a vector with one-based indices.
058: *
059: * @author Stan Bailes
060: */
061: public class Row {
062: int blobCnt = 0;
063: ArrayList v = null;
064:
065: /**
066: * Default constructor
067: */
068: public Row() {
069: v = new ArrayList();
070: }
071:
072: /**
073: * Construct an empty row with the specified number of columns
074: */
075: public Row(int size) {
076: this .v = new ArrayList(size);
077: while (v.size() < size)
078: v.add(null);
079: }
080:
081: /**
082: * Construct a row as a copy of another row.
083: */
084: public Row(Row r, Tuple tuple) throws SQLException {
085: v = new ArrayList();
086: for (int i = 1; i <= r.size(); i++) {
087: Value rv = r.item(i);
088: if (tuple != null) {
089: Type type = tuple.getColumn(i).getType();
090: Value v1 = type.convert(rv);
091: rv = v1;
092: }
093: if (rv instanceof ValueBlob)
094: blobCnt++;
095: v.add(rv);
096: }
097: }
098:
099: /**
100: * Construct a row using a ArrayList of values. The vector is zero-based,
101: * even though the row is one-based!
102: */
103: public Row(ArrayList v) {
104: this .v = v;
105: for (int i = 0; i < v.size(); i++) {
106: if (v.get(i) instanceof ValueBlob)
107: blobCnt++;
108: }
109: }
110:
111: /**
112: * Return the number of values in this row.
113: */
114: public int size() {
115: return v.size();
116: }
117:
118: /**
119: * Return the number of BLOB values in this row. (We can shortcut
120: * some possibly lengthy operations if we know there are no blobs)
121: */
122: public int getBlobCount() throws IOException {
123: return blobCnt;
124: }
125:
126: /**
127: * Return the specified value (one-based) from the row.
128: */
129: public Value item(int i) throws SQLException {
130: if (i < 1 || i > v.size()) {
131: throw new SQLException("bad column: " + i, "42000");
132: }
133: return (Value) v.get(i - 1);
134: }
135:
136: /**
137: * Non-checked access: return the specified value (one-based) from the row.
138: */
139: public Value getItem(int i) {
140: return (Value) v.get(i - 1);
141: }
142:
143: /**
144: * Set value in the specified position (one-based) to a new value
145: */
146: public void set(int i, Value val) throws SQLException {
147: final int idx = i - 1;
148: if (v.get(idx) instanceof ValueBlob)
149: blobCnt--;
150: if (val instanceof ValueBlob)
151: blobCnt++;
152: v.set(idx, val);
153: //#ifdef TRACE
154: if (Trace.bit(7)) {
155: Debug.println("Row.set(" + i + "), " + blobCnt + " blobs");
156: }
157: //#endif
158: }
159:
160: /**
161: * Is the specified element updatable?
162: */
163: public boolean isUpdateable(int i) {
164: return true;
165: }
166:
167: public void addElement(Value val) {
168: if (val instanceof ValueBlob)
169: blobCnt++;
170: v.add(val);
171: }
172:
173: //#ifdef DEBUG
174: public String toString() {
175: StringBuffer sb = new StringBuffer();
176: sb.append("{");
177: for (int i = 1; i <= size(); i++) {
178: if (i > 1)
179: sb.append(",");
180: try {
181: Value vx = item(i);
182: //sb.append(v.getType().getTypeName());
183: //sb.append(":");
184: sb.append(vx.toString());
185: } catch (Throwable e) {
186: sb.append("<" + e.toString() + ">");
187: }
188: }
189: sb.append("}");
190: return sb.toString();
191: }
192: //#endif
193: }
|