public abstract class Database extends Object implements TransactionPool
PoemDatabase, which includes the boilerplate code for the standard
tables such as user and columninfo which all POEM
databases must contain. If the database is predefined by a Data Structure
Definition Bar.dsd, there will be an application-specialised
subclass of PoemDatabase called BarDatabase which
provides named methods for accessing application-specialised objects
representing the predefined tables.PoemDatabase| Modifier and Type | Class and Description |
|---|---|
class |
Database.ConnectingException
Thrown when a request is made whilst the connection to
the underlying database is still in progress.
|
| Constructor and Description |
|---|
Database()
Don't subclass this, subclass PoemDatabase.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addConstraints()
Add database constraints.
|
Table<?> |
addTableAndCommit(TableInfo info,
String troidName)
Add a Table to this Database and commit the Transaction.
|
Capability |
administerCapability() |
User |
administratorUser() |
void |
beginExclusiveLock()
Acquire a lock on the database.
|
void |
beginSession(AccessToken accessToken)
Start a db session.
|
int |
columnCount() |
Enumeration<Column<?>> |
columns() |
void |
connect(String nameIn,
String dbmsclass,
String url,
String username,
String password,
int transactionsMaxP)
Connect to an RDBMS database.
|
void |
deleteTableAndCommit(TableInfo info) |
void |
disconnect()
Releases database connections.
|
Enumeration<Table<?>> |
displayTables()
All the tables in the database in DisplayOrder
order, using current transaction if there is one.
|
Enumeration<Table<?>> |
displayTables(PoemTransaction transaction)
Currently all the tables in the database in DisplayOrder
order.
|
void |
dump()
Print information about the structure of the database to stdout.
|
void |
dumpCacheAnalysis()
Print some diagnostic information about the contents and consistency of
POEM's cache to stderr.
|
void |
endExclusiveLock()
Release lock.
|
void |
endSession()
End a db session.
|
Capability |
getCanAdminister()
By default, anyone can administer a database.
|
abstract CapabilityTable<Capability> |
getCapabilityTable()
The table of capabilities (required for reading and/or writing records)
defined for the database.
|
abstract ColumnInfoTable<ColumnInfo> |
getColumnInfoTable() |
List<Column<?>> |
getColumns()
Wrapper around columns()
|
Connection |
getCommittedConnection() |
Dbms |
getDbms() |
String |
getDisplayName() |
List<Table<?>> |
getDisplayTables()
A convenience wrapper around displayTables()
|
int |
getFreeTransactionsCount() |
abstract GroupCapabilityTable<GroupCapability> |
getGroupCapabilityTable()
The table containing group-capability records.
|
abstract GroupMembershipTable<GroupMembership> |
getGroupMembershipTable()
A user is a member of a group iff there is a record in this table to say so.
|
abstract GroupTable<Group> |
getGroupTable() |
String |
getLastQuery() |
String |
getName() |
int |
getQueryCount()
Used in testing to check if the cache is being used
or a new query is being issued.
|
List<Persistent> |
getReferencesTo(Persistent persistent)
Wrapper around referencesTo()
|
List<Column<?>> |
getReferencesTo(Table<?> table)
Wrapper around referencesTo()
|
abstract SettingTable<Setting> |
getSettingTable() |
Table<?> |
getTable(String tableName)
Retrieve the table with a given name.
|
abstract TableCategoryTable<TableCategory> |
getTableCategoryTable() |
abstract TableInfoTable<TableInfo> |
getTableInfoTable() |
List<Table<?>> |
getTables()
All the tables in the database.
|
int |
getTransactionsCount() |
abstract UserTable<User> |
getUserTable() |
String |
givesCapabilitySQL(User user,
Capability capability)
Get the raw SQL statement for this database's DBMS for Capability
check for a User.
|
AccessToken |
guestAccessToken() |
User |
guestUser() |
boolean |
hasCapability(User user,
Capability capability)
Check if a user has the specified Capability.
|
void |
inCommittedTransaction(AccessToken accessToken,
PoemTask task)
Perform a task with the database, but not in an insulated transaction.
|
void |
incrementQueryCount(String sql)
Increment query count.
|
void |
inSession(AccessToken accessToken,
PoemTask task)
Perform a task with the database.
|
void |
inSessionAsRoot(PoemTask task) |
boolean |
isFree(PoemTransaction trans) |
boolean |
logCommits() |
boolean |
logSQL() |
void |
modifyStructure(String sql)
Use this for DDL statements, ie those which alter the structure of the db.
|
PoemTransaction |
poemTransaction(int index)
Find a transaction by its index.
|
String |
quotedName(String nameIn)
Quote a name in the DBMS' specific dialect.
|
int |
recordCount() |
<P extends Persistent> |
referencesTo(Persistent persistent)
Find all references to specified object in all tables.
|
Enumeration<Column<?>> |
referencesTo(Table<?> tableIn) |
void |
setCanAdminister()
Set administrator capability to default.
|
void |
setCanAdminister(String capabilityName)
Set administrator capability to named Capability.
|
void |
setDisplayName(String displayName) |
void |
setLogCommits(boolean value)
Toggle commit logging.
|
void |
setLogSQL(boolean value)
Toggle logging.
|
void |
setTransactionsMax(int t)
Set the maximum number of transactions.
|
ResultSet |
sqlQuery(String sql)
Run an arbitrary SQL query against the database.
|
int |
sqlUpdate(String sql)
Run an arbitrary SQL update against the database.
|
int |
tableCount() |
Enumeration<Table<?>> |
tables()
All the tables in the database.
|
String |
toString()
Returns the connection url.
|
Transaction |
transaction(int index)
Get a
Transaction by its index. |
int |
transactionsMax()
The number of transactions available for concurrent use on the database.
|
void |
trimCache(int maxSize)
Trim POEM's cache to a given size.
|
void |
uncache()
Set the contents of the cache to empty.
|
public Database()
PoemDatabasepublic void connect(String nameIn, String dbmsclass, String url, String username, String password, int transactionsMaxP) throws PoemException
dbmsclass - The Melati DBMS class (see org/melati/poem/dbms)
to use, usually specified in
org.melati.LogicalDatabase.properties.url - The JDBC URL for the database; for instance
jdbc:postgresql:williamc. It is the
programmer's responsibility to make sure that an
appropriate driver has been loaded.username - The username under which to establish JDBC connections
to the database. This has nothing to do with the
user/group/capability authentication performed by
Melati.password - The password to go with the username.transactionsMaxP - The maximum number of concurrent Transactions allowed,
usually specified in
org.melati.LogicalDatabase.properties.PoemExceptiontransactionsMax()public void disconnect()
throws PoemException
PoemExceptionpublic Table<?> addTableAndCommit(TableInfo info, String troidName) throws PoemException
info - Table metadata objecttroidName - name of troidColumnTablePoemExceptionpublic void deleteTableAndCommit(TableInfo info)
info - the tableInfo for the table to deletepublic void addConstraints()
public final int transactionsMax()
transactionsMax in interface TransactionPoolTransactionPool.transactionsMax()public final void setTransactionsMax(int t)
setTransactionsMax in interface TransactionPoolt - maximum number of transactions to setTransactionPool.setTransactionsMax(int)public int getTransactionsCount()
getTransactionsCount in interface TransactionPoolTransactionPool.getTransactionsCount()public int getFreeTransactionsCount()
getFreeTransactionsCount in interface TransactionPoolTransactionPool.getFreeTransactionsCount()public PoemTransaction poemTransaction(int index)
transaction(i).index() == i
index - the index of the Transaction to returnpublic final Transaction transaction(int index)
Transaction by its index.transaction in interface TransactionPoolindex - the Transaction index to retrieveTransactionPool.transaction(int)public boolean isFree(PoemTransaction trans)
trans - a PoemTransactionpublic void beginExclusiveLock()
public void endExclusiveLock()
public void inSession(AccessToken accessToken, PoemTask task)
accessToken - A token determining the Capabilitys
available to the task, which in turn determine
what data it can attempt to read and write
without triggering an
AccessPoemException. Note that a
User can be an AccessToken.task - What to do: its run() is invoked, in
the current Java thread; until run()
returns, all POEM accesses made by the thread
are taken to be performed with the capabilities
given by accessToken, and in a private
transaction. No changes made to the database
by other transactions will be visible to it (in the
sense that once it has seen a particular
version of a record, it will always
subsequently see the same one), and its own
changes will not be made permanent until it
completes successfully or performs an explicit
PoemThread.commit(). If it terminates
with an exception or issues a
PoemThread.rollback() its changes will
be lost. (The task is allowed to continue
after either a commit() or a
rollback().)PoemThread,
PoemThread.commit(),
PoemThread.rollback(),
Userpublic void inSessionAsRoot(PoemTask task)
task - the task to runpublic void beginSession(AccessToken accessToken)
public void endSession()
This is the very manual way of doing db work - not recommended - use inSession.
public void inCommittedTransaction(AccessToken accessToken, PoemTask task)
A modification will trigger a WriteCommittedException,
however a create operation will trigger a NullPointerException,
as we have no Transaction.
Not recommended; why exactly do you want to sidestep the Transaction handling?
public final Table<?> getTable(String tableName) throws NoSuchTablePoemException
tableName - The name of the table to return, as in the RDBMS
database. It's case-sensitive, and some RDBMSs such as
Postgres 6.4.2 (and perhaps other versions) treat upper
case letters in identifiers inconsistently, so the
name is forced to lowercase.NoSuchTablePoemException - if no table with the given name exists in the RDBMSpublic final Enumeration<Table<?>> tables()
public final List<Table<?>> getTables()
public Enumeration<Table<?>> displayTables()
public List<Table<?>> getDisplayTables()
public Enumeration<Table<?>> displayTables(PoemTransaction transaction)
public Enumeration<Column<?>> columns()
public int tableCount()
public int columnCount()
public int recordCount()
public abstract TableInfoTable<TableInfo> getTableInfoTable()
public abstract TableCategoryTable<TableCategory> getTableCategoryTable()
public abstract ColumnInfoTable<ColumnInfo> getColumnInfoTable()
public abstract CapabilityTable<Capability> getCapabilityTable()
Persistent.assertCanRead(),
Persistent.assertCanWrite(),
Persistent.assertCanDelete(),
JdbcTable.getDefaultCanRead(),
JdbcTable.getDefaultCanWrite(),
User,
getUserTable(),
Group,
getGroupTable()public abstract UserTable<User> getUserTable()
public abstract GroupTable<Group> getGroupTable()
public abstract GroupMembershipTable<GroupMembership> getGroupMembershipTable()
public abstract GroupCapabilityTable<GroupCapability> getGroupCapabilityTable()
public abstract SettingTable<Setting> getSettingTable()
public ResultSet sqlQuery(String sql) throws SQLPoemException
SQLPoemExceptionTable.selection(),
Table.selection(java.lang.String),
Column.selectionWhereEq(java.lang.Object)public int sqlUpdate(String sql) throws SQLPoemException
NOTE This bypasses the access control mechanism. Furthermore, the cache will be left out of sync with the database and must be cleared out (explicitly, manually) after the current transaction has been committed or completed.
INSERT, UPDATE
or DELETE statements, or 0 for SQL statements
that return nothingSQLPoemExceptionTable.selection(),
Table.selection(java.lang.String),
Column.selectionWhereEq(java.lang.Object),
uncache()public User guestUser()
public User administratorUser()
public String givesCapabilitySQL(User user, Capability capability)
user - capability - public boolean hasCapability(User user, Capability capability)
user - the User to checkcapability - the Capability requiredpublic AccessToken guestAccessToken()
public Capability administerCapability()
public Capability getCanAdminister()
Capability to administer the db
(null unless overridden)public void setCanAdminister(String capabilityName)
capabilityName - name of Capabilitypublic void setCanAdminister()
NOTE Once a database has had its canAdminister capability set there is no mechanism to set it back to null.
public void trimCache(int maxSize)
maxSize - The data for all but this many records per table will
be dropped from POEM's cache, on a least-recently-used
basis.public void uncache()
public <P extends Persistent> Enumeration<P> referencesTo(Persistent persistent)
persistent - the object being referred toPersistentspublic List<Persistent> getReferencesTo(Persistent persistent)
public Enumeration<Column<?>> referencesTo(Table<?> tableIn)
public List<Column<?>> getReferencesTo(Table<?> table)
public void dumpCacheAnalysis()
public void dump()
public Dbms getDbms()
public final String quotedName(String nameIn)
nameIn - public String toString()
toString in class ObjectObject.toString()public Connection getCommittedConnection()
public boolean logSQL()
public void setLogSQL(boolean value)
public boolean logCommits()
public void setLogCommits(boolean value)
public int getQueryCount()
public void incrementQueryCount(String sql)
public String getLastQuery()
public String getName()
public String getDisplayName()
public void setDisplayName(String displayName)
displayName - the displayName to setpublic void modifyStructure(String sql) throws StructuralModificationFailedPoemException
sql - the SQL DDL statement to executeStructuralModificationFailedPoemExceptionCopyright © 2000–2021 PanEris. All rights reserved.