com.googlecode.fascinator.portal.services.impl
Class DatabaseServicesImpl

java.lang.Object
  extended by com.googlecode.fascinator.portal.services.impl.DatabaseServicesImpl
All Implemented Interfaces:
DatabaseServices, EventListener, org.apache.tapestry5.ioc.services.RegistryShutdownListener

public class DatabaseServicesImpl
extends Object
implements DatabaseServices

Instantiates a database used for persistence of generic data in the scripting layer, and offers utility functions for same.

Author:
Greg Pendlebury

Constructor Summary
DatabaseServicesImpl()
          Basic constructor, run by Tapestry through injection.
 
Method Summary
 void bindParam(PreparedStatement sql, int index, Object data)
          Bind a parameter to a SQL statement.
 Connection checkConnection(String database)
          Return a connection to the specified database, failing if it does not exist.
 void delete(String db, String index, String table, Map<String,Object> where)
          Top level wrapper for a delete statement.
 void execute(String db, String index, String sql, List<Object> fields)
          Top level wrapper to execute simple non-returning SQL, such as create or update statements.
 void free(PreparedStatement sql)
          Free the resources for a prepared statement.
 Connection getConnection(String database)
          Return a connection to the specified database.
 List<Map<String,String>> getResults(PreparedStatement sql)
          Parse the results of the query into a basic Java data structure.
 void insert(String db, String index, String table, Map<String,Object> fields)
          Top level wrapper for an insert statement.
 PreparedStatement prepare(Connection db, String index, String sql)
          Prepare and return an SQL statement, filing it under the provided index.
 void registryDidShutdown()
          Tapestry notification that server is shutting down
 List<Map<String,String>> select(String db, String index, String sql, List<Object> fields)
          Top level wrapper for a select statement.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DatabaseServicesImpl

public DatabaseServicesImpl()
Basic constructor, run by Tapestry through injection.

Method Detail

registryDidShutdown

public void registryDidShutdown()
Tapestry notification that server is shutting down

Specified by:
registryDidShutdown in interface org.apache.tapestry5.ioc.services.RegistryShutdownListener

checkConnection

public Connection checkConnection(String database)
                           throws Exception
Return a connection to the specified database, failing if it does not exist.

Specified by:
checkConnection in interface DatabaseServices
Parameters:
database - The name of the database to connect to.
Returns:
Connection The instantiated database connection or NULL.
Throws:
Exception - if there is a connection error.

getConnection

public Connection getConnection(String database)
                         throws Exception
Return a connection to the specified database. The database will be created if it does not exist.

Specified by:
getConnection in interface DatabaseServices
Parameters:
database - The name of the database to connect to.
Returns:
Connection The instantiated database connection, NULL if an error occurs.
Throws:
Exception - if there is a connection error.

prepare

public PreparedStatement prepare(Connection db,
                                 String index,
                                 String sql)
                          throws Exception
Prepare and return an SQL statement, filing it under the provided index. Subsequent calls to this function using the same index will return the previously prepared statement.

Specified by:
prepare in interface DatabaseServices
Parameters:
db - The database connection to use.
index - The index to store the statement under.
sql - The SQL statement to prepare.
Returns:
PreparedStatement The prepared statement.
Throws:
Exception - if there is an error.

bindParam

public void bindParam(PreparedStatement sql,
                      int index,
                      Object data)
               throws Exception
Bind a parameter to a SQL statement. All Java types should be acceptable, except NULL. Use 'IS NULL' in your SQL for this.

Specified by:
bindParam in interface DatabaseServices
Parameters:
sql - The prepared statement to bind to.
index - Specifies which placeholder to bind to (starts at 1).
data - The data to bind to that placeholder.
Throws:
Exception - if there is an error.

free

public void free(PreparedStatement sql)
          throws Exception
Free the resources for a prepared statement. For very commonly occurring statements this is not necessarily advised since DatabaseServices tracks all statements and will free them at server shutdown. The performance gains are useful from this approach IF you use the same query routinely.

Specified by:
free in interface DatabaseServices
Parameters:
sql - The prepared statement to release.
Throws:
Exception - if there is an error.

getResults

public List<Map<String,String>> getResults(PreparedStatement sql)
                                    throws Exception
Parse the results of the query into a basic Java data structure. Users wanting the original result set should call getResultSet() directly against the prepared statement.

Specified by:
getResults in interface DatabaseServices
Parameters:
sql - The prepared statement to get the results from.
Returns:
List> A list of result rows as key/value pairs in HashMaps
Throws:
Exception - if there is an error.

select

public List<Map<String,String>> select(String db,
                                       String index,
                                       String sql,
                                       List<Object> fields)
                                throws Exception
Top level wrapper for a select statement.

Specified by:
select in interface DatabaseServices
Parameters:
db - The database connection to use.
index - The index to file this statement under for caching.
sql - The sql string to execute.
fields - The data to bind against placeholders. NULL is valid.
Returns:
List> A list of result rows as key/value pairs in HashMaps
Throws:
Exception - if there is an error.

insert

public void insert(String db,
                   String index,
                   String table,
                   Map<String,Object> fields)
            throws Exception
Top level wrapper for an insert statement.

Specified by:
insert in interface DatabaseServices
Parameters:
db - The database connection to use.
index - The index to file this statement under for caching.
table - The name of the table to insert into.
fields - The data to insert, a map of .
Throws:
Exception - if there is an error.

delete

public void delete(String db,
                   String index,
                   String table,
                   Map<String,Object> where)
            throws Exception
Top level wrapper for a delete statement. Simple equality tests are possible for the where clause.

Specified by:
delete in interface DatabaseServices
Parameters:
db - The database connection to use.
index - The index to file this statement under for caching.
table - The name of the table to delete.
fields - The data to use in a where clause. key/value pairs
Throws:
Exception - if there is an error.

execute

public void execute(String db,
                    String index,
                    String sql,
                    List<Object> fields)
             throws Exception
Top level wrapper to execute simple non-returning SQL, such as create or update statements.

Specified by:
execute in interface DatabaseServices
Parameters:
db - The database connection to use.
index - The index to file this statement under for caching.
sql - The sql string to execute.
fields - The data to bind against placeholders. NULL is valid.
Throws:
Exception - if there is an error.


Copyright © 2009-2013. All Rights Reserved.