Quadcap Embeddable Database

com/quadcap/sql/Row.java

Go to the documentation of this file.
00001 package com.quadcap.sql; 00002 00003 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved. 00004 * 00005 * This software is distributed under the Quadcap Free Software License. 00006 * This software may be used or modified for any purpose, personal or 00007 * commercial. Open Source redistributions are permitted. Commercial 00008 * redistribution of larger works derived from, or works which bundle 00009 * this software requires a "Commercial Redistribution License"; see 00010 * http://www.quadcap.com/purchase. 00011 * 00012 * Redistributions qualify as "Open Source" under one of the following terms: 00013 * 00014 * Redistributions are made at no charge beyond the reasonable cost of 00015 * materials and delivery. 00016 * 00017 * Redistributions are accompanied by a copy of the Source Code or by an 00018 * irrevocable offer to provide a copy of the Source Code for up to three 00019 * years at the cost of materials and delivery. Such redistributions 00020 * must allow further use, modification, and redistribution of the Source 00021 * Code under substantially the same terms as this license. 00022 * 00023 * Redistributions of source code must retain the copyright notices as they 00024 * appear in each source code file, these license terms, and the 00025 * disclaimer/limitation of liability set forth as paragraph 6 below. 00026 * 00027 * Redistributions in binary form must reproduce this Copyright Notice, 00028 * these license terms, and the disclaimer/limitation of liability set 00029 * forth as paragraph 6 below, in the documentation and/or other materials 00030 * provided with the distribution. 00031 * 00032 * The Software is provided on an "AS IS" basis. No warranty is 00033 * provided that the Software is free of defects, or fit for a 00034 * particular purpose. 00035 * 00036 * Limitation of Liability. Quadcap Software shall not be liable 00037 * for any damages suffered by the Licensee or any third party resulting 00038 * from use of the Software. 00039 */ 00040 00041 import java.io.Externalizable; 00042 import java.io.IOException; 00043 import java.io.ObjectInput; 00044 import java.io.ObjectOutput; 00045 00046 import java.util.ArrayList; 00047 00048 import java.sql.SQLException; 00049 00050 import com.quadcap.sql.types.Type; 00051 import com.quadcap.sql.types.Value; 00052 import com.quadcap.sql.types.ValueBlob; 00053 00054 import com.quadcap.util.Debug; 00055 00056 /** 00057 * Essentially, a vector with one-based indices. 00058 * 00059 * @author Stan Bailes 00060 */ 00061 public class Row { 00062 int blobCnt = 0; 00063 ArrayList v = null; 00064 00065 /** 00066 * Default constructor 00067 */ 00068 public Row() { v = new ArrayList(); } 00069 00070 /** 00071 * Construct an empty row with the specified number of columns 00072 */ 00073 public Row(int size) { 00074 this.v = new ArrayList(size); 00075 while (v.size() < size) v.add(null); 00076 } 00077 00078 /** 00079 * Construct a row as a copy of another row. 00080 */ 00081 public Row(Row r, Tuple tuple) throws SQLException { 00082 v = new ArrayList(); 00083 for (int i = 1; i <= r.size(); i++) { 00084 Value rv = r.item(i); 00085 if (tuple != null) { 00086 Type type = tuple.getColumn(i).getType(); 00087 Value v1 = type.convert(rv); 00088 rv = v1; 00089 } 00090 if (rv instanceof ValueBlob) blobCnt++; 00091 v.add(rv); 00092 } 00093 } 00094 00095 /** 00096 * Construct a row using a ArrayList of values. The vector is zero-based, 00097 * even though the row is one-based! 00098 */ 00099 public Row(ArrayList v) { 00100 this.v = v; 00101 for (int i = 0; i < v.size(); i++) { 00102 if (v.get(i) instanceof ValueBlob) blobCnt++; 00103 } 00104 } 00105 00106 /** 00107 * Return the number of values in this row. 00108 */ 00109 public int size() { return v.size(); } 00110 00111 /** 00112 * Return the number of BLOB values in this row. (We can shortcut 00113 * some possibly lengthy operations if we know there are no blobs) 00114 */ 00115 public int getBlobCount() throws IOException { return blobCnt; } 00116 00117 /** 00118 * Return the specified value (one-based) from the row. 00119 */ 00120 public Value item(int i) throws SQLException { 00121 if (i < 1 || i > v.size()) { 00122 throw new SQLException("bad column: " + i, "42000"); 00123 } 00124 return (Value)v.get(i-1); 00125 } 00126 00127 /** 00128 * Non-checked access: return the specified value (one-based) from the row. 00129 */ 00130 public Value getItem(int i) { 00131 return (Value)v.get(i-1); 00132 } 00133 00134 /** 00135 * Set value in the specified position (one-based) to a new value 00136 */ 00137 public void set(int i, Value val) throws SQLException { 00138 final int idx = i-1; 00139 if (v.get(idx) instanceof ValueBlob) blobCnt--; 00140 if (val instanceof ValueBlob) blobCnt++; 00141 v.set(idx, val); 00142 //#ifdef TRACE 00143 if (Trace.bit(7)) { 00144 Debug.println("Row.set(" + i + "), " + blobCnt + " blobs"); 00145 } 00146 //#endif 00147 } 00148 00149 /** 00150 * Is the specified element updatable? 00151 */ 00152 public boolean isUpdateable(int i) { 00153 return true; 00154 } 00155 00156 public void addElement(Value val) { 00157 if (val instanceof ValueBlob) blobCnt++; 00158 v.add(val); 00159 } 00160 00161 //#ifdef DEBUG 00162 public String toString() { 00163 StringBuffer sb = new StringBuffer(); 00164 sb.append("{"); 00165 for (int i = 1; i <= size(); i++) { 00166 if (i > 1) sb.append(","); 00167 try { 00168 Value vx = item(i); 00169 //sb.append(v.getType().getTypeName()); 00170 //sb.append(":"); 00171 sb.append(vx.toString()); 00172 } catch (Throwable e) { 00173 sb.append("<" + e.toString() + ">"); 00174 } 00175 } 00176 sb.append("}"); 00177 return sb.toString(); 00178 } 00179 //#endif 00180 }