| java.lang.Object com.hp.hpl.jena.db.impl.SQLCache
SQLCache | public class SQLCache (Code) | | Stores a set of sql statements loaded from a resource file.
Caches prepared versions of the statements for a given db connection.
The resource file is located on the classpath and has the format:
# comment at start of line
operationName1
sql code line 1
...
sql code last line
operationName2
...
where the blank lines delimit one sql block from the next.
The sql code is typically a single SQL statement but some operations,
specifically database initialization and cleanup may require a variable number
of statments. To cater for this terminate each statement in those groups with
the string ";;". Note that a single ";" is not used because these compound
statements are often stored procedure definitions which end to have ";" line
terminators!
author: Dave Reynolds. Updated by hkuno to support GraphRDB. version: $Revision: 1.18 $ on $Date: 2008/01/02 12:08:23 $ |
Field Summary | |
protected boolean | CACHE_PREPARED_STATEMENTS Set to true to enable cache of pre-prepared statements. | final protected static int | MAX_PS_CACHE Maximum number of pre-prepared statements to keep for each operator. | protected static Log | logger | protected Map | m_cachedStmtInUse Track which cached, prepared statements are in use and the corresponding
list to which the statement should be returned. | protected IDBConnection | m_connection the packaged jdbc connection to the database itself. | protected Map | m_preparedStatements Cache of prepared versions of the statements. | protected Properties | m_sql Set of sql statements indexed by operation name. |
Method Summary | |
public void | close() | public static String | concatOpName(String opName, String attr) | public static String | concatOpName(String opName, String attrA, String attrB) | protected ResultSetIterator | executeSQL(PreparedStatement ps, String opname, ResultSetIterator iterator) | public void | flushPreparedStatementCache() Flush the cache of all currently prepared statements. | protected String | genSQLStatement(String opname) Return dynamically generated SQL for the specified operation.
Parameters: opname - the command to generate; must start with "*", the opname and then op params. | protected String | genSQLStmtFindReif(String op, String args) Return generate SQL for finding reified statements from a triple pattern.
Parameters: op - the command to generate. | public boolean | getCachePreparedStatements() Return true if cache of pre-prepared statements is enabled. | public Connection | getConnection() Return the associated jdbc connection. | public synchronized PreparedStatement | getPreparedSQLStatement(String opname, String[] attr) Return a prepared SQL statement corresponding to the named operation. | public synchronized PreparedStatement | getPreparedSQLStatement(String opname) | public synchronized PreparedStatement | getPreparedSQLStatement(String opname, String attr) Variant on
SQLCache.getPreparedSQLStatement getPreparedSQLStatement which
accesses the attribute variant correspond to the given attribute suffix. | public synchronized PreparedStatement | getPreparedSQLStatement(String opname, String attrA, String attrB) Variant on
SQLCache.getPreparedSQLStatement getPreparedSQLStatement which
access the attribute variant correspond to the given attribute suffix. | public String | getSQLStatement(String opname) Return the raw SQL statement corresponding to the named operation. | public String | getSQLStatement(String opname, String[] attr) Return the raw SQL statement corresponding to the named operation. | public String | getSQLStatement(String opname, String attr) | public String | getSQLStatement(String opname, String attrA, String attrB) Return the raw SQL statement corresponding to the named operation. | public Collection | getSQLStatementGroup(String opname) Return a set of raw SQL statements corresponding to the named operation.
This is used for compound operations where more than one SQL command is needed to
implement the operation (e.g. | protected Properties | getSQLTable() Accessor. | public static Properties | loadSQLFile(String sqlFile, Properties defaultOps, String idType) Load in a defined set of sql statements - see class comment for format.
The loaded file is return as a Property table. | public static BufferedReader | openResourceFile(String filename) Open a resource file for reading. | public synchronized PreparedStatement | prepareSQLStatement(String sql) Return a prepared SQL statement for the given statement string.
The statement should either be closed after use.
Only works for single statements, not compound statements.
Parameters: stmt - the sql statement to prepare. | public synchronized void | returnPreparedSQLStatement(PreparedStatement ps) Return a prepared statement to the statement pool for reuse by
another caller. | public void | runSQLGroup(String opname, String[] attr) Run a group of sql statements - normally used for db formating and clean up. | public void | runSQLGroup(String opname) Run a group of sql statements - normally used for db formating and clean up. | public void | runSQLGroup(String opname, String attr) Run a group of sql statements - normally used for db formating and clean up. | public void | runSQLGroup(String opname, String attrA, String attrB) Run a group of sql statements - normally used for db formating and clean up. | public ResultSetIterator | runSQLQuery(String opname, Object[] args) Execute a named pre-prepared SQL query statement taking a set of arguments and return
a set of results as an iterator (probably a subclass of ResultSetIterator. | public ResultSetIterator | runSQLQuery(String opname, String attr, Object[] args) Variant on
SQLCache.runSQLQuery which
access the attribute variant correspond to the given attribute suffix. | public ResultSetIterator | runSQLQuery(String opname, String attrA, String attrB, Object[] args) Variant on
SQLCache.runSQLQuery which
access the attribute variant correspond to the given attribute suffix. | public ResultSetIterator | runSQLQuery(String opname, Object[] args, ResultSetIterator iterator) Execute a named pre-prepared SQL query statement taking a set of arguments and return
a set of results as an iterator (probably a subclass of ResultSetIterator. | public ResultSetIterator | runSQLQuery(String opname, String attrA, Object[] args, ResultSetIterator iterator) Variant on
SQLCache.runSQLQuery which
access the attribute variant correspond to the given attribute suffix. | public ResultSetIterator | runSQLQuery(String opname, String attrA, String attrB, Object[] args, ResultSetIterator iterator) Variant on
SQLCache.runSQLQuery which
access the attribute variant correspond to the given attribute suffix. | public int | runSQLUpdate(String opname, Object[] args) Execute a named pre-prepared SQL update statement taking a set of arguments and returning
the update count. | public int | runSQLUpdate(String opname, String attrA, Object[] args) Variant on
SQLCache.runSQLUpdate which
access the attribute variant correspond to the given attribute suffix. | public int | runSQLUpdate(String opname, String attrA, String attrB, Object[] args) Variant on
SQLCache.runSQLUpdate which
access the attribute variant correspond to the given attribute suffix. | public void | setCachePreparedStatements(boolean state) Set to true to enable cache of pre-prepared statements. | public void | setConnection(IDBConnection connection) Set the associated jdbc connection. | public static String | substitute(String line, String macro, String subs) |
CACHE_PREPARED_STATEMENTS | protected boolean CACHE_PREPARED_STATEMENTS(Code) | | Set to true to enable cache of pre-prepared statements.
|
MAX_PS_CACHE | final protected static int MAX_PS_CACHE(Code) | | Maximum number of pre-prepared statements to keep for each operator.
|
logger | protected static Log logger(Code) | | |
m_cachedStmtInUse | protected Map m_cachedStmtInUse(Code) | | Track which cached, prepared statements are in use and the corresponding
list to which the statement should be returned.
|
m_connection | protected IDBConnection m_connection(Code) | | the packaged jdbc connection to the database itself.
|
m_preparedStatements | protected Map m_preparedStatements(Code) | | Cache of prepared versions of the statements. Each map entry is a list
of copies of the prepared statement for multi-threaded apps.
|
m_sql | protected Properties m_sql(Code) | | Set of sql statements indexed by operation name.
|
SQLCache | public SQLCache(String sqlFile, Properties defaultOps, IDBConnection connection, String idType) throws IOException(Code) | | Constructor. Creates a new cache sql statements for interfacing to
a specific database.
Parameters: sqlFile - the name of the file of sql statements to load, this isloaded from the classpath. Parameters: defaultOps - Properties table which provides the defaultsql statements, any definitions of a given operation in the loaded filewill override the default. Parameters: connection - the jdbc connection to the database itself Parameters: idType - the sql string to use for id types (substitutes for $id in files) |
concatOpName | public static String concatOpName(String opName, String attr)(Code) | | Helper function calculate op name given substitutions
|
flushPreparedStatementCache | public void flushPreparedStatementCache() throws RDFRDBException(Code) | | Flush the cache of all currently prepared statements.
|
genSQLStatement | protected String genSQLStatement(String opname) throws SQLException(Code) | | Return dynamically generated SQL for the specified operation.
Parameters: opname - the command to generate; must start with "*", the opname and then op params. the generated command as a String. |
genSQLStmtFindReif | protected String genSQLStmtFindReif(String op, String args) throws SQLException(Code) | | Return generate SQL for finding reified statements from a triple pattern.
Parameters: op - the command to generate. should be findReif. Parameters: args - a string describing which command to generate. it has the form [N][PS|PP|PO|PT][O[C]] where N means to searchfor the statement URI; Px means to search for reified subjects, properties,objects or types; O means to search for reified objects; OC means the objectvalue is rdf:Statement. |
getCachePreparedStatements | public boolean getCachePreparedStatements()(Code) | | Return true if cache of pre-prepared statements is enabled.
|
getPreparedSQLStatement | public synchronized PreparedStatement getPreparedSQLStatement(String opname, String[] attr) throws SQLException(Code) | | Return a prepared SQL statement corresponding to the named operation.
The statement should either be closed after use or returned to the
prepared statement pool using
SQLCache.returnPreparedSQLStatement returnPreparedSQLStatement Only works for single statements, not compound statements.
Parameters: con - the jdbc connection to use for preparing statements Parameters: opname - the name of the sql operation to locate a prepared SQL statement appropriate for the JDBC connectionused when this SQLCache was constructed or null if there is no suchoperation or no such connection |
getSQLStatement | public String getSQLStatement(String opname) throws SQLException(Code) | | Return the raw SQL statement corresponding to the named operation.
|
getSQLStatement | public String getSQLStatement(String opname, String[] attr) throws SQLException(Code) | | Return the raw SQL statement corresponding to the named operation.
Substitute the ${a} attribute macro for the current attribute number.
|
getSQLStatement | public String getSQLStatement(String opname, String attrA, String attrB) throws SQLException(Code) | | Return the raw SQL statement corresponding to the named operation.
Attribute version - substitute the ${a} attribute macro for
the current attribute number.
|
getSQLStatementGroup | public Collection getSQLStatementGroup(String opname) throws SQLException(Code) | | Return a set of raw SQL statements corresponding to the named operation.
This is used for compound operations where more than one SQL command is needed to
implement the operation (e.g. database formating and clean up). The
individual statements should be separated by double-semicolons at the end of the line.
Needs refactoring to clarify what operations are and are not compound but for now
it is assumed the caller knows which is correct. Compound statements are not called
repeatedly so don't currently cache the parsed statement set.
|
getSQLTable | protected Properties getSQLTable()(Code) | | Accessor. Returns the Properties table which maps operation names to
the plain text sql statements. This is using internally in the constructor.
|
loadSQLFile | public static Properties loadSQLFile(String sqlFile, Properties defaultOps, String idType) throws IOException(Code) | | Load in a defined set of sql statements - see class comment for format.
The loaded file is return as a Property table. This call is static
to support the loading of a default sql mapping.
Parameters: sqlFile - the name of the file of sql statements to load, this isloaded from the classpath. Parameters: defaultOps - a Properties table of default sql definitions. Parameters: idType - the sql string to use for id types (substitutes for $id in files) |
prepareSQLStatement | public synchronized PreparedStatement prepareSQLStatement(String sql) throws SQLException(Code) | | Return a prepared SQL statement for the given statement string.
The statement should either be closed after use.
Only works for single statements, not compound statements.
Parameters: stmt - the sql statement to prepare. a prepared SQL statement appropriate for the JDBC connectionused when this SQLCache was constructed or null if there is no suchconnection. |
returnPreparedSQLStatement | public synchronized void returnPreparedSQLStatement(PreparedStatement ps)(Code) | | Return a prepared statement to the statement pool for reuse by
another caller. Any close problems logged rather than raising exception
so that iterator close() operations can be silent so that they can meet
the ClosableIterator signature.
|
runSQLGroup | public void runSQLGroup(String opname, String[] attr) throws SQLException(Code) | | Run a group of sql statements - normally used for db formating and clean up.
All statements are executed even if one raises an error then the error is
reported at the end.
Attribute version -- substitute the ${a} attribute macro
for the current attribute
|
runSQLGroup | public void runSQLGroup(String opname) throws SQLException(Code) | | Run a group of sql statements - normally used for db formating and clean up.
All statements are executed even if one raises an error then the error is
reported at the end.
|
runSQLGroup | public void runSQLGroup(String opname, String attr) throws SQLException(Code) | | Run a group of sql statements - normally used for db formating and clean up.
All statements are executed even if one raises an error then the error is
reported at the end.
Attribute version -- substitute the ${a} attribute macro
for the current attribute
|
runSQLGroup | public void runSQLGroup(String opname, String attrA, String attrB) throws SQLException(Code) | | Run a group of sql statements - normally used for db formating and clean up.
All statements are executed even if one raises an error then the error is
reported at the end.
Attribute version -- substitute the ${a} attribute macro
for the current attribute
|
runSQLQuery | public ResultSetIterator runSQLQuery(String opname, Object[] args) throws SQLException(Code) | | Execute a named pre-prepared SQL query statement taking a set of arguments and return
a set of results as an iterator (probably a subclass of ResultSetIterator. Returns null
if they query is an update (as opposed to an empty iterator for a true query which happens
to return no answers).
Not sure this is a good design. Reducing this to a general interface leads to lots of clunky
wrapping and unwrapping of primitive types, coercions and lack of compile-time type checking.
On the other hand letting the clients do this themselves with direct jdbc calls leaves us up
to the mercy of the client to correctly use returnPreparedSQLStatement and on average seems
to lead to more duplication of boiler plate code. Currently the client can chose either approach.
The calling arguments are passed in as an array.
|
runSQLQuery | public ResultSetIterator runSQLQuery(String opname, Object[] args, ResultSetIterator iterator) throws SQLException(Code) | | Execute a named pre-prepared SQL query statement taking a set of arguments and return
a set of results as an iterator (probably a subclass of ResultSetIterator. Returns null
if they query is an update (as opposed to an empty iterator for a true query which happens
to return no answers).
Not sure this is a good design. Reducing this to a general interface leads to lots of clunky
wrapping and unwrapping of primitive types, coercions and lack of compile-time type checking.
On the other hand letting the clients do this themselves with direct jdbc calls leaves us up
to the mercy of the client to correctly use returnPreparedSQLStatement and on average seems
to lead to more duplication of boiler plate code. Currently the client can chose either approach.
Parameters: opname - the name of the SQL operation to perform Parameters: args - the arguments to pass to the SQL operation as an array of Objects Parameters: iterator - the iterator to use to return the results |
runSQLUpdate | public int runSQLUpdate(String opname, Object[] args) throws SQLException(Code) | | Execute a named pre-prepared SQL update statement taking a set of arguments and returning
the update count.
|
setCachePreparedStatements | public void setCachePreparedStatements(boolean state)(Code) | | Set to true to enable cache of pre-prepared statements.
|
setConnection | public void setConnection(IDBConnection connection)(Code) | | Set the associated jdbc connection.
|
|
|