1 package com.quadcap.sql; 2 3 40 41 import java.io.Externalizable ; 42 import java.io.IOException ; 43 import java.io.ObjectInput ; 44 import java.io.ObjectOutput ; 45 46 import java.util.HashMap ; 47 import java.util.Iterator ; 48 import java.util.Vector ; 49 50 import java.sql.SQLException ; 51 52 import com.quadcap.sql.types.Type; 53 54 import com.quadcap.util.Debug; 55 56 61 public class TupleImpl implements Tuple, Externalizable { 62 protected String qual; 63 protected String qualifier; 64 protected Vector columns = new Vector (); 65 protected HashMap names = new HashMap (); 66 67 70 public TupleImpl() { 71 this.qualifier = ""; 72 this.qual = ""; 73 } 74 75 78 public TupleImpl(String qualifier) { 79 setName(qualifier); 80 } 81 82 85 public void setName(String name) { 86 if (name == null) name = ""; 87 this.qualifier = name; 88 if (qualifier.length() > 0) { 89 this.qual = qualifier + "."; 90 } else { 91 this.qual = ""; 92 } 93 } 94 95 99 public void addColumns(Session session, Tuple t) throws SQLException { 100 addColumns(session, t, true); 101 } 102 103 106 public void addColumns(Session session, Tuple t, boolean resolve) 107 throws SQLException 108 { 109 Connection conn = session.getConnection(); 110 boolean as = qual.length() > 0; 111 for (int i = 1; i <= t.getColumnCount(); i++) { 112 Column col = t.getColumn(i); 113 String name = conn.resolveColname(col.getName(), this); 114 Column ncol = new Column(name, col); 117 if (col.isJoinColumn()) ncol.setJoinColumn(true); 118 addColumn(ncol); 119 } 120 if (resolve) resolveColumns(); 121 } 122 123 127 public void resolveColumns() throws SQLException { 128 for (int i = 1; i <= getColumnCount(); i++) { 129 Column col = getColumn(i); 130 String name = col.getName(); 131 for (int j = name.length() - 1; j > 0; j--) { 132 if (name.charAt(j) == '.') { 133 String qname = name.substring(j+1); 134 Object obj = names.get(qname); 135 if (obj == col) { 136 col.setShortName(qname); 137 break; 138 } 139 } 140 } 141 } 142 } 144 145 148 private final void addName(String name, Column col) { 149 if (names.get(name) != null) { 150 names.put(name, "duplicate"); 151 } else { 152 names.put(name, col); 153 } 154 } 155 156 159 public int getColumnCount() throws SQLException { 160 return columns.size(); 161 } 162 163 166 public Column getColumn(int i) throws SQLException { 167 return (Column)columns.elementAt(i-1); 168 } 169 170 173 public Column getColumn(String name) throws SQLException { 174 Column c = null; 175 Object obj = names.get(name); 176 if (obj != null && obj instanceof Column) { 177 c = (Column)obj; 178 } 179 return c; 180 } 181 182 186 public int[] mapColumns(Vector names) throws SQLException { 187 int[] ret; 188 if (names == null) { 189 ret = new int[columns.size()]; 190 for (int i = 0; i < ret.length; i++) { 191 ret[i] = i+1; 192 } 193 } else { 194 ret = new int[names.size()]; 195 for (int i = 0; i < ret.length; i++) { 196 String name = (String )names.elementAt(i); 197 ret[i] = getColumnIndex(name); 198 if (ret[i] <= 0) { 199 throw new SQLException ("No such column: " + name, "42000"); 200 } 201 } 202 } 203 return ret; 204 } 205 206 209 public String getName() { return qualifier; } 210 211 215 static int nextUnquotedPeriod(String s) { 216 boolean q = false; 217 for (int i = 0; i < s.length(); i++) { 218 char c = s.charAt(i); 219 if (c == '"') q = !q; 220 if (!q && c == '.') return i; 221 } 222 return -1; 223 } 224 225 228 public void addColumn(Column col) throws SQLException { 229 columns.addElement(col); 230 col.setColumn(columns.size()); 231 col.setTable(this); 232 String name = col.getName(); 233 addName(name, col); 234 int idx = nextUnquotedPeriod(name); 235 while (idx >= 0) { 236 name = name.substring(idx+1); 237 addName(name, col); 238 idx = nextUnquotedPeriod(name); 239 } 240 } 241 242 247 public void addColumn(Column col, int pos) throws SQLException { 248 columns.addElement(col); 249 int np = columns.size(); 250 while (np > pos) { 251 Column col2 = (Column)columns.get(np-2); 252 col2.setColumn(np); 253 columns.set(np-1, col2); 254 np--; 255 } 256 columns.set(pos-1, col); 257 col.setColumn(pos); 258 col.setTable(this); 259 String name = col.getName(); 260 addName(name, col); 261 int idx = nextUnquotedPeriod(name); 262 while (idx >= 0) { 263 name = name.substring(idx+1); 264 addName(name, col); 265 idx = nextUnquotedPeriod(name); 266 } 267 } 268 269 272 public void addColumn(String name, Type type) throws SQLException { 273 addColumn(new Column(name, type)); 274 } 275 276 279 public void deleteColumn(int col) throws SQLException , IOException { 280 Column delCol = getColumn(col); 281 int del = 0; 282 for (int i = 1; i <= columns.size(); i++) { 283 if (i == col) del++; 284 else { 285 Column cx = getColumn(i); 286 cx.setColumn(i-del); 287 columns.set(i-del-1, cx); 288 } 289 } 290 columns.setSize(columns.size()-1); 291 292 Iterator iter = names.keySet().iterator(); 293 while (iter.hasNext()) { 294 String name = iter.next().toString(); 295 if (names.get(name) == delCol) iter.remove(); 296 } 297 } 298 299 303 public int getColumnIndex(String name) throws SQLException { 304 Column col = getColumn(name); 305 if (col == null) { 306 return -1; 307 } 308 return col.getColumn(); 309 } 310 311 314 public void readExternal(ObjectInput in) 315 throws IOException , ClassNotFoundException 316 { 317 qualifier = (String )in.readObject(); 318 if (qualifier.length() > 0) { 319 this.qual = qualifier + "."; 320 } else { 321 this.qual = ""; 322 } 323 Vector c = (Vector )in.readObject(); 324 this.columns = new Vector (); 325 this.names = new HashMap (); 326 for (int i = 0; i < c.size(); i++) { 327 try { 328 addColumn((Column)c.elementAt(i)); 329 } catch (SQLException e) { 330 Debug.print(e); 332 throw new IOException (e.toString()); 334 } 335 } 336 try { 337 resolveColumns(); 338 } catch (SQLException e) { 339 Debug.print(e); 341 throw new IOException (e.toString()); 343 } 344 } 345 346 349 public void writeExternal(ObjectOutput out) throws IOException { 350 out.writeObject(qualifier); 351 out.writeObject(columns); 352 } 353 354 358 public String toString() { 359 try { 360 StringBuffer sb = new StringBuffer ( 361 Table.strip(getClass().getName())); 362 sb.append(" "); 363 sb.append(qualifier); 364 sb.append("\n"); 365 for (int i = 1; i <= getColumnCount(); i++) { 366 Column c = getColumn(i); 367 if (i > 1) sb.append("\n"); 368 sb.append(" col " + i + ": "); 369 sb.append(c.toString()); 370 } 371 sb.append("\n"); 372 return sb.toString(); 374 } catch (Exception e) { 375 Debug.print(e); 376 return e.toString(); 377 } 378 } 379 381 } 382 383 | Popular Tags |