KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > sql > Row


1 package com.quadcap.sql;
2
3 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.io.Externalizable JavaDoc;
42 import java.io.IOException JavaDoc;
43 import java.io.ObjectInput JavaDoc;
44 import java.io.ObjectOutput JavaDoc;
45
46 import java.util.ArrayList JavaDoc;
47
48 import java.sql.SQLException JavaDoc;
49
50 import com.quadcap.sql.types.Type;
51 import com.quadcap.sql.types.Value;
52 import com.quadcap.sql.types.ValueBlob;
53
54 import com.quadcap.util.Debug;
55
56 /**
57  * Essentially, a vector with one-based indices.
58  *
59  * @author Stan Bailes
60  */

61 public class Row {
62     int blobCnt = 0;
63     ArrayList JavaDoc v = null;
64
65     /**
66      * Default constructor
67      */

68     public Row() { v = new ArrayList JavaDoc(); }
69
70     /**
71      * Construct an empty row with the specified number of columns
72      */

73     public Row(int size) {
74         this.v = new ArrayList JavaDoc(size);
75         while (v.size() < size) v.add(null);
76     }
77
78     /**
79      * Construct a row as a copy of another row.
80      */

81     public Row(Row r, Tuple tuple) throws SQLException JavaDoc {
82         v = new ArrayList JavaDoc();
83         for (int i = 1; i <= r.size(); i++) {
84             Value rv = r.item(i);
85             if (tuple != null) {
86                 Type type = tuple.getColumn(i).getType();
87                 Value v1 = type.convert(rv);
88                 rv = v1;
89             }
90             if (rv instanceof ValueBlob) blobCnt++;
91             v.add(rv);
92         }
93     }
94
95     /**
96      * Construct a row using a ArrayList of values. The vector is zero-based,
97      * even though the row is one-based!
98      */

99     public Row(ArrayList JavaDoc v) {
100         this.v = v;
101         for (int i = 0; i < v.size(); i++) {
102             if (v.get(i) instanceof ValueBlob) blobCnt++;
103         }
104     }
105
106     /**
107      * Return the number of values in this row.
108      */

109     public int size() { return v.size(); }
110
111     /**
112      * Return the number of BLOB values in this row. (We can shortcut
113      * some possibly lengthy operations if we know there are no blobs)
114      */

115     public int getBlobCount() throws IOException JavaDoc { return blobCnt; }
116
117     /**
118      * Return the specified value (one-based) from the row.
119      */

120     public Value item(int i) throws SQLException JavaDoc {
121         if (i < 1 || i > v.size()) {
122             throw new SQLException JavaDoc("bad column: " + i, "42000");
123         }
124         return (Value)v.get(i-1);
125     }
126
127     /**
128      * Non-checked access: return the specified value (one-based) from the row.
129      */

130     public Value getItem(int i) {
131         return (Value)v.get(i-1);
132     }
133
134     /**
135      * Set value in the specified position (one-based) to a new value
136      */

137     public void set(int i, Value val) throws SQLException JavaDoc {
138         final int idx = i-1;
139         if (v.get(idx) instanceof ValueBlob) blobCnt--;
140         if (val instanceof ValueBlob) blobCnt++;
141         v.set(idx, val);
142         //#ifdef TRACE
143
if (Trace.bit(7)) {
144             Debug.println("Row.set(" + i + "), " + blobCnt + " blobs");
145         }
146         //#endif
147
}
148
149     /**
150      * Is the specified element updatable?
151      */

152     public boolean isUpdateable(int i) {
153         return true;
154     }
155     
156     public void addElement(Value val) {
157         if (val instanceof ValueBlob) blobCnt++;
158         v.add(val);
159     }
160
161     //#ifdef DEBUG
162
public String JavaDoc toString() {
163         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
164         sb.append("{");
165         for (int i = 1; i <= size(); i++) {
166             if (i > 1) sb.append(",");
167             try {
168                 Value vx = item(i);
169                 //sb.append(v.getType().getTypeName());
170
//sb.append(":");
171
sb.append(vx.toString());
172             } catch (Throwable JavaDoc e) {
173                 sb.append("<" + e.toString() + ">");
174             }
175         }
176         sb.append("}");
177         return sb.toString();
178     }
179     //#endif
180
}
181
Popular Tags