org.mycore.backend.sql
Class MCRSQLConnection

java.lang.Object
  extended by org.mycore.backend.sql.MCRSQLConnection

public class MCRSQLConnection
extends Object

Instances of this class represent a database connection to a relational database like DB2. MCRSQLConnection is a wrapper around java.sql.Connection and provides some convenience methods for easier SQL usage in MyCoRe. There are two types of methods in this class:

Author:
Frank Lützenkirchen, Johannes Buehler
See Also:
doQuery( String), Connection, MCRSQLConnectionPool

Field Summary
protected  Connection connection
          The wrapped JDBC connection
protected static Logger LOGGER
           
 
Constructor Summary
MCRSQLConnection()
          Creates a new connection.
 
Method Summary
(package private)  void close()
          Closes this connection to the underlying JDBC datastore.
(package private)  void closeJDBCConnection()
           
 int countRows(String condition)
          Executes an SQL "SELECT COUNT(*) FROM" statement on this connection, returning the number of rows that match the condition.
static boolean doesTableExist(String tablename)
          Checks existence of table
 MCRSQLRowReader doQuery(String query)
          Executes an SQL select statement on this connection.
 void doUpdate(String statement)
          Executes an SQL update statement on this connection.
 boolean exists(String condition)
          Checks if there are any matching rows for a given SQL condition by executing an SQL select statement on this connection.
 void finalize()
           
 Connection getJDBCConnection()
          Returns the underlying JDBC java.sql.Connection object
 String getSingleValue(String query)
          Executes an SQL select statement on this connection, where the expected result is just a single value of a row.
static boolean justCheckExists(String condition)
          Checks if there are any matching rows for a given SQL condition by executing an SQL select statement, using any currently free connection from the pool.
static int justCountRows(String condition)
          Executes an SQL "SELECT COUNT(*) FROM" statement, returning the number of rows that match the condition, using any currently free connection from the pool.
static void justDoUpdate(String statement)
          Executes an SQL update statement, using any currently free connection from the pool.
static String justGetSingleValue(String query)
          Executes an SQL select statement where the expected result is just a single value of a row, using any currently free connection from the pool.
 void release()
          Releases this connection back to the connection pool, indicating that it is no longer needed by the current task.
(package private)  void use()
           
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

connection

protected Connection connection
The wrapped JDBC connection


LOGGER

protected static Logger LOGGER
Constructor Detail

MCRSQLConnection

MCRSQLConnection()
           throws MCRPersistenceException
Creates a new connection. This constructor is used by the connection pool class.

Throws:
MCRPersistenceException
See Also:
MCRSQLConnectionPool.getConnection()
Method Detail

use

void use()

release

public void release()
Releases this connection back to the connection pool, indicating that it is no longer needed by the current task.

See Also:
MCRSQLConnectionPool.releaseConnection( MCRSQLConnection )

close

void close()
     throws MCRPersistenceException
Closes this connection to the underlying JDBC datastore. This is called when the connection pool is finalized.

Throws:
MCRPersistenceException
See Also:
MCRSQLConnectionPool.finalize()

closeJDBCConnection

void closeJDBCConnection()
                   throws MCRPersistenceException
Throws:
MCRPersistenceException

finalize

public void finalize()
Overrides:
finalize in class Object

getJDBCConnection

public Connection getJDBCConnection()
Returns the underlying JDBC java.sql.Connection object

Returns:
the underlying JDBC java.sql.Connection object

doQuery

public MCRSQLRowReader doQuery(String query)
                        throws MCRPersistenceException
Executes an SQL select statement on this connection. The results of the query are returned as MCRSQLRowReader instance.

Parameters:
query - the SQL select statement to be executed
Returns:
the MCRSQLRowReader that can be used for reading the result rows
Throws:
MCRPersistenceException

doUpdate

public void doUpdate(String statement)
              throws MCRPersistenceException
Executes an SQL update statement on this connection.

Parameters:
statement - the SQL create, insert or delete statement to be executed
Throws:
MCRPersistenceException

getSingleValue

public String getSingleValue(String query)
                      throws MCRPersistenceException
Executes an SQL select statement on this connection, where the expected result is just a single value of a row.

Parameters:
query - the SQL select statement to be executed
Returns:
the value of the first column of the first result row as a String
Throws:
MCRPersistenceException

countRows

public int countRows(String condition)
              throws MCRPersistenceException
Executes an SQL "SELECT COUNT(*) FROM" statement on this connection, returning the number of rows that match the condition.

Parameters:
condition - the SQL select statement to be executed, beginning at the SQL "FROM" keyword
Returns:
the number of matching rows, or 0 if no rows match
Throws:
MCRPersistenceException

exists

public boolean exists(String condition)
               throws MCRPersistenceException
Checks if there are any matching rows for a given SQL condition by executing an SQL select statement on this connection.

Parameters:
condition - the condition of an SQL select statement to be executed, beginning at the SQL "FROM" keyword
Returns:
true, if there are any rows matching this condition
Throws:
MCRPersistenceException

justDoUpdate

public static void justDoUpdate(String statement)
                         throws MCRPersistenceException
Executes an SQL update statement, using any currently free connection from the pool.

Parameters:
statement - the SQL create, insert or delete statement to be executed
Throws:
MCRPersistenceException

justGetSingleValue

public static String justGetSingleValue(String query)
                                 throws MCRPersistenceException
Executes an SQL select statement where the expected result is just a single value of a row, using any currently free connection from the pool.

Parameters:
query - the SQL select statement to be executed
Returns:
the value of the first column of the first result row as a String
Throws:
MCRPersistenceException

justCountRows

public static int justCountRows(String condition)
                         throws MCRPersistenceException
Executes an SQL "SELECT COUNT(*) FROM" statement, returning the number of rows that match the condition, using any currently free connection from the pool.

Parameters:
condition - the SQL select statement to be executed, beginning at the SQL "FROM" keyword
Returns:
the number of matching rows, or 0 if no rows match
Throws:
MCRPersistenceException

justCheckExists

public static boolean justCheckExists(String condition)
                               throws MCRPersistenceException
Checks if there are any matching rows for a given SQL condition by executing an SQL select statement, using any currently free connection from the pool.

Parameters:
condition - the condition of an SQL select statement to be executed, beginning at the SQL "FROM" keyword
Returns:
true, if there are any rows matching this condition
Throws:
MCRPersistenceException

doesTableExist

public static boolean doesTableExist(String tablename)
                              throws MCRPersistenceException
Checks existence of table

Parameters:
tablename -
Returns:
true or false
Throws:
MCRPersistenceException - if the JDBC driver could not be loaded or initial connections could not be created or can not get a connection