org.xerial.db.sql
Class DatabaseAccessBase

java.lang.Object
  extended by org.xerial.db.sql.DatabaseAccessBase
All Implemented Interfaces:
DatabaseAccess
Direct Known Subclasses:
MySQLAccess, PostgresAccess, SQLiteAccess

public class DatabaseAccessBase
extends Object
implements DatabaseAccess

A base implementation of the DatabaseAccess interface.

Author:
leo

Constructor Summary
DatabaseAccessBase(ConnectionPool connectionPool)
           
 
Method Summary
<T> T
accumulate(String sql, ResultSetHandler<T> handler)
          Accumulate the query result within the ResultSetHandler, then return the result from the handler
protected  Statement createStatement(Connection connection)
           
protected  String createValueTupleFromBean(String tableName, Object bean)
          Align the content of a bean object so that it matches with the corresponding relation (table schema) For example, give a bean class, e.g.
 void dispose()
          Close all connections managed by this database access
 ConnectionPool getConnectionPool()
           
 Relation getRelation(String tableName)
          Gets the relation (schema) information of the table.
 List<String> getTableNameList()
          Gets the table names contained in this database.
<T> int
insert(String tableName, T bean)
          Inserts a bean into a table
<T> void
query(String sql, BeanResultHandler<T> beanResultHandler)
          Performs an SQL query, while consuming the results with the given handler
<T> List<T>
query(String sql, Class<T> resultRowType)
          perforam a given SQL query, then output its results
<T> void
query(String sql, ResultSetHandler<T> pullHandler)
          Performs an SQL query, while consuming the results with the given handler
<T> List<T>
queryWithHandler(String sql, ResultSetHandler<T> handler)
           
 void setAutoCommit(boolean enableAutoCommit)
          Enable/disable auto commit mode for all subsequent queries.
 void setQueryTimeout(int sec)
          Sets the time of the query until it will be terminated in seconds
<T> List<T>
singleColumnQuery(String sql, String targetColumn, Class<T> resultColumnType)
          Retrieves only the single column from the SQL query result
<T> void
toJSON(String sql, Class<T> beanClass, Writer writer)
           
 int update(String sql)
          Performs the update query
 int update(String sql, boolean autoCommit)
          Performs the update query
 int updateWithPreparedStatement(String sqlForPreparedStatement, PreparedStatementHandler handler)
          Performs the update using prepared statement.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DatabaseAccessBase

public DatabaseAccessBase(ConnectionPool connectionPool)
                   throws DBException
Throws:
DBException
Method Detail

dispose

public void dispose()
             throws DBException
Description copied from interface: DatabaseAccess
Close all connections managed by this database access

Specified by:
dispose in interface DatabaseAccess
Throws:
DBException

createStatement

protected Statement createStatement(Connection connection)
                             throws SQLException
Throws:
SQLException

query

public <T> List<T> query(String sql,
                         Class<T> resultRowType)
              throws DBException
perforam a given SQL query, then output its results

Specified by:
query in interface DatabaseAccess
Type Parameters:
T - row type : Bean class type
Parameters:
sql - sql statement
resultRowType - it must be equal to the T
result -
Throws:
DBException

query

public <T> void query(String sql,
                      ResultSetHandler<T> pullHandler)
           throws DBException
Description copied from interface: DatabaseAccess
Performs an SQL query, while consuming the results with the given handler

Specified by:
query in interface DatabaseAccess
Parameters:
sql - the SQL query expression
pullHandler - the result set handler that consumes the result set of the query.
Throws:
DBException

queryWithHandler

public <T> List<T> queryWithHandler(String sql,
                                    ResultSetHandler<T> handler)
                         throws DBException
Throws:
DBException

accumulate

public <T> T accumulate(String sql,
                        ResultSetHandler<T> handler)
             throws DBException
Accumulate the query result within the ResultSetHandler, then return the result from the handler

Specified by:
accumulate in interface DatabaseAccess
Type Parameters:
T -
Parameters:
sql -
handler -
Returns:
Throws:
DBException

updateWithPreparedStatement

public int updateWithPreparedStatement(String sqlForPreparedStatement,
                                       PreparedStatementHandler handler)
                                throws DBException
Description copied from interface: DatabaseAccess
Performs the update using prepared statement. You can set the tuple values in the PreparedStatementHandler via PreparedStatement.setBytes(int, byte[]), etc.

Specified by:
updateWithPreparedStatement in interface DatabaseAccess
Parameters:
sqlForPreparedStatement - the update SQL statement that may contain '?' mark that will be filled in the PreparedStatementHandler
handler - the handler for filling '?' marks in the sql statement
Returns:
Throws:
DBException

update

public int update(String sql)
           throws DBException
Description copied from interface: DatabaseAccess
Performs the update query

Specified by:
update in interface DatabaseAccess
Parameters:
sql - the update SQL query
Returns:
the number of rows updated
Throws:
DBException

update

public int update(String sql,
                  boolean autoCommit)
           throws DBException
Description copied from interface: DatabaseAccess
Performs the update query

Specified by:
update in interface DatabaseAccess
Parameters:
sql - the update SQL query
autoCommit - true when enable auto commit arounc this update query, false otherwise
Returns:
the number of rows updated
Throws:
DBException

getConnectionPool

public ConnectionPool getConnectionPool()

setAutoCommit

public void setAutoCommit(boolean enableAutoCommit)
Description copied from interface: DatabaseAccess
Enable/disable auto commit mode for all subsequent queries.

Specified by:
setAutoCommit in interface DatabaseAccess

setQueryTimeout

public void setQueryTimeout(int sec)
Description copied from interface: DatabaseAccess
Sets the time of the query until it will be terminated in seconds

Specified by:
setQueryTimeout in interface DatabaseAccess
Parameters:
sec - timeout (sec.)

getRelation

public Relation getRelation(String tableName)
                     throws DBException
Description copied from interface: DatabaseAccess
Gets the relation (schema) information of the table.

Specified by:
getRelation in interface DatabaseAccess
Parameters:
tableName - the target table name
Returns:
the relation of the table
Throws:
DBException

getTableNameList

public List<String> getTableNameList()
                              throws DBException
Description copied from interface: DatabaseAccess
Gets the table names contained in this database.

Specified by:
getTableNameList in interface DatabaseAccess
Returns:
the list of table names
Throws:
DBException

singleColumnQuery

public <T> List<T> singleColumnQuery(String sql,
                                     String targetColumn,
                                     Class<T> resultColumnType)
                          throws DBException
Description copied from interface: DatabaseAccess
Retrieves only the single column from the SQL query result

Specified by:
singleColumnQuery in interface DatabaseAccess
targetColumn - the target column
resultColumnType - the target column type to be transformed
Returns:
the list of the specified column data
Throws:
DBException

query

public <T> void query(String sql,
                      BeanResultHandler<T> beanResultHandler)
           throws DBException
Description copied from interface: DatabaseAccess
Performs an SQL query, while consuming the results with the given handler

Specified by:
query in interface DatabaseAccess
Throws:
DBException

insert

public <T> int insert(String tableName,
                      T bean)
           throws DBException
Description copied from interface: DatabaseAccess
Inserts a bean into a table

Specified by:
insert in interface DatabaseAccess
Returns:
the number of rows updated (1 if succeeded)
Throws:
DBException

createValueTupleFromBean

protected String createValueTupleFromBean(String tableName,
                                          Object bean)
                                   throws DBException,
                                          BeanException
Align the content of a bean object so that it matches with the corresponding relation (table schema) For example, give a bean class, e.g. class Person { int id; String name; (getters are ommited) } and a table named person with a schema 'id, name', the createValueTupleFromBean("person", (a Person object)) will give a tuple representation of the Person object (id=1, name="leo"), that is '1,"leo"'. This returned string can be used as it is within an insert statement of the SQL, i.e., insert into person values(1, "leo")

Parameters:
tableName -
bean -
Returns:
Throws:
DBException
InvalidBeanException
BeanException

toJSON

public <T> void toJSON(String sql,
                       Class<T> beanClass,
                       Writer writer)
            throws DBException,
                   IOException
Specified by:
toJSON in interface DatabaseAccess
Throws:
DBException
IOException


Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 2.1 Japan License.