| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
package org.melati.poem; |
| 46 | |
|
| 47 | |
import java.io.PrintStream; |
| 48 | |
import java.sql.Connection; |
| 49 | |
import java.sql.PreparedStatement; |
| 50 | |
import java.sql.ResultSet; |
| 51 | |
import java.sql.SQLException; |
| 52 | |
import java.sql.Statement; |
| 53 | |
import java.util.Arrays; |
| 54 | |
import java.util.Enumeration; |
| 55 | |
import java.util.Hashtable; |
| 56 | |
import java.util.List; |
| 57 | |
import java.util.Vector; |
| 58 | |
|
| 59 | |
import org.melati.poem.dbms.Dbms; |
| 60 | |
import org.melati.poem.transaction.Transactioned; |
| 61 | |
import org.melati.poem.transaction.TransactionedSerial; |
| 62 | |
import org.melati.poem.util.ArrayEnumeration; |
| 63 | |
import org.melati.poem.util.ArrayUtils; |
| 64 | |
import org.melati.poem.util.Cache; |
| 65 | |
import org.melati.poem.util.CachedIndexFactory; |
| 66 | |
import org.melati.poem.util.EnumUtils; |
| 67 | |
import org.melati.poem.util.MappedEnumeration; |
| 68 | |
import org.melati.poem.util.Procedure; |
| 69 | |
import org.melati.poem.util.FilteredEnumeration; |
| 70 | |
import org.melati.poem.util.FlattenedEnumeration; |
| 71 | |
import org.melati.poem.util.Order; |
| 72 | |
import org.melati.poem.util.SortUtils; |
| 73 | |
import org.melati.poem.util.StringUtils; |
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | 6150 | public class JdbcTable <P extends Persistent> implements Selectable<P>, Table<P> { |
| 80 | |
|
| 81 | |
|
| 82 | |
private static final int CACHE_LIMIT_DEFAULT = 100; |
| 83 | |
private static final int DISPLAY_ORDER_DEFAULT = 100; |
| 84 | |
|
| 85 | 932 | private JdbcTable<P> _this = this; |
| 86 | |
|
| 87 | |
Database database; |
| 88 | |
private String name; |
| 89 | |
private String quotedName; |
| 90 | |
private DefinitionSource definitionSource; |
| 91 | |
|
| 92 | 932 | private TableInfo info = null; |
| 93 | |
|
| 94 | 932 | private TableListener[] listeners = {}; |
| 95 | |
|
| 96 | 932 | private Column<?>[] columns = {}; |
| 97 | 932 | private Hashtable<String, Column<?>> columnsByName = new Hashtable<String, Column<?>>(); |
| 98 | |
|
| 99 | 932 | private Column<Integer> troidColumn = null; |
| 100 | 932 | private Column<Boolean> deletedColumn = null; |
| 101 | 932 | private Column<Capability> canReadColumn = null; |
| 102 | 932 | private Column<Capability> canSelectColumn = null; |
| 103 | 932 | private Column<Capability> canWriteColumn = null; |
| 104 | 932 | private Column<Capability> canDeleteColumn = null; |
| 105 | 932 | private Column<?> displayColumn = null; |
| 106 | 932 | private Column<?> searchColumn = null; |
| 107 | |
|
| 108 | 932 | private String defaultOrderByClause = null; |
| 109 | |
|
| 110 | 932 | private Column<?>[][] displayColumns = new Column[DisplayLevel.count()][]; |
| 111 | 932 | private Column<?>[] searchColumns = null; |
| 112 | |
|
| 113 | |
private TransactionedSerial serial; |
| 114 | |
|
| 115 | 932 | private CachedSelection<P> allTroids = null; |
| 116 | 932 | private Hashtable<String, CachedSelection<P>> cachedSelections = new Hashtable<String, CachedSelection<P>>(); |
| 117 | 932 | private Hashtable<String, CachedCount> cachedCounts = new Hashtable<String, CachedCount>(); |
| 118 | 932 | private Hashtable<String, CachedExists> cachedExists = new Hashtable<String, CachedExists>(); |
| 119 | |
|
| 120 | 932 | private int mostRecentTroid = -1; |
| 121 | 932 | private int extrasIndex = 0; |
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
public JdbcTable(Database database, String name, |
| 128 | 932 | DefinitionSource definitionSource) { |
| 129 | 932 | this.database = database; |
| 130 | 932 | this.name = name; |
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | 932 | this.definitionSource = definitionSource; |
| 136 | 932 | serial = new TransactionedSerial(database); |
| 137 | 932 | } |
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
public void init() { |
| 143 | 705 | } |
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | |
|
| 158 | |
public void postInitialise() { |
| 159 | 708 | clearColumnInfoCaches(); |
| 160 | 708 | database.getColumnInfoTable().addListener( |
| 161 | 708 | new TableListener() { |
| 162 | |
public void notifyTouched(PoemTransaction transaction, Table<?> table, |
| 163 | |
Persistent persistent) { |
| 164 | 2600 | _this.notifyColumnInfo((ColumnInfo)persistent); |
| 165 | 2600 | } |
| 166 | |
|
| 167 | |
public void notifyUncached(Table<?> table) { |
| 168 | 1492 | _this.clearColumnInfoCaches(); |
| 169 | 1492 | } |
| 170 | |
}); |
| 171 | 708 | } |
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
public final Database getDatabase() { |
| 184 | 60900 | return database; |
| 185 | |
} |
| 186 | |
|
| 187 | |
|
| 188 | |
|
| 189 | |
|
| 190 | |
|
| 191 | |
|
| 192 | |
|
| 193 | |
|
| 194 | |
|
| 195 | |
|
| 196 | |
public final String getName() { |
| 197 | 6119 | return name; |
| 198 | |
} |
| 199 | |
|
| 200 | |
|
| 201 | |
|
| 202 | |
|
| 203 | |
public final String quotedName() { |
| 204 | 19289 | if (quotedName == null) quotedName = database.quotedName(name); |
| 205 | 19289 | return quotedName; |
| 206 | |
} |
| 207 | |
|
| 208 | |
|
| 209 | |
|
| 210 | |
|
| 211 | |
|
| 212 | |
|
| 213 | |
|
| 214 | |
public final String getDisplayName() { |
| 215 | 2 | return info.getDisplayname(); |
| 216 | |
} |
| 217 | |
|
| 218 | |
|
| 219 | |
|
| 220 | |
|
| 221 | |
|
| 222 | |
|
| 223 | |
|
| 224 | |
public final String getDescription() { |
| 225 | 2 | return info.getDescription(); |
| 226 | |
} |
| 227 | |
|
| 228 | |
|
| 229 | |
|
| 230 | |
|
| 231 | |
|
| 232 | |
|
| 233 | |
|
| 234 | |
|
| 235 | |
public final TableCategory getCategory() { |
| 236 | 1 | return info.getCategory(); |
| 237 | |
} |
| 238 | |
|
| 239 | |
|
| 240 | |
|
| 241 | |
|
| 242 | |
public final TableInfo getInfo() { |
| 243 | 69 | return info; |
| 244 | |
} |
| 245 | |
|
| 246 | |
|
| 247 | |
|
| 248 | |
|
| 249 | |
|
| 250 | |
|
| 251 | |
|
| 252 | |
|
| 253 | |
public final Integer tableInfoID() { |
| 254 | 8984 | return info == null ? null : info.troid(); |
| 255 | |
} |
| 256 | |
|
| 257 | |
|
| 258 | |
|
| 259 | |
|
| 260 | |
|
| 261 | |
|
| 262 | |
|
| 263 | |
|
| 264 | |
|
| 265 | |
|
| 266 | |
|
| 267 | |
|
| 268 | |
|
| 269 | |
public final Column<?> getColumn(String nameP) throws NoSuchColumnPoemException { |
| 270 | 891 | Column<?> column = _getColumn(nameP); |
| 271 | 891 | if (column == null) |
| 272 | 1 | throw new NoSuchColumnPoemException(this, nameP); |
| 273 | |
else |
| 274 | 890 | return column; |
| 275 | |
} |
| 276 | |
protected final Column<?> _getColumn(String nameP) { |
| 277 | 10024 | Column<?> column = columnsByName.get(nameP.toLowerCase()); |
| 278 | 10024 | return column; |
| 279 | |
} |
| 280 | |
|
| 281 | |
|
| 282 | |
|
| 283 | |
|
| 284 | |
|
| 285 | |
|
| 286 | |
|
| 287 | |
public final Enumeration<Column<?>> columns() { |
| 288 | 15524 | return new ArrayEnumeration<Column<?>>(columns); |
| 289 | |
} |
| 290 | |
|
| 291 | |
public final List<Column<?>> getColumns() { |
| 292 | 0 | return Arrays.asList(columns); |
| 293 | |
} |
| 294 | |
|
| 295 | |
|
| 296 | |
|
| 297 | |
|
| 298 | |
public final int getColumnsCount() { |
| 299 | 1 | return columns.length; |
| 300 | |
} |
| 301 | |
|
| 302 | |
|
| 303 | |
|
| 304 | |
|
| 305 | |
|
| 306 | |
public Column<?> columnWithColumnInfoID(int columnInfoID) { |
| 307 | 152 | for (Enumeration<Column<?>> c = columns(); c.hasMoreElements();) { |
| 308 | 1015 | Column<?> column = c.nextElement(); |
| 309 | 1015 | Integer id = column.columnInfoID(); |
| 310 | 1015 | if (id != null && id.intValue() == columnInfoID) |
| 311 | 134 | return column; |
| 312 | 881 | } |
| 313 | 18 | return null; |
| 314 | |
} |
| 315 | |
|
| 316 | |
|
| 317 | |
|
| 318 | |
|
| 319 | |
|
| 320 | |
|
| 321 | |
|
| 322 | |
|
| 323 | |
|
| 324 | |
|
| 325 | |
public final Column<Integer> troidColumn() { |
| 326 | 1185 | return troidColumn; |
| 327 | |
} |
| 328 | |
|
| 329 | |
|
| 330 | |
|
| 331 | |
|
| 332 | |
public final Column<Boolean> deletedColumn() { |
| 333 | 1 | return deletedColumn; |
| 334 | |
} |
| 335 | |
|
| 336 | |
|
| 337 | |
|
| 338 | |
|
| 339 | |
|
| 340 | |
|
| 341 | |
|
| 342 | |
|
| 343 | |
|
| 344 | |
|
| 345 | |
|
| 346 | |
|
| 347 | |
|
| 348 | |
|
| 349 | |
public final Column<?> displayColumn() { |
| 350 | 667 | return displayColumn == null ? troidColumn : displayColumn; |
| 351 | |
} |
| 352 | |
|
| 353 | |
|
| 354 | |
|
| 355 | |
|
| 356 | |
@SuppressWarnings("rawtypes") |
| 357 | |
public final void setDisplayColumn(Column column) { |
| 358 | 440 | displayColumn = column; |
| 359 | 440 | } |
| 360 | |
|
| 361 | |
|
| 362 | |
|
| 363 | |
|
| 364 | |
|
| 365 | |
|
| 366 | |
|
| 367 | |
|
| 368 | |
|
| 369 | |
|
| 370 | |
|
| 371 | |
|
| 372 | |
|
| 373 | |
public final Column<?> primaryCriterionColumn() { |
| 374 | 1 | return searchColumn; |
| 375 | |
} |
| 376 | |
|
| 377 | |
|
| 378 | |
|
| 379 | |
|
| 380 | |
public void setSearchColumn(Column<?> column) { |
| 381 | 432 | searchColumn = column; |
| 382 | 432 | } |
| 383 | |
|
| 384 | |
|
| 385 | |
|
| 386 | |
|
| 387 | |
|
| 388 | |
|
| 389 | |
@SuppressWarnings({ "unchecked", "rawtypes" }) |
| 390 | |
public String defaultOrderByClause() { |
| 391 | 687 | String clause = defaultOrderByClause; |
| 392 | |
|
| 393 | 687 | if (clause == null) { |
| 394 | 822 | clause = EnumUtils.concatenated( |
| 395 | |
", ", |
| 396 | 411 | new MappedEnumeration(new ArrayEnumeration(SortUtils.sorted( |
| 397 | 411 | new Order() { |
| 398 | |
public boolean lessOrEqual(Object a, Object b) { |
| 399 | 33 | return |
| 400 | 33 | ((Column)a).getDisplayOrderPriority().intValue() <= |
| 401 | 33 | ((Column)b).getDisplayOrderPriority().intValue(); |
| 402 | |
} |
| 403 | |
}, |
| 404 | 3554 | new FilteredEnumeration<Column<?>>(columns()) { |
| 405 | |
public boolean isIncluded(Column<?> column) { |
| 406 | 3143 | return ((Column)column).getDisplayOrderPriority() != null; |
| 407 | |
} |
| 408 | 411 | }))) { |
| 409 | |
public Object mapped(Object column) { |
| 410 | 140 | String sort = ((Column)column).fullQuotedName(); |
| 411 | 140 | if (((Column)column).getSortDescending()) sort += " desc"; |
| 412 | 140 | return sort; |
| 413 | |
} |
| 414 | |
}); |
| 415 | |
|
| 416 | 411 | if (clause.equals("") && displayColumn() != null) |
| 417 | 299 | clause = displayColumn().fullQuotedName(); |
| 418 | |
|
| 419 | 411 | defaultOrderByClause = clause; |
| 420 | |
} |
| 421 | |
|
| 422 | 687 | return clause; |
| 423 | |
} |
| 424 | |
|
| 425 | |
|
| 426 | |
|
| 427 | |
|
| 428 | |
public void clearColumnInfoCaches() { |
| 429 | 2437 | defaultOrderByClause = null; |
| 430 | 14622 | for (int i = 0; i < displayColumns.length; ++i) |
| 431 | 12185 | displayColumns[i] = null; |
| 432 | 2437 | } |
| 433 | |
|
| 434 | |
|
| 435 | |
|
| 436 | |
|
| 437 | |
|
| 438 | |
|
| 439 | |
public void notifyColumnInfo(ColumnInfo infoP) { |
| 440 | |
|
| 441 | 2763 | if (infoP == null || infoP.getTableinfo_unsafe().equals(tableInfoID())) |
| 442 | 234 | clearColumnInfoCaches(); |
| 443 | 2763 | } |
| 444 | |
|
| 445 | |
|
| 446 | |
|
| 447 | |
|
| 448 | |
|
| 449 | |
|
| 450 | |
|
| 451 | |
|
| 452 | |
|
| 453 | |
|
| 454 | |
private Column<?>[] columnsWhere(String whereClause) { |
| 455 | |
|
| 456 | 26 | Enumeration<Integer> colIDs = |
| 457 | 52 | getDatabase().getColumnInfoTable().troidSelection( |
| 458 | 26 | database.quotedName("tableinfo") + " = " + tableInfoID() + |
| 459 | |
" AND (" + whereClause + ")", |
| 460 | 26 | null, false, PoemThread.inSession() ? PoemThread.transaction() : null); |
| 461 | |
|
| 462 | 26 | Vector<Column<?>> them = new Vector<Column<?>>(); |
| 463 | 155 | while (colIDs.hasMoreElements()) { |
| 464 | 129 | Column<?> column = |
| 465 | 129 | columnWithColumnInfoID(((Integer)colIDs.nextElement()).intValue()); |
| 466 | |
|
| 467 | 129 | if (column != null) |
| 468 | 129 | them.addElement(column); |
| 469 | 129 | } |
| 470 | |
|
| 471 | 26 | Column<?>[] columnsLocal = new Column<?>[them.size()]; |
| 472 | 26 | them.copyInto(columnsLocal); |
| 473 | 26 | return columnsLocal; |
| 474 | |
} |
| 475 | |
|
| 476 | |
|
| 477 | |
|
| 478 | |
|
| 479 | |
|
| 480 | |
|
| 481 | |
|
| 482 | |
public final Enumeration<Column<?>> displayColumns(DisplayLevel level) { |
| 483 | 104 | Column<?>[] columnsLocal = displayColumns[level.getIndex().intValue()]; |
| 484 | |
|
| 485 | 104 | if (columnsLocal == null) { |
| 486 | 24 | columnsLocal = |
| 487 | 48 | columnsWhere(database.quotedName("displaylevel") + " <= " + |
| 488 | 24 | level.getIndex()); |
| 489 | 24 | displayColumns[level.getIndex().intValue()] = columnsLocal; |
| 490 | |
} |
| 491 | 104 | return new ArrayEnumeration<Column<?>>(columnsLocal); |
| 492 | |
} |
| 493 | |
|
| 494 | |
|
| 495 | |
|
| 496 | |
|
| 497 | |
|
| 498 | |
public final int displayColumnsCount(DisplayLevel level) { |
| 499 | 22 | int l = level.getIndex().intValue(); |
| 500 | 22 | if (displayColumns[l] == null) |
| 501 | |
|
| 502 | 10 | displayColumns(level); |
| 503 | |
|
| 504 | 22 | return displayColumns[l].length; |
| 505 | |
} |
| 506 | |
|
| 507 | |
|
| 508 | |
|
| 509 | |
|
| 510 | |
|
| 511 | |
|
| 512 | |
|
| 513 | |
|
| 514 | |
|
| 515 | |
public final Enumeration<Column<?>> getDetailDisplayColumns() { |
| 516 | 72 | return displayColumns(DisplayLevel.detail); |
| 517 | |
} |
| 518 | |
|
| 519 | |
|
| 520 | |
|
| 521 | |
|
| 522 | |
public final int getDetailDisplayColumnsCount() { |
| 523 | 19 | return displayColumnsCount(DisplayLevel.detail); |
| 524 | |
} |
| 525 | |
|
| 526 | |
|
| 527 | |
|
| 528 | |
|
| 529 | |
|
| 530 | |
|
| 531 | |
|
| 532 | |
|
| 533 | |
|
| 534 | |
public final Enumeration<Column<?>> getRecordDisplayColumns() { |
| 535 | 17 | return displayColumns(DisplayLevel.record); |
| 536 | |
} |
| 537 | |
|
| 538 | |
|
| 539 | |
|
| 540 | |
|
| 541 | |
public final int getRecordDisplayColumnsCount() { |
| 542 | 1 | return displayColumnsCount(DisplayLevel.record); |
| 543 | |
} |
| 544 | |
|
| 545 | |
|
| 546 | |
|
| 547 | |
|
| 548 | |
|
| 549 | |
|
| 550 | |
|
| 551 | |
|
| 552 | |
|
| 553 | |
|
| 554 | |
public final Enumeration<Column<?>> getSummaryDisplayColumns() { |
| 555 | 5 | return displayColumns(DisplayLevel.summary); |
| 556 | |
} |
| 557 | |
|
| 558 | |
|
| 559 | |
|
| 560 | |
|
| 561 | |
public final int getSummaryDisplayColumnsCount() { |
| 562 | 1 | return displayColumnsCount(DisplayLevel.summary); |
| 563 | |
} |
| 564 | |
|
| 565 | |
|
| 566 | |
|
| 567 | |
|
| 568 | |
|
| 569 | |
|
| 570 | |
|
| 571 | |
|
| 572 | |
public final Enumeration<Column<?>> getSearchCriterionColumns() { |
| 573 | 3 | Column<?>[] columnsLocal = searchColumns; |
| 574 | |
|
| 575 | 3 | if (columnsLocal == null) { |
| 576 | 2 | columnsLocal = |
| 577 | 4 | columnsWhere(database.quotedName("searchability") + " <= " + |
| 578 | 2 | Searchability.yes.getIndex()); |
| 579 | 2 | searchColumns = columnsLocal; |
| 580 | |
} |
| 581 | 3 | return new ArrayEnumeration<Column<?>>(searchColumns); |
| 582 | |
} |
| 583 | |
|
| 584 | |
|
| 585 | |
|
| 586 | |
|
| 587 | |
public final int getSearchCriterionColumnsCount() { |
| 588 | 1 | if (searchColumns == null) |
| 589 | |
|
| 590 | 1 | getSearchCriterionColumns(); |
| 591 | |
|
| 592 | 1 | return searchColumns.length; |
| 593 | |
} |
| 594 | |
|
| 595 | |
private Dbms dbms() { |
| 596 | 11126 | return getDatabase().getDbms(); |
| 597 | |
} |
| 598 | |
|
| 599 | |
|
| 600 | |
|
| 601 | |
|
| 602 | |
|
| 603 | |
|
| 604 | |
|
| 605 | |
|
| 606 | |
|
| 607 | |
|
| 608 | |
|
| 609 | |
|
| 610 | |
|
| 611 | |
|
| 612 | |
|
| 613 | |
|
| 614 | |
public void dbModifyStructure(String sql) |
| 615 | |
throws StructuralModificationFailedPoemException { |
| 616 | 976 | database.modifyStructure(sql); |
| 617 | 976 | } |
| 618 | |
|
| 619 | |
private void dbCreateTable() { |
| 620 | 293 | String createTableSql = dbms().createTableSql(this); |
| 621 | 293 | database.modifyStructure(createTableSql); |
| 622 | 293 | String tableSetup = database.getDbms().tableInitialisationSql(this); |
| 623 | 293 | if (tableSetup != null) { |
| 624 | 0 | database.modifyStructure(tableSetup); |
| 625 | |
} |
| 626 | 293 | } |
| 627 | |
|
| 628 | |
|
| 629 | |
|
| 630 | |
|
| 631 | |
|
| 632 | |
|
| 633 | |
|
| 634 | |
public String getDbmsTableType() { |
| 635 | 294 | return null; |
| 636 | |
} |
| 637 | |
|
| 638 | |
|
| 639 | |
|
| 640 | |
|
| 641 | |
|
| 642 | |
public void dbAddConstraints() { |
| 643 | 153 | StringBuffer sqb = new StringBuffer(); |
| 644 | 1326 | for (int c = 0; c < columns.length; ++c) { |
| 645 | 1173 | if (columns[c].getSQLType() instanceof TroidPoemType){ |
| 646 | 153 | sqb.append("ALTER TABLE " + quotedName()); |
| 647 | 306 | sqb.append(dbms().getPrimaryKeyDefinition( |
| 648 | 153 | columns[c].getName())); |
| 649 | |
try { |
| 650 | 153 | dbModifyStructure(sqb.toString()); |
| 651 | 0 | } catch (StructuralModificationFailedPoemException e) { |
| 652 | |
|
| 653 | |
|
| 654 | 0 | e = null; |
| 655 | 153 | } |
| 656 | |
} |
| 657 | |
} |
| 658 | 1326 | for (int c = 0; c < columns.length; ++c) { |
| 659 | 1173 | if (columns[c].getSQLType() instanceof ReferencePoemType){ |
| 660 | 170 | IntegrityFix fix = columns[c].getIntegrityFix(); |
| 661 | 170 | sqb = new StringBuffer(); |
| 662 | 170 | sqb.append("ALTER TABLE " + quotedName()); |
| 663 | 340 | sqb.append(dbms().getForeignKeyDefinition( |
| 664 | 170 | getName(), |
| 665 | 170 | columns[c].getName(), |
| 666 | 170 | ((PersistentReferencePoemType)columns[c].getSQLType()). |
| 667 | 170 | targetTable().getName(), |
| 668 | 170 | ((PersistentReferencePoemType)columns[c].getSQLType()). |
| 669 | 170 | targetTable().troidColumn().getName(), |
| 670 | 170 | fix.getName())); |
| 671 | |
try { |
| 672 | 170 | dbModifyStructure(sqb.toString()); |
| 673 | 0 | } catch (StructuralModificationFailedPoemException e) { |
| 674 | |
|
| 675 | |
|
| 676 | 0 | e = null; |
| 677 | 170 | } |
| 678 | |
} |
| 679 | |
} |
| 680 | |
|
| 681 | |
|
| 682 | 153 | } |
| 683 | |
|
| 684 | |
private void dbAddColumn(Column<?> column) { |
| 685 | 84 | if (column.getType().getNullable()) { |
| 686 | 130 | dbModifyStructure( |
| 687 | 65 | "ALTER TABLE " + quotedName() + |
| 688 | 65 | " ADD " + column.quotedName() + |
| 689 | 65 | " " + column.getSQLType().sqlDefinition(dbms())); |
| 690 | |
} else { |
| 691 | 19 | if (column.getUnique()) { |
| 692 | 2 | throw new UnificationPoemException("Cannot add new unique, non-nullable column " |
| 693 | 2 | + column.getName() + " to table " + getName()); |
| 694 | |
} else { |
| 695 | 34 | dbModifyStructure( |
| 696 | 17 | "ALTER TABLE " + quotedName() + |
| 697 | 17 | " ADD " + column.quotedName() + |
| 698 | 17 | " " + column.getSQLType().sqlTypeDefinition(dbms())); |
| 699 | 34 | dbModifyStructure( |
| 700 | 17 | "UPDATE " + quotedName() + |
| 701 | 17 | " SET " + column.quotedName() + |
| 702 | 34 | " = " + dbms().getQuotedValue(column.getSQLType(), |
| 703 | 17 | column.getSQLType().sqlDefaultValue(dbms()))); |
| 704 | |
} |
| 705 | 34 | dbModifyStructure( |
| 706 | 17 | dbms().alterColumnNotNullableSQL(name, column)); |
| 707 | |
|
| 708 | |
} |
| 709 | 82 | } |
| 710 | |
|
| 711 | |
|
| 712 | |
private void dbCreateIndex(Column<?> column) { |
| 713 | 4198 | if (column.getIndexed()) { |
| 714 | 535 | if (!dbms().canBeIndexed(column)) { |
| 715 | 0 | database.log(new UnindexableLogEvent(column)); |
| 716 | |
} else { |
| 717 | 1070 | dbModifyStructure( |
| 718 | 535 | "CREATE " + (column.getUnique() ? "UNIQUE " : "") + "INDEX " + |
| 719 | 535 | indexName(column) + |
| 720 | 535 | " ON " + quotedName() + " " + |
| 721 | 535 | "(" + column.quotedName() + |
| 722 | 535 | dbms().getIndexLength(column) + ")"); |
| 723 | |
} |
| 724 | |
} |
| 725 | 4198 | } |
| 726 | |
|
| 727 | |
private String indexName(Column<?> column) { |
| 728 | 2276 | return database.quotedName( |
| 729 | 1138 | dbms().unreservedName(name) + "_" + |
| 730 | 1138 | dbms().unreservedName(column.getName()) + "_index"); |
| 731 | |
} |
| 732 | |
|
| 733 | |
|
| 734 | |
|
| 735 | |
|
| 736 | |
|
| 737 | |
|
| 738 | |
|
| 739 | |
|
| 740 | |
|
| 741 | |
|
| 742 | |
|
| 743 | |
private PreparedStatement simpleInsert(Connection connection) { |
| 744 | 343 | StringBuffer sql = new StringBuffer(); |
| 745 | 343 | sql.append("INSERT INTO " + quotedName() + " ("); |
| 746 | 2644 | for (int c = 0; c < columns.length; ++c) { |
| 747 | 2301 | if (c > 0) sql.append(", "); |
| 748 | 2301 | sql.append(columns[c].quotedName()); |
| 749 | |
} |
| 750 | 343 | sql.append(") VALUES ("); |
| 751 | 2644 | for (int c = 0; c < columns.length; ++c) { |
| 752 | 2301 | if (c > 0) sql.append(", "); |
| 753 | 2301 | sql.append("?"); |
| 754 | |
} |
| 755 | |
|
| 756 | 343 | sql.append(")"); |
| 757 | |
|
| 758 | |
try { |
| 759 | 343 | return connection.prepareStatement(sql.toString()); |
| 760 | |
} |
| 761 | 0 | catch (SQLException e) { |
| 762 | 0 | throw new SimplePrepareFailedPoemException(sql.toString(), e); |
| 763 | |
} |
| 764 | |
} |
| 765 | |
|
| 766 | |
private PreparedStatement simpleGet(Connection connection) { |
| 767 | 343 | StringBuffer sql = new StringBuffer(); |
| 768 | 343 | sql.append("SELECT "); |
| 769 | 2644 | for (int c = 0; c < columns.length; ++c) { |
| 770 | 2301 | if (c > 0) sql.append(", "); |
| 771 | 2301 | sql.append(columns[c].quotedName()); |
| 772 | |
} |
| 773 | 686 | sql.append(" FROM " + quotedName() + |
| 774 | 343 | " WHERE " + troidColumn.quotedName() + " = ?"); |
| 775 | |
|
| 776 | |
try { |
| 777 | 343 | return connection.prepareStatement(sql.toString()); |
| 778 | |
} |
| 779 | 0 | catch (SQLException e) { |
| 780 | 0 | throw new SimplePrepareFailedPoemException(sql.toString(), e); |
| 781 | |
} |
| 782 | |
} |
| 783 | |
|
| 784 | |
private PreparedStatement simpleModify(Connection connection) { |
| 785 | |
|
| 786 | 343 | StringBuffer sql = new StringBuffer(); |
| 787 | 343 | sql.append("UPDATE " + quotedName() + " SET "); |
| 788 | 2644 | for (int c = 0; c < columns.length; ++c) { |
| 789 | 2301 | if (c > 0) sql.append(", "); |
| 790 | 2301 | sql.append(columns[c].quotedName()); |
| 791 | 2301 | sql.append(" = ?"); |
| 792 | |
} |
| 793 | 343 | sql.append(" WHERE " + troidColumn.quotedName() + " = ?"); |
| 794 | |
|
| 795 | |
try { |
| 796 | 343 | return connection.prepareStatement(sql.toString()); |
| 797 | |
} |
| 798 | 0 | catch (SQLException e) { |
| 799 | 0 | throw new SimplePrepareFailedPoemException(sql.toString(), e); |
| 800 | |
} |
| 801 | |
} |
| 802 | |
|
| 803 | |
|
| 804 | |
|
| 805 | |
|
| 806 | |
|
| 807 | |
|
| 808 | |
|
| 809 | |
private class TransactionStuff { |
| 810 | |
PreparedStatement insert, modify, get; |
| 811 | 343 | TransactionStuff(Connection connection) { |
| 812 | 343 | insert = _this.simpleInsert(connection); |
| 813 | 343 | modify = _this.simpleModify(connection); |
| 814 | 343 | get = _this.simpleGet(connection); |
| 815 | 343 | } |
| 816 | |
} |
| 817 | |
|
| 818 | 932 | private CachedIndexFactory transactionStuffs = new CachedIndexFactory() { |
| 819 | |
public Object reallyGet(int index) { |
| 820 | |
|
| 821 | 340 | return new TransactionStuff( |
| 822 | 340 | JdbcTable.this.database.poemTransaction(index).getConnection()); |
| 823 | |
} |
| 824 | |
}; |
| 825 | |
|
| 826 | 932 | private TransactionStuff committedTransactionStuff = null; |
| 827 | |
|
| 828 | |
|
| 829 | |
|
| 830 | |
|
| 831 | |
public void invalidateTransactionStuffs() { |
| 832 | 1 | transactionStuffs.invalidate(); |
| 833 | 1 | } |
| 834 | |
|
| 835 | |
|
| 836 | |
|
| 837 | |
|
| 838 | |
|
| 839 | |
private synchronized TransactionStuff getCommittedTransactionStuff() { |
| 840 | 3 | if (committedTransactionStuff == null) |
| 841 | 3 | committedTransactionStuff = |
| 842 | 3 | new TransactionStuff(database.getCommittedConnection()); |
| 843 | 3 | return committedTransactionStuff; |
| 844 | |
} |
| 845 | |
|
| 846 | |
|
| 847 | |
|
| 848 | |
|
| 849 | |
|
| 850 | |
|
| 851 | |
|
| 852 | |
private void load(PreparedStatement select, Persistent p) { |
| 853 | 2395 | JdbcPersistent persistent = (JdbcPersistent)p; |
| 854 | |
try { |
| 855 | 2395 | synchronized (select) { |
| 856 | 2395 | select.setInt(1, persistent.troid().intValue()); |
| 857 | 2395 | ResultSet rs = select.executeQuery(); |
| 858 | 2395 | if (database.logSQL()) |
| 859 | 460 | database.log(new SQLLogEvent(select.toString())); |
| 860 | 2395 | database.incrementQueryCount(select.toString()); |
| 861 | |
try { |
| 862 | 2395 | if (!rs.next()) |
| 863 | 1313 | persistent.setStatusNonexistent(); |
| 864 | |
else { |
| 865 | 1082 | persistent.setStatusExistent(); |
| 866 | 17220 | for (int c = 0; c < columns.length; ++c) |
| 867 | 16138 | columns[c].load_unsafe(rs, c + 1, persistent); |
| 868 | |
} |
| 869 | 2395 | persistent.setDirty(false); |
| 870 | 2395 | persistent.markValid(); |
| 871 | 2395 | if (rs.next()) |
| 872 | 0 | throw new DuplicateTroidPoemException(this, persistent.troid()); |
| 873 | |
} |
| 874 | |
finally { |
| 875 | 2395 | try { rs.close(); } catch (Exception e) { |
| 876 | 0 | database.log("Cannot close resultset after exception."); |
| 877 | 2395 | } |
| 878 | 0 | } |
| 879 | 2395 | } |
| 880 | |
} |
| 881 | 0 | catch (SQLException e) { |
| 882 | 0 | throw new SimpleRetrievalFailedPoemException(e, select.toString()); |
| 883 | |
} |
| 884 | 0 | catch (ValidationPoemException e) { |
| 885 | 0 | throw new UnexpectedValidationPoemException(e); |
| 886 | 2395 | } |
| 887 | 2395 | } |
| 888 | |
|
| 889 | |
|
| 890 | |
|
| 891 | |
|
| 892 | |
|
| 893 | |
@SuppressWarnings("unchecked") |
| 894 | |
public void load(PoemTransaction transaction, Persistent persistent) { |
| 895 | 4790 | load(transaction == null ? |
| 896 | 3 | getCommittedTransactionStuff().get : |
| 897 | 2392 | ((TransactionStuff)transactionStuffs.get(transaction.index)).get, |
| 898 | |
persistent); |
| 899 | 2395 | } |
| 900 | |
|
| 901 | |
private void modify(PoemTransaction transaction, Persistent persistent) { |
| 902 | |
@SuppressWarnings("unchecked") |
| 903 | 514 | PreparedStatement modify = |
| 904 | 514 | ((TransactionStuff)transactionStuffs.get(transaction.index)).modify; |
| 905 | 514 | synchronized (modify) { |
| 906 | 6244 | for (int c = 0; c < columns.length; ++c) |
| 907 | 5730 | columns[c].save_unsafe(persistent, modify, c + 1); |
| 908 | |
|
| 909 | |
try { |
| 910 | 514 | modify.setInt(columns.length + 1, persistent.troid().intValue()); |
| 911 | |
} |
| 912 | 0 | catch (SQLException e) { |
| 913 | 0 | throw new SQLSeriousPoemException(e); |
| 914 | 514 | } |
| 915 | |
|
| 916 | |
try { |
| 917 | 514 | modify.executeUpdate(); |
| 918 | |
} |
| 919 | 0 | catch (SQLException e) { |
| 920 | 0 | throw dbms().exceptionForUpdate(this, modify, false, e); |
| 921 | 514 | } |
| 922 | 514 | database.incrementQueryCount(modify.toString()); |
| 923 | |
|
| 924 | 514 | if (database.logSQL()) |
| 925 | 301 | database.log(new SQLLogEvent(modify.toString())); |
| 926 | 514 | } |
| 927 | 514 | persistent.postModify(); |
| 928 | 514 | } |
| 929 | |
|
| 930 | |
@SuppressWarnings("unchecked") |
| 931 | |
private void insert(PoemTransaction transaction, Persistent persistent) { |
| 932 | |
|
| 933 | 6009 | PreparedStatement insert = |
| 934 | 6008 | ((TransactionStuff)transactionStuffs.get(transaction.index)).insert; |
| 935 | 6008 | synchronized (insert) { |
| 936 | 136320 | for (int c = 0; c < columns.length; ++c) |
| 937 | 130312 | columns[c].save_unsafe(persistent, insert, c + 1); |
| 938 | |
try { |
| 939 | 6008 | insert.executeUpdate(); |
| 940 | |
} |
| 941 | 0 | catch (SQLException e) { |
| 942 | 0 | throw dbms().exceptionForUpdate(this, insert, true, e); |
| 943 | 6008 | } |
| 944 | 6008 | database.incrementQueryCount(insert.toString()); |
| 945 | 6008 | if (database.logSQL()) |
| 946 | 4124 | database.log(new SQLLogEvent(insert.toString())); |
| 947 | 6008 | } |
| 948 | 6008 | persistent.postInsert(); |
| 949 | 6008 | } |
| 950 | |
|
| 951 | |
|
| 952 | |
|
| 953 | |
|
| 954 | |
|
| 955 | |
|
| 956 | |
|
| 957 | |
public void delete(Integer troid, PoemTransaction transaction) { |
| 958 | 118 | String sql = |
| 959 | 118 | "DELETE FROM " + quotedName() + |
| 960 | 118 | " WHERE " + troidColumn.quotedName() + " = " + |
| 961 | 118 | troid.toString(); |
| 962 | |
try { |
| 963 | 118 | transaction.writeDown(); |
| 964 | 118 | Connection connection = transaction.getConnection(); |
| 965 | |
|
| 966 | 118 | Statement deleteStatement = connection.createStatement(); |
| 967 | 118 | int deleted = deleteStatement.executeUpdate(sql); |
| 968 | 118 | if (deleted != 1) { |
| 969 | 0 | throw new RowDisappearedPoemException(this,troid); |
| 970 | |
} |
| 971 | 118 | deleteStatement.close(); |
| 972 | 118 | database.incrementQueryCount(sql); |
| 973 | 118 | if (database.logSQL()) |
| 974 | 78 | database.log(new SQLLogEvent(sql)); |
| 975 | |
|
| 976 | 118 | cache.delete(troid); |
| 977 | |
} |
| 978 | 0 | catch (SQLException e) { |
| 979 | 0 | throw new ExecutingSQLPoemException(sql, e); |
| 980 | 118 | } |
| 981 | 118 | } |
| 982 | |
|
| 983 | |
|
| 984 | |
|
| 985 | |
|
| 986 | |
|
| 987 | |
public void writeDown(PoemTransaction transaction, Persistent p) { |
| 988 | 8353 | JdbcPersistent persistent = (JdbcPersistent)p; |
| 989 | |
|
| 990 | |
|
| 991 | |
|
| 992 | 8353 | if (persistent.isDirty()) { |
| 993 | 6523 | troidColumn.setRaw_unsafe(persistent, persistent.troid()); |
| 994 | |
|
| 995 | 6523 | if (persistent.statusExistent()) { |
| 996 | 514 | modify(transaction, persistent); |
| 997 | 6009 | } else if (persistent.statusNonexistent()) { |
| 998 | 6009 | insert(transaction, persistent); |
| 999 | 6008 | persistent.setStatusExistent(); |
| 1000 | |
} |
| 1001 | |
|
| 1002 | 6522 | persistent.setDirty(false); |
| 1003 | 6522 | persistent.postWrite(); |
| 1004 | |
} |
| 1005 | 8352 | } |
| 1006 | |
|
| 1007 | |
|
| 1008 | |
|
| 1009 | |
|
| 1010 | |
|
| 1011 | |
|
| 1012 | |
|
| 1013 | |
|
| 1014 | |
|
| 1015 | |
|
| 1016 | |
|
| 1017 | |
|
| 1018 | |
|
| 1019 | 932 | private Cache cache = new Cache(CACHE_LIMIT_DEFAULT); |
| 1020 | |
|
| 1021 | 1 | private static final Procedure invalidator = |
| 1022 | 1 | new Procedure() { |
| 1023 | |
public void apply(Object arg) { |
| 1024 | 14356 | ((Transactioned)arg).invalidate(); |
| 1025 | 14356 | } |
| 1026 | |
}; |
| 1027 | |
|
| 1028 | |
|
| 1029 | |
|
| 1030 | |
|
| 1031 | |
|
| 1032 | |
|
| 1033 | |
public void uncache() { |
| 1034 | 1670 | cache.iterate(invalidator); |
| 1035 | 1670 | serial.invalidate(); |
| 1036 | 1670 | TableListener[] listenersLocal = this.listeners; |
| 1037 | 3162 | for (int l = 0; l < listenersLocal.length; ++l) |
| 1038 | 1492 | listenersLocal[l].notifyUncached(this); |
| 1039 | 1670 | } |
| 1040 | |
|
| 1041 | |
|
| 1042 | |
|
| 1043 | |
|
| 1044 | |
public void trimCache(int maxSize) { |
| 1045 | 9 | cache.trim(maxSize); |
| 1046 | 9 | } |
| 1047 | |
|
| 1048 | |
|
| 1049 | |
|
| 1050 | |
|
| 1051 | |
|
| 1052 | |
|
| 1053 | |
public Cache.Info getCacheInfo() { |
| 1054 | 3 | return cache.getInfo(); |
| 1055 | |
} |
| 1056 | |
|
| 1057 | |
|
| 1058 | |
|
| 1059 | |
|
| 1060 | |
public void addListener(TableListener listener) { |
| 1061 | 708 | listeners = (TableListener[])ArrayUtils.added(listeners, listener); |
| 1062 | 708 | } |
| 1063 | |
|
| 1064 | |
|
| 1065 | |
|
| 1066 | |
|
| 1067 | |
|
| 1068 | |
|
| 1069 | |
|
| 1070 | |
|
| 1071 | |
|
| 1072 | |
public void notifyTouched(PoemTransaction transaction, Persistent persistent) { |
| 1073 | 7462 | serial.increment(transaction); |
| 1074 | |
|
| 1075 | 7462 | TableListener[] listenersLocal = this.listeners; |
| 1076 | 10062 | for (int l = 0; l < listenersLocal.length; ++l) |
| 1077 | 2600 | listenersLocal[l].notifyTouched(transaction, this, persistent); |
| 1078 | 7462 | } |
| 1079 | |
|
| 1080 | |
|
| 1081 | |
|
| 1082 | |
|
| 1083 | |
public long serial(PoemTransaction transaction) { |
| 1084 | 198 | return serial.current(transaction); |
| 1085 | |
} |
| 1086 | |
|
| 1087 | |
|
| 1088 | |
|
| 1089 | |
|
| 1090 | |
public void readLock() { |
| 1091 | 3 | serial(PoemThread.transaction()); |
| 1092 | 3 | } |
| 1093 | |
|
| 1094 | |
|
| 1095 | |
|
| 1096 | |
|
| 1097 | |
|
| 1098 | |
|
| 1099 | |
|
| 1100 | |
|
| 1101 | |
|
| 1102 | |
|
| 1103 | |
|
| 1104 | |
|
| 1105 | |
|
| 1106 | |
|
| 1107 | |
|
| 1108 | |
|
| 1109 | |
|
| 1110 | |
|
| 1111 | |
|
| 1112 | |
|
| 1113 | |
|
| 1114 | |
|
| 1115 | |
|
| 1116 | |
|
| 1117 | |
|
| 1118 | |
|
| 1119 | |
|
| 1120 | |
|
| 1121 | |
|
| 1122 | |
|
| 1123 | |
|
| 1124 | |
|
| 1125 | |
|
| 1126 | |
|
| 1127 | |
|
| 1128 | |
@SuppressWarnings("unchecked") |
| 1129 | |
public P getObject(Integer troid) throws NoSuchRowPoemException { |
| 1130 | 7921 | JdbcPersistent persistent = (JdbcPersistent)cache.get(troid); |
| 1131 | |
|
| 1132 | 7921 | if (persistent == null) { |
| 1133 | 1568 | persistent = (JdbcPersistent)newPersistent(); |
| 1134 | 1568 | claim(persistent, troid); |
| 1135 | 1568 | load(PoemThread.transaction(), persistent); |
| 1136 | 1568 | if (persistent.statusExistent()) |
| 1137 | 257 | synchronized (cache) { |
| 1138 | 257 | JdbcPersistent tryAgain = (JdbcPersistent)cache.get(troid); |
| 1139 | 257 | if (tryAgain == null) { |
| 1140 | |
try { |
| 1141 | 257 | cache.put(troid, persistent); |
| 1142 | 0 | } catch (Cache.InconsistencyException e) { |
| 1143 | 0 | throw new PoemBugPoemException( |
| 1144 | |
"Problem putting persistent " + persistent + " into cache:", e); |
| 1145 | 257 | } |
| 1146 | |
} else |
| 1147 | 0 | persistent = tryAgain; |
| 1148 | 257 | } |
| 1149 | |
} |
| 1150 | |
|
| 1151 | 7921 | if (!persistent.statusExistent()) |
| 1152 | 1311 | throw new NoSuchRowPoemException(this, troid); |
| 1153 | |
|
| 1154 | 6610 | persistent.existenceLock(PoemThread.sessionToken()); |
| 1155 | |
|
| 1156 | 6610 | return (P)persistent; |
| 1157 | |
} |
| 1158 | |
|
| 1159 | |
|
| 1160 | |
|
| 1161 | |
|
| 1162 | |
|
| 1163 | |
|
| 1164 | |
|
| 1165 | |
|
| 1166 | |
|
| 1167 | |
public Persistent getObject(int troid) throws NoSuchRowPoemException { |
| 1168 | 2370 | return getObject(new Integer(troid)); |
| 1169 | |
} |
| 1170 | |
|
| 1171 | |
|
| 1172 | |
|
| 1173 | |
|
| 1174 | |
|
| 1175 | |
|
| 1176 | |
|
| 1177 | |
|
| 1178 | |
|
| 1179 | |
|
| 1180 | |
|
| 1181 | |
|
| 1182 | |
|
| 1183 | |
|
| 1184 | |
|
| 1185 | |
|
| 1186 | |
|
| 1187 | |
|
| 1188 | |
|
| 1189 | |
|
| 1190 | |
|
| 1191 | |
|
| 1192 | |
|
| 1193 | |
|
| 1194 | |
public String selectionSQL(String fromClause, String whereClause, |
| 1195 | |
String orderByClause, boolean includeDeleted, |
| 1196 | |
boolean excludeUnselectable) { |
| 1197 | 691 | return selectOrCountSQL(troidColumn().fullQuotedName(), |
| 1198 | |
fromClause, whereClause, orderByClause, |
| 1199 | |
includeDeleted, excludeUnselectable); |
| 1200 | |
} |
| 1201 | |
|
| 1202 | |
|
| 1203 | |
|
| 1204 | |
|
| 1205 | |
|
| 1206 | |
|
| 1207 | |
|
| 1208 | |
|
| 1209 | |
|
| 1210 | |
|
| 1211 | |
|
| 1212 | |
|
| 1213 | |
|
| 1214 | |
|
| 1215 | |
|
| 1216 | |
|
| 1217 | |
private ResultSet selectionResultSet(String fromClause, String whereClause, |
| 1218 | |
String orderByClause, |
| 1219 | |
boolean includeDeleted, |
| 1220 | |
boolean excludeUnselectable, |
| 1221 | |
PoemTransaction transaction) |
| 1222 | |
throws SQLPoemException { |
| 1223 | |
|
| 1224 | 103 | String sql = selectionSQL(fromClause, whereClause, orderByClause, |
| 1225 | |
includeDeleted, excludeUnselectable); |
| 1226 | |
|
| 1227 | |
|
| 1228 | |
try { |
| 1229 | |
Connection connection; |
| 1230 | 103 | if (transaction == null) { |
| 1231 | 17 | connection = getDatabase().getCommittedConnection(); |
| 1232 | |
} else { |
| 1233 | 86 | transaction.writeDown(); |
| 1234 | 86 | connection = transaction.getConnection(); |
| 1235 | |
} |
| 1236 | |
|
| 1237 | 103 | Statement selectionStatement = connection.createStatement(); |
| 1238 | 103 | ResultSet rs = selectionStatement.executeQuery(sql); |
| 1239 | 103 | database.incrementQueryCount(sql); |
| 1240 | |
|
| 1241 | 103 | SessionToken token = PoemThread._sessionToken(); |
| 1242 | 103 | if (token != null) { |
| 1243 | 103 | token.toTidy().add(rs); |
| 1244 | 103 | token.toTidy().add(selectionStatement); |
| 1245 | |
} |
| 1246 | 103 | if (database.logSQL()) |
| 1247 | 50 | database.log(new SQLLogEvent(sql)); |
| 1248 | 103 | return rs; |
| 1249 | |
} |
| 1250 | 0 | catch (SQLException e) { |
| 1251 | 0 | throw new ExecutingSQLPoemException(sql, e); |
| 1252 | |
} |
| 1253 | |
} |
| 1254 | |
|
| 1255 | |
|
| 1256 | |
|
| 1257 | |
|
| 1258 | |
|
| 1259 | |
|
| 1260 | |
|
| 1261 | |
public Enumeration<Integer> troidSelection(String whereClause, String orderByClause, |
| 1262 | |
boolean includeDeleted, |
| 1263 | |
PoemTransaction transaction) { |
| 1264 | 86 | return troidsFrom(selectionResultSet(null, whereClause, orderByClause, |
| 1265 | |
includeDeleted, true, |
| 1266 | |
transaction)); |
| 1267 | |
} |
| 1268 | |
|
| 1269 | |
|
| 1270 | |
|
| 1271 | |
|
| 1272 | |
|
| 1273 | |
|
| 1274 | |
|
| 1275 | |
|
| 1276 | |
|
| 1277 | |
public Enumeration<Integer> troidSelection(Persistent criteria, String orderByClause, |
| 1278 | |
boolean includeDeleted, |
| 1279 | |
boolean excludeUnselectable, |
| 1280 | |
PoemTransaction transaction) { |
| 1281 | 34 | return troidsFrom(selectionResultSet(((JdbcPersistent)criteria).fromClause(), |
| 1282 | 17 | whereClause(criteria), |
| 1283 | |
orderByClause, |
| 1284 | |
includeDeleted, excludeUnselectable, |
| 1285 | |
transaction)); |
| 1286 | |
} |
| 1287 | |
|
| 1288 | |
|
| 1289 | |
|
| 1290 | |
|
| 1291 | |
|
| 1292 | |
private Enumeration<Integer> troidsFrom(ResultSet them) { |
| 1293 | 310 | return new ResultSetEnumeration<Integer>(them) { |
| 1294 | |
public Integer mapped(ResultSet rs) throws SQLException { |
| 1295 | 207 | return new Integer(rs.getInt(1)); |
| 1296 | |
} |
| 1297 | |
}; |
| 1298 | |
} |
| 1299 | |
|
| 1300 | |
|
| 1301 | |
|
| 1302 | |
|
| 1303 | |
public void rememberAllTroids(boolean flag) { |
| 1304 | 725 | if (flag) { |
| 1305 | 287 | if (allTroids == null && |
| 1306 | |
|
| 1307 | 287 | troidColumn() != null) |
| 1308 | 287 | allTroids = new CachedSelection<P>(this, null, null); |
| 1309 | |
} |
| 1310 | |
else |
| 1311 | 438 | allTroids = null; |
| 1312 | 725 | } |
| 1313 | |
|
| 1314 | |
|
| 1315 | |
|
| 1316 | |
|
| 1317 | |
public void setCacheLimit(Integer limit) { |
| 1318 | 725 | cache.setSize(limit == null ? CACHE_LIMIT_DEFAULT : limit.intValue()); |
| 1319 | 725 | } |
| 1320 | |
|
| 1321 | |
|
| 1322 | |
|
| 1323 | |
|
| 1324 | |
|
| 1325 | |
|
| 1326 | |
|
| 1327 | |
|
| 1328 | |
|
| 1329 | |
|
| 1330 | |
|
| 1331 | |
|
| 1332 | |
|
| 1333 | |
|
| 1334 | |
|
| 1335 | |
|
| 1336 | |
|
| 1337 | |
|
| 1338 | |
|
| 1339 | |
|
| 1340 | |
|
| 1341 | |
|
| 1342 | |
|
| 1343 | |
public Enumeration<Integer> troidSelection(String whereClause, String orderByClause, |
| 1344 | |
boolean includeDeleted) |
| 1345 | |
throws SQLPoemException { |
| 1346 | 83 | if (allTroids != null && |
| 1347 | 1 | (whereClause == null || whereClause.equals("")) && |
| 1348 | 0 | (orderByClause == null || orderByClause.equals("") || |
| 1349 | 0 | orderByClause == defaultOrderByClause()) && |
| 1350 | |
!includeDeleted) |
| 1351 | 25 | return allTroids.troids(); |
| 1352 | |
else |
| 1353 | 116 | return troidSelection(whereClause, orderByClause, includeDeleted, |
| 1354 | 58 | PoemThread.inSession() ? PoemThread.transaction() : null); |
| 1355 | |
} |
| 1356 | |
|
| 1357 | |
|
| 1358 | |
|
| 1359 | |
|
| 1360 | |
|
| 1361 | |
|
| 1362 | |
|
| 1363 | |
|
| 1364 | |
|
| 1365 | |
|
| 1366 | |
|
| 1367 | |
|
| 1368 | |
|
| 1369 | |
|
| 1370 | |
|
| 1371 | |
public Enumeration<P> selection() throws SQLPoemException { |
| 1372 | 65 | return selection((String)null, (String)null, false); |
| 1373 | |
} |
| 1374 | |
|
| 1375 | |
|
| 1376 | |
|
| 1377 | |
|
| 1378 | |
|
| 1379 | |
|
| 1380 | |
|
| 1381 | |
|
| 1382 | |
|
| 1383 | |
|
| 1384 | |
|
| 1385 | |
|
| 1386 | |
|
| 1387 | |
|
| 1388 | |
|
| 1389 | |
|
| 1390 | |
|
| 1391 | |
|
| 1392 | |
|
| 1393 | |
|
| 1394 | |
|
| 1395 | |
|
| 1396 | |
|
| 1397 | |
|
| 1398 | |
public final Enumeration<P> selection(String whereClause) |
| 1399 | |
throws SQLPoemException { |
| 1400 | 8 | return selection(whereClause, null, false); |
| 1401 | |
} |
| 1402 | |
|
| 1403 | |
|
| 1404 | |
|
| 1405 | |
|
| 1406 | |
|
| 1407 | |
|
| 1408 | |
|
| 1409 | |
|
| 1410 | |
|
| 1411 | |
|
| 1412 | |
|
| 1413 | |
|
| 1414 | |
|
| 1415 | |
|
| 1416 | |
|
| 1417 | |
|
| 1418 | |
public P firstSelection(String whereClause) { |
| 1419 | 8 | Enumeration<P> them = selection(whereClause); |
| 1420 | 8 | return them.hasMoreElements() ? them.nextElement() : null; |
| 1421 | |
} |
| 1422 | |
|
| 1423 | |
|
| 1424 | |
|
| 1425 | |
|
| 1426 | |
|
| 1427 | |
|
| 1428 | |
|
| 1429 | |
|
| 1430 | |
|
| 1431 | |
|
| 1432 | |
|
| 1433 | |
|
| 1434 | |
|
| 1435 | |
|
| 1436 | |
|
| 1437 | |
|
| 1438 | |
|
| 1439 | |
|
| 1440 | |
public Enumeration<P> selection(String whereClause, String orderByClause, |
| 1441 | |
boolean includeDeleted) |
| 1442 | |
throws SQLPoemException { |
| 1443 | 73 | return objectsFromTroids(troidSelection(whereClause, orderByClause, |
| 1444 | |
includeDeleted)); |
| 1445 | |
} |
| 1446 | |
|
| 1447 | |
|
| 1448 | |
|
| 1449 | |
|
| 1450 | |
|
| 1451 | |
|
| 1452 | |
|
| 1453 | |
|
| 1454 | |
public Enumeration<P> selection(Persistent criteria) |
| 1455 | |
throws SQLPoemException { |
| 1456 | 32 | return selection(criteria, |
| 1457 | 16 | criteria.getTable().defaultOrderByClause(), false, true); |
| 1458 | |
} |
| 1459 | |
|
| 1460 | |
|
| 1461 | |
|
| 1462 | |
|
| 1463 | |
|
| 1464 | |
|
| 1465 | |
|
| 1466 | |
|
| 1467 | |
|
| 1468 | |
public Enumeration<P> selection(Persistent criteria, String orderByClause) |
| 1469 | |
throws SQLPoemException { |
| 1470 | 1 | return selection(criteria, orderByClause, false, true); |
| 1471 | |
} |
| 1472 | |
|
| 1473 | |
|
| 1474 | |
|
| 1475 | |
|
| 1476 | |
|
| 1477 | |
|
| 1478 | |
|
| 1479 | |
|
| 1480 | |
|
| 1481 | |
|
| 1482 | |
public Enumeration<P> selection(Persistent criteria, String orderByClause, |
| 1483 | |
boolean includeDeleted, boolean excludeUnselectable) |
| 1484 | |
throws SQLPoemException { |
| 1485 | 17 | return objectsFromTroids(troidSelection(criteria, orderByClause, |
| 1486 | |
includeDeleted, excludeUnselectable, |
| 1487 | |
null)); |
| 1488 | |
} |
| 1489 | |
|
| 1490 | |
|
| 1491 | |
|
| 1492 | |
|
| 1493 | |
private Enumeration<P> objectsFromTroids(Enumeration<Integer> troids) { |
| 1494 | 154 | return new MappedEnumeration<P, Integer>(troids) { |
| 1495 | |
public P mapped(Integer troid) { |
| 1496 | 64 | return getObject(troid); |
| 1497 | |
} |
| 1498 | |
}; |
| 1499 | |
} |
| 1500 | |
|
| 1501 | |
|
| 1502 | |
|
| 1503 | |
|
| 1504 | |
|
| 1505 | |
|
| 1506 | |
public String countSQL(String whereClause) { |
| 1507 | 11897 | return countSQL(null, whereClause, false, true); |
| 1508 | |
} |
| 1509 | |
|
| 1510 | |
|
| 1511 | |
|
| 1512 | |
|
| 1513 | |
|
| 1514 | |
|
| 1515 | |
|
| 1516 | |
|
| 1517 | |
|
| 1518 | |
|
| 1519 | |
public String countSQL(String fromClause, String whereClause, |
| 1520 | |
boolean includeDeleted, boolean excludeUnselectable) { |
| 1521 | 11898 | return selectOrCountSQL("count(*)", fromClause, whereClause, "", |
| 1522 | |
includeDeleted, excludeUnselectable); |
| 1523 | |
} |
| 1524 | |
|
| 1525 | |
|
| 1526 | |
|
| 1527 | |
|
| 1528 | |
|
| 1529 | |
|
| 1530 | |
|
| 1531 | |
|
| 1532 | |
|
| 1533 | |
|
| 1534 | |
|
| 1535 | |
|
| 1536 | |
|
| 1537 | |
|
| 1538 | |
|
| 1539 | |
private String selectOrCountSQL(String selectClause, String fromClause, |
| 1540 | |
String whereClause, String orderByClause, |
| 1541 | |
boolean includeDeleted, |
| 1542 | |
boolean excludeUnselectable) { |
| 1543 | |
|
| 1544 | 12589 | if (fromClause == null) { |
| 1545 | 11985 | fromClause = quotedName(); |
| 1546 | |
} |
| 1547 | |
|
| 1548 | 12589 | String result = "SELECT " + selectClause + " FROM " + fromClause; |
| 1549 | |
|
| 1550 | 12589 | whereClause = appendWhereClauseFilters(whereClause, includeDeleted, |
| 1551 | |
excludeUnselectable); |
| 1552 | |
|
| 1553 | 12589 | if (whereClause.length() > 0) { |
| 1554 | 429 | result += " WHERE " + whereClause; |
| 1555 | |
} |
| 1556 | |
|
| 1557 | 12589 | if (orderByClause == null) { |
| 1558 | 671 | orderByClause = defaultOrderByClause(); |
| 1559 | |
} |
| 1560 | |
|
| 1561 | 12589 | if (orderByClause.trim().length() > 0) { |
| 1562 | 691 | result += " ORDER BY " + orderByClause; |
| 1563 | |
} |
| 1564 | 12589 | return result; |
| 1565 | |
} |
| 1566 | |
|
| 1567 | |
|
| 1568 | |
|
| 1569 | |
|
| 1570 | |
|
| 1571 | |
|
| 1572 | |
|
| 1573 | |
|
| 1574 | |
|
| 1575 | |
|
| 1576 | |
|
| 1577 | |
|
| 1578 | |
|
| 1579 | |
|
| 1580 | |
|
| 1581 | |
|
| 1582 | |
|
| 1583 | |
|
| 1584 | |
|
| 1585 | |
|
| 1586 | |
|
| 1587 | |
|
| 1588 | |
private String appendWhereClauseFilters(String whereClause, |
| 1589 | |
boolean includeDeleted, |
| 1590 | |
boolean excludeUnselectable) { |
| 1591 | 12698 | if (whereClause == null || whereClause.trim().length() == 0) { |
| 1592 | 12166 | whereClause = ""; |
| 1593 | |
} else { |
| 1594 | |
|
| 1595 | |
|
| 1596 | 532 | whereClause = "(" + whereClause + ")"; |
| 1597 | |
} |
| 1598 | |
|
| 1599 | 12698 | if (deletedColumn != null && !includeDeleted) { |
| 1600 | 1 | if(whereClause.length() > 0) { |
| 1601 | 1 | whereClause += " AND"; |
| 1602 | |
} |
| 1603 | 1 | whereClause += " NOT " + dbms().booleanTrueExpression(deletedColumn); |
| 1604 | |
} |
| 1605 | |
|
| 1606 | 12698 | if (excludeUnselectable){ |
| 1607 | 12692 | String s = canSelectClause(); |
| 1608 | 12692 | if (s != null) { |
| 1609 | 2 | if (whereClause.length() > 0) { |
| 1610 | 2 | whereClause += " AND "; |
| 1611 | |
} |
| 1612 | 2 | whereClause += s; |
| 1613 | |
} |
| 1614 | |
} |
| 1615 | 12698 | return whereClause; |
| 1616 | |
} |
| 1617 | |
|
| 1618 | |
|
| 1619 | |
|
| 1620 | |
|
| 1621 | |
|
| 1622 | |
|
| 1623 | |
|
| 1624 | |
|
| 1625 | |
|
| 1626 | |
|
| 1627 | |
|
| 1628 | |
|
| 1629 | |
private String canSelectClause() { |
| 1630 | 12692 | Column<Capability> canSelect = canSelectColumn(); |
| 1631 | 12692 | AccessToken accessToken = PoemThread.inSession() ? |
| 1632 | 12689 | PoemThread.sessionToken().accessToken : null; |
| 1633 | 12692 | if (canSelect == null || |
| 1634 | |
accessToken instanceof RootAccessToken) { |
| 1635 | 12690 | return null; |
| 1636 | 2 | } else if (accessToken instanceof User) { |
| 1637 | 2 | String query = "(" + |
| 1638 | 2 | canSelect.fullQuotedName() + " IS NULL OR EXISTS( SELECT 1 FROM " + |
| 1639 | 2 | quotedName() + |
| 1640 | |
", " + |
| 1641 | 2 | database.getGroupCapabilityTable().quotedName() + |
| 1642 | |
", " + |
| 1643 | 2 | database.getGroupMembershipTable().quotedName() + |
| 1644 | |
" WHERE " + |
| 1645 | 2 | database.getGroupMembershipTable().getUserColumn().fullQuotedName() + |
| 1646 | |
" = " + |
| 1647 | 2 | ((User)accessToken).getId() + |
| 1648 | |
" AND " + |
| 1649 | 2 | database.getGroupMembershipTable().getGroupColumn().fullQuotedName() + |
| 1650 | |
" = " + |
| 1651 | 2 | database.getGroupCapabilityTable().getGroupColumn().fullQuotedName() + |
| 1652 | |
" AND " + |
| 1653 | 2 | database.getGroupCapabilityTable().getCapabilityColumn(). |
| 1654 | 2 | fullQuotedName() + |
| 1655 | |
" = " + |
| 1656 | 2 | canSelect.fullQuotedName() + |
| 1657 | |
"))"; |
| 1658 | 2 | return query; |
| 1659 | |
} else { |
| 1660 | 0 | return canSelect.fullQuotedName() + " IS NULL"; |
| 1661 | |
} |
| 1662 | |
} |
| 1663 | |
|
| 1664 | |
|
| 1665 | |
|
| 1666 | |
|
| 1667 | |
|
| 1668 | |
|
| 1669 | |
|
| 1670 | |
public int count(String whereClause, |
| 1671 | |
boolean includeDeleted, boolean excludeUnselectable) |
| 1672 | |
throws SQLPoemException { |
| 1673 | 1 | return count(appendWhereClauseFilters(whereClause, |
| 1674 | |
includeDeleted, excludeUnselectable)); |
| 1675 | |
} |
| 1676 | |
|
| 1677 | |
|
| 1678 | |
|
| 1679 | |
|
| 1680 | |
|
| 1681 | |
|
| 1682 | |
|
| 1683 | |
public int count(String whereClause, boolean includeDeleted) |
| 1684 | |
throws SQLPoemException { |
| 1685 | 1 | return count(whereClause, includeDeleted, true); |
| 1686 | |
} |
| 1687 | |
|
| 1688 | |
|
| 1689 | |
|
| 1690 | |
|
| 1691 | |
|
| 1692 | |
|
| 1693 | |
|
| 1694 | |
public int count(String whereClause) |
| 1695 | |
throws SQLPoemException { |
| 1696 | |
|
| 1697 | 11886 | String sql = countSQL(whereClause); |
| 1698 | |
|
| 1699 | |
try { |
| 1700 | |
Connection connection; |
| 1701 | 11886 | if (PoemThread.inSession()) { |
| 1702 | 11884 | PoemTransaction transaction = PoemThread.transaction(); |
| 1703 | 11884 | transaction.writeDown(); |
| 1704 | 11884 | connection = transaction.getConnection(); |
| 1705 | 11884 | } else |
| 1706 | 2 | connection = getDatabase().getCommittedConnection(); |
| 1707 | |
|
| 1708 | |
|
| 1709 | 11886 | Statement s = connection.createStatement(); |
| 1710 | 11886 | ResultSet rs = s.executeQuery(sql); |
| 1711 | 11886 | database.incrementQueryCount(sql); |
| 1712 | 11886 | if (database.logSQL()) |
| 1713 | 1370 | database.log(new SQLLogEvent(sql)); |
| 1714 | 11886 | rs.next(); |
| 1715 | 11886 | int count = rs.getInt(1); |
| 1716 | 11886 | rs.close(); |
| 1717 | 11886 | s.close(); |
| 1718 | 11886 | return count; |
| 1719 | |
} |
| 1720 | 0 | catch (SQLException e) { |
| 1721 | 0 | throw new ExecutingSQLPoemException(sql, e); |
| 1722 | |
} |
| 1723 | |
} |
| 1724 | |
|
| 1725 | |
|
| 1726 | |
|
| 1727 | |
|
| 1728 | |
public int count() |
| 1729 | |
throws SQLPoemException { |
| 1730 | 11803 | return count(null); |
| 1731 | |
} |
| 1732 | |
|
| 1733 | |
|
| 1734 | |
|
| 1735 | |
|
| 1736 | |
|
| 1737 | |
|
| 1738 | |
|
| 1739 | |
|
| 1740 | |
|
| 1741 | |
public boolean exists(String whereClause) throws SQLPoemException { |
| 1742 | 82 | return count(whereClause) > 0; |
| 1743 | |
} |
| 1744 | |
|
| 1745 | |
|
| 1746 | |
|
| 1747 | |
|
| 1748 | |
|
| 1749 | |
public boolean exists(Persistent persistent) { |
| 1750 | 82 | return exists(whereClause(persistent)); |
| 1751 | |
} |
| 1752 | |
|
| 1753 | |
|
| 1754 | |
|
| 1755 | |
|
| 1756 | |
|
| 1757 | |
|
| 1758 | |
|
| 1759 | |
|
| 1760 | |
|
| 1761 | |
|
| 1762 | |
|
| 1763 | |
|
| 1764 | |
|
| 1765 | |
|
| 1766 | |
|
| 1767 | |
|
| 1768 | |
|
| 1769 | |
|
| 1770 | |
public void appendWhereClause(StringBuffer clause, Persistent persistent) { |
| 1771 | 107 | Column<?>[] columnsLocal = this.columns; |
| 1772 | 107 | boolean hadOne = false; |
| 1773 | 513 | for (int c = 0; c < columnsLocal.length; ++c) { |
| 1774 | 406 | Column<?> column = columnsLocal[c]; |
| 1775 | 406 | Object raw = column.getRaw_unsafe(persistent); |
| 1776 | 406 | if (raw != null) { |
| 1777 | 226 | if (hadOne) |
| 1778 | 123 | clause.append(" AND "); |
| 1779 | |
else |
| 1780 | 103 | hadOne = true; |
| 1781 | |
|
| 1782 | 226 | String columnSQL = column.fullQuotedName(); |
| 1783 | 226 | if (column.getType() instanceof StringPoemType) { |
| 1784 | 42 | clause.append( |
| 1785 | 42 | dbms().caseInsensitiveRegExpSQL( |
| 1786 | |
columnSQL, |
| 1787 | 21 | column.getSQLType().quotedRaw(raw))); |
| 1788 | 205 | } else if (column.getType() instanceof BooleanPoemType) { |
| 1789 | 10 | clause.append(columnSQL); |
| 1790 | 10 | clause.append(" = "); |
| 1791 | 10 | clause.append(dbms().sqlBooleanValueOfRaw(raw)); |
| 1792 | |
} else { |
| 1793 | 195 | clause.append(columnSQL); |
| 1794 | 195 | clause.append(" = "); |
| 1795 | 195 | clause.append(column.getSQLType().quotedRaw(raw)); |
| 1796 | |
} |
| 1797 | |
} |
| 1798 | |
} |
| 1799 | 107 | } |
| 1800 | |
|
| 1801 | |
|
| 1802 | |
|
| 1803 | |
|
| 1804 | |
|
| 1805 | |
|
| 1806 | |
|
| 1807 | |
|
| 1808 | |
|
| 1809 | |
|
| 1810 | |
|
| 1811 | |
public String whereClause(Persistent criteria) { |
| 1812 | 100 | return whereClause(criteria, true, true); |
| 1813 | |
} |
| 1814 | |
|
| 1815 | |
|
| 1816 | |
|
| 1817 | |
|
| 1818 | |
|
| 1819 | |
|
| 1820 | |
|
| 1821 | |
|
| 1822 | |
|
| 1823 | |
|
| 1824 | |
public String whereClause(Persistent criteria, |
| 1825 | |
boolean includeDeleted, boolean excludeUnselectable) { |
| 1826 | 104 | StringBuffer clause = new StringBuffer(); |
| 1827 | 104 | appendWhereClause(clause, criteria); |
| 1828 | 104 | return appendWhereClauseFilters(clause.toString(), |
| 1829 | |
includeDeleted, excludeUnselectable); |
| 1830 | |
} |
| 1831 | |
|
| 1832 | |
|
| 1833 | |
|
| 1834 | |
|
| 1835 | |
|
| 1836 | |
|
| 1837 | |
public String cnfWhereClause(Enumeration<P> persistents) { |
| 1838 | 3 | return cnfWhereClause(persistents, false, true); |
| 1839 | |
} |
| 1840 | |
|
| 1841 | |
|
| 1842 | |
|
| 1843 | |
|
| 1844 | |
|
| 1845 | |
|
| 1846 | |
|
| 1847 | |
public String cnfWhereClause(Enumeration<P> persistents, |
| 1848 | |
boolean includeDeleted, boolean excludeUnselectable) { |
| 1849 | 3 | StringBuffer clause = new StringBuffer(); |
| 1850 | |
|
| 1851 | 3 | boolean hadOne = false; |
| 1852 | 6 | while (persistents.hasMoreElements()) { |
| 1853 | 3 | StringBuffer pClause = new StringBuffer(); |
| 1854 | 3 | appendWhereClause(pClause, (Persistent)persistents.nextElement()); |
| 1855 | 3 | if (pClause.length() > 0) { |
| 1856 | 2 | if (hadOne) |
| 1857 | 1 | clause.append(" OR "); |
| 1858 | |
else |
| 1859 | 1 | hadOne = true; |
| 1860 | 2 | clause.append("("); |
| 1861 | 2 | clause.append(pClause); |
| 1862 | 2 | clause.append(")"); |
| 1863 | |
} |
| 1864 | 3 | } |
| 1865 | |
|
| 1866 | 3 | return appendWhereClauseFilters(clause.toString(), |
| 1867 | |
includeDeleted, excludeUnselectable); |
| 1868 | |
} |
| 1869 | |
|
| 1870 | |
|
| 1871 | |
|
| 1872 | |
|
| 1873 | |
|
| 1874 | |
|
| 1875 | |
|
| 1876 | |
|
| 1877 | |
|
| 1878 | |
|
| 1879 | |
|
| 1880 | |
|
| 1881 | |
@SuppressWarnings({ "unchecked", "rawtypes" }) |
| 1882 | |
public Enumeration<P> referencesTo(final Persistent object) { |
| 1883 | 18 | return new FlattenedEnumeration<P>( |
| 1884 | 156 | new MappedEnumeration(columns()) { |
| 1885 | |
public Enumeration mapped(Object column) { |
| 1886 | 138 | return ((Column)column).referencesTo(object); |
| 1887 | |
} |
| 1888 | |
}); |
| 1889 | |
} |
| 1890 | |
|
| 1891 | |
|
| 1892 | |
|
| 1893 | |
|
| 1894 | |
|
| 1895 | |
|
| 1896 | |
|
| 1897 | |
|
| 1898 | |
public Enumeration<Column<?>> referencesTo(final Table<?> table) { |
| 1899 | 2222 | return |
| 1900 | 16972 | new FilteredEnumeration<Column<?>>(columns()) { |
| 1901 | |
public boolean isIncluded(Column<?> column) { |
| 1902 | 14750 | PoemType<?> type = ((Column<?>)column).getType(); |
| 1903 | 14750 | return type instanceof PersistentReferencePoemType && |
| 1904 | 1671 | ((PersistentReferencePoemType)type).targetTable() == table; |
| 1905 | |
} |
| 1906 | |
}; |
| 1907 | |
} |
| 1908 | |
|
| 1909 | |
|
| 1910 | |
|
| 1911 | |
|
| 1912 | |
|
| 1913 | |
|
| 1914 | |
|
| 1915 | |
private void validate(Persistent persistent) |
| 1916 | |
throws FieldContentsPoemException { |
| 1917 | 136313 | for (int c = 0; c < columns.length; ++c) { |
| 1918 | 130306 | Column<?> column = columns[c]; |
| 1919 | |
try { |
| 1920 | 130306 | column.getType().assertValidRaw(column.getRaw_unsafe(persistent)); |
| 1921 | |
} |
| 1922 | 0 | catch (Exception e) { |
| 1923 | 0 | throw new FieldContentsPoemException(column, e); |
| 1924 | 130306 | } |
| 1925 | |
} |
| 1926 | 6007 | } |
| 1927 | |
|
| 1928 | |
|
| 1929 | |
|
| 1930 | |
|
| 1931 | |
public int getMostRecentTroid() { |
| 1932 | 0 | if (mostRecentTroid == -1) |
| 1933 | 0 | throw new PoemBugPoemException("Troid still unitialised in " + name); |
| 1934 | 0 | return mostRecentTroid; |
| 1935 | |
} |
| 1936 | |
|
| 1937 | |
|
| 1938 | |
|
| 1939 | |
|
| 1940 | |
|
| 1941 | |
public synchronized Integer troidFor(Persistent persistent) { |
| 1942 | 6015 | Persistent foolEclipse = persistent; |
| 1943 | 6015 | persistent = foolEclipse; |
| 1944 | 6015 | if (mostRecentTroid == -1) |
| 1945 | 0 | throw new PoemBugPoemException("Troid still unitialised in " + name); |
| 1946 | 6015 | return new Integer(mostRecentTroid++); |
| 1947 | |
} |
| 1948 | |
|
| 1949 | |
|
| 1950 | |
|
| 1951 | |
|
| 1952 | |
|
| 1953 | |
|
| 1954 | |
|
| 1955 | |
|
| 1956 | |
|
| 1957 | |
public void create(Persistent p) |
| 1958 | |
throws AccessPoemException, ValidationPoemException, |
| 1959 | |
InitialisationPoemException { |
| 1960 | 6015 | JdbcPersistent persistent = (JdbcPersistent)p; |
| 1961 | |
|
| 1962 | 6015 | SessionToken sessionToken = PoemThread.sessionToken(); |
| 1963 | |
|
| 1964 | 6015 | if (persistent.getTable() == null) |
| 1965 | 713 | persistent.setTable(this, null); |
| 1966 | 6015 | persistent.assertCanCreate(sessionToken.accessToken); |
| 1967 | |
|
| 1968 | 6015 | claim(persistent, troidFor(persistent)); |
| 1969 | 6007 | persistent.setStatusNonexistent(); |
| 1970 | |
|
| 1971 | |
|
| 1972 | |
|
| 1973 | |
|
| 1974 | |
try { |
| 1975 | 6007 | validate(persistent); |
| 1976 | |
} |
| 1977 | 0 | catch (Exception e) { |
| 1978 | 0 | throw new InitialisationPoemException(this, e); |
| 1979 | 6007 | } |
| 1980 | |
|
| 1981 | |
|
| 1982 | |
|
| 1983 | |
|
| 1984 | 6007 | synchronized (cache) { |
| 1985 | 6007 | persistent.setDirty(true); |
| 1986 | 6007 | writeDown(sessionToken.transaction, persistent); |
| 1987 | |
|
| 1988 | |
|
| 1989 | |
|
| 1990 | 6006 | persistent.readLock(sessionToken.transaction); |
| 1991 | 6006 | cache.put(persistent.troid(), persistent); |
| 1992 | 6006 | } |
| 1993 | |
|
| 1994 | 6006 | notifyTouched(sessionToken.transaction, persistent); |
| 1995 | 6006 | } |
| 1996 | |
|
| 1997 | |
|
| 1998 | |
|
| 1999 | |
|
| 2000 | |
|
| 2001 | |
|
| 2002 | |
|
| 2003 | |
|
| 2004 | |
|
| 2005 | |
|
| 2006 | |
|
| 2007 | |
|
| 2008 | |
|
| 2009 | |
|
| 2010 | |
|
| 2011 | |
|
| 2012 | |
|
| 2013 | |
|
| 2014 | |
|
| 2015 | |
|
| 2016 | |
|
| 2017 | |
|
| 2018 | |
|
| 2019 | |
|
| 2020 | |
|
| 2021 | |
|
| 2022 | |
|
| 2023 | |
|
| 2024 | |
|
| 2025 | |
|
| 2026 | |
|
| 2027 | |
public Persistent create(Initialiser initialiser) |
| 2028 | |
throws AccessPoemException, ValidationPoemException, |
| 2029 | |
InitialisationPoemException { |
| 2030 | 4723 | Persistent persistent = newPersistent(); |
| 2031 | 4723 | initialiser.init(persistent); |
| 2032 | 4723 | create(persistent); |
| 2033 | 4723 | return persistent; |
| 2034 | |
} |
| 2035 | |
|
| 2036 | |
private void claim(Persistent p, Integer troid) { |
| 2037 | 7583 | JdbcPersistent persistent = (JdbcPersistent)p; |
| 2038 | |
|
| 2039 | |
|
| 2040 | 7583 | if (cache.get(troid) != null) |
| 2041 | 0 | throw new DuplicateTroidPoemException(this, troid); |
| 2042 | |
|
| 2043 | 7583 | if (persistent.troid() != null) |
| 2044 | 8 | throw new DoubleCreatePoemException(persistent); |
| 2045 | |
|
| 2046 | 7575 | persistent.setTable(this, troid); |
| 2047 | |
|
| 2048 | 7575 | troidColumn.setRaw_unsafe(persistent, troid); |
| 2049 | 7575 | if (deletedColumn != null) |
| 2050 | 0 | deletedColumn.setRaw_unsafe(persistent, Boolean.FALSE); |
| 2051 | 7575 | } |
| 2052 | |
|
| 2053 | |
|
| 2054 | |
|
| 2055 | |
|
| 2056 | |
|
| 2057 | |
public Persistent newPersistent() { |
| 2058 | 7608 | JdbcPersistent it = _newPersistent(); |
| 2059 | 7608 | it.setTable(this, null); |
| 2060 | 7608 | return it; |
| 2061 | |
} |
| 2062 | |
|
| 2063 | |
|
| 2064 | |
|
| 2065 | |
|
| 2066 | |
|
| 2067 | |
|
| 2068 | |
|
| 2069 | |
protected JdbcPersistent _newPersistent() { |
| 2070 | 12 | return new JdbcPersistent(); |
| 2071 | |
} |
| 2072 | |
|
| 2073 | |
|
| 2074 | |
|
| 2075 | |
|
| 2076 | |
|
| 2077 | |
|
| 2078 | |
|
| 2079 | |
public void delete_unsafe(String whereClause) { |
| 2080 | 1 | serial.increment(PoemThread.transaction()); |
| 2081 | 1 | getDatabase().sqlUpdate("DELETE FROM " + quotedName + |
| 2082 | |
" WHERE " + whereClause); |
| 2083 | 1 | uncache(); |
| 2084 | 1 | } |
| 2085 | |
|
| 2086 | |
|
| 2087 | |
|
| 2088 | |
|
| 2089 | |
public int extrasCount() { |
| 2090 | 420 | return extrasIndex; |
| 2091 | |
} |
| 2092 | |
|
| 2093 | |
|
| 2094 | |
|
| 2095 | |
|
| 2096 | |
|
| 2097 | |
|
| 2098 | |
|
| 2099 | |
|
| 2100 | |
|
| 2101 | |
|
| 2102 | |
|
| 2103 | |
|
| 2104 | |
|
| 2105 | |
|
| 2106 | |
public final Capability getDefaultCanRead() { |
| 2107 | 350 | return info == null ? null : info.getDefaultcanread(); |
| 2108 | |
} |
| 2109 | |
|
| 2110 | |
|
| 2111 | |
|
| 2112 | |
|
| 2113 | |
|
| 2114 | |
|
| 2115 | |
|
| 2116 | |
|
| 2117 | |
|
| 2118 | |
public final Capability getDefaultCanWrite() { |
| 2119 | 921 | return info == null ? null : info.getDefaultcanwrite(); |
| 2120 | |
} |
| 2121 | |
|
| 2122 | |
|
| 2123 | |
|
| 2124 | |
|
| 2125 | |
|
| 2126 | |
|
| 2127 | |
|
| 2128 | |
|
| 2129 | |
public final Capability getDefaultCanDelete() { |
| 2130 | 122 | return info == null ? null : info.getDefaultcandelete(); |
| 2131 | |
} |
| 2132 | |
|
| 2133 | |
|
| 2134 | |
|
| 2135 | |
|
| 2136 | |
|
| 2137 | |
|
| 2138 | |
|
| 2139 | |
|
| 2140 | |
public final Capability getCanCreate() { |
| 2141 | 6021 | return info == null ? null : info.getCancreate(); |
| 2142 | |
} |
| 2143 | |
|
| 2144 | |
|
| 2145 | |
|
| 2146 | |
|
| 2147 | |
public final Column<Capability> canReadColumn() { |
| 2148 | 24 | return canReadColumn == null ? canSelectColumn() : canReadColumn; |
| 2149 | |
} |
| 2150 | |
|
| 2151 | |
|
| 2152 | |
|
| 2153 | |
|
| 2154 | |
|
| 2155 | |
public final Column<Capability> canSelectColumn() { |
| 2156 | 12713 | return canSelectColumn; |
| 2157 | |
} |
| 2158 | |
|
| 2159 | |
|
| 2160 | |
|
| 2161 | |
|
| 2162 | |
public final Column<Capability> canWriteColumn() { |
| 2163 | 0 | return canWriteColumn; |
| 2164 | |
} |
| 2165 | |
|
| 2166 | |
|
| 2167 | |
|
| 2168 | |
|
| 2169 | |
public final Column<Capability> canDeleteColumn() { |
| 2170 | 0 | return canDeleteColumn; |
| 2171 | |
} |
| 2172 | |
|
| 2173 | |
|
| 2174 | |
|
| 2175 | |
|
| 2176 | |
|
| 2177 | |
|
| 2178 | |
|
| 2179 | |
|
| 2180 | |
|
| 2181 | |
|
| 2182 | |
|
| 2183 | |
|
| 2184 | |
|
| 2185 | |
public Column<?> addColumnAndCommit(ColumnInfo infoP) throws PoemException { |
| 2186 | |
|
| 2187 | |
|
| 2188 | |
|
| 2189 | 174 | database.log("Adding extra column from runtime " + |
| 2190 | 87 | dbms().melatiName(infoP.getName_unsafe()) + |
| 2191 | |
" to " + name); |
| 2192 | 87 | Column<?> column = ExtraColumn.from(this, infoP, getNextExtrasIndex(), |
| 2193 | |
DefinitionSource.runtime); |
| 2194 | 87 | column.setColumnInfo(infoP); |
| 2195 | |
|
| 2196 | |
|
| 2197 | |
|
| 2198 | |
|
| 2199 | 87 | defineColumn(column, false); |
| 2200 | |
|
| 2201 | |
|
| 2202 | |
|
| 2203 | 84 | database.beginStructuralModification(); |
| 2204 | |
try { |
| 2205 | 84 | dbAddColumn(column); |
| 2206 | 82 | synchronized (cache) { |
| 2207 | 82 | uncache(); |
| 2208 | 82 | transactionStuffs.invalidate(); |
| 2209 | 82 | defineColumn(column, true); |
| 2210 | 82 | } |
| 2211 | 82 | PoemThread.commit(); |
| 2212 | |
} |
| 2213 | |
finally { |
| 2214 | 84 | database.endStructuralModification(); |
| 2215 | 82 | } |
| 2216 | |
|
| 2217 | 82 | return column; |
| 2218 | |
} |
| 2219 | |
|
| 2220 | |
|
| 2221 | |
|
| 2222 | |
|
| 2223 | |
public void deleteColumnAndCommit(ColumnInfo columnInfo) throws PoemException { |
| 2224 | 1 | database.beginStructuralModification(); |
| 2225 | |
try { |
| 2226 | 1 | Column<?> column = columnInfo.column(); |
| 2227 | 1 | columnInfo.delete(); |
| 2228 | 1 | if (database.getDbms().canDropColumns()) |
| 2229 | 2 | dbModifyStructure( |
| 2230 | 1 | "ALTER TABLE " + quotedName() + |
| 2231 | 1 | " DROP " + column.quotedName()); |
| 2232 | |
|
| 2233 | |
|
| 2234 | 1 | columns = (Column[])ArrayUtils.removed(columns, column); |
| 2235 | 1 | columnsByName.remove(column.getName().toLowerCase()); |
| 2236 | |
|
| 2237 | 1 | synchronized (cache) { |
| 2238 | 1 | uncache(); |
| 2239 | 1 | transactionStuffs.invalidate(); |
| 2240 | 1 | } |
| 2241 | 1 | PoemThread.commit(); |
| 2242 | |
} |
| 2243 | |
finally { |
| 2244 | 1 | database.endStructuralModification(); |
| 2245 | 1 | } |
| 2246 | |
|
| 2247 | 1 | } |
| 2248 | |
|
| 2249 | |
|
| 2250 | |
|
| 2251 | |
|
| 2252 | |
|
| 2253 | |
|
| 2254 | |
|
| 2255 | |
|
| 2256 | |
|
| 2257 | |
|
| 2258 | |
|
| 2259 | |
|
| 2260 | |
|
| 2261 | |
public String toString() { |
| 2262 | 36 | return getName() + " (from " + definitionSource + ")"; |
| 2263 | |
} |
| 2264 | |
|
| 2265 | |
|
| 2266 | |
|
| 2267 | |
|
| 2268 | |
|
| 2269 | |
public void dumpCacheAnalysis() { |
| 2270 | 9 | database.log("\n-------- Analysis of " + name + "'s cache\n"); |
| 2271 | 9 | cache.dumpAnalysis(); |
| 2272 | 9 | } |
| 2273 | |
|
| 2274 | |
|
| 2275 | |
|
| 2276 | |
|
| 2277 | |
public void dump() { |
| 2278 | 9 | dump(System.out); |
| 2279 | 9 | } |
| 2280 | |
|
| 2281 | |
|
| 2282 | |
|
| 2283 | |
|
| 2284 | |
|
| 2285 | |
|
| 2286 | |
public void dump(PrintStream ps) { |
| 2287 | 20 | ps.println("=== table " + name + |
| 2288 | 10 | " (tableinfo id " + tableInfoID() + ")"); |
| 2289 | 83 | for (int c = 0; c < columns.length; ++c) |
| 2290 | 73 | columns[c].dump(ps); |
| 2291 | 10 | } |
| 2292 | |
|
| 2293 | |
|
| 2294 | |
|
| 2295 | |
|
| 2296 | |
|
| 2297 | |
|
| 2298 | |
|
| 2299 | |
|
| 2300 | |
|
| 2301 | |
|
| 2302 | |
|
| 2303 | |
public CachedSelection<P> cachedSelection(String whereClause, |
| 2304 | |
String orderByClause) { |
| 2305 | 4 | String key = whereClause + "/" + orderByClause; |
| 2306 | 4 | CachedSelection<P> them = cachedSelections.get(key); |
| 2307 | 4 | if (them == null) { |
| 2308 | 2 | CachedSelection<P> newThem = |
| 2309 | |
new CachedSelection<P>(this, whereClause, orderByClause); |
| 2310 | 2 | cachedSelections.put(key, newThem); |
| 2311 | 2 | them = newThem; |
| 2312 | |
} |
| 2313 | 4 | return them; |
| 2314 | |
} |
| 2315 | |
|
| 2316 | |
|
| 2317 | |
|
| 2318 | |
|
| 2319 | |
|
| 2320 | |
|
| 2321 | |
|
| 2322 | |
|
| 2323 | |
|
| 2324 | |
|
| 2325 | |
|
| 2326 | |
public CachedCount cachedCount(String whereClause, boolean includeDeleted) { |
| 2327 | 0 | return cachedCount(whereClause, includeDeleted, true); |
| 2328 | |
} |
| 2329 | |
|
| 2330 | |
|
| 2331 | |
|
| 2332 | |
|
| 2333 | |
|
| 2334 | |
|
| 2335 | |
|
| 2336 | |
|
| 2337 | |
|
| 2338 | |
|
| 2339 | |
|
| 2340 | |
|
| 2341 | |
public CachedCount cachedCount(String whereClause, boolean includeDeleted, |
| 2342 | |
boolean excludeUnselectable) { |
| 2343 | 1 | return cachedCount(appendWhereClauseFilters(whereClause, |
| 2344 | |
includeDeleted, excludeUnselectable)); |
| 2345 | |
} |
| 2346 | |
|
| 2347 | |
|
| 2348 | |
|
| 2349 | |
|
| 2350 | |
|
| 2351 | |
|
| 2352 | |
|
| 2353 | |
|
| 2354 | |
|
| 2355 | |
public CachedCount cachedCount(Persistent criteria, boolean includeDeleted, |
| 2356 | |
boolean excludeUnselectable) { |
| 2357 | 2 | return cachedCount(whereClause(criteria, includeDeleted, excludeUnselectable)); |
| 2358 | |
} |
| 2359 | |
|
| 2360 | |
|
| 2361 | |
|
| 2362 | |
|
| 2363 | |
|
| 2364 | |
public CachedCount cachedCount(Persistent criteria) { |
| 2365 | 2 | return cachedCount(whereClause(criteria, true, false)); |
| 2366 | |
} |
| 2367 | |
|
| 2368 | |
|
| 2369 | |
|
| 2370 | |
|
| 2371 | |
|
| 2372 | |
|
| 2373 | |
|
| 2374 | |
|
| 2375 | |
|
| 2376 | |
|
| 2377 | |
public CachedCount cachedCount(String whereClause) { |
| 2378 | 8 | String key = "" + whereClause; |
| 2379 | 8 | CachedCount it = cachedCounts.get(key); |
| 2380 | 8 | if (it == null) { |
| 2381 | 4 | it = new CachedCount(this, whereClause); |
| 2382 | 4 | cachedCounts.put(key, it); |
| 2383 | |
} |
| 2384 | 8 | return it; |
| 2385 | |
} |
| 2386 | |
|
| 2387 | |
|
| 2388 | |
|
| 2389 | |
|
| 2390 | |
|
| 2391 | |
public CachedCount cachedCount() { |
| 2392 | 0 | return cachedCount((String)null); |
| 2393 | |
} |
| 2394 | |
|
| 2395 | |
|
| 2396 | |
|
| 2397 | |
|
| 2398 | |
|
| 2399 | |
|
| 2400 | |
|
| 2401 | |
|
| 2402 | |
|
| 2403 | |
|
| 2404 | |
|
| 2405 | |
|
| 2406 | |
|
| 2407 | |
public CachedExists cachedExists(String whereClause) { |
| 2408 | 1 | String key = "" + whereClause; |
| 2409 | 1 | CachedExists it = null; |
| 2410 | 1 | it = cachedExists.get(key); |
| 2411 | 1 | if (it == null) { |
| 2412 | 1 | it = new CachedExists(this, whereClause); |
| 2413 | 1 | cachedExists.put(key, it); |
| 2414 | |
} |
| 2415 | 1 | return it; |
| 2416 | |
} |
| 2417 | |
|
| 2418 | |
|
| 2419 | |
|
| 2420 | |
|
| 2421 | |
|
| 2422 | |
|
| 2423 | |
|
| 2424 | |
|
| 2425 | |
|
| 2426 | |
|
| 2427 | |
|
| 2428 | |
|
| 2429 | |
@SuppressWarnings({ "unchecked", "rawtypes" }) |
| 2430 | |
public RestrictedReferencePoemType<?> cachedSelectionType(String whereClause, |
| 2431 | |
String orderByClause, boolean nullable) { |
| 2432 | 4 | return new RestrictedReferencePoemType( |
| 2433 | 4 | cachedSelection(whereClause, orderByClause), nullable); |
| 2434 | |
} |
| 2435 | |
|
| 2436 | |
|
| 2437 | |
|
| 2438 | |
|
| 2439 | |
|
| 2440 | |
|
| 2441 | |
|
| 2442 | |
|
| 2443 | |
|
| 2444 | |
|
| 2445 | |
|
| 2446 | |
|
| 2447 | |
|
| 2448 | |
|
| 2449 | |
|
| 2450 | |
|
| 2451 | |
|
| 2452 | |
|
| 2453 | |
|
| 2454 | |
|
| 2455 | |
|
| 2456 | |
|
| 2457 | |
|
| 2458 | |
|
| 2459 | |
|
| 2460 | |
|
| 2461 | |
|
| 2462 | |
|
| 2463 | |
|
| 2464 | |
|
| 2465 | |
|
| 2466 | |
|
| 2467 | |
|
| 2468 | |
|
| 2469 | |
|
| 2470 | |
|
| 2471 | |
|
| 2472 | |
|
| 2473 | |
|
| 2474 | |
|
| 2475 | |
|
| 2476 | |
|
| 2477 | |
|
| 2478 | |
|
| 2479 | |
|
| 2480 | |
|
| 2481 | |
|
| 2482 | |
|
| 2483 | |
|
| 2484 | |
|
| 2485 | |
|
| 2486 | |
|
| 2487 | |
@SuppressWarnings({ "rawtypes", "unchecked" }) |
| 2488 | |
public Field<?> cachedSelectionField( |
| 2489 | |
String whereClause, String orderByClause, boolean nullable, |
| 2490 | |
Integer selectedTroid, String nameP) { |
| 2491 | 4 | return new Field( |
| 2492 | |
selectedTroid, |
| 2493 | |
new BaseFieldAttributes(nameP, |
| 2494 | 4 | cachedSelectionType(whereClause, |
| 2495 | |
orderByClause, nullable))); |
| 2496 | |
} |
| 2497 | |
|
| 2498 | |
|
| 2499 | |
|
| 2500 | |
|
| 2501 | |
|
| 2502 | |
|
| 2503 | |
|
| 2504 | |
@SuppressWarnings({ "rawtypes", "unchecked" }) |
| 2505 | |
private synchronized void defineColumn(Column<?> column, boolean reallyDoIt) |
| 2506 | |
throws DuplicateColumnNamePoemException, |
| 2507 | |
DuplicateTroidColumnPoemException, |
| 2508 | |
DuplicateDeletedColumnPoemException { |
| 2509 | 4968 | if (column.getTable() != this) |
| 2510 | 0 | throw new ColumnInUsePoemException(this, column); |
| 2511 | |
|
| 2512 | 4968 | if (_getColumn(column.getName()) != null) |
| 2513 | 1 | throw new DuplicateColumnNamePoemException(this, column); |
| 2514 | |
|
| 2515 | 4967 | if (column.isTroidColumn()) { |
| 2516 | 726 | if (troidColumn != null) |
| 2517 | 1 | throw new DuplicateTroidColumnPoemException(this, column); |
| 2518 | 725 | if (reallyDoIt) |
| 2519 | 725 | troidColumn = (Column<Integer>) column; |
| 2520 | |
} |
| 2521 | 4241 | else if (column.isDeletedColumn()) { |
| 2522 | 48 | if (deletedColumn != null) |
| 2523 | 1 | throw new DuplicateDeletedColumnPoemException(this, column); |
| 2524 | 47 | if (reallyDoIt) |
| 2525 | 46 | deletedColumn = (Column<Boolean>)column; |
| 2526 | |
} |
| 2527 | |
else { |
| 2528 | 4193 | if (reallyDoIt) { |
| 2529 | 4110 | PoemType type = column.getType(); |
| 2530 | 4110 | if (type instanceof ReferencePoemType && |
| 2531 | 540 | ((PersistentReferencePoemType)type).targetTable() == |
| 2532 | 540 | database.getCapabilityTable()) { |
| 2533 | 289 | if (column.getName().equals("canRead")) |
| 2534 | 21 | canReadColumn = (Column<Capability>) column; |
| 2535 | 268 | else if (column.getName().equals("canWrite")) |
| 2536 | 21 | canWriteColumn = (Column<Capability>) column; |
| 2537 | 247 | else if (column.getName().equals("canDelete")) |
| 2538 | 21 | canDeleteColumn = (Column<Capability>) column; |
| 2539 | 226 | else if (column.getName().equals("canSelect")) |
| 2540 | 21 | canSelectColumn = (Column<Capability>) column; |
| 2541 | |
} |
| 2542 | |
} |
| 2543 | |
} |
| 2544 | |
|
| 2545 | 4965 | if (reallyDoIt) { |
| 2546 | 4881 | column.setTable(this); |
| 2547 | 4881 | columns = (Column[])ArrayUtils.added(columns, column); |
| 2548 | 4881 | columnsByName.put(column.getName().toLowerCase(), column); |
| 2549 | |
} |
| 2550 | 4965 | } |
| 2551 | |
|
| 2552 | |
|
| 2553 | |
|
| 2554 | |
|
| 2555 | |
|
| 2556 | |
|
| 2557 | |
|
| 2558 | |
|
| 2559 | |
public final void defineColumn(Column<?> column) |
| 2560 | |
throws DuplicateColumnNamePoemException, |
| 2561 | |
DuplicateTroidColumnPoemException, |
| 2562 | |
DuplicateDeletedColumnPoemException { |
| 2563 | 4799 | defineColumn(column, true); |
| 2564 | 4799 | } |
| 2565 | |
|
| 2566 | |
private void _defineColumn(Column<?> column) { |
| 2567 | |
try { |
| 2568 | 21 | defineColumn(column); |
| 2569 | |
} |
| 2570 | 0 | catch (DuplicateColumnNamePoemException e) { |
| 2571 | 0 | throw new UnexpectedExceptionPoemException(e); |
| 2572 | |
} |
| 2573 | 0 | catch (DuplicateTroidColumnPoemException e) { |
| 2574 | 0 | throw new UnexpectedExceptionPoemException(e); |
| 2575 | 21 | } |
| 2576 | 21 | } |
| 2577 | |
|
| 2578 | |
|
| 2579 | |
|
| 2580 | |
|
| 2581 | |
public int getNextExtrasIndex() { |
| 2582 | 125 | return extrasIndex++; |
| 2583 | |
} |
| 2584 | |
|
| 2585 | |
|
| 2586 | |
|
| 2587 | |
|
| 2588 | |
public void setTableInfo(TableInfo tableInfo) { |
| 2589 | 725 | info = tableInfo; |
| 2590 | 725 | rememberAllTroids(tableInfo.getSeqcached().booleanValue()); |
| 2591 | 725 | setCacheLimit(tableInfo.getCachelimit()); |
| 2592 | 725 | } |
| 2593 | |
|
| 2594 | |
|
| 2595 | |
|
| 2596 | |
|
| 2597 | |
public TableInfo getTableInfo() { |
| 2598 | 17701 | return info; |
| 2599 | |
} |
| 2600 | |
|
| 2601 | |
|
| 2602 | |
|
| 2603 | |
|
| 2604 | |
|
| 2605 | |
|
| 2606 | |
|
| 2607 | |
|
| 2608 | |
public String defaultDisplayName() { |
| 2609 | 225 | return StringUtils.capitalised(getName()); |
| 2610 | |
} |
| 2611 | |
|
| 2612 | |
|
| 2613 | |
public int defaultDisplayOrder() { |
| 2614 | 2 | return DISPLAY_ORDER_DEFAULT; |
| 2615 | |
} |
| 2616 | |
|
| 2617 | |
|
| 2618 | |
|
| 2619 | |
|
| 2620 | |
|
| 2621 | |
|
| 2622 | |
|
| 2623 | |
|
| 2624 | |
public String defaultDescription() { |
| 2625 | 2 | return null; |
| 2626 | |
} |
| 2627 | |
|
| 2628 | |
public Integer defaultCacheLimit() { |
| 2629 | 265 | return new Integer(CACHE_LIMIT_DEFAULT); |
| 2630 | |
} |
| 2631 | |
|
| 2632 | |
public boolean defaultRememberAllTroids() { |
| 2633 | 418 | return false; |
| 2634 | |
} |
| 2635 | |
|
| 2636 | |
public String defaultCategory() { |
| 2637 | 2 | return TableCategoryTable.normalTableCategoryName; |
| 2638 | |
} |
| 2639 | |
|
| 2640 | |
|
| 2641 | |
|
| 2642 | |
|
| 2643 | |
|
| 2644 | |
|
| 2645 | |
public void createTableInfo() throws PoemException { |
| 2646 | 708 | if (info == null) { |
| 2647 | 698 | info = getDatabase().getTableInfoTable().defaultTableInfoFor(this); |
| 2648 | |
try { |
| 2649 | 698 | getDatabase().getTableInfoTable().create(info); |
| 2650 | 0 | } catch (PoemException e) { |
| 2651 | 0 | throw new UnificationPoemException( |
| 2652 | 0 | "Problem creating new tableInfo for table " + getName() + ":", e); |
| 2653 | 698 | } |
| 2654 | 698 | setTableInfo(info); |
| 2655 | |
} |
| 2656 | 708 | } |
| 2657 | |
|
| 2658 | |
|
| 2659 | |
|
| 2660 | |
|
| 2661 | |
|
| 2662 | |
public synchronized void unifyWithColumnInfo() throws PoemException { |
| 2663 | |
|
| 2664 | 723 | if (info == null) |
| 2665 | 0 | throw new PoemBugPoemException("Get the initialisation order right ..."); |
| 2666 | |
|
| 2667 | 723 | for (Enumeration<?> ci = |
| 2668 | 723 | database.getColumnInfoTable().getTableinfoColumn(). |
| 2669 | 723 | selectionWhereEq(info.troid()); |
| 2670 | 799 | ci.hasMoreElements();) { |
| 2671 | 76 | ColumnInfo columnInfo = (ColumnInfo)ci.nextElement(); |
| 2672 | 76 | Column<?> column = _getColumn(columnInfo.getName()); |
| 2673 | 76 | if (column == null) { |
| 2674 | 14 | database.log("Adding extra column " |
| 2675 | 7 | + dbms().melatiName(columnInfo.getName_unsafe()) |
| 2676 | |
+ " to " + name + " from definition in columninfo table."); |
| 2677 | 7 | column = ExtraColumn.from(this, columnInfo, getNextExtrasIndex(), |
| 2678 | |
DefinitionSource.infoTables); |
| 2679 | 7 | _defineColumn(column); |
| 2680 | |
} |
| 2681 | 76 | column.setColumnInfo(columnInfo); |
| 2682 | 76 | } |
| 2683 | |
|
| 2684 | 723 | for (Enumeration<Column<?>> c = columns(); c.hasMoreElements();) |
| 2685 | 4785 | c.nextElement().createColumnInfo(); |
| 2686 | 723 | } |
| 2687 | |
|
| 2688 | |
@Override |
| 2689 | |
public void unifyWithMetadata(ResultSet tableDescriptions) throws SQLException { |
| 2690 | 0 | if (info == null) |
| 2691 | 0 | return; |
| 2692 | 0 | String remarks = tableDescriptions.getString("REMARKS"); |
| 2693 | 0 | if (getDescription() == null) { |
| 2694 | 0 | if (remarks != null && !remarks.trim().equals("")) { |
| 2695 | 0 | info.setDescription(remarks); |
| 2696 | 0 | getDatabase().log("Adding comment to table " + name + |
| 2697 | |
" from SQL metadata:" + remarks); |
| 2698 | |
} |
| 2699 | |
} else { |
| 2700 | 0 | if (!this.getDescription().equals(remarks)) { |
| 2701 | 0 | String sql = this.dbms().alterTableAddCommentSQL(this, null); |
| 2702 | 0 | if (sql != null) |
| 2703 | 0 | this.getDatabase().modifyStructure(sql); |
| 2704 | |
} |
| 2705 | |
} |
| 2706 | 0 | } |
| 2707 | |
|
| 2708 | |
|
| 2709 | |
|
| 2710 | |
|
| 2711 | |
|
| 2712 | |
|
| 2713 | |
|
| 2714 | |
|
| 2715 | |
@Override |
| 2716 | |
@SuppressWarnings({ "unchecked", "rawtypes" }) |
| 2717 | |
public synchronized void unifyWithDB(ResultSet colDescs, String troidColumnName) |
| 2718 | |
throws PoemException { |
| 2719 | 848 | boolean debug = false; |
| 2720 | |
|
| 2721 | 848 | Hashtable<Column<?>, Boolean> dbColumns = new Hashtable<Column<?>, Boolean>(); |
| 2722 | |
|
| 2723 | 848 | int colCount = 0; |
| 2724 | 848 | if (colDescs != null){ |
| 2725 | |
|
| 2726 | |
try { |
| 2727 | 8853 | for (; colDescs.next(); ++colCount) { |
| 2728 | 4089 | String colName = colDescs.getString("COLUMN_NAME"); |
| 2729 | 4089 | Column<?> column = _getColumn(dbms().melatiName(colName)); |
| 2730 | |
|
| 2731 | 4089 | if (column == null) { |
| 2732 | 14 | SQLPoemType<?> colType = |
| 2733 | 14 | database.defaultPoemTypeOfColumnMetaData(colDescs); |
| 2734 | |
|
| 2735 | |
|
| 2736 | 14 | if (troidColumn == null && colName.equalsIgnoreCase(troidColumnName) && |
| 2737 | 2 | dbms().canRepresent(colType, TroidPoemType.it) != null) |
| 2738 | 2 | colType = TroidPoemType.it; |
| 2739 | |
|
| 2740 | |
|
| 2741 | |
|
| 2742 | 14 | if (deletedColumn == null && colName.equalsIgnoreCase(dbms().unreservedName("deleted")) && |
| 2743 | 2 | dbms().canRepresent(colType, DeletedPoemType.it) != null) |
| 2744 | 2 | colType = DeletedPoemType.it; |
| 2745 | |
|
| 2746 | 28 | database.log("Adding extra column from sql meta data " |
| 2747 | 14 | + name + "." + dbms().melatiName(colName)); |
| 2748 | 14 | column = new ExtraColumn(this, |
| 2749 | 14 | dbms().melatiName( |
| 2750 | |
colName), |
| 2751 | |
colType, DefinitionSource.sqlMetaData, |
| 2752 | 14 | getNextExtrasIndex()); |
| 2753 | |
|
| 2754 | 14 | _defineColumn(column); |
| 2755 | |
|
| 2756 | |
|
| 2757 | |
|
| 2758 | |
|
| 2759 | |
|
| 2760 | |
|
| 2761 | 14 | if (info != null) |
| 2762 | 14 | column.createColumnInfo(); |
| 2763 | 14 | } |
| 2764 | |
else { |
| 2765 | 4075 | column.assertMatches(colDescs); |
| 2766 | |
} |
| 2767 | 4089 | column.unifyWithMetadata(colDescs); |
| 2768 | 4089 | dbColumns.put(column, Boolean.TRUE); |
| 2769 | |
} |
| 2770 | 0 | } catch (SQLException e) { |
| 2771 | 0 | throw new SQLSeriousPoemException(e); |
| 2772 | 675 | } |
| 2773 | 173 | } else if (debug) database.log( |
| 2774 | |
"Table.unifyWithDB called with null ResultsSet"); |
| 2775 | |
|
| 2776 | 848 | if (colCount == 0) { |
| 2777 | |
|
| 2778 | 293 | dbCreateTable(); |
| 2779 | |
} else { |
| 2780 | |
|
| 2781 | 4644 | for (int c = 0; c < columns.length; ++c) { |
| 2782 | 4089 | if (dbColumns.get(columns[c]) == null) { |
| 2783 | 0 | database.log("Adding column to underlying database : " + columns[c]); |
| 2784 | 0 | dbAddColumn(columns[c]); |
| 2785 | |
} |
| 2786 | |
} |
| 2787 | |
} |
| 2788 | |
|
| 2789 | 848 | if (troidColumn == null) |
| 2790 | 0 | throw new NoTroidColumnException(this); |
| 2791 | |
|
| 2792 | |
|
| 2793 | |
|
| 2794 | |
|
| 2795 | |
|
| 2796 | |
|
| 2797 | 848 | if (info != null) { |
| 2798 | |
|
| 2799 | |
|
| 2800 | 725 | Hashtable<Column<?>,Boolean> dbHasIndexForColumn = new Hashtable<Column<?>,Boolean>(); |
| 2801 | 1450 | String unreservedName = dbms().getJdbcMetadataName( |
| 2802 | 725 | dbms().unreservedName(getName())); |
| 2803 | 725 | if (debug) database.log("Getting indexes for " + unreservedName + "(was " + getName() + ")"); |
| 2804 | |
ResultSet index; |
| 2805 | |
try { |
| 2806 | 725 | index = getDatabase().getCommittedConnection().getMetaData(). |
| 2807 | |
|
| 2808 | |
|
| 2809 | |
|
| 2810 | 725 | getIndexInfo(null, dbms().getSchema(), |
| 2811 | |
unreservedName, |
| 2812 | |
false, true); |
| 2813 | 1328 | while (index.next()) { |
| 2814 | |
try { |
| 2815 | 603 | String mdIndexName = index.getString("INDEX_NAME"); |
| 2816 | 603 | String mdColName = index.getString("COLUMN_NAME"); |
| 2817 | 603 | if (mdColName != null) { |
| 2818 | 603 | String columnName = dbms().melatiName(mdColName); |
| 2819 | 603 | Column<?> column = getColumn(columnName); |
| 2820 | |
|
| 2821 | |
|
| 2822 | 603 | String expectedIndex = indexName(column).toUpperCase(); |
| 2823 | |
|
| 2824 | 603 | if (expectedIndex.indexOf(mdIndexName.toUpperCase()) == 0) { |
| 2825 | 0 | column.unifyWithIndex(mdIndexName, index); |
| 2826 | 0 | dbHasIndexForColumn.put(column, Boolean.TRUE); |
| 2827 | 0 | if(debug)database.log("Found Expected Index:" + |
| 2828 | 0 | expectedIndex + " IndexName:" + mdIndexName.toUpperCase()); |
| 2829 | |
} else { |
| 2830 | |
try { |
| 2831 | 603 | column.unifyWithIndex(mdIndexName, index); |
| 2832 | 603 | dbHasIndexForColumn.put(column, Boolean.TRUE); |
| 2833 | 603 | if(debug) database.log("Not creating index because one exists with different name:" + |
| 2834 | 0 | mdIndexName.toUpperCase() + " != " + expectedIndex); |
| 2835 | 0 | } catch (IndexUniquenessPoemException e) { |
| 2836 | |
|
| 2837 | 0 | if(debug) database.log("Creating index because existing one has different properties:" + |
| 2838 | 0 | mdIndexName.toUpperCase() + " != " + expectedIndex); |
| 2839 | 603 | } |
| 2840 | |
} |
| 2841 | |
} |
| 2842 | |
|
| 2843 | |
|
| 2844 | |
} |
| 2845 | 0 | catch (NoSuchColumnPoemException e) { |
| 2846 | |
|
| 2847 | 0 | throw new UnexpectedExceptionPoemException(e); |
| 2848 | 603 | } |
| 2849 | |
} |
| 2850 | 0 | } catch (SQLException e) { |
| 2851 | 0 | throw new SQLSeriousPoemException(e); |
| 2852 | 725 | } |
| 2853 | |
|
| 2854 | |
|
| 2855 | 5524 | for (int c = 0; c < columns.length; ++c) { |
| 2856 | 4799 | if (dbHasIndexForColumn.get(columns[c]) != Boolean.TRUE) |
| 2857 | 4198 | dbCreateIndex(columns[c]); |
| 2858 | |
} |
| 2859 | |
} |
| 2860 | |
|
| 2861 | |
|
| 2862 | |
|
| 2863 | 848 | if (PoemThread.inSession()) |
| 2864 | 725 | PoemThread.writeDown(); |
| 2865 | |
|
| 2866 | 848 | String sql = |
| 2867 | 848 | "SELECT " + troidColumn.fullQuotedName() + |
| 2868 | 848 | " FROM " + quotedName() + |
| 2869 | 848 | " ORDER BY " + troidColumn.fullQuotedName() + " DESC"; |
| 2870 | |
try { |
| 2871 | 848 | Statement selectionStatement = getDatabase().getCommittedConnection().createStatement(); |
| 2872 | 848 | ResultSet maxTroid = |
| 2873 | |
selectionStatement. |
| 2874 | 848 | executeQuery(sql); |
| 2875 | 848 | database.incrementQueryCount(sql); |
| 2876 | 848 | if (database.logSQL()) |
| 2877 | 592 | database.log(new SQLLogEvent(sql)); |
| 2878 | 848 | if (maxTroid.next()) |
| 2879 | 234 | mostRecentTroid = maxTroid.getInt(1) + 1; |
| 2880 | |
else |
| 2881 | 614 | mostRecentTroid = 0; |
| 2882 | 848 | maxTroid.close(); |
| 2883 | 848 | selectionStatement.close(); |
| 2884 | |
} |
| 2885 | 0 | catch (SQLException e) { |
| 2886 | 0 | throw new SQLSeriousPoemException(e); |
| 2887 | 848 | } |
| 2888 | 848 | } |
| 2889 | |
|
| 2890 | |
|
| 2891 | |
|
| 2892 | |
|
| 2893 | |
|
| 2894 | |
|
| 2895 | |
public final int hashCode() { |
| 2896 | 4 | return name.hashCode(); |
| 2897 | |
} |
| 2898 | |
|
| 2899 | |
|
| 2900 | |
|
| 2901 | |
|
| 2902 | |
|
| 2903 | |
|
| 2904 | |
|
| 2905 | |
public boolean equals(Object t) { |
| 2906 | 1020 | return (t instanceof JdbcTable && |
| 2907 | 1018 | ((Table<?>)t).getName().equals(name)); |
| 2908 | |
|
| 2909 | |
} |
| 2910 | |
|
| 2911 | |
|
| 2912 | |
} |