1 2 12 package com.versant.core.jdbc.metadata; 13 14 import com.versant.core.common.Debug; 15 import com.versant.core.common.NotImplementedException; 16 import com.versant.core.metadata.MDStaticUtils; 17 import com.versant.core.jdbc.JdbcConverter; 18 import com.versant.core.jdbc.JdbcConverterFactory; 19 import com.versant.core.jdbc.JdbcTypeRegistry; 20 import com.versant.core.jdbc.JdbcUtils; 21 import com.versant.core.jdbc.sql.JdbcNameGenerator; 22 import com.versant.core.jdbc.sql.SqlDriver; 23 import com.versant.core.jdbc.sql.exp.*; 24 import com.versant.core.util.CharBuf; 25 26 import java.io.Serializable ; 27 import java.sql.PreparedStatement ; 28 import java.sql.ResultSet ; 29 import java.sql.SQLException ; 30 31 import com.versant.core.common.BindingSupportImpl; 32 33 36 public final class JdbcColumn implements Serializable { 37 38 41 public transient JdbcTable table; 42 47 public transient Class javaType; 48 53 public int javaTypeCode; 54 57 public boolean pk; 58 61 public boolean foreignKey; 62 65 public boolean partOfIndex; 66 69 public boolean autoinc; 70 73 public String name; 74 77 public int jdbcType; 78 81 public String sqlType; 82 83 84 87 public int length; 88 91 public int scale; 92 95 public boolean nulls; 96 100 public boolean equalityTest; 101 105 public boolean shared; 106 112 public transient JdbcConverter converter; 113 118 public transient JdbcSimpleField refField; 119 122 public String comment; 123 124 public JdbcColumn() { 125 } 126 127 public JdbcColumn(JdbcJavaTypeMapping m, JdbcTypeRegistry jdbcTypeRegistry) { 128 sqlType = m.getSqlType(); 129 if (sqlType == null) { 130 throw BindingSupportImpl.getInstance().internal( 131 "sqlType is null: " + m); 132 } 133 134 setJavaType(m.getJavaType()); 135 jdbcType = m.getJdbcType(); 136 length = m.getLength(); 137 scale = m.getScale(); 138 nulls = m.getNulls() != JdbcJavaTypeMapping.FALSE; 139 equalityTest = m.getEqualityTest() != JdbcJavaTypeMapping.FALSE; 140 setShared(m.getShared() == JdbcJavaTypeMapping.TRUE); 141 JdbcConverterFactory cf = m.getConverterFactory(); 142 if (cf != null) { 143 converter = cf.createJdbcConverter(this, null, jdbcTypeRegistry); 144 if (converter != null && converter.isOracleStyleLOB()) equalityTest = false; 145 } 146 } 147 148 153 public void updateFrom(JdbcJavaTypeMapping m, 154 JdbcTypeRegistry jdbcTypeRegistry) { 155 if (m.getSqlType() != null) sqlType = m.getSqlType(); 156 if (m.getJavaType() != null) setJavaType(m.getJavaType()); 157 if (m.getJdbcType() != 0) jdbcType = m.getJdbcType(); 158 if (m.getLength() != -1) length = m.getLength(); 159 if (m.getScale() != -1) scale = m.getScale(); 160 if (m.getNulls() != JdbcJavaTypeMapping.NOT_SET) { 161 nulls = m.getNulls() != JdbcJavaTypeMapping.FALSE; 162 } 163 if (m.getEqualityTest() != JdbcJavaTypeMapping.NOT_SET) { 164 equalityTest = m.getEqualityTest() != JdbcJavaTypeMapping.FALSE; 165 } 166 if (m.getShared() != JdbcJavaTypeMapping.NOT_SET) { 167 setShared(m.getShared() != JdbcJavaTypeMapping.FALSE); 168 } 169 JdbcConverterFactory cf = m.getConverterFactory(); 170 if (cf != null) { 171 converter = cf.createJdbcConverter(this, null, jdbcTypeRegistry); 172 if (converter != null && converter.isOracleStyleLOB()) equalityTest = false; 173 } 174 } 175 176 179 public void setJavaType(Class javaType) { 180 this.javaType = javaType; 181 javaTypeCode = MDStaticUtils.toTypeCode(javaType); 182 } 183 184 public Class getJavaType() { 185 return javaType; 186 } 187 188 194 public void addColumnNames(String tableName, JdbcNameGenerator nameGen) 195 throws IllegalArgumentException { 196 if (name != null) nameGen.addColumnName(tableName, name); 197 } 198 199 202 public String [] getColumnNames() { 203 return new String []{name}; 204 } 205 206 209 public void getColumnNames(String [] names) { 210 names[0] = name; 211 } 212 213 216 public void setColumnNames(String [] names) { 217 name = names[0]; 218 } 219 220 223 public void setTable(JdbcTable t) { 224 table = t; 225 } 226 227 231 public JdbcColumn copy() { 232 JdbcColumn d = new JdbcColumn(); 233 d.javaType = javaType; 234 d.pk = pk; 235 d.javaTypeCode = javaTypeCode; 236 d.jdbcType = jdbcType; 237 d.sqlType = sqlType; 238 d.length = length; 239 d.scale = scale; 240 d.nulls = nulls; 241 d.equalityTest = equalityTest; 242 d.setShared(shared); 243 d.converter = converter; 244 return d; 245 } 246 247 251 public static JdbcColumn[] concat(JdbcColumn[] a, JdbcColumn[] b) { 252 if (b == null) return a; 253 if (a == null) return b; 254 int na = a.length; 255 int nb = b.length; 256 JdbcColumn[] ans = new JdbcColumn[na + nb]; 257 System.arraycopy(a, 0, ans, 0, na); 258 System.arraycopy(b, 0, ans, na, nb); 259 return ans; 260 } 261 262 266 public static JdbcColumn[] concat(JdbcColumn[] a, JdbcColumn b) { 267 if (b == null) return a; 268 int na = a.length; 269 JdbcColumn[] ans = new JdbcColumn[na + 1]; 270 System.arraycopy(a, 0, ans, 0, na); 271 ans[na] = b; 272 return ans; 273 } 274 275 278 public static SqlExp toSqlExp(JdbcColumn[] cols, SelectExp se) { 279 SqlExp list = new ColumnExp(cols[0], se, null); 280 SqlExp e = list; 281 int nc = cols.length; 282 for (int i = 1; i < nc; i++) { 283 e = e.next = new ColumnExp(cols[i], se, null); 284 } 285 return list; 286 } 287 288 292 public static SqlExp toSqlExp(JdbcColumn[] cols, SelectExp se, 293 SqlExp sList) { 294 SqlExp list = new ColumnExp(cols[0], se, null); 295 SqlExp e = list; 296 int nc = cols.length; 297 for (int i = 1; i < nc; i++) { 298 e = e.next = new ColumnExp(cols[i], se, null); 299 } 300 e.next = sList; 301 return list; 302 } 303 304 307 public SqlExp toSqlExp(SelectExp se) { 308 return new ColumnExp(this, SelectExp.createJoinToSuperTable(se, table), 309 null); 310 } 311 312 318 public static void addColumnNames(String tableName, JdbcColumn[] cols, 319 JdbcNameGenerator namegen) throws IllegalArgumentException { 320 for (int i = 0; i < cols.length; i++) { 321 cols[i].addColumnNames(tableName, namegen); 322 } 323 } 324 325 328 public static String [] getColumnNames(JdbcColumn[] cols) { 329 if (cols == null) { 330 return new String [0]; 331 } 332 int n = cols.length; 333 String [] ans = new String [n]; 334 for (int i = 0; i < n; i++) { 335 ans[i] = cols[i].name; 336 } 337 return ans; 338 } 339 340 343 public static void setColumnNames(JdbcColumn[] cols, String [] names) { 344 if (cols == null) { 345 return; 346 } 347 int n = cols.length; 348 for (int i = 0; i < n; i++) { 349 cols[i].name = names[i]; 350 } 351 } 352 353 356 public static String toNameString(JdbcColumn[] cols) { 357 StringBuffer s = new StringBuffer (); 358 int len = cols.length; 359 for (int i = 0; i < len; i++) { 360 if (i > 0) s.append(", "); 361 s.append(cols[i].name); 362 } 363 return s.toString(); 364 } 365 366 369 public void setNulls(boolean nulls) { 370 this.nulls = nulls; 371 } 372 373 377 public static SqlExp createEqualsParamExp(JdbcColumn[] scols, SelectExp se) { 378 int nc = scols.length; 379 if (nc == 1) { 380 return scols[0].createEqualsParamExp(se); 381 } else { 382 SqlExp list = scols[0].createEqualsParamExp(se); 383 SqlExp pos = list; 384 for (int i = 1; i < nc; i++) { 385 pos = pos.next = scols[i].createEqualsParamExp(se); 386 } 387 return new AndExp(list); 388 } 389 } 390 391 public static InExp createInParamExp(JdbcColumn[] scols, SelectExp se, 392 int size) { 393 if (scols.length > 1) { 394 throw BindingSupportImpl.getInstance().notImplemented( 395 "'In' expressions is not support on multi-pk classes"); 396 } 397 398 ColumnExp columnExp = new ColumnExp(scols[0], se, null); 399 ParamExp rootParam = new ParamExp(scols[0].jdbcType, null); 400 columnExp.next = rootParam; 401 for (int i = 0; i < (size - 1); i++) { 402 rootParam.next = new ParamExp(scols[0].jdbcType, null); 403 rootParam = (ParamExp)rootParam.next; 404 } 405 return new InExp(columnExp); 406 } 407 408 413 public static void appendEqualsParam(CharBuf s, JdbcColumn[] cols, 414 SqlDriver driver) { 415 int nc = cols.length; 416 JdbcColumn sc = cols[0]; 417 s.append(sc.name); 418 s.append(' '); 419 s.append('='); 420 s.append(' '); 421 driver.appendWhereParam(s, sc); 422 for (int i = 1; i < nc; i++) { 423 s.append(" AND "); 424 sc = cols[i]; 425 s.append(sc.name); 426 s.append(' '); 427 s.append('='); 428 s.append(' '); 429 driver.appendWhereParam(s, sc); 430 } 431 } 432 433 436 public Object get(ResultSet rs, int index) throws SQLException { 437 if (converter == null) { 438 return JdbcUtils.get(rs, index, javaTypeCode, scale); 439 } else { 440 return converter.get(rs, index, this); 441 } 442 } 443 444 445 446 449 public void set(PreparedStatement ps, int index, Object value) 450 throws SQLException { 451 if (converter == null) { 452 JdbcUtils.set(ps, index, value, javaTypeCode, jdbcType); 453 } else { 454 converter.set(ps, index, this, value); 455 } 456 } 457 458 461 public void set(PreparedStatement ps, int index, int value) 462 throws SQLException { 463 if (converter == null) { 464 JdbcUtils.set(ps, index, value, javaTypeCode, jdbcType); 465 } else { 466 converter.set(ps, index, this, value); 467 } 468 } 469 470 473 public int getInt(ResultSet rs, int index) throws SQLException { 474 if (converter == null) { 475 return rs.getInt(index); 476 } else { 477 return ((Integer )converter.get(rs, index, this)).intValue(); 478 } 479 } 480 481 484 public void appendNames(CharBuf s) { 485 s.append(name); 486 } 487 488 491 public void appendParams(CharBuf s) { 492 s.append('?'); 493 } 494 495 498 public SqlExp createEqualsParamExp(SelectExp se) { 499 return new BinaryOpExp(new ColumnExp(this, se, null), 500 BinaryOpExp.EQUAL, 501 new ParamExp(jdbcType, null)); 502 } 503 504 507 public boolean isForUpdate() { 508 return !shared; 509 } 510 511 public String toString() { 512 StringBuffer s = new StringBuffer (); 513 s.append(javaType == null ? "(null javaType)" : javaType.getName()); 514 s.append(' '); 515 s.append(name); 516 s.append(' '); 517 s.append(sqlType); 518 s.append('['); 519 s.append(JdbcTypes.toString(jdbcType)); 520 s.append(']'); 521 if (length != 0 || scale != 0) { 522 s.append('('); 523 s.append(length); 524 if (scale != 0) { 525 s.append(','); 526 s.append(scale); 527 } 528 s.append(')'); 529 } 530 s.append(nulls ? " null" : " not null"); 531 if (refField != null) { 532 s.append(" ref "); 533 s.append(refField); 534 } 535 s.append(shared ? " shared" : ""); 536 s.append(autoinc ? " autoinc" : ""); 537 if (Debug.DEBUG) { 538 s.append(" 0x"); 539 s.append(Integer.toHexString(System.identityHashCode(this))); 540 } 541 return s.toString(); 542 } 543 544 public void setShared(boolean shared) { 545 this.shared = shared; 546 } 547 548 public String getShortName() { 549 StringBuffer buffer = new StringBuffer (name); 550 buffer.append(" "); 551 buffer.append(sqlType); 552 if (length > 0) { 553 buffer.append("("); 554 buffer.append(length); 555 buffer.append(")"); 556 } 557 return buffer.toString(); 558 } 559 560 public String getTypeString() { 561 StringBuffer buffer = new StringBuffer (); 562 buffer.append(sqlType); 563 if (length > 0) { 564 buffer.append("("); 565 buffer.append(length); 566 buffer.append(")"); 567 } 568 return buffer.toString(); 569 } 570 571 574 public LiteralExp createClassIdLiteralExp(Object val) { 575 return new LiteralExp(JdbcTypes.getLiteralType(javaTypeCode), 576 val.toString()); 577 } 578 579 public boolean equals(Object o) { 580 if (this == o) return true; 581 if (!(o instanceof JdbcColumn)) return false; 582 583 final JdbcColumn jdbcColumn = (JdbcColumn)o; 584 585 if (name != null ? !name.equals(jdbcColumn.name) : jdbcColumn.name != null) return false; 586 if (table != null ? !table.equals(jdbcColumn.table) : jdbcColumn.table != null) return false; 587 588 return true; 589 } 590 591 public int hashCode() { 592 int result; 593 result = (table != null ? table.hashCode() : 0); 594 result = 29 * result + (name != null ? name.hashCode() : 0); 595 return result; 596 } 597 } 598 | Popular Tags |