| java.lang.Object biz.hammurapi.sql.SQLProcessor
SQLProcessor | public class SQLProcessor (Code) | | This class contains methods to process SQL statements in more convenient
way comparing to standard JDBC.
author: Pavel Vlasov version: $Revision: 1.14 $ |
Inner Class :interface Person | |
Constructor Summary | |
public | SQLProcessor(DataSource dataSource, Context nameMap) If SQLProcessor constructed with this constructor
then it obtains connection from the datasource, processes
request and closes connection. | public | SQLProcessor(Connection connection, Context nameMap) If SQLProcessor created with this constructor then is doesn't
close the connection after processing. |
Method Summary | |
public void | executeScript(Reader reader) | public void | executeScript(Reader reader, char statementSeparator) | public void | executeScript(Reader reader, char statementSeparator, ExceptionSink exceptionSink) Parameters: reader - Script source Parameters: statementSeparator - Statement separator Parameters: exceptionSink - Exception sink. | public void | executeTransaction(Transaction transaction) | public static String | findProjectSingleMethodName(String className) Finds projectSingleXXX method for a particular type. | public Connection | getConnection() | protected DataSource | getDataSource() | public Context | getNameMap() | public TimeIntervalCategory | getTimeIntervalCategory() | public void | inject(String sql, Parameterizer parameterizer, Map columnMap, Object target) Executes query and injects values from the first row to target object. | public static void | main(String[] args) | public int | nextPK(String primaryKeysTable, String keyName) Generates primary key.
Parameters: primaryKeysTable - Table holding primary keys counters. | public int | nextPK(Connection con, String primaryKeysTable, String keyName) | public String | parse(String str) Replaces ${} with property value. | public void | processResourceSelect(String resourceName, Parameterizer parameterizer, RowProcessor rowProcessor) Processes SELECT statement read from resource file. | public int | processResourceUpdate(String resourceName, Parameterizer parameterizer) Processes SELECT statement read from resource file. | public void | processSelect(String sql, Parameterizer parameterizer, RowProcessor rowProcessor) | public int | processUpdate(String sql, Parameterizer parameterizer) Processes SQL INSERT, UPDATE or DELETE statement in the following way:
- Obtains connection
- If parameterizer==null creates Statement, creates PreparedStatement otherwise
- Invokes parameterizer.parameterize() if parameterizer is not null
- Executes update
- ResultSet, Statement and connection are properly released
Parameters: sql - SQL statment to execute Parameters: parameterizer - Parameterizer Number of updates. | public Collection | project(String sql, Parameterizer parameterizer, Projector projector) Executes SQL statement and returns collection backed by the database.
Parameters: sql - Select statement to execute Parameters: parameterizer - Parameterizer Parameters: projector - Projector which instantiates objects. | public Collection | project(String sql, Parameterizer parameterizer) Executes SQL statement and returns collection backed by the database.
Rows are projected to collection of field values. | public Collection | project(String sql) Executes SQL statement and returns collection backed by the database.
Parameters: sql - Select statement to execute Collection backed by the database. | public Collection | project(String sql, Parameterizer parameterizer, Class theInterface) Executes SQL statement and returns collection backed by the database.
Parameters: sql - Select statement to execute Parameters: parameterizer - Parameterizer Parameters: theInterface - Iterface to implement Collection backed by the database. | public Collection | project(String sql, Parameterizer parameterizer, Class theInterface, Object delegate) Executes SQL statement and returns collection backed by the database.
Parameters: sql - Select statement to execute Parameters: parameterizer - Parameterizer Parameters: theInterface - Interface to implement Collection backed by the database. | public Collection | project(String sql, Parameterizer parameterizer, Projector projector, int pageNum, int pageSize) Executes SQL statement and returns collection backed by the database.
Parameters: sql - Select statement to execute Parameters: parameterizer - Parameterizer Parameters: projector - Projector which instantiates objects. | public Collection | project(String sql, Parameterizer parameterizer, Class theInterface, int pageNum, int pageSize) Executes SQL statement and returns collection backed by the database.
Parameters: sql - Select statement to execute Parameters: parameterizer - Parameterizer Parameters: theInterface - Interface to implement Parameters: pageSize - Maximum number of records to return Parameters: pageNum - Number of page. | public Collection | project(String sql, Parameterizer parameterizer, Class theInterface, Object delegate, int pageNum, int pageSize) Executes SQL statement and returns collection backed by the database.
Parameters: sql - Select statement to execute Parameters: parameterizer - Parameterizer Parameters: theInterface - Interface to implement Parameters: delegate - Object to delegate invocations which didn't match field getters/setters Parameters: pageSize - Maximum number of records to return Parameters: pageNum - Number of page. | public Collection | project(String sql, Parameterizer parameterizer, Projector projector, Collection receiver) Executes SQL statement and puts results to receiver
Parameters: sql - Select statement to execute Parameters: parameterizer - Parameterizer Parameters: projector - Projector which instantiates objects. | public Collection | project(String sql, Parameterizer parameterizer, Class theInterface, Collection receiver) Executes SQL statement and puts results to receiver
Parameters: sql - Select statement to execute Parameters: parameterizer - Parameterizer Parameters: theInterface - Interface to implement Parameters: receiver - Collection to put results to receiver with added objects. | public Collection | project(String sql, Parameterizer parameterizer, Class theInterface, Object delegate, Collection receiver) Executes SQL statement and puts results to receiver
Parameters: sql - Select statement to execute Parameters: parameterizer - Parameterizer Parameters: theInterface - Interface to implement Parameters: delegate - Object to delegate invocations which didn't match field getters/setters. Parameters: receiver - Collection to put results to receiver with added objects. | public Collection | project(String sql, Parameterizer parameterizer, Projector projector, Collection receiver, int pageSize, int pageNum) Executes SQL statement and puts results to receiver
Parameters: sql - Select statement to execute Parameters: parameterizer - Parameterizer Parameters: projector - Projector which instantiates objects. | public boolean | projectSingleBoolean(String sql, Parameterizer parameterizer) | public byte | projectSingleByte(String sql, Parameterizer parameterizer) | public byte[] | projectSingleBytes(String sql, Parameterizer parameterizer) | public char | projectSingleChar(String sql, Parameterizer parameterizer) | public double | projectSingleDouble(String sql, Parameterizer parameterizer) | public float | projectSingleFloat(String sql, Parameterizer parameterizer) | public int | projectSingleInt(String sql, Parameterizer parameterizer) | public long | projectSingleLong(String sql, Parameterizer parameterizer) | public Object | projectSingleObject(String sql, Parameterizer parameterizer, Projector projector) Parameters: string - Parameters: parameterizer - Parameters: projector - Projector. | public Object | projectSingleObject(String sql, Parameterizer parameterizer, Class theInterface) | public Object | projectSingleObject(String sql, Parameterizer parameterizer) | public Object | projectSingleObject(String sql, Parameterizer parameterizer, Class theInterface, Object delegate) | public short | projectSingleShort(String sql, Parameterizer parameterizer) | public String | projectSingleString(String sql, Parameterizer parameterizer) | public void | releaseConnection(Connection connection) Closes connection if it was provided by DataSource. | public void | setTimeIntervalCategory(TimeIntervalCategory timeIntervalCategory) |
SQLProcessor | public SQLProcessor(DataSource dataSource, Context nameMap)(Code) | | If SQLProcessor constructed with this constructor
then it obtains connection from the datasource, processes
request and closes connection.
Parameters: dataSource - DataSource Parameters: nameMap - NameMap allows to write parameterized SQL statementslike "SELECT ${AMOUNT} FROM ${ACCOUNT} WHERE ${ANUM}=? AND CLOSED=1"nameMap shall contain mapping from AMOUNT, ACCOUNT and ANUM to actualdatabase field names. If nameMap doesn't contain mapping for some properties then property names will be used as property values. See biz.hammurapi.config.PropertyParser.One property value can contain a reference to another property.If nameMap is null then no property parsing will happen. |
executeScript | public void executeScript(Reader reader, char statementSeparator) throws IOException, SQLException(Code) | | Executes series of SQL statement read from reader and separated by statementSeparator
Parameters: reader - Script source Parameters: statementSeparator - Statement separator throws: IOException - throws: SQLException - |
executeScript | public void executeScript(Reader reader, char statementSeparator, ExceptionSink exceptionSink) throws IOException, SQLException(Code) | | Parameters: reader - Script source Parameters: statementSeparator - Statement separator Parameters: exceptionSink - Exception sink. Consumes exceptions thrown by individual statements. If sink is null then exception isrethrown and script execution terminates. throws: IOException - throws: SQLException - |
findProjectSingleMethodName | public static String findProjectSingleMethodName(String className)(Code) | | Finds projectSingleXXX method for a particular type.
Parameters: className - |
inject | public void inject(String sql, Parameterizer parameterizer, Map columnMap, Object target) throws SQLException(Code) | | Executes query and injects values from the first row to target object.
Parameters: string - Parameters: parameterizer - Parameters: target - Object to inject values to throws: SQLException - |
nextPK | public int nextPK(String primaryKeysTable, String keyName) throws SQLException(Code) | | Generates primary key.
Parameters: primaryKeysTable - Table holding primary keys counters. DDL: CREATE TABLE table name (KEY_NAME VARCHAR(50) NOT NULL, KEY_VALUE INTEGER DEFAULT '0' NOT NULL, PRIMARY KEY (KEY_NAME)); Parameters: keyName - Key name throws: SQLException - |
processResourceSelect | public void processResourceSelect(String resourceName, Parameterizer parameterizer, RowProcessor rowProcessor) throws SQLException, IOException(Code) | | Processes SELECT statement read from resource file. Suitable for huge SQL statements or SQL statements
maintained by other developers.
Parameters: resourceName - Parameters: parameterizer - Parameters: rowProcessor - throws: SQLException - throws: IOException - |
processSelect | public void processSelect(String sql, Parameterizer parameterizer, RowProcessor rowProcessor) throws SQLException(Code) | | Processes SQL SELECT statement in the following way:
- Obtains connection
- If parameterizer==null creates Statement, creates PreparedStatement otherwise
- Invokes parameterizer.parameterize() if parameterizer is not null
- Iterates through result set and invokes rowProcessor.process() on each row
- If there was no rows and rowProcess is instance of
RowProcessorEx then rowProcessor.onEmptyResultSet() is invoked
- ResultSet, Statement and connection are properly released
Parameters: sql - SQL statment to execute Parameters: parameterizer - Parameterizer Parameters: rowProcessor - RowProcessor throws: SQLException - |
processUpdate | public int processUpdate(String sql, Parameterizer parameterizer) throws SQLException(Code) | | Processes SQL INSERT, UPDATE or DELETE statement in the following way:
- Obtains connection
- If parameterizer==null creates Statement, creates PreparedStatement otherwise
- Invokes parameterizer.parameterize() if parameterizer is not null
- Executes update
- ResultSet, Statement and connection are properly released
Parameters: sql - SQL statment to execute Parameters: parameterizer - Parameterizer Number of updates. See Statement.executeUpdate(java.lang.String) throws: SQLException - |
project | public Collection project(String sql, Parameterizer parameterizer, Projector projector)(Code) | | Executes SQL statement and returns collection backed by the database.
Parameters: sql - Select statement to execute Parameters: parameterizer - Parameterizer Parameters: projector - Projector which instantiates objects. It is null then projectorwhich projects row to collection will be used. Collection backed by the database. The collection doesn't holdany SQL resources open, neither it keeps any refernces to created objects.It keeps only references to the SQLProcessor, parameterizerand projector. Thus any call to one of collection methods retursn 'fresh' results from the database.Iterators created by this collection open ResultSet and close it whenIterator.hasNext() returns false. |
project | public Collection project(String sql, Parameterizer parameterizer)(Code) | | Executes SQL statement and returns collection backed by the database.
Rows are projected to collection of field values.
Parameters: sql - Select statement to execute Parameters: parameterizer - Parameterizer Parameters: projector - Projector which instantiates objects. It is null then projectorwhich projects row to collection will be used. Collection backed by the database. The collection doesn't holdany SQL resources open, neither it keeps any refernces to created objects.It keeps only references to the SQLProcessor and parameterizer. Thus any call to one of collection methods retursn 'fresh' results from the database.Iterators created by this collection open ResultSet and close it whenIterator.hasNext() returns false. |
project | public Collection project(String sql)(Code) | | Executes SQL statement and returns collection backed by the database.
Parameters: sql - Select statement to execute Collection backed by the database. The collection doesn't holdany SQL resources open, neither it keeps any refernces to created objects.It keeps only references to the SQLProcessor. Thus any call to one of collection methods retursn 'fresh' results from the database.Iterators created by this collection open ResultSet and close it whenIterator.hasNext() returns false. |
project | public Collection project(String sql, Parameterizer parameterizer, Class theInterface)(Code) | | Executes SQL statement and returns collection backed by the database.
Parameters: sql - Select statement to execute Parameters: parameterizer - Parameterizer Parameters: theInterface - Iterface to implement Collection backed by the database. The collection doesn't holdany SQL resources open, neither it keeps any refernces to created objects.It keeps only references to the SQLProcessor, parameterizerand projector. Thus any call to one of collection methods retursn 'fresh' results from the database.Iterators created by this collection open ResultSet and close it whenIterator.hasNext() returns false. |
project | public Collection project(String sql, Parameterizer parameterizer, Class theInterface, Object delegate)(Code) | | Executes SQL statement and returns collection backed by the database.
Parameters: sql - Select statement to execute Parameters: parameterizer - Parameterizer Parameters: theInterface - Interface to implement Collection backed by the database. The collection doesn't holdany SQL resources open, neither it keeps any refernces to created objects.It keeps only references to the SQLProcessor, parameterizerand projector. Thus any call to one of collection methods retursn 'fresh' results from the database.Iterators created by this collection open ResultSet and close it whenIterator.hasNext() returns false. |
project | public Collection project(String sql, Parameterizer parameterizer, Projector projector, int pageNum, int pageSize)(Code) | | Executes SQL statement and returns collection backed by the database.
Parameters: sql - Select statement to execute Parameters: parameterizer - Parameterizer Parameters: projector - Projector which instantiates objects. If it is null then projector which projects row to collection will be used Parameters: pageSize - Maximum number of records to return Parameters: pageNum - Number of page. Starts with 1. Collection backed by the database. The collection doesn't holdany SQL resources open, neither it keeps any refernces to created objects.It keeps only references to the SQLProcessor, parameterizerand projector. Thus any call to one of collection methods retursn 'fresh' results from the database.Iterators created by this collection open ResultSet and close it whenIterator.hasNext() returns false. |
project | public Collection project(String sql, Parameterizer parameterizer, Class theInterface, int pageNum, int pageSize)(Code) | | Executes SQL statement and returns collection backed by the database.
Parameters: sql - Select statement to execute Parameters: parameterizer - Parameterizer Parameters: theInterface - Interface to implement Parameters: pageSize - Maximum number of records to return Parameters: pageNum - Number of page. Starts with 1. Collection backed by the database. The collection doesn't holdany SQL resources open, neither it keeps any refernces to created objects.It keeps only references to the SQLProcessor, parameterizerand projector. Thus any call to one of collection methods retursn 'fresh' results from the database.Iterators created by this collection open ResultSet and close it whenIterator.hasNext() returns false. |
project | public Collection project(String sql, Parameterizer parameterizer, Class theInterface, Object delegate, int pageNum, int pageSize)(Code) | | Executes SQL statement and returns collection backed by the database.
Parameters: sql - Select statement to execute Parameters: parameterizer - Parameterizer Parameters: theInterface - Interface to implement Parameters: delegate - Object to delegate invocations which didn't match field getters/setters Parameters: pageSize - Maximum number of records to return Parameters: pageNum - Number of page. Starts with 1. Collection backed by the database. The collection doesn't holdany SQL resources open, neither it keeps any refernces to created objects.It keeps only references to the SQLProcessor, parameterizerand projector. Thus any call to one of collection methods retursn 'fresh' results from the database.Iterators created by this collection open ResultSet and close it whenIterator.hasNext() returns false. |
project | public Collection project(String sql, Parameterizer parameterizer, Projector projector, Collection receiver) throws SQLException(Code) | | Executes SQL statement and puts results to receiver
Parameters: sql - Select statement to execute Parameters: parameterizer - Parameterizer Parameters: projector - Projector which instantiates objects. If it is null then projector which projects row to collection will be used Parameters: receiver - Collection to put results to receiver with added objects. Convenient for calls like Iterator it=processor.project(..., new LinkedList()); throws: SQLException - |
project | public Collection project(String sql, Parameterizer parameterizer, Class theInterface, Collection receiver) throws SQLException(Code) | | Executes SQL statement and puts results to receiver
Parameters: sql - Select statement to execute Parameters: parameterizer - Parameterizer Parameters: theInterface - Interface to implement Parameters: receiver - Collection to put results to receiver with added objects. Convenient for calls like Iterator it=processor.project(..., new LinkedList()); throws: SQLException - |
project | public Collection project(String sql, Parameterizer parameterizer, Class theInterface, Object delegate, Collection receiver) throws SQLException(Code) | | Executes SQL statement and puts results to receiver
Parameters: sql - Select statement to execute Parameters: parameterizer - Parameterizer Parameters: theInterface - Interface to implement Parameters: delegate - Object to delegate invocations which didn't match field getters/setters. Parameters: receiver - Collection to put results to receiver with added objects. Convenient for calls like Iterator it=processor.project(..., new LinkedList()); throws: SQLException - |
project | public Collection project(String sql, Parameterizer parameterizer, Projector projector, Collection receiver, int pageSize, int pageNum) throws SQLException(Code) | | Executes SQL statement and puts results to receiver
Parameters: sql - Select statement to execute Parameters: parameterizer - Parameterizer Parameters: projector - Projector which instantiates objects. If it is null thenprojector which projects row to collection will be used. Parameters: receiver - Collection to put results to Parameters: pageSize - Maximum number of records to return Parameters: pageNum - Number of page. Starts with 1. receiver with added objects. Convenient for calls like Iterator it=processor.project(..., new LinkedList()); throws: SQLException - |
projectSingleBoolean | public boolean projectSingleBoolean(String sql, Parameterizer parameterizer) throws SQLException(Code) | | Parameters: sql - Parameters: parameterizer - boolean value of the first column of the first row or 'false' if there are no rows. throws: SQLException - |
projectSingleByte | public byte projectSingleByte(String sql, Parameterizer parameterizer) throws SQLException(Code) | | Parameters: sql - Parameters: parameterizer - byte value of the first column of the first row or 0 if there are no rows. throws: SQLException - |
projectSingleBytes | public byte[] projectSingleBytes(String sql, Parameterizer parameterizer) throws SQLException(Code) | | Parameters: sql - Parameters: parameterizer - byte[] value of the first column of the first row or null if there are no rows. throws: SQLException - |
projectSingleChar | public char projectSingleChar(String sql, Parameterizer parameterizer) throws SQLException(Code) | | Parameters: sql - Parameters: parameterizer - char value of the first column of the first row or 0 if there are no rows. throws: SQLException - |
projectSingleDouble | public double projectSingleDouble(String sql, Parameterizer parameterizer) throws SQLException(Code) | | Parameters: sql - Parameters: parameterizer - double value of the first column of the first row or 0 if there are no rows. throws: SQLException - |
projectSingleFloat | public float projectSingleFloat(String sql, Parameterizer parameterizer) throws SQLException(Code) | | Parameters: sql - Parameters: parameterizer - float value of the first column of the first row or 0 if there are no rows. throws: SQLException - |
projectSingleInt | public int projectSingleInt(String sql, Parameterizer parameterizer) throws SQLException(Code) | | Parameters: sql - Parameters: parameterizer - int value of the first column of the first row or 0 if there are no rows. throws: SQLException - |
projectSingleLong | public long projectSingleLong(String sql, Parameterizer parameterizer) throws SQLException(Code) | | Parameters: sql - Parameters: parameterizer - long value of the first column of the first row or 0 if there are no rows. throws: SQLException - |
projectSingleObject | public Object projectSingleObject(String sql, Parameterizer parameterizer, Projector projector) throws SQLException(Code) | | Parameters: string - Parameters: parameterizer - Parameters: projector - Projector. If it is null then projector which projects row to collection will be used. throws: SQLException - |
projectSingleShort | public short projectSingleShort(String sql, Parameterizer parameterizer) throws SQLException(Code) | | Parameters: sql - Parameters: parameterizer - short value of the first column of the first row or 0 if there are no rows. throws: SQLException - |
releaseConnection | public void releaseConnection(Connection connection) throws SQLException(Code) | | Closes connection if it was provided by DataSource. Does nothing otherwise.
Parameters: connection - Connection to release. throws: SQLException - |
|
|