1 2 12 package com.versant.core.jdbc.metadata; 13 14 import com.versant.core.common.Debug; 15 import com.versant.core.jdbc.sql.exp.AndExp; 16 import com.versant.core.jdbc.sql.exp.SelectExp; 17 import com.versant.core.jdbc.sql.exp.SqlExp; 18 import com.versant.core.util.CharBuf; 19 import com.versant.core.jdbc.sql.exp.*; 20 import com.versant.core.jdbc.sql.JdbcNameGenerator; 21 import com.versant.core.jdbc.sql.SqlDriver; 22 23 import java.io.PrintStream ; 24 import java.io.Serializable ; 25 import java.util.*; 26 27 30 public class JdbcTable implements Serializable , Comparable { 31 32 35 public String deleteRowSql; 36 37 public String name; 38 public SqlDriver sqlDriver; 39 42 public JdbcColumn[] cols; 43 47 public JdbcColumn[] pk; 48 51 public int pkSimpleColumnCount; 52 55 public JdbcColumn[] pkSimpleCols; 56 59 public String pkConstraintName; 60 63 public JdbcIndex[] indexes; 64 67 public JdbcConstraint[] constraints; 68 71 public String comment; 72 73 private JdbcColumn lockRowColumn; 74 75 public JdbcTable() { 76 } 77 78 public List getConstraintList() { 79 if (constraints == null) { 80 return Collections.EMPTY_LIST; 81 } 82 ArrayList list = new ArrayList(constraints.length); 83 for (int i = 0; i < constraints.length; i++) { 84 JdbcConstraint constraint = constraints[i]; 85 list.add(constraint); 86 } 87 return list; 88 } 89 90 public List getColumnList() { 91 if (cols == null) { 92 return Collections.EMPTY_LIST; 93 } 94 ArrayList list = new ArrayList(cols.length); 95 for (int i = 0; i < cols.length; i++) { 96 JdbcColumn col = cols[i]; 97 list.add(col); 98 } 99 return list; 100 } 101 102 public String toString() { 103 return name; 104 } 105 106 public int hashCode() { 107 return name.hashCode(); 108 } 109 110 113 public boolean equals(Object o) { 114 if (o instanceof JdbcTable) { 115 JdbcTable t = (JdbcTable)o; 116 return name.equals(t.name); 117 } else { 118 return false; 119 } 120 } 121 122 125 public int compareTo(Object o) { 126 return name != null ? o != null ? 127 name.compareTo(((JdbcTable)o).name) : 1 : -1; 128 } 129 130 133 public String formatPkString() { 134 if (pk == null) return "(null)"; 135 StringBuffer s = new StringBuffer (); 136 for (int i = 0; i < pk.length; i++) { 137 if (i > 0) s.append(", "); 138 s.append(pk[i]); 139 } 140 return s.toString(); 141 } 142 143 public void dump() { 144 dump(Debug.OUT, ""); 145 } 146 147 public void dump(PrintStream out, String indent) { 148 out.println(indent + this + " PK " + formatPkString() + " constraint " + 149 pkConstraintName); 150 String is = indent + " "; 151 if (cols != null) { 152 out.println(is + cols.length + " col(s)"); 153 for (int i = 0; i < cols.length; i++) { 154 out.println(is + "[" + i + "] " + cols[i]); 155 } 156 } 157 if (indexes != null) { 158 out.println(is + indexes.length + " index(es)"); 159 for (int i = 0; i < indexes.length; i++) { 160 out.println(is + "[" + i + "] " + indexes[i]); 161 } 162 } 163 if (constraints != null) { 164 out.println(is + constraints.length + " constraint(s)"); 165 for (int i = 0; i < constraints.length; i++) { 166 out.println(is + "[" + i + "] " + constraints[i]); 167 } 168 } 169 } 170 171 175 public JdbcColumn findPkColumn(String columnName) { 176 for (int i = pk.length - 1; i >= 0; i--) { 177 JdbcColumn c = pk[i]; 178 if (c.name.equals(columnName)) return c; 179 } 180 return null; 181 } 182 183 186 public String [] getPkNames() { 187 int pklen = pk == null ? 0 : pk.length; 188 String [] a = new String [pklen]; 189 for (int i = 0; i < pklen; i++) { 190 a[i] = pk[i].name; 191 } 192 return a; 193 } 194 195 198 public void setTableOnCols() { 199 for (int i = 0; i < cols.length; i++) cols[i].table = this; 200 for (int i = 0; i < pk.length; i++) pk[i].table = this; 201 } 202 203 207 public void appendWherePK(CharBuf s) { 208 JdbcColumn.appendEqualsParam(s, pkSimpleCols, sqlDriver); 209 } 210 211 215 public void appendInsertPKColumnList(CharBuf s) { 216 int nc = pkSimpleCols.length; 217 s.append(pkSimpleCols[0].name); 218 for (int i = 1; i < nc; i++) { 219 s.append(", "); 220 s.append(pkSimpleCols[i].name); 221 } 222 } 223 224 228 public void appendInsertPKValueList(CharBuf s) { 229 s.append("?"); 230 int nc = pkSimpleCols.length; 231 for (int i = 1; i < nc; i++) s.append(", ?"); 232 } 233 234 238 public void appendInsertColumnList(CharBuf s) { 239 int nc = cols.length; 240 s.append(cols[0].name); 241 for (int i = 1; i < nc; i++) { 242 s.append(", "); 243 s.append(cols[i].name); 244 } 245 } 246 247 251 public void appendInsertValueList(CharBuf s) { 252 s.append("?"); 253 int nc = cols.length; 254 for (int i = 1; i < nc; i++) s.append(", ?"); 255 } 256 257 260 public SqlExp createPkEqualsParamExp(SelectExp se) { 261 int nc = pkSimpleCols.length; 262 if (nc == 1) { 263 return pkSimpleCols[0].createEqualsParamExp(se); 264 } else { 265 SqlExp list = pkSimpleCols[0].createEqualsParamExp(se); 266 SqlExp pos = list; 267 for (int i = 1; i < nc; i++) { 268 pos = pos.next = pkSimpleCols[i].createEqualsParamExp(se); 269 } 270 return new AndExp(list); 271 } 272 } 273 274 277 public SqlExp createOrderByPKList(SelectExp se) { 278 SqlExp ans = pkSimpleCols[0].toSqlExp(se); 279 int nc = pkSimpleCols.length; 280 if (nc > 1) { 281 SqlExp e = ans; 282 for (int i = 1; i < nc; i++) { 283 e = e.next = pkSimpleCols[i].toSqlExp(se); 284 } 285 } 286 return ans; 287 } 288 289 293 public void setPk(JdbcColumn[] pk) { 294 this.pk = pk; 295 pkSimpleCols = pk; 296 pkSimpleColumnCount = pkSimpleCols.length; 297 } 298 299 302 public boolean isInPrimaryKey(String columnName) { 303 for (int i = pk.length - 1; i >= 0; i--) { 304 if (pk[i].name.equals(columnName)) return true; 305 } 306 return false; 307 } 308 309 315 public JdbcColumn[] getColsForCreateTable() { 316 ArrayList a = new ArrayList(cols.length); 317 HashMap map = new HashMap(); 318 for (int i = 0; i < cols.length; i++) { 319 JdbcColumn c = cols[i]; 320 if (c.shared) continue; 321 JdbcColumn o = (JdbcColumn)map.get(c.name); 322 if (o != null && (o.foreignKey || !c.foreignKey)) continue; 323 map.put(c.name, c); 324 a.add(c); 325 } 326 JdbcColumn[] ans = new JdbcColumn[a.size()]; 327 a.toArray(ans); 328 return ans; 329 } 330 331 335 public JdbcColumn getLockRowColumn() { 336 if (lockRowColumn == null) { 337 lockRowColumn = pk[0]; 338 for (int i = cols.length - 1; i >= 0; i--) { 339 lockRowColumn = chooseLockRowCol(cols[i], lockRowColumn); 340 } 341 } 342 return lockRowColumn; 343 } 344 345 private static JdbcColumn chooseLockRowCol(JdbcColumn a, JdbcColumn b) { 346 if (!a.pk && b.pk) return a; 348 if (a.pk && !b.pk) return b; 349 350 if (!a.partOfIndex && b.partOfIndex) return a; 352 if (a.partOfIndex && !b.partOfIndex) return b; 353 354 if (!a.foreignKey && b.foreignKey) return a; 356 if (a.foreignKey && !b.foreignKey) return b; 357 358 int diff = JdbcTypes.getUpdateCost(a.jdbcType) - 360 JdbcTypes.getUpdateCost(b.jdbcType); 361 if (diff < 0) return a; 362 if (diff > 0) return b; 363 364 return b; 365 } 366 367 public void addConstraints(ArrayList cons) { 368 int length = constraints != null ? constraints.length : 0; 369 int size = cons.size(); 370 HashSet set = new HashSet((length + size) * 2); 371 for (int i = 0; i < length; i++) { 372 JdbcConstraint constraint = constraints[i]; 373 if (!set.contains(constraint)) { 374 set.add(constraint); 375 } 376 } 377 for (int i = 0; i < size; i++) { 378 Object o = cons.get(i); 379 if (!set.contains(o)) { 380 set.add(o); 381 } 382 } 383 constraints = new JdbcConstraint[set.size()]; 384 set.toArray(constraints); 385 } 386 387 public void nameConstraints(JdbcNameGenerator nameGenerator) { 388 if (constraints != null) { 389 for (int i = 0; i < constraints.length; i++) { 390 JdbcConstraint constraint = constraints[i]; 391 if (constraint.name == null) { 392 constraint.generateName(nameGenerator); 393 } 394 } 395 } 396 } 397 } 398 | Popular Tags |