public class DatabaseManager extends Object
| Modifier and Type | Field and Description |
|---|---|
static String |
DBMS_H2 |
static String |
DBMS_ORACLE |
static 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(StringBuffer query,
List<Serializable> params,
int offset,
int limit) |
static TableRow |
create(Context context,
String table)
Create a new row in the given table, and assigns a unique id.
|
static int |
delete(Context context,
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,
String table,
String column,
Object value)
Delete all table rows with the given value.
|
static TableRow |
find(Context context,
String table,
int id)
Find a table row by its primary key.
|
static TableRow |
findByUnique(Context context,
String table,
String column,
Object value)
Find a table row by a unique value.
|
protected static String |
findDbKeyword(DatabaseMetaData meta)
Return the "DbKeyword" for a specific database name.
|
static void |
freeConnection(Connection c)
Release resources associated with this connection.
|
static Connection |
getConnection()
Obtain an RDBMS connection.
|
static DataSource |
getDataSource() |
static String |
getDbKeyword()
What is the string that we use to name the DBMS brand?
|
static String |
getDbName()
What is the name of our DBMS?
|
protected static String |
getPrimaryKeyColumn(String table)
Return the name of the primary key column in the given table.
|
static String |
getPrimaryKeyColumn(TableRow row)
Return the name of the primary key column.
|
protected static 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(PreparedStatement statement,
Object[] parameters)
Iterate over the given parameters and add them to the given prepared statement.
|
static TableRowIterator |
query(Context context,
String query,
Object... parameters)
Return an iterator with the results of the query.
|
static TableRow |
querySingle(Context context,
String query,
Object... parameters)
Return the single row result to this query, or null if no result.
|
static TableRow |
querySingleTable(Context context,
String table,
String query,
Object... parameters)
Return the single row result to this query, or null if no result.
|
static TableRowIterator |
queryTable(Context context,
String table,
String query,
Object... parameters)
Return an iterator with the results of the query.
|
static TableRow |
row(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 void |
setConstraintDeferred(Context context,
String constraintName)
Set the constraint check to deferred (commit time)
|
static void |
setConstraintImmediate(Context context,
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,
String query,
Object... parameters)
Execute an update, insert or delete query.
|
public static final String DBMS_POSTGRES
public static final String DBMS_ORACLE
public static final String DBMS_H2
protected DatabaseManager()
public static boolean isOracle()
public static void setConstraintDeferred(Context context, String constraintName) throws SQLException
context - The context objectconstraintName - the constraint name to deferredSQLExceptionpublic static void setConstraintImmediate(Context context, String constraintName) throws SQLException
context - The context objectconstraintName - the constraint name to check immediately after every querySQLExceptionpublic static TableRowIterator queryTable(Context context, String table, String query, Object... parameters) throws 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.SQLException - If a database error occurspublic static TableRowIterator query(Context context, String query, Object... parameters) throws 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.SQLException - If a database error occurspublic static TableRow querySingle(Context context, String query, Object... parameters) throws 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.SQLException - If a database error occurspublic static TableRow querySingleTable(Context context, String table, String query, Object... parameters) throws 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.SQLException - If a database error occurspublic static int updateQuery(Context context, String query, Object... parameters) throws 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.SQLException - If a database error occurspublic static TableRow create(Context context, String table) throws SQLException
context - Current DSpace contexttable - The RDBMS table in which to create the new rowSQLExceptionpublic static TableRow find(Context context, String table, int id) throws SQLException
context - Current DSpace contexttable - The table in which to find the rowid - The primary key valueSQLException - If a database error occurspublic static TableRow findByUnique(Context context, String table, String column, Object value) throws SQLException
context - Current DSpace contexttable - The table to use to find the objectcolumn - The name of the unique columnvalue - The value of the unique columnSQLException - If a database error occurspublic static int delete(Context context, String table, int id) throws SQLException
context - Current DSpace contexttable - The table to delete fromid - The primary key valueSQLException - If a database error occurspublic static int deleteByValue(Context context, String table, String column, Object value) throws SQLException
context - Current DSpace contexttable - The table to delete fromcolumn - The name of the columnvalue - The value of the columnSQLException - If a database error occurspublic static Connection getConnection() throws SQLException
SQLException - If a database error occurs, or a connection cannot be
obtained.public static DataSource getDataSource()
public static void freeConnection(Connection c)
c - The connection to releasepublic static TableRow row(String table) throws SQLException
table - The RDBMS table in which to create the new rowSQLExceptionpublic static void insert(Context context, TableRow row) throws SQLException
context - Current DSpace contextrow - The row to insertSQLException - If a database error occurspublic static int update(Context context, TableRow row) throws SQLException
context - Current DSpace contextrow - The row to updateSQLException - If a database error occurspublic static int delete(Context context, TableRow row) throws SQLException
context - Current DSpace contextrow - The row to deleteSQLException - If a database error occurspublic static String getPrimaryKeyColumn(TableRow row) throws SQLException
row - The TableRow to return the primary key for.SQLException - If a database error occursprotected static String getPrimaryKeyColumn(String table) throws SQLException
table - The name of the RDBMS tableSQLException - If a database error occurspublic static void shutdown()
throws SQLException
SQLExceptionprotected static DataSource initDataSource() throws 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().
SQLException - if an initialization error occursprotected static String findDbKeyword(DatabaseMetaData meta) throws 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 DatabaseMetaDataSQLException - if an initialization error occurspublic static String getDbName()
public static String getDbKeyword()
This will return one of: DatabaseManager.DBMS_POSTGRES, DatabaseManager.DBMS_ORACLE, or DatabaseManager.DBMS_H2
protected static void loadParameters(PreparedStatement statement, Object[] parameters) throws SQLException
statement - The unparameterized statement.parameters - The parameters to be set on the statement.SQLExceptionpublic static void applyOffsetAndLimit(StringBuffer query, List<Serializable> params, int offset, int limit)
Copyright © 2014 DuraSpace. All Rights Reserved.