org.dspace.storage.rdbms
Class DatabaseManager

java.lang.Object
  extended by org.dspace.storage.rdbms.DatabaseManager

public class DatabaseManager
extends Object

Executes SQL queries.

Version:
$Revision: 4658 $
Author:
Peter Breton, Jim Downing

Constructor Summary
protected DatabaseManager()
          Protected Constructor to prevent instantiation except by derived classes.
 
Method Summary
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.
static void freeConnection(Connection c)
          Release resources associated with this connection.
protected static List getColumnNames(ResultSetMetaData meta)
          Return the names of all the columns of the ResultSet.
protected static List getColumnNames(String table)
          Return the names of all the columns of the given table.
static Connection getConnection()
          Obtain an RDBMS connection.
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.
static void insert(Context context, TableRow row)
          Insert a table row into the RDBMS.
protected static void loadParameters(PreparedStatement statement, Object[] parameters)
          Iterate over the given parameters and add them to the given prepared statement.
static void loadSql(Reader r)
          Load SQL from a reader into the RDBMS.
static void loadSql(String sql)
          Load SQL into the RDBMS.
static void main(String[] args)
          Main method used to perform tests on the database
static TableRowIterator query(Context context, String query, Object... parameters)
          Return an iterator with the results of the query.
static TableRowIterator queryPrepared(PreparedStatement statement)
          Return an iterator with the results of executing statement.
static TableRowIterator queryPreparedTable(String table, PreparedStatement statement)
          Return an iterator with the results of executing statement.
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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DatabaseManager

protected DatabaseManager()
Protected Constructor to prevent instantiation except by derived classes.

Method Detail

setConstraintDeferred

public static void setConstraintDeferred(Context context,
                                         String constraintName)
                                  throws SQLException
Set the constraint check to deferred (commit time)

Parameters:
context - The context object
constraintName - the constraint name to deferred
Throws:
SQLException

setConstraintImmediate

public static void setConstraintImmediate(Context context,
                                          String constraintName)
                                   throws SQLException
Set the constraint check to immediate (every query)

Parameters:
context - The context object
constraintName - the constraint name to check immediately after every query
Throws:
SQLException

queryTable

public static TableRowIterator queryTable(Context context,
                                          String table,
                                          String query,
                                          Object... parameters)
                                   throws SQLException
Return an iterator with the results of the query. The table parameter indicates the type of result. If table is null, the column names are read from the ResultSetMetaData.

Parameters:
context - The context object
table - The name of the table which results
query - The SQL query
parameters - 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.
Returns:
A TableRowIterator with the results of the query
Throws:
SQLException - If a database error occurs

query

public static TableRowIterator query(Context context,
                                     String query,
                                     Object... parameters)
                              throws SQLException
Return an iterator with the results of the query.

Parameters:
context - The context object
query - The SQL query
parameters - 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.
Returns:
A TableRowIterator with the results of the query
Throws:
SQLException - If a database error occurs

queryPreparedTable

public static TableRowIterator queryPreparedTable(String table,
                                                  PreparedStatement statement)
                                           throws SQLException
Return an iterator with the results of executing statement. The table parameter indicates the type of result. If table is null, the column names are read from the ResultSetMetaData. The context is that of the connection which was used to create the statement.

Parameters:
statement - The prepared statement to execute.
table - The name of the table which results
Returns:
A TableRowIterator with the results of the query
Throws:
SQLException - If a database error occurs

queryPrepared

public static TableRowIterator queryPrepared(PreparedStatement statement)
                                      throws SQLException
Return an iterator with the results of executing statement. The context is that of the connection which was used to create the statement.

Parameters:
statement - The prepared statement to execute.
Returns:
A TableRowIterator with the results of the query
Throws:
SQLException - If a database error occurs

querySingle

public static TableRow querySingle(Context context,
                                   String query,
                                   Object... parameters)
                            throws SQLException
Return the single row result to this query, or null if no result. If more than one row results, only the first is returned.

Parameters:
context - Current DSpace context
query - The SQL query
parameters - 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.
Returns:
A TableRow object, or null if no result
Throws:
SQLException - If a database error occurs

querySingleTable

public static TableRow querySingleTable(Context context,
                                        String table,
                                        String query,
                                        Object... parameters)
                                 throws SQLException
Return the single row result to this query, or null if no result. If more than one row results, only the first is returned.

Parameters:
context - Current DSpace context
table - The name of the table which results
query - The SQL query
parameters - 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.
Returns:
A TableRow object, or null if no result
Throws:
SQLException - If a database error occurs

updateQuery

public static int updateQuery(Context context,
                              String query,
                              Object... parameters)
                       throws SQLException
Execute an update, insert or delete query. Returns the number of rows affected by the query.

Parameters:
context - Current DSpace context
query - The SQL query to execute
parameters - 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.
Returns:
The number of rows affected by the query.
Throws:
SQLException - If a database error occurs

create

public static TableRow create(Context context,
                              String table)
                       throws SQLException
Create a new row in the given table, and assigns a unique id.

Parameters:
context - Current DSpace context
table - The RDBMS table in which to create the new row
Returns:
The newly created row
Throws:
SQLException

find

public static TableRow find(Context context,
                            String table,
                            int id)
                     throws SQLException
Find a table row by its primary key. Returns the row, or null if no row with that primary key value exists.

Parameters:
context - Current DSpace context
table - The table in which to find the row
id - The primary key value
Returns:
The row resulting from the query, or null if no row with that primary key value exists.
Throws:
SQLException - If a database error occurs

findByUnique

public static TableRow findByUnique(Context context,
                                    String table,
                                    String column,
                                    Object value)
                             throws SQLException
Find a table row by a unique value. Returns the row, or null if no row with that primary key value exists. If multiple rows with the value exist, one is returned.

Parameters:
context - Current DSpace context
table - The table to use to find the object
column - The name of the unique column
value - The value of the unique column
Returns:
The row resulting from the query, or null if no row with that value exists.
Throws:
SQLException - If a database error occurs

delete

public static int delete(Context context,
                         String table,
                         int id)
                  throws SQLException
Delete a table row via its primary key. Returns the number of rows deleted.

Parameters:
context - Current DSpace context
table - The table to delete from
id - The primary key value
Returns:
The number of rows deleted
Throws:
SQLException - If a database error occurs

deleteByValue

public static int deleteByValue(Context context,
                                String table,
                                String column,
                                Object value)
                         throws SQLException
Delete all table rows with the given value. Returns the number of rows deleted.

Parameters:
context - Current DSpace context
table - The table to delete from
column - The name of the column
value - The value of the column
Returns:
The number of rows deleted
Throws:
SQLException - If a database error occurs

getConnection

public static Connection getConnection()
                                throws SQLException
Obtain an RDBMS connection.

Returns:
A new database connection.
Throws:
SQLException - If a database error occurs, or a connection cannot be obtained.

freeConnection

public static void freeConnection(Connection c)
Release resources associated with this connection.

Parameters:
c - The connection to release

row

public static TableRow row(String table)
                    throws SQLException
Create a table row object that can be passed into the insert method, not commonly used unless the table has a referential integrity constraint.

Parameters:
table - The RDBMS table in which to create the new row
Returns:
The newly created row
Throws:
SQLException

insert

public static void insert(Context context,
                          TableRow row)
                   throws SQLException
Insert a table row into the RDBMS.

Parameters:
context - Current DSpace context
row - The row to insert
Throws:
SQLException - If a database error occurs

update

public static int update(Context context,
                         TableRow row)
                  throws SQLException
Update changes to the RDBMS. Note that if the update fails, the values in the row will NOT be reverted.

Parameters:
context - Current DSpace context
row - The row to update
Returns:
The number of rows affected (1 or 0)
Throws:
SQLException - If a database error occurs

delete

public static int delete(Context context,
                         TableRow row)
                  throws SQLException
Delete row from the RDBMS.

Parameters:
context - Current DSpace context
row - The row to delete
Returns:
The number of rows affected (1 or 0)
Throws:
SQLException - If a database error occurs

getColumnNames

protected static List getColumnNames(String table)
                              throws SQLException
Return the names of all the columns of the given table.

Parameters:
table - The name of the table
Returns:
The names of all the columns of the given table, as a List. Each element of the list is a String.
Throws:
SQLException - If a database error occurs

getColumnNames

protected static List getColumnNames(ResultSetMetaData meta)
                              throws SQLException
Return the names of all the columns of the ResultSet.

Parameters:
meta - The ResultSetMetaData
Returns:
The names of all the columns of the given table, as a List. Each element of the list is a String.
Throws:
SQLException - If a database error occurs

loadSql

public static void loadSql(String sql)
                    throws SQLException
Load SQL into the RDBMS.

Parameters:
sql - The SQL to load. throws SQLException If a database error occurs
Throws:
SQLException

loadSql

public static void loadSql(Reader r)
                    throws SQLException,
                           IOException
Load SQL from a reader into the RDBMS.

Parameters:
r - The Reader from which to read the SQL.
Throws:
SQLException - If a database error occurs
IOException - If an error occurs obtaining data from the reader

getPrimaryKeyColumn

public static String getPrimaryKeyColumn(TableRow row)
                                  throws SQLException
Return the name of the primary key column. We assume there's only one primary key per table; if there are more, only the first one will be returned.

Parameters:
row - The TableRow to return the primary key for.
Returns:
The name of the primary key column, or null if the row has no primary key.
Throws:
SQLException - If a database error occurs

getPrimaryKeyColumn

protected static String getPrimaryKeyColumn(String table)
                                     throws SQLException
Return the name of the primary key column in the given table. We assume there's only one primary key per table; if there are more, only the first one will be returned.

Parameters:
table - The name of the RDBMS table
Returns:
The name of the primary key column, or null if the table has no primary key.
Throws:
SQLException - If a database error occurs

shutdown

public static void shutdown()
                     throws SQLException
Provide a means for a (web) application to cleanly terminate the connection pool.

Throws:
SQLException

loadParameters

protected static void loadParameters(PreparedStatement statement,
                                     Object[] parameters)
                              throws SQLException
Iterate over the given parameters and add them to the given prepared statement. Only a select number of datatypes are supported by the JDBC driver.

Parameters:
statement - The unparameterized statement.
parameters - The parameters to be set on the statement.
Throws:
SQLException

main

public static void main(String[] args)
Main method used to perform tests on the database

Parameters:
args - The command line arguments


Copyright © 2010 DuraSpace. All Rights Reserved.