public class DatabaseManager
extends java.lang.Object
| Modifier and Type | Field and Description |
|---|---|
static java.lang.String |
DBMS_H2 |
static java.lang.String |
DBMS_ORACLE |
static java.lang.String |
DBMS_POSTGRES
The static variables which represent the DBMS keyword
|
| Modifier | Constructor and Description |
|---|---|
protected |
DatabaseManager()
Protected Constructor to prevent instantiation except by derived classes.
|
| Modifier and Type | Method and Description |
|---|---|
static void |
applyOffsetAndLimit(java.lang.StringBuffer query,
java.util.List<java.io.Serializable> params,
int offset,
int limit) |
static TableRow |
create(Context context,
java.lang.String table)
Create a new row in the given table, and assigns a unique id.
|
static int |
delete(Context context,
java.lang.String table,
int id)
Delete a table row via its primary key.
|
static int |
delete(Context context,
TableRow row)
Delete row from the RDBMS.
|
static int |
deleteByValue(Context context,
java.lang.String table,
java.lang.String column,
java.lang.Object value)
Delete all table rows with the given value.
|
static TableRow |
find(Context context,
java.lang.String table,
int id)
Find a table row by its primary key.
|
static TableRow |
findByUnique(Context context,
java.lang.String table,
java.lang.String column,
java.lang.Object value)
Find a table row by a unique value.
|
protected static java.lang.String |
findDbKeyword(java.sql.DatabaseMetaData meta)
Return the "DbKeyword" for a specific database name.
|
static void |
freeConnection(java.sql.Connection c)
Release resources associated with this connection.
|
static java.sql.Connection |
getConnection()
Obtain an RDBMS connection.
|
static javax.sql.DataSource |
getDataSource() |
static java.lang.String |
getDbKeyword()
What is the string that we use to name the DBMS brand?
|
static java.lang.String |
getDbName()
What is the name of our DBMS?
|
protected static java.lang.String |
getPrimaryKeyColumn(Context context,
java.lang.String table)
Return the name of the primary key column in the given table.
|
static java.lang.String |
getPrimaryKeyColumn(Context context,
TableRow row)
Return the name of the primary key column.
|
protected static javax.sql.DataSource |
initDataSource()
Initialize just the DataSource for the DatabaseManager.
|
static void |
insert(Context context,
TableRow row)
Insert a table row into the RDBMS.
|
static boolean |
isOracle() |
protected static void |
loadParameters(java.sql.PreparedStatement statement,
java.lang.Object[] parameters)
Iterate over the given parameters and add them to the given prepared statement.
|
static TableRowIterator |
query(Context context,
java.lang.String query,
java.lang.Object... parameters)
Return an iterator with the results of the query.
|
static TableRow |
querySingle(Context context,
java.lang.String query,
java.lang.Object... parameters)
Return the single row result to this query, or null if no result.
|
static TableRow |
querySingleTable(Context context,
java.lang.String table,
java.lang.String query,
java.lang.Object... parameters)
Return the single row result to this query, or null if no result.
|
static TableRowIterator |
queryTable(Context context,
java.lang.String table,
java.lang.String query,
java.lang.Object... parameters)
Return an iterator with the results of the query.
|
static TableRow |
row(Context context,
java.lang.String table)
Create a table row object that can be passed into the insert method, not
commonly used unless the table has a referential integrity constraint.
|
static TableRow |
row(java.lang.String table) |
static void |
setConstraintDeferred(Context context,
java.lang.String constraintName)
Set the constraint check to deferred (commit time)
|
static void |
setConstraintImmediate(Context context,
java.lang.String constraintName)
Set the constraint check to immediate (every query)
|
static void |
shutdown()
Provide a means for a (web) application to cleanly terminate the connection pool.
|
static int |
update(Context context,
TableRow row)
Update changes to the RDBMS.
|
static int |
updateQuery(Context context,
java.lang.String query,
java.lang.Object... parameters)
Execute an update, insert or delete query.
|
public static final java.lang.String DBMS_POSTGRES
public static final java.lang.String DBMS_ORACLE
public static final java.lang.String DBMS_H2
protected DatabaseManager()
public static boolean isOracle()
public static void setConstraintDeferred(Context context, java.lang.String constraintName) throws java.sql.SQLException
context - The context objectconstraintName - the constraint name to deferredjava.sql.SQLExceptionpublic static void setConstraintImmediate(Context context, java.lang.String constraintName) throws java.sql.SQLException
context - The context objectconstraintName - the constraint name to check immediately after every queryjava.sql.SQLExceptionpublic static TableRowIterator queryTable(Context context, java.lang.String table, java.lang.String query, java.lang.Object... parameters) throws java.sql.SQLException
context - The context objecttable - The name of the table which resultsquery - The SQL queryparameters - A set of SQL parameters to be included in query. The order of
the parameters must correspond to the order of their reference
within the query.java.sql.SQLException - If a database error occurspublic static TableRowIterator query(Context context, java.lang.String query, java.lang.Object... parameters) throws java.sql.SQLException
context - The context objectquery - The SQL queryparameters - A set of SQL parameters to be included in query. The order of
the parameters must correspond to the order of their reference
within the query.java.sql.SQLException - If a database error occurspublic static TableRow querySingle(Context context, java.lang.String query, java.lang.Object... parameters) throws java.sql.SQLException
context - Current DSpace contextquery - The SQL queryparameters - A set of SQL parameters to be included in query. The order of
the parameters must correspond to the order of their reference
within the query.java.sql.SQLException - If a database error occurspublic static TableRow querySingleTable(Context context, java.lang.String table, java.lang.String query, java.lang.Object... parameters) throws java.sql.SQLException
context - Current DSpace contexttable - The name of the table which resultsquery - The SQL queryparameters - A set of SQL parameters to be included in query. The order of
the parameters must correspond to the order of their reference
within the query.java.sql.SQLException - If a database error occurspublic static int updateQuery(Context context, java.lang.String query, java.lang.Object... parameters) throws java.sql.SQLException
context - Current DSpace contextquery - The SQL query to executeparameters - A set of SQL parameters to be included in query. The order of
the parameters must correspond to the order of their reference
within the query.java.sql.SQLException - If a database error occurspublic static TableRow create(Context context, java.lang.String table) throws java.sql.SQLException
context - Current DSpace contexttable - The RDBMS table in which to create the new rowjava.sql.SQLExceptionpublic static TableRow find(Context context, java.lang.String table, int id) throws java.sql.SQLException
context - Current DSpace contexttable - The table in which to find the rowid - The primary key valuejava.sql.SQLException - If a database error occurspublic static TableRow findByUnique(Context context, java.lang.String table, java.lang.String column, java.lang.Object value) throws java.sql.SQLException
context - Current DSpace contexttable - The table to use to find the objectcolumn - The name of the unique columnvalue - The value of the unique columnjava.sql.SQLException - If a database error occurspublic static int delete(Context context, java.lang.String table, int id) throws java.sql.SQLException
context - Current DSpace contexttable - The table to delete fromid - The primary key valuejava.sql.SQLException - If a database error occurspublic static int deleteByValue(Context context, java.lang.String table, java.lang.String column, java.lang.Object value) throws java.sql.SQLException
context - Current DSpace contexttable - The table to delete fromcolumn - The name of the columnvalue - The value of the columnjava.sql.SQLException - If a database error occurspublic static java.sql.Connection getConnection()
throws java.sql.SQLException
java.sql.SQLException - If a database error occurs, or a connection cannot be
obtained.public static javax.sql.DataSource getDataSource()
public static void freeConnection(java.sql.Connection c)
c - The connection to releasepublic static TableRow row(Context context, java.lang.String table) throws java.sql.SQLException
table - The RDBMS table in which to create the new rowjava.sql.SQLExceptionpublic static TableRow row(java.lang.String table) throws java.sql.SQLException
java.sql.SQLExceptionpublic static void insert(Context context, TableRow row) throws java.sql.SQLException
context - Current DSpace contextrow - The row to insertjava.sql.SQLException - If a database error occurspublic static int update(Context context, TableRow row) throws java.sql.SQLException
context - Current DSpace contextrow - The row to updatejava.sql.SQLException - If a database error occurspublic static int delete(Context context, TableRow row) throws java.sql.SQLException
context - Current DSpace contextrow - The row to deletejava.sql.SQLException - If a database error occurspublic static java.lang.String getPrimaryKeyColumn(Context context, TableRow row) throws java.sql.SQLException
row - The TableRow to return the primary key for.java.sql.SQLException - If a database error occursprotected static java.lang.String getPrimaryKeyColumn(Context context, java.lang.String table) throws java.sql.SQLException
table - The name of the RDBMS tablejava.sql.SQLException - If a database error occurspublic static void shutdown()
throws java.sql.SQLException
java.sql.SQLExceptionprotected static javax.sql.DataSource initDataSource()
throws java.sql.SQLException
While this is normally called via initialize() to create the globally shared DataSource, it also may be called individually just to test the Database Connection settings. This second use case often needs to avoid a full initialization/migration of the Database, which takes much longer and may not be necessary just for testing a basic connection. See, for example, DatabaseUtils.main().
java.sql.SQLException - if an initialization error occursprotected static java.lang.String findDbKeyword(java.sql.DatabaseMetaData meta)
throws java.sql.SQLException
This is mostly a utility method for initialize(), but also comes in handy when you want basic info about the Database but *don't* want to actually fully initialize the DatabaseManager (as it will also run all pending DB migrations)
meta - the DatabaseMetaDatajava.sql.SQLException - if an initialization error occurspublic static java.lang.String getDbName()
public static java.lang.String getDbKeyword()
This will return one of: DatabaseManager.DBMS_POSTGRES, DatabaseManager.DBMS_ORACLE, or DatabaseManager.DBMS_H2
protected static void loadParameters(java.sql.PreparedStatement statement,
java.lang.Object[] parameters)
throws java.sql.SQLException
statement - The unparameterized statement.parameters - The parameters to be set on the statement.java.sql.SQLExceptionpublic static void applyOffsetAndLimit(java.lang.StringBuffer query,
java.util.List<java.io.Serializable> params,
int offset,
int limit)
Copyright © 2018 DuraSpace. All Rights Reserved.