| 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 | |
|
| 46 | |
package org.melati.poem; |
| 47 | |
|
| 48 | |
import java.sql.Connection; |
| 49 | |
import java.sql.DatabaseMetaData; |
| 50 | |
import java.sql.ResultSet; |
| 51 | |
import java.sql.SQLException; |
| 52 | |
import java.sql.Statement; |
| 53 | |
import java.util.Enumeration; |
| 54 | |
import java.util.Hashtable; |
| 55 | |
import java.util.List; |
| 56 | |
import java.util.Vector; |
| 57 | |
|
| 58 | |
import org.melati.poem.dbms.Dbms; |
| 59 | |
import org.melati.poem.dbms.DbmsFactory; |
| 60 | |
import org.melati.poem.transaction.Transaction; |
| 61 | |
import org.melati.poem.transaction.TransactionPool; |
| 62 | |
import org.melati.poem.util.ArrayEnumeration; |
| 63 | |
import org.melati.poem.util.ArrayUtils; |
| 64 | |
import org.melati.poem.util.EnumUtils; |
| 65 | |
import org.melati.poem.util.FlattenedEnumeration; |
| 66 | |
import org.melati.poem.util.MappedEnumeration; |
| 67 | |
import org.melati.poem.util.StringUtils; |
| 68 | |
|
| 69 | |
import java.util.concurrent.locks.ReadWriteLock; |
| 70 | |
import java.util.concurrent.locks.ReentrantReadWriteLock; |
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | 70 | public abstract class Database implements TransactionPool { |
| 86 | |
|
| 87 | 43 | final Database _this = this; |
| 88 | |
|
| 89 | 43 | private Vector<Transaction> transactions = null; |
| 90 | 43 | private Vector<Transaction> freeTransactions = null; |
| 91 | |
|
| 92 | |
private Connection committedConnection; |
| 93 | 43 | private final ReadWriteLock lock = new ReentrantReadWriteLock(); |
| 94 | 43 | private long structureSerial = 0L; |
| 95 | |
|
| 96 | 43 | private Vector<Table<?>> tables = new Vector<Table<?>>(); |
| 97 | 43 | private Hashtable<String, Table<?>> tablesByName = new Hashtable<String, Table<?>>(); |
| 98 | 43 | private Table<?>[] displayTables = null; |
| 99 | |
|
| 100 | |
private String name; |
| 101 | |
private String displayName; |
| 102 | |
private Dbms dbms; |
| 103 | 43 | private boolean logSQL = false; |
| 104 | 43 | private boolean logCommits = false; |
| 105 | |
private int transactionsMax; |
| 106 | |
|
| 107 | |
private String connectionUrl; |
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | 43 | private int queryCount = 0; |
| 113 | |
|
| 114 | 43 | private String lastQuery = null; |
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | 43 | public Database() { |
| 129 | 43 | } |
| 130 | |
|
| 131 | 43 | private boolean initialised = false; |
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
private synchronized void init() { |
| 137 | 41 | if (!initialised) { |
| 138 | 41 | for (Table<?> t : this.tables) |
| 139 | 705 | t.init(); |
| 140 | 41 | initialised = true; |
| 141 | |
} |
| 142 | 41 | } |
| 143 | |
|
| 144 | 43 | private final boolean[] connecting = new boolean[1]; |
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
public class ConnectingException extends PoemException { |
| 151 | |
private static final long serialVersionUID = 1L; |
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
public String getMessage() { |
| 156 | |
return "Connection to the database is currently in progress; " + |
| 157 | |
"please try again in a moment"; |
| 158 | |
} |
| 159 | |
} |
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
|
| 184 | |
|
| 185 | |
|
| 186 | |
|
| 187 | |
|
| 188 | |
|
| 189 | |
|
| 190 | |
|
| 191 | |
|
| 192 | |
|
| 193 | |
|
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 198 | |
|
| 199 | |
|
| 200 | |
|
| 201 | |
|
| 202 | |
|
| 203 | |
|
| 204 | |
|
| 205 | |
|
| 206 | |
|
| 207 | |
|
| 208 | |
|
| 209 | |
|
| 210 | |
|
| 211 | |
|
| 212 | |
|
| 213 | |
|
| 214 | |
|
| 215 | |
|
| 216 | |
|
| 217 | |
|
| 218 | |
|
| 219 | |
@SuppressWarnings("unchecked") |
| 220 | |
public void connect(String nameIn, String dbmsclass, String url, |
| 221 | |
String username, String password, |
| 222 | |
int transactionsMaxP) throws PoemException { |
| 223 | |
|
| 224 | 42 | this.name = nameIn; |
| 225 | 42 | this.connectionUrl = url; |
| 226 | |
|
| 227 | 42 | synchronized (connecting) { |
| 228 | 42 | if (connecting[0]) |
| 229 | 0 | throw new ConnectingException(); |
| 230 | 42 | connecting[0] = true; |
| 231 | 42 | } |
| 232 | |
|
| 233 | 42 | if (committedConnection != null) |
| 234 | 1 | throw new ReconnectionPoemException(this); |
| 235 | |
|
| 236 | 41 | setDbms(DbmsFactory.getDbms(dbmsclass)); |
| 237 | |
|
| 238 | 41 | setTransactionsMax(transactionsMaxP); |
| 239 | 41 | committedConnection = getDbms().getConnection(url, username, password); |
| 240 | 41 | transactions = new Vector<Transaction>(); |
| 241 | 397 | for (int s = 0; s < transactionsMax(); ++s) |
| 242 | 712 | transactions.add( |
| 243 | |
new PoemTransaction( |
| 244 | |
this, |
| 245 | 356 | getDbms().getConnection(url, username, password), |
| 246 | |
s)); |
| 247 | |
|
| 248 | 41 | freeTransactions = (Vector<Transaction>)transactions.clone(); |
| 249 | |
|
| 250 | |
try { |
| 251 | |
|
| 252 | 41 | init(); |
| 253 | |
|
| 254 | |
|
| 255 | 41 | DatabaseMetaData m = committedConnection.getMetaData(); |
| 256 | 82 | getTableInfoTable().unifyWithDB( |
| 257 | 82 | m.getColumns(null, dbms.getSchema(), |
| 258 | 82 | dbms.unreservedName(getTableInfoTable().getName()), null), dbms.unreservedName("id")); |
| 259 | 82 | getColumnInfoTable().unifyWithDB( |
| 260 | 82 | m.getColumns(null, dbms.getSchema(), |
| 261 | 82 | dbms.unreservedName(getColumnInfoTable().getName()), null), dbms.unreservedName("id")); |
| 262 | 82 | getTableCategoryTable().unifyWithDB( |
| 263 | 82 | m.getColumns(null, dbms.getSchema(), |
| 264 | 82 | dbms.unreservedName(getTableCategoryTable().getName()), null), dbms.unreservedName("id")); |
| 265 | |
|
| 266 | 41 | inSession(AccessToken.root, |
| 267 | 41 | new PoemTask() { |
| 268 | |
public void run() throws PoemException { |
| 269 | |
try { |
| 270 | 41 | _this.unifyWithDB(); |
| 271 | |
} |
| 272 | 0 | catch (SQLException e) { |
| 273 | 0 | throw new SQLPoemException(e); |
| 274 | 41 | } |
| 275 | 41 | } |
| 276 | |
|
| 277 | |
public String toString() { |
| 278 | 0 | return "Unifying with DB"; |
| 279 | |
} |
| 280 | |
}); |
| 281 | 0 | } catch (Exception e) { |
| 282 | 0 | if (committedConnection != null) disconnect(); |
| 283 | 0 | throw new UnificationPoemException(e); |
| 284 | |
} finally { |
| 285 | 41 | synchronized (connecting) { |
| 286 | 41 | connecting[0] = false; |
| 287 | 41 | } |
| 288 | 41 | } |
| 289 | 41 | } |
| 290 | |
|
| 291 | |
|
| 292 | |
|
| 293 | |
|
| 294 | |
public void disconnect() throws PoemException { |
| 295 | 18 | if (committedConnection == null) |
| 296 | 0 | throw new ReconnectionPoemException(this); |
| 297 | |
|
| 298 | |
try { |
| 299 | 18 | for (Transaction poemTransaction : freeTransactions){ |
| 300 | 144 | ((PoemTransaction)poemTransaction).getConnection().close(); |
| 301 | 144 | } |
| 302 | 18 | freeTransactions.removeAllElements(); |
| 303 | |
|
| 304 | 18 | getDbms().shutdown(committedConnection); |
| 305 | 18 | committedConnection.close(); |
| 306 | 0 | } catch (SQLException e) { |
| 307 | 0 | throw new SQLPoemException(e); |
| 308 | 18 | } |
| 309 | 18 | committedConnection = null; |
| 310 | 18 | } |
| 311 | |
|
| 312 | |
|
| 313 | |
|
| 314 | |
|
| 315 | |
|
| 316 | |
|
| 317 | |
|
| 318 | |
|
| 319 | |
|
| 320 | |
protected synchronized void defineTable(Table<?> table) |
| 321 | |
throws DuplicateTableNamePoemException { |
| 322 | 20 | if (getTableIgnoringCase(table.getName()) != null) |
| 323 | 1 | throw new DuplicateTableNamePoemException(this, table.getName()); |
| 324 | 19 | redefineTable(table); |
| 325 | 19 | } |
| 326 | |
|
| 327 | |
protected synchronized void redefineTable(Table<?> table) { |
| 328 | 931 | if (table.getDatabase() != this) |
| 329 | 0 | throw new TableInUsePoemException(this, table); |
| 330 | |
|
| 331 | 931 | if (getTableIgnoringCase(table.getName()) == null) { |
| 332 | 742 | tablesByName.put(table.getName().toLowerCase(), table); |
| 333 | 742 | tables.addElement(table); |
| 334 | |
} |
| 335 | |
else |
| 336 | 378 | tables.setElementAt(table, |
| 337 | 189 | tables.indexOf( |
| 338 | 189 | tablesByName.put(table.getName().toLowerCase(), table))); |
| 339 | 931 | displayTables = null; |
| 340 | 931 | } |
| 341 | |
|
| 342 | |
private ResultSet columnsMetadata(DatabaseMetaData m, String tableName) |
| 343 | |
throws SQLException { |
| 344 | 1260 | return m.getColumns(null, dbms.getSchema(), dbms.unreservedName(tableName), null); |
| 345 | |
} |
| 346 | |
|
| 347 | |
|
| 348 | |
|
| 349 | |
|
| 350 | |
|
| 351 | |
|
| 352 | |
|
| 353 | |
|
| 354 | |
@SuppressWarnings({ "unchecked", "rawtypes" }) |
| 355 | |
public Table<?> addTableAndCommit(TableInfo info, String troidName) |
| 356 | |
throws PoemException { |
| 357 | |
|
| 358 | |
|
| 359 | |
|
| 360 | |
|
| 361 | 6 | Table<?> table = new JdbcTable<Persistent>(this, info.getName(), |
| 362 | |
DefinitionSource.infoTables); |
| 363 | 10 | table.defineColumn(new ExtraColumn(table, troidName, |
| 364 | |
TroidPoemType.it, |
| 365 | |
DefinitionSource.infoTables, |
| 366 | 5 | table.getNextExtrasIndex())); |
| 367 | 5 | table.setTableInfo(info); |
| 368 | 5 | table.unifyWithColumnInfo(); |
| 369 | 5 | table.unifyWithDB(null,troidName); |
| 370 | |
|
| 371 | 5 | PoemThread.commit(); |
| 372 | 5 | defineTable(table); |
| 373 | |
|
| 374 | 4 | return table; |
| 375 | |
} |
| 376 | |
|
| 377 | |
|
| 378 | |
|
| 379 | |
|
| 380 | |
public void deleteTableAndCommit(TableInfo info) { |
| 381 | |
try { |
| 382 | 1 | Table<?> table = info.actualTable(); |
| 383 | 1 | Enumeration<Column<?>> columns = table.columns(); |
| 384 | 2 | while (columns.hasMoreElements()){ |
| 385 | 1 | Column<?> c = columns.nextElement(); |
| 386 | 1 | table.deleteColumnAndCommit(c.getColumnInfo()); |
| 387 | 1 | } |
| 388 | |
|
| 389 | 1 | info.delete(); |
| 390 | 1 | beginStructuralModification(); |
| 391 | 1 | table.dbModifyStructure(" DROP TABLE " + table.quotedName()); |
| 392 | 1 | synchronized (tables) { |
| 393 | 1 | tables.remove(table); |
| 394 | 1 | tablesByName.remove(table.getName().toLowerCase()); |
| 395 | 1 | if (displayTables != null) |
| 396 | 0 | displayTables = (Table[])ArrayUtils.removed(displayTables, table); |
| 397 | 1 | uncache(); |
| 398 | 1 | table.invalidateTransactionStuffs(); |
| 399 | 1 | } |
| 400 | 1 | PoemThread.commit(); |
| 401 | |
} |
| 402 | |
finally { |
| 403 | 1 | endStructuralModification(); |
| 404 | 1 | } |
| 405 | 1 | } |
| 406 | |
|
| 407 | |
private String getTroidColumnName(DatabaseMetaData m, String tableName) throws SQLException { |
| 408 | 198 | String troidColumnName = null; |
| 409 | 198 | ResultSet tables = m.getTables(null, dbms.getSchema(), dbms.unreservedName(tableName), null); |
| 410 | 198 | if (tables.next()) { |
| 411 | 42 | ResultSet r = m.getPrimaryKeys(null, dbms.getSchema(), dbms.unreservedName(tableName)); |
| 412 | 44 | while (r.next()) |
| 413 | 2 | troidColumnName = r.getString("COLUMN_NAME"); |
| 414 | 42 | r.close(); |
| 415 | |
|
| 416 | 42 | if (troidColumnName != null) { |
| 417 | 2 | log(dbms.getJdbcMetadataName(dbms.unreservedName(troidColumnName))); |
| 418 | 2 | ResultSet idCol = m.getColumns(null, dbms.getSchema(), dbms.unreservedName(tableName), dbms.getJdbcMetadataName(dbms.unreservedName(troidColumnName))); |
| 419 | 2 | log("Discovered a primary key troid candidate column for jdbc table :" + tableName + ":" + troidColumnName); |
| 420 | 2 | if (idCol.next()) { |
| 421 | 2 | if (dbms.canRepresent(defaultPoemTypeOfColumnMetaData(idCol), TroidPoemType.it) == null) |
| 422 | 0 | if (troidColumnName.equals("id")) |
| 423 | |
|
| 424 | 0 | throw new UnificationPoemException("Primary Key " + troidColumnName + " cannot represent a Troid"); |
| 425 | |
else { |
| 426 | 0 | log("Column " + troidColumnName + " cannot represent troid as it has type " + defaultPoemTypeOfColumnMetaData(idCol)); |
| 427 | 0 | ResultSet u = m.getIndexInfo |
| 428 | 0 | (null, dbms.getSchema(), dbms.unreservedName(tableName), true, false); |
| 429 | 0 | String unusableKey = troidColumnName; |
| 430 | 0 | troidColumnName = null; |
| 431 | 0 | String uniqueKey = null; |
| 432 | 0 | String foundKey = null; |
| 433 | 0 | while (u.next()) { |
| 434 | 0 | uniqueKey = u.getString("COLUMN_NAME"); |
| 435 | 0 | if (!uniqueKey.equals(unusableKey)) { |
| 436 | 0 | ResultSet idColNotPrimeKey = m.getColumns(null, |
| 437 | 0 | dbms.getSchema(), |
| 438 | 0 | dbms.unreservedName(tableName), |
| 439 | 0 | dbms.getJdbcMetadataName(dbms.unreservedName(uniqueKey))); |
| 440 | 0 | if (idColNotPrimeKey.next()) { |
| 441 | 0 | if (idColNotPrimeKey.getInt("NULLABLE") != DatabaseMetaData.columnNoNulls) { |
| 442 | 0 | idColNotPrimeKey.close(); |
| 443 | 0 | break; |
| 444 | |
} |
| 445 | 0 | SQLPoemType<?> t = defaultPoemTypeOfColumnMetaData(idColNotPrimeKey); |
| 446 | 0 | if (dbms.canRepresent(t, TroidPoemType.it) == null) { |
| 447 | 0 | log("Unique Column " + uniqueKey + " cannot represent troid as it has type " + t); |
| 448 | 0 | uniqueKey = null; |
| 449 | |
} |
| 450 | 0 | if (uniqueKey != null) { |
| 451 | 0 | if (foundKey != null) { |
| 452 | 0 | idColNotPrimeKey.close(); |
| 453 | 0 | throw new UnificationPoemException( |
| 454 | |
"Second unique, non-nullable numeric index found :" + uniqueKey |
| 455 | |
+ " already found " + foundKey); |
| 456 | |
} |
| 457 | 0 | log("Unique Column " + uniqueKey + " can represent troid as it has type " + t); |
| 458 | 0 | foundKey = uniqueKey; |
| 459 | |
} |
| 460 | 0 | idColNotPrimeKey.close(); |
| 461 | 0 | } else |
| 462 | 0 | throw new UnexpectedExceptionPoemException( |
| 463 | |
"Found a unique key but no corresponding column"); |
| 464 | |
|
| 465 | 0 | troidColumnName = uniqueKey; |
| 466 | 0 | } |
| 467 | |
} |
| 468 | 0 | u.close(); |
| 469 | 0 | } |
| 470 | |
} else |
| 471 | 0 | throw new UnexpectedExceptionPoemException( |
| 472 | |
"Found a primary key but no corresponding column"); |
| 473 | 2 | idCol.close(); |
| 474 | |
} |
| 475 | 42 | tables.close(); |
| 476 | |
} |
| 477 | 198 | return troidColumnName; |
| 478 | |
} |
| 479 | |
|
| 480 | |
private synchronized void unifyWithDB() throws PoemException, SQLException { |
| 481 | 41 | boolean debug = false; |
| 482 | |
|
| 483 | |
|
| 484 | |
|
| 485 | |
|
| 486 | 41 | for (Enumeration<TableInfo> ti = getTableInfoTable().selection(); |
| 487 | 51 | ti.hasMoreElements();) { |
| 488 | 10 | TableInfo tableInfo = ti.nextElement(); |
| 489 | 10 | Table<?> table = getTableIgnoringCase(tableInfo.getName()); |
| 490 | 10 | if (table == null) { |
| 491 | 1 | if (debug) log("Defining table:" + tableInfo.getName()); |
| 492 | 1 | table = new JdbcTable<Persistent>(this, tableInfo.getName(), |
| 493 | |
DefinitionSource.infoTables); |
| 494 | 1 | defineTable(table); |
| 495 | |
} |
| 496 | 10 | table.setTableInfo(tableInfo); |
| 497 | 10 | } |
| 498 | |
|
| 499 | |
|
| 500 | |
|
| 501 | 41 | for (Table<?> t : tables) |
| 502 | 706 | t.createTableInfo(); |
| 503 | |
|
| 504 | |
|
| 505 | |
|
| 506 | 41 | for (Table<?> t : tables) |
| 507 | 706 | t.unifyWithColumnInfo(); |
| 508 | |
|
| 509 | |
|
| 510 | |
|
| 511 | 41 | String[] normalTables = { "TABLE" }; |
| 512 | |
|
| 513 | 41 | DatabaseMetaData m = committedConnection.getMetaData(); |
| 514 | 41 | ResultSet tableDescs = m.getTables(null, dbms.getSchema(), null, |
| 515 | |
normalTables); |
| 516 | 633 | while (tableDescs.next()) { |
| 517 | 592 | if (debug) log("Table:" + tableDescs.getString("TABLE_NAME") + |
| 518 | 0 | " Type:" + tableDescs.getString("TABLE_TYPE")); |
| 519 | 592 | String tableName = dbms.melatiName(tableDescs.getString("TABLE_NAME")); |
| 520 | 592 | if (debug) log("Melati Table name :" + tableName); |
| 521 | 592 | Table<?> table = null; |
| 522 | 592 | String troidColumnName = null; |
| 523 | 592 | if (tableName != null) { |
| 524 | 592 | table = getTableIgnoringCase(tableName); |
| 525 | 592 | if (table == null) { |
| 526 | 42 | if (debug) log("Unknown to POEM, with JDBC name " + tableName); |
| 527 | |
|
| 528 | |
|
| 529 | 42 | troidColumnName = getTroidColumnName(m,dbms.unreservedName(tableName)); |
| 530 | 42 | if(debug) log("Primary key:"+ troidColumnName); |
| 531 | 42 | if (troidColumnName != null) { |
| 532 | 2 | if (debug) log("Got a troid column for discovered jdbc table :" + tableName + ":" + troidColumnName); |
| 533 | |
try { |
| 534 | 2 | table = new JdbcTable<Persistent>(this, tableName, |
| 535 | |
DefinitionSource.sqlMetaData); |
| 536 | 2 | defineTable(table); |
| 537 | |
} |
| 538 | 0 | catch (DuplicateTableNamePoemException e) { |
| 539 | 0 | throw new UnexpectedExceptionPoemException(e); |
| 540 | 2 | } |
| 541 | 2 | table.createTableInfo(); |
| 542 | 40 | } else log ("Ignoring table " + tableName + " as it has no plausible troid"); |
| 543 | 550 | } else if (debug) log("Table not null:" + tableName + " has name " + table.getName()); |
| 544 | |
} |
| 545 | |
|
| 546 | 592 | if (table != null) { |
| 547 | 552 | if (debug) log("table not null now:" + tableName); |
| 548 | 552 | if (debug) log("columnsMetadata(m, tableName):" |
| 549 | 0 | + columnsMetadata(m, tableName)); |
| 550 | |
|
| 551 | |
|
| 552 | 552 | table.unifyWithDB(columnsMetadata(m, tableName), troidColumnName); |
| 553 | 40 | } else if (debug) log("table still null, probably doesn't have a troid:" + tableName); |
| 554 | |
|
| 555 | 592 | } |
| 556 | |
|
| 557 | |
|
| 558 | |
|
| 559 | 41 | for (Table<?> table : tables) { |
| 560 | 708 | if (debug) log("Unifying:" + table.getName() + "(" + dbms.unreservedName(table.getName()) + ")"); |
| 561 | |
|
| 562 | |
|
| 563 | 1416 | ResultSet colDescs = columnsMetadata(m, |
| 564 | 708 | dbms.unreservedName(table.getName())); |
| 565 | 708 | if (!colDescs.next()) { |
| 566 | |
|
| 567 | |
|
| 568 | 156 | table.unifyWithDB(null, getTroidColumnName(m,dbms.unreservedName(table.getName()))); |
| 569 | |
} |
| 570 | 708 | } |
| 571 | |
|
| 572 | 41 | for (Table<?> table : tables) |
| 573 | 708 | table.postInitialise(); |
| 574 | |
|
| 575 | 41 | } |
| 576 | |
|
| 577 | |
|
| 578 | |
|
| 579 | |
|
| 580 | |
|
| 581 | |
|
| 582 | |
|
| 583 | |
|
| 584 | |
|
| 585 | |
|
| 586 | |
|
| 587 | |
public void addConstraints() { |
| 588 | 17 | inSession(AccessToken.root, |
| 589 | 17 | new PoemTask() { |
| 590 | |
public void run() throws PoemException { |
| 591 | 17 | PoemThread.commit(); |
| 592 | 17 | beginStructuralModification(); |
| 593 | |
try { |
| 594 | 17 | for (Table<?> table : tables) |
| 595 | 153 | table.dbAddConstraints(); |
| 596 | 17 | PoemThread.commit(); |
| 597 | |
} |
| 598 | |
finally { |
| 599 | 17 | endStructuralModification(); |
| 600 | 17 | } |
| 601 | 17 | } |
| 602 | |
|
| 603 | |
public String toString() { |
| 604 | 0 | return "Adding constraints to DB"; |
| 605 | |
} |
| 606 | |
}); |
| 607 | 17 | } |
| 608 | |
|
| 609 | |
|
| 610 | |
|
| 611 | |
|
| 612 | |
|
| 613 | |
|
| 614 | |
|
| 615 | |
|
| 616 | |
|
| 617 | |
|
| 618 | |
|
| 619 | |
|
| 620 | |
|
| 621 | |
|
| 622 | |
|
| 623 | |
|
| 624 | |
public final int transactionsMax() { |
| 625 | 759 | return transactionsMax; |
| 626 | |
} |
| 627 | |
|
| 628 | |
|
| 629 | |
|
| 630 | |
|
| 631 | |
|
| 632 | |
|
| 633 | |
|
| 634 | |
|
| 635 | |
|
| 636 | |
public final void setTransactionsMax(int t) { |
| 637 | 43 | transactionsMax = t; |
| 638 | 43 | } |
| 639 | |
|
| 640 | |
|
| 641 | |
|
| 642 | |
|
| 643 | |
|
| 644 | |
public int getTransactionsCount() { |
| 645 | 3 | return transactions.size(); |
| 646 | |
} |
| 647 | |
|
| 648 | |
|
| 649 | |
|
| 650 | |
|
| 651 | |
|
| 652 | |
public int getFreeTransactionsCount() { |
| 653 | 2936 | return freeTransactions.size(); |
| 654 | |
} |
| 655 | |
|
| 656 | |
|
| 657 | |
|
| 658 | |
|
| 659 | |
|
| 660 | |
|
| 661 | |
|
| 662 | |
|
| 663 | |
|
| 664 | |
|
| 665 | |
|
| 666 | |
private PoemTransaction openTransaction() { |
| 667 | 3437 | synchronized (freeTransactions) { |
| 668 | 3437 | if (freeTransactions.size() == 0) |
| 669 | 0 | throw new NoMoreTransactionsException("Database " + name + " has no free transactions remaining of " |
| 670 | 0 | + transactions.size() + " transactions."); |
| 671 | 3437 | PoemTransaction transaction = |
| 672 | 3437 | (PoemTransaction)freeTransactions.lastElement(); |
| 673 | 3437 | freeTransactions.setSize(freeTransactions.size() - 1); |
| 674 | 3437 | return transaction; } |
| 675 | |
} |
| 676 | |
|
| 677 | |
|
| 678 | |
|
| 679 | |
|
| 680 | |
void notifyClosed(PoemTransaction transaction) { |
| 681 | 3437 | freeTransactions.addElement(transaction); |
| 682 | 3437 | } |
| 683 | |
|
| 684 | |
|
| 685 | |
|
| 686 | |
|
| 687 | |
|
| 688 | |
|
| 689 | |
|
| 690 | |
|
| 691 | |
|
| 692 | |
public PoemTransaction poemTransaction(int index) { |
| 693 | 697 | return (PoemTransaction)transactions.elementAt(index); |
| 694 | |
} |
| 695 | |
|
| 696 | |
|
| 697 | |
|
| 698 | |
|
| 699 | |
|
| 700 | |
public final Transaction transaction(int index) { |
| 701 | 1 | return poemTransaction(index); |
| 702 | |
} |
| 703 | |
|
| 704 | |
|
| 705 | |
|
| 706 | |
|
| 707 | |
|
| 708 | |
public boolean isFree(PoemTransaction trans) { |
| 709 | 3442 | return freeTransactions.contains(trans); |
| 710 | |
} |
| 711 | |
|
| 712 | |
|
| 713 | |
|
| 714 | |
|
| 715 | |
public void beginExclusiveLock() { |
| 716 | |
|
| 717 | 116 | if (PoemThread.inSession()) { |
| 718 | 116 | lock.readLock().unlock(); |
| 719 | |
|
| 720 | |
} |
| 721 | 116 | lock.writeLock().lock(); |
| 722 | 116 | } |
| 723 | |
|
| 724 | |
|
| 725 | |
|
| 726 | |
|
| 727 | |
public void endExclusiveLock() { |
| 728 | 116 | lock.writeLock().unlock(); |
| 729 | |
|
| 730 | |
|
| 731 | |
|
| 732 | 116 | if (PoemThread.inSession()) |
| 733 | 116 | lock.readLock().lock(); |
| 734 | 116 | } |
| 735 | |
|
| 736 | |
|
| 737 | |
|
| 738 | |
|
| 739 | |
|
| 740 | |
|
| 741 | |
|
| 742 | |
|
| 743 | |
|
| 744 | |
|
| 745 | |
|
| 746 | |
|
| 747 | |
|
| 748 | |
private void perform(AccessToken accessToken, final PoemTask task, |
| 749 | |
boolean useCommittedTransaction) throws PoemException { |
| 750 | |
|
| 751 | 3439 | lock.readLock().lock(); |
| 752 | |
|
| 753 | 3440 | final PoemTransaction transaction = |
| 754 | 3434 | useCommittedTransaction ? null : openTransaction(); |
| 755 | |
try { |
| 756 | 3440 | PoemThread.inSession(new PoemTask() { |
| 757 | |
public void run() throws PoemException { |
| 758 | 3440 | task.run(); |
| 759 | 3440 | if (transaction != null) |
| 760 | 3434 | transaction.close(true); |
| 761 | 3440 | } |
| 762 | |
|
| 763 | |
public String toString() { |
| 764 | 1 | return task.toString(); |
| 765 | |
} |
| 766 | |
}, |
| 767 | |
accessToken, |
| 768 | |
transaction); |
| 769 | |
} |
| 770 | |
finally { |
| 771 | 0 | try { |
| 772 | 3440 | if (transaction != null && !isFree(transaction)) { |
| 773 | 0 | transaction.close(false); |
| 774 | |
} |
| 775 | |
} finally { |
| 776 | |
|
| 777 | 3440 | lock.readLock().unlock(); |
| 778 | 3440 | } |
| 779 | 3440 | } |
| 780 | 3440 | } |
| 781 | |
|
| 782 | |
|
| 783 | |
|
| 784 | |
|
| 785 | |
|
| 786 | |
|
| 787 | |
|
| 788 | |
|
| 789 | |
|
| 790 | |
|
| 791 | |
|
| 792 | |
|
| 793 | |
|
| 794 | |
|
| 795 | |
|
| 796 | |
|
| 797 | |
|
| 798 | |
|
| 799 | |
|
| 800 | |
|
| 801 | |
|
| 802 | |
|
| 803 | |
|
| 804 | |
|
| 805 | |
|
| 806 | |
|
| 807 | |
|
| 808 | |
|
| 809 | |
|
| 810 | |
|
| 811 | |
|
| 812 | |
|
| 813 | |
|
| 814 | |
|
| 815 | |
|
| 816 | |
|
| 817 | |
|
| 818 | |
|
| 819 | |
public void inSession(AccessToken accessToken, PoemTask task) { |
| 820 | 3433 | perform(accessToken, task, false); |
| 821 | 3434 | } |
| 822 | |
|
| 823 | |
|
| 824 | |
|
| 825 | |
|
| 826 | |
public void inSessionAsRoot(PoemTask task) { |
| 827 | 0 | perform(AccessToken.root, task, false); |
| 828 | 0 | } |
| 829 | |
|
| 830 | |
|
| 831 | |
|
| 832 | |
|
| 833 | |
|
| 834 | |
|
| 835 | |
public void beginSession(AccessToken accessToken) { |
| 836 | 3 | lock.readLock().lock(); |
| 837 | 3 | PoemTransaction transaction = openTransaction(); |
| 838 | |
try { |
| 839 | 3 | PoemThread.beginSession(accessToken,transaction); |
| 840 | 1 | } catch (AlreadyInSessionPoemException e) { |
| 841 | 1 | notifyClosed(transaction); |
| 842 | 1 | lock.readLock().unlock(); |
| 843 | 1 | throw e; |
| 844 | 2 | } |
| 845 | 2 | } |
| 846 | |
|
| 847 | |
|
| 848 | |
|
| 849 | |
|
| 850 | |
|
| 851 | |
|
| 852 | |
|
| 853 | |
public void endSession() { |
| 854 | 2 | PoemTransaction tx = PoemThread.sessionToken().transaction; |
| 855 | 2 | PoemThread.endSession(); |
| 856 | 2 | tx.close(true); |
| 857 | 2 | lock.readLock().unlock(); |
| 858 | 2 | } |
| 859 | |
|
| 860 | |
|
| 861 | |
|
| 862 | |
|
| 863 | |
|
| 864 | |
|
| 865 | |
|
| 866 | |
|
| 867 | |
|
| 868 | |
|
| 869 | |
|
| 870 | |
|
| 871 | |
|
| 872 | |
|
| 873 | |
|
| 874 | |
|
| 875 | |
public void inCommittedTransaction(AccessToken accessToken, PoemTask task) { |
| 876 | 6 | perform(accessToken, task, true); |
| 877 | 6 | } |
| 878 | |
|
| 879 | |
|
| 880 | |
|
| 881 | |
|
| 882 | |
|
| 883 | |
|
| 884 | |
|
| 885 | |
|
| 886 | |
|
| 887 | |
|
| 888 | |
|
| 889 | |
|
| 890 | |
|
| 891 | |
|
| 892 | |
|
| 893 | |
|
| 894 | |
|
| 895 | |
|
| 896 | |
|
| 897 | |
|
| 898 | |
|
| 899 | |
public final Table<?> getTable(String tableName) throws NoSuchTablePoemException { |
| 900 | 176 | Table<?> table = getTableIgnoringCase(tableName); |
| 901 | 176 | if (table == null) throw new NoSuchTablePoemException(this, tableName); |
| 902 | 107 | return table; |
| 903 | |
} |
| 904 | |
private Table<?> getTableIgnoringCase(String tableName) { |
| 905 | 1729 | return tablesByName.get(tableName.toLowerCase()); |
| 906 | |
} |
| 907 | |
|
| 908 | |
|
| 909 | |
|
| 910 | |
|
| 911 | |
|
| 912 | |
|
| 913 | |
|
| 914 | |
|
| 915 | |
public final Enumeration<Table<?>> tables() { |
| 916 | 2860 | return tables.elements(); |
| 917 | |
} |
| 918 | |
|
| 919 | |
|
| 920 | |
|
| 921 | |
|
| 922 | |
|
| 923 | |
public final List<Table<?>> getTables() { |
| 924 | 1 | return tables; |
| 925 | |
} |
| 926 | |
|
| 927 | |
|
| 928 | |
|
| 929 | |
|
| 930 | |
|
| 931 | |
|
| 932 | |
|
| 933 | |
public Enumeration<Table<?>> displayTables() { |
| 934 | 2 | return displayTables(PoemThread.inSession() ? PoemThread.transaction() : null); |
| 935 | |
} |
| 936 | |
|
| 937 | |
public List<Table<?>> getDisplayTables() { |
| 938 | 2 | return EnumUtils.list(displayTables()); |
| 939 | |
} |
| 940 | |
|
| 941 | |
|
| 942 | |
|
| 943 | |
|
| 944 | |
|
| 945 | |
|
| 946 | |
|
| 947 | |
public Enumeration<Table<?>> displayTables(PoemTransaction transaction) { |
| 948 | 2 | Table<?>[] displayTablesL = this.displayTables; |
| 949 | |
|
| 950 | 2 | if (displayTablesL == null) { |
| 951 | 4 | Enumeration<Integer> tableIDs = getTableInfoTable().troidSelection( |
| 952 | |
(String)null , |
| 953 | 2 | quotedName("displayorder") + ", " + quotedName("name"), |
| 954 | |
false, transaction); |
| 955 | |
|
| 956 | 2 | Vector<Table<?>> them = new Vector<Table<?>>(); |
| 957 | 36 | while (tableIDs.hasMoreElements()) { |
| 958 | 34 | Table<?> table = |
| 959 | 34 | tableWithTableInfoID(tableIDs.nextElement().intValue()); |
| 960 | 34 | if (table != null) |
| 961 | 34 | them.addElement(table); |
| 962 | 34 | } |
| 963 | |
|
| 964 | 2 | displayTablesL = new Table[them.size()]; |
| 965 | 2 | them.copyInto(displayTablesL); |
| 966 | 2 | this.displayTables = displayTablesL; |
| 967 | |
} |
| 968 | |
|
| 969 | 2 | return new ArrayEnumeration<Table<?>>(this.displayTables); |
| 970 | |
} |
| 971 | |
|
| 972 | |
|
| 973 | |
|
| 974 | |
|
| 975 | |
|
| 976 | |
|
| 977 | |
|
| 978 | |
Table<?> tableWithTableInfoID(int tableInfoID) { |
| 979 | 76 | for (Table<?> table : tables) { |
| 980 | 915 | Integer id = table.tableInfoID(); |
| 981 | 915 | if (id != null && id.intValue() == tableInfoID) |
| 982 | 76 | return table; |
| 983 | 839 | } |
| 984 | 0 | return null; |
| 985 | |
} |
| 986 | |
|
| 987 | |
|
| 988 | |
|
| 989 | |
|
| 990 | |
public Enumeration<Column<?>> columns() { |
| 991 | 1296 | return new FlattenedEnumeration<Column<?>>( |
| 992 | 12960 | new MappedEnumeration<Enumeration<Column<?>>, Table<?>>(tables()) { |
| 993 | |
public Enumeration<Column<?>> mapped(Table<?> table) { |
| 994 | 11664 | return table.columns(); |
| 995 | |
} |
| 996 | |
}); |
| 997 | |
} |
| 998 | |
|
| 999 | |
public List<Column<?>> getColumns() { |
| 1000 | 1 | return EnumUtils.list(columns()); |
| 1001 | |
} |
| 1002 | |
public int tableCount() { |
| 1003 | 0 | return tables.size(); |
| 1004 | |
} |
| 1005 | |
public int columnCount() { |
| 1006 | 0 | return getColumns().size(); |
| 1007 | |
} |
| 1008 | |
public int recordCount() { |
| 1009 | 0 | Enumeration<Integer> counts = new MappedEnumeration<Integer, Table<?>>(tables()) { |
| 1010 | |
public Integer mapped(Table<?> table) { |
| 1011 | 0 | return new Integer(table.count()); |
| 1012 | |
} |
| 1013 | |
}; |
| 1014 | 0 | int total = 0; |
| 1015 | 0 | while(counts.hasMoreElements()) |
| 1016 | 0 | total = total + counts.nextElement().intValue(); |
| 1017 | |
|
| 1018 | 0 | return total; |
| 1019 | |
} |
| 1020 | |
|
| 1021 | |
|
| 1022 | |
|
| 1023 | |
|
| 1024 | |
|
| 1025 | |
Column<?> columnWithColumnInfoID(int columnInfoID) { |
| 1026 | 6 | for (Table<?> table : tables) { |
| 1027 | 23 | Column<?> column = table.columnWithColumnInfoID(columnInfoID); |
| 1028 | 23 | if (column != null) |
| 1029 | 5 | return column; |
| 1030 | 18 | } |
| 1031 | 1 | return null; |
| 1032 | |
} |
| 1033 | |
|
| 1034 | |
|
| 1035 | |
|
| 1036 | |
|
| 1037 | |
|
| 1038 | |
public abstract TableInfoTable<TableInfo> getTableInfoTable(); |
| 1039 | |
|
| 1040 | |
|
| 1041 | |
|
| 1042 | |
|
| 1043 | |
public abstract TableCategoryTable<TableCategory> getTableCategoryTable(); |
| 1044 | |
|
| 1045 | |
|
| 1046 | |
|
| 1047 | |
|
| 1048 | |
|
| 1049 | |
public abstract ColumnInfoTable<ColumnInfo> getColumnInfoTable(); |
| 1050 | |
|
| 1051 | |
|
| 1052 | |
|
| 1053 | |
|
| 1054 | |
|
| 1055 | |
|
| 1056 | |
|
| 1057 | |
|
| 1058 | |
|
| 1059 | |
|
| 1060 | |
|
| 1061 | |
|
| 1062 | |
|
| 1063 | |
|
| 1064 | |
|
| 1065 | |
|
| 1066 | |
|
| 1067 | |
public abstract CapabilityTable<Capability> getCapabilityTable(); |
| 1068 | |
|
| 1069 | |
|
| 1070 | |
|
| 1071 | |
|
| 1072 | |
public abstract UserTable<User> getUserTable(); |
| 1073 | |
|
| 1074 | |
|
| 1075 | |
|
| 1076 | |
|
| 1077 | |
public abstract GroupTable<Group> getGroupTable(); |
| 1078 | |
|
| 1079 | |
|
| 1080 | |
|
| 1081 | |
|
| 1082 | |
|
| 1083 | |
public abstract GroupMembershipTable<GroupMembership> getGroupMembershipTable(); |
| 1084 | |
|
| 1085 | |
|
| 1086 | |
|
| 1087 | |
|
| 1088 | |
|
| 1089 | |
|
| 1090 | |
public abstract GroupCapabilityTable<GroupCapability> getGroupCapabilityTable(); |
| 1091 | |
|
| 1092 | |
|
| 1093 | |
|
| 1094 | |
|
| 1095 | |
public abstract SettingTable<Setting> getSettingTable(); |
| 1096 | |
|
| 1097 | |
|
| 1098 | |
|
| 1099 | |
|
| 1100 | |
|
| 1101 | |
|
| 1102 | |
|
| 1103 | |
|
| 1104 | |
|
| 1105 | |
|
| 1106 | |
|
| 1107 | |
|
| 1108 | |
|
| 1109 | |
|
| 1110 | |
|
| 1111 | |
|
| 1112 | |
|
| 1113 | |
|
| 1114 | |
public ResultSet sqlQuery(String sql) throws SQLPoemException { |
| 1115 | 23 | SessionToken token = PoemThread.sessionToken(); |
| 1116 | 23 | token.transaction.writeDown(); |
| 1117 | |
try { |
| 1118 | 23 | Statement s = token.transaction.getConnection().createStatement(); |
| 1119 | 23 | token.toTidy().add(s); |
| 1120 | 23 | ResultSet rs = s.executeQuery(sql); |
| 1121 | 22 | token.toTidy().add(rs); |
| 1122 | 22 | if (logSQL()) |
| 1123 | 14 | log(new SQLLogEvent(sql)); |
| 1124 | 22 | incrementQueryCount(sql); |
| 1125 | 22 | return rs; |
| 1126 | |
} |
| 1127 | 1 | catch (SQLException e) { |
| 1128 | 1 | throw new ExecutingSQLPoemException(sql, e); |
| 1129 | |
} |
| 1130 | |
} |
| 1131 | |
|
| 1132 | |
|
| 1133 | |
|
| 1134 | |
|
| 1135 | |
|
| 1136 | |
|
| 1137 | |
|
| 1138 | |
|
| 1139 | |
|
| 1140 | |
|
| 1141 | |
|
| 1142 | |
|
| 1143 | |
|
| 1144 | |
|
| 1145 | |
|
| 1146 | |
|
| 1147 | |
|
| 1148 | |
|
| 1149 | |
|
| 1150 | |
|
| 1151 | |
public int sqlUpdate(String sql) throws SQLPoemException { |
| 1152 | 5 | SessionToken token = PoemThread.sessionToken(); |
| 1153 | 5 | token.transaction.writeDown(); |
| 1154 | |
|
| 1155 | |
try { |
| 1156 | 5 | Statement s = token.transaction.getConnection().createStatement(); |
| 1157 | 5 | token.toTidy().add(s); |
| 1158 | 5 | int n = s.executeUpdate(sql); |
| 1159 | 3 | if (logSQL()) |
| 1160 | 2 | log(new SQLLogEvent(sql)); |
| 1161 | 3 | incrementQueryCount(sql); |
| 1162 | 3 | return n; |
| 1163 | |
} |
| 1164 | 2 | catch (SQLException e) { |
| 1165 | 4 | throw dbms.exceptionForUpdate(null, sql, |
| 1166 | 2 | sql.indexOf("INSERT") >= 0 || |
| 1167 | 1 | sql.indexOf("insert") >= 0, |
| 1168 | |
e); |
| 1169 | |
} |
| 1170 | |
} |
| 1171 | |
|
| 1172 | |
|
| 1173 | |
|
| 1174 | |
|
| 1175 | |
|
| 1176 | |
|
| 1177 | |
|
| 1178 | 43 | private User guest = null; |
| 1179 | |
|
| 1180 | |
|
| 1181 | |
|
| 1182 | |
public User guestUser() { |
| 1183 | 21 | if (guest == null) |
| 1184 | 3 | guest = getUserTable().guestUser(); |
| 1185 | 21 | return guest; |
| 1186 | |
} |
| 1187 | |
|
| 1188 | 43 | private User administrator = null; |
| 1189 | |
|
| 1190 | |
|
| 1191 | |
|
| 1192 | |
public User administratorUser() { |
| 1193 | 3 | if (administrator == null) |
| 1194 | 2 | administrator = getUserTable().administratorUser(); |
| 1195 | 3 | return administrator; |
| 1196 | |
} |
| 1197 | |
|
| 1198 | |
|
| 1199 | |
|
| 1200 | |
|
| 1201 | |
|
| 1202 | |
|
| 1203 | |
|
| 1204 | |
|
| 1205 | |
|
| 1206 | |
public String givesCapabilitySQL(User user, Capability capability) { |
| 1207 | |
|
| 1208 | 13 | return dbms.givesCapabilitySQL(user.troid(), capability.troid().toString()); |
| 1209 | |
} |
| 1210 | |
|
| 1211 | |
|
| 1212 | |
|
| 1213 | |
|
| 1214 | |
private boolean dbGivesCapability(User user, Capability capability) { |
| 1215 | |
|
| 1216 | 12 | String sql = givesCapabilitySQL(user, capability); |
| 1217 | 12 | ResultSet rs = null; |
| 1218 | |
try { |
| 1219 | 12 | rs = sqlQuery(sql); |
| 1220 | 12 | return rs.next(); |
| 1221 | |
} |
| 1222 | 0 | catch (SQLPoemException e) { |
| 1223 | 0 | throw new UnexpectedExceptionPoemException(e); |
| 1224 | |
} |
| 1225 | 0 | catch (SQLException e) { |
| 1226 | 0 | throw new SQLSeriousPoemException(e, sql); |
| 1227 | |
} |
| 1228 | |
finally { |
| 1229 | 12 | try { if (rs != null) rs.close(); } catch (Exception e) { |
| 1230 | 0 | System.err.println("Cannot close resultset after exception."); |
| 1231 | 24 | } |
| 1232 | |
} |
| 1233 | |
} |
| 1234 | |
|
| 1235 | 86 | private class UserCapabilityCache { |
| 1236 | 43 | private Hashtable<Long,Boolean> userCapabilities = null; |
| 1237 | |
private long groupMembershipSerial; |
| 1238 | |
private long groupCapabilitySerial; |
| 1239 | |
|
| 1240 | |
boolean hasCapability(User user, Capability capability) { |
| 1241 | 33 | PoemTransaction transaction = PoemThread.transaction(); |
| 1242 | 33 | long currentGroupMembershipSerial = |
| 1243 | 33 | getGroupMembershipTable().serial(transaction); |
| 1244 | 33 | long currentGroupCapabilitySerial = |
| 1245 | 33 | getGroupCapabilityTable().serial(transaction); |
| 1246 | |
|
| 1247 | 33 | if (userCapabilities == null || |
| 1248 | |
groupMembershipSerial != currentGroupMembershipSerial || |
| 1249 | |
groupCapabilitySerial != currentGroupCapabilitySerial) { |
| 1250 | 8 | userCapabilities = new Hashtable<Long,Boolean>(); |
| 1251 | 8 | groupMembershipSerial = currentGroupMembershipSerial; |
| 1252 | 8 | groupCapabilitySerial = currentGroupCapabilitySerial; |
| 1253 | |
} |
| 1254 | |
|
| 1255 | 33 | Long pair = new Long( |
| 1256 | 33 | (user.troid().longValue() << 32) | (capability.troid().longValue())); |
| 1257 | 33 | Boolean known = userCapabilities.get(pair); |
| 1258 | |
|
| 1259 | 33 | if (known != null) |
| 1260 | 21 | return known.booleanValue(); |
| 1261 | |
else { |
| 1262 | 12 | boolean does = dbGivesCapability(user, capability); |
| 1263 | 12 | userCapabilities.put(pair, does ? Boolean.TRUE : Boolean.FALSE); |
| 1264 | 12 | return does; |
| 1265 | |
} |
| 1266 | |
} |
| 1267 | |
} |
| 1268 | |
|
| 1269 | 43 | private UserCapabilityCache capabilityCache = new UserCapabilityCache(); |
| 1270 | |
|
| 1271 | |
|
| 1272 | |
|
| 1273 | |
|
| 1274 | |
|
| 1275 | |
|
| 1276 | |
|
| 1277 | |
public boolean hasCapability(User user, Capability capability) { |
| 1278 | |
|
| 1279 | 35 | if (capability == null) return true; |
| 1280 | |
|
| 1281 | 33 | return capabilityCache.hasCapability(user, capability); |
| 1282 | |
} |
| 1283 | |
|
| 1284 | |
|
| 1285 | |
|
| 1286 | |
|
| 1287 | |
public AccessToken guestAccessToken() { |
| 1288 | 9 | return getUserTable().guestUser(); |
| 1289 | |
} |
| 1290 | |
|
| 1291 | 43 | private Capability canAdminister = null; |
| 1292 | |
|
| 1293 | |
|
| 1294 | |
|
| 1295 | |
|
| 1296 | |
public Capability administerCapability() { |
| 1297 | 1156 | return getCapabilityTable().administer(); |
| 1298 | |
} |
| 1299 | |
|
| 1300 | |
|
| 1301 | |
|
| 1302 | |
|
| 1303 | |
|
| 1304 | |
|
| 1305 | |
|
| 1306 | |
|
| 1307 | |
public Capability getCanAdminister() { |
| 1308 | 5 | return canAdminister; |
| 1309 | |
} |
| 1310 | |
|
| 1311 | |
|
| 1312 | |
|
| 1313 | |
|
| 1314 | |
|
| 1315 | |
|
| 1316 | |
|
| 1317 | |
public void setCanAdminister() { |
| 1318 | 3 | canAdminister = administerCapability(); |
| 1319 | 3 | } |
| 1320 | |
|
| 1321 | |
|
| 1322 | |
|
| 1323 | |
|
| 1324 | |
public void setCanAdminister(String capabilityName) { |
| 1325 | 1 | canAdminister = getCapabilityTable().ensure(capabilityName); |
| 1326 | 1 | } |
| 1327 | |
|
| 1328 | |
|
| 1329 | |
|
| 1330 | |
|
| 1331 | |
|
| 1332 | |
|
| 1333 | |
|
| 1334 | |
|
| 1335 | |
|
| 1336 | |
|
| 1337 | |
|
| 1338 | |
|
| 1339 | |
|
| 1340 | |
|
| 1341 | |
public void trimCache(int maxSize) { |
| 1342 | 1 | for (Table<?> table : tables) |
| 1343 | 9 | table.trimCache(maxSize); |
| 1344 | 1 | } |
| 1345 | |
|
| 1346 | |
|
| 1347 | |
|
| 1348 | |
|
| 1349 | |
public void uncache() { |
| 1350 | 258 | for (int t = 0; t < tables.size(); ++t) |
| 1351 | 245 | tables.elementAt(t).uncache(); |
| 1352 | 13 | } |
| 1353 | |
|
| 1354 | |
|
| 1355 | |
|
| 1356 | |
|
| 1357 | |
|
| 1358 | |
|
| 1359 | |
|
| 1360 | |
|
| 1361 | |
|
| 1362 | |
|
| 1363 | |
|
| 1364 | |
|
| 1365 | |
|
| 1366 | |
@SuppressWarnings({ "rawtypes", "unchecked" }) |
| 1367 | |
public <P extends Persistent> Enumeration<P> referencesTo(final Persistent persistent) { |
| 1368 | 2 | return new FlattenedEnumeration<P>( |
| 1369 | 2 | new MappedEnumeration(tables()) { |
| 1370 | |
@Override |
| 1371 | |
public Object mapped(Object table) { |
| 1372 | 18 | return ((Table<P>)table).referencesTo(persistent); |
| 1373 | |
} |
| 1374 | |
}); |
| 1375 | |
} |
| 1376 | |
|
| 1377 | |
|
| 1378 | |
|
| 1379 | |
|
| 1380 | |
|
| 1381 | |
public List<Persistent> getReferencesTo(final Persistent persistent) { |
| 1382 | 1 | return EnumUtils.list(referencesTo(persistent)); |
| 1383 | |
} |
| 1384 | |
|
| 1385 | |
|
| 1386 | |
|
| 1387 | |
|
| 1388 | |
public Enumeration<Column<?>> referencesTo(final Table<?> tableIn) { |
| 1389 | 116 | return new FlattenedEnumeration<Column<?>>( |
| 1390 | 2338 | new MappedEnumeration<Enumeration<Column<?>>, Table<?>>(tables()) { |
| 1391 | |
public Enumeration<Column<?>> mapped(Table<?> table) { |
| 1392 | 2222 | return table.referencesTo(tableIn); |
| 1393 | |
} |
| 1394 | |
}); |
| 1395 | |
} |
| 1396 | |
|
| 1397 | |
|
| 1398 | |
|
| 1399 | |
|
| 1400 | |
|
| 1401 | |
public List<Column<?>> getReferencesTo(final Table<?> table) { |
| 1402 | 0 | return EnumUtils.list(referencesTo(table)); |
| 1403 | |
} |
| 1404 | |
|
| 1405 | |
|
| 1406 | |
|
| 1407 | |
|
| 1408 | |
|
| 1409 | |
public void dumpCacheAnalysis() { |
| 1410 | 1 | for (Table<?> table : tables) |
| 1411 | 9 | table.dumpCacheAnalysis(); |
| 1412 | 1 | } |
| 1413 | |
|
| 1414 | |
|
| 1415 | |
|
| 1416 | |
|
| 1417 | |
public void dump() { |
| 1418 | 10 | for (int t = 0; t < tables.size(); ++t) { |
| 1419 | 9 | System.out.println(); |
| 1420 | 9 | tables.elementAt(t).dump(); |
| 1421 | |
} |
| 1422 | |
|
| 1423 | 2 | System.err.println("there are " + getTransactionsCount() + " transactions " + |
| 1424 | 1 | "of which " + getFreeTransactionsCount() + " are free"); |
| 1425 | 1 | } |
| 1426 | |
|
| 1427 | |
|
| 1428 | |
|
| 1429 | |
|
| 1430 | |
|
| 1431 | |
|
| 1432 | |
|
| 1433 | |
|
| 1434 | |
|
| 1435 | |
|
| 1436 | |
public Dbms getDbms() { |
| 1437 | 31914 | return dbms; |
| 1438 | |
} |
| 1439 | |
|
| 1440 | |
|
| 1441 | |
|
| 1442 | |
|
| 1443 | |
|
| 1444 | |
|
| 1445 | |
private void setDbms(Dbms aDbms) { |
| 1446 | 41 | dbms = aDbms; |
| 1447 | 41 | } |
| 1448 | |
|
| 1449 | |
|
| 1450 | |
|
| 1451 | |
|
| 1452 | |
|
| 1453 | |
|
| 1454 | |
public final String quotedName(String nameIn) { |
| 1455 | 6809 | return getDbms().getQuotedName(nameIn); |
| 1456 | |
} |
| 1457 | |
|
| 1458 | |
|
| 1459 | |
|
| 1460 | |
|
| 1461 | |
|
| 1462 | |
|
| 1463 | |
|
| 1464 | |
final SQLPoemType<?> defaultPoemTypeOfColumnMetaData(ResultSet md) |
| 1465 | |
throws SQLException { |
| 1466 | 4091 | return getDbms().defaultPoemTypeOfColumnMetaData(md); |
| 1467 | |
} |
| 1468 | |
|
| 1469 | |
|
| 1470 | |
|
| 1471 | |
|
| 1472 | |
|
| 1473 | |
|
| 1474 | |
|
| 1475 | |
|
| 1476 | |
|
| 1477 | |
|
| 1478 | |
|
| 1479 | |
|
| 1480 | |
|
| 1481 | |
|
| 1482 | |
public String toString() { |
| 1483 | 15 | if (connectionUrl == null) |
| 1484 | 1 | return "unconnected database"; |
| 1485 | |
else |
| 1486 | 14 | return connectionUrl; |
| 1487 | |
} |
| 1488 | |
|
| 1489 | |
|
| 1490 | |
|
| 1491 | |
|
| 1492 | |
public Connection getCommittedConnection() { |
| 1493 | 4290 | return committedConnection; |
| 1494 | |
} |
| 1495 | |
|
| 1496 | |
|
| 1497 | |
|
| 1498 | |
|
| 1499 | |
public boolean logSQL() { |
| 1500 | 25913 | return logSQL; |
| 1501 | |
} |
| 1502 | |
|
| 1503 | |
|
| 1504 | |
|
| 1505 | |
|
| 1506 | |
public void setLogSQL(boolean value) { |
| 1507 | 110 | logSQL = value; |
| 1508 | 110 | } |
| 1509 | |
|
| 1510 | |
|
| 1511 | |
|
| 1512 | |
|
| 1513 | |
public boolean logCommits() { |
| 1514 | 6084 | return logCommits; |
| 1515 | |
} |
| 1516 | |
|
| 1517 | |
|
| 1518 | |
|
| 1519 | |
|
| 1520 | |
public void setLogCommits(boolean value) { |
| 1521 | 105 | logCommits = value; |
| 1522 | 105 | } |
| 1523 | |
|
| 1524 | |
void log(PoemLogEvent e) { |
| 1525 | 10061 | System.err.println("---\n" + e.toString()); |
| 1526 | 10061 | } |
| 1527 | |
void log(String s) { |
| 1528 | 168 | System.err.println(s); |
| 1529 | 168 | } |
| 1530 | |
|
| 1531 | |
protected void beginStructuralModification() { |
| 1532 | 103 | beginExclusiveLock(); |
| 1533 | 103 | } |
| 1534 | |
|
| 1535 | |
|
| 1536 | |
|
| 1537 | |
|
| 1538 | |
protected void endStructuralModification() { |
| 1539 | 1444 | for (int t = 0; t < tables.size(); ++t) |
| 1540 | 1341 | tables.elementAt(t).uncache(); |
| 1541 | 103 | ++structureSerial; |
| 1542 | 103 | endExclusiveLock(); |
| 1543 | 103 | } |
| 1544 | |
|
| 1545 | |
|
| 1546 | |
|
| 1547 | |
|
| 1548 | |
long structureSerial() { |
| 1549 | 3091 | return structureSerial; |
| 1550 | |
} |
| 1551 | |
|
| 1552 | |
|
| 1553 | |
|
| 1554 | |
|
| 1555 | |
|
| 1556 | |
|
| 1557 | |
public int getQueryCount() { |
| 1558 | 76 | return queryCount; |
| 1559 | |
} |
| 1560 | |
|
| 1561 | |
|
| 1562 | |
|
| 1563 | |
public void incrementQueryCount(String sql) { |
| 1564 | 25909 | lastQuery = sql; |
| 1565 | 25909 | queryCount++; |
| 1566 | 25909 | } |
| 1567 | |
|
| 1568 | |
|
| 1569 | |
|
| 1570 | |
|
| 1571 | |
public String getLastQuery() { |
| 1572 | 3 | return lastQuery; |
| 1573 | |
} |
| 1574 | |
|
| 1575 | |
|
| 1576 | |
|
| 1577 | |
public String getName() { |
| 1578 | 4 | return name; |
| 1579 | |
} |
| 1580 | |
|
| 1581 | |
|
| 1582 | |
|
| 1583 | |
|
| 1584 | |
public String getDisplayName() { |
| 1585 | 1 | if (displayName == null) |
| 1586 | 1 | return StringUtils.capitalised(getName()); |
| 1587 | 0 | return displayName; |
| 1588 | |
} |
| 1589 | |
|
| 1590 | |
|
| 1591 | |
|
| 1592 | |
|
| 1593 | |
public void setDisplayName(String displayName) { |
| 1594 | 0 | this.displayName = displayName; |
| 1595 | 0 | } |
| 1596 | |
|
| 1597 | |
|
| 1598 | |
|
| 1599 | |
|
| 1600 | |
|
| 1601 | |
|
| 1602 | |
|
| 1603 | |
public void modifyStructure(String sql) |
| 1604 | |
throws StructuralModificationFailedPoemException { |
| 1605 | |
|
| 1606 | |
|
| 1607 | 1269 | if (PoemThread.inSession()) |
| 1608 | 1149 | PoemThread.commit(); |
| 1609 | |
|
| 1610 | |
try { |
| 1611 | 1269 | Statement updateStatement = getCommittedConnection().createStatement(); |
| 1612 | 1269 | updateStatement.executeUpdate(sql); |
| 1613 | 1269 | updateStatement.close(); |
| 1614 | 1269 | getCommittedConnection().commit(); |
| 1615 | 1269 | if (logCommits()) log(new CommitLogEvent(null)); |
| 1616 | 1269 | if (logSQL()) log(new StructuralModificationLogEvent(sql)); |
| 1617 | 1269 | incrementQueryCount(sql); |
| 1618 | |
} |
| 1619 | 0 | catch (SQLException e) { |
| 1620 | 0 | throw new StructuralModificationFailedPoemException(sql, e); |
| 1621 | 1269 | } |
| 1622 | 1269 | } |
| 1623 | |
|
| 1624 | |
} |
| 1625 | |
|