|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--com.arsdigita.acs.db.Database
Wrapper class for Oracle JDBC. Opens database connections, manages connection pools, handles queries, and provides utility methods for large-object (BLOB/CLOB) operations.
Constructor Summary | |
Database()
Returns a database handle for an open database connection from the connection pool. |
|
Database(ServletContext sc)
Returns a database handle for an open database connection from the connection pool. |
Method Summary | |
(package private) static void |
|
void |
abortTransaction()
Rolls back and aborts a transaction. |
void |
beginTransaction()
Starts a transaction by turning off auto-commit mode. |
static String |
bindObj(Object obj)
Utility function returning an empty string if an input object is null. |
int |
blobDmlFile(SqlStatement sql,
File f)
Utility function for writing a file to a BLOB in the database. |
int |
blobDmlFile(String sql,
Object[] bind,
File f)
Utility function for writing a file to a BLOB in the database. |
int |
clobDml(SqlStatement sqlst,
String s)
Utility function for a large string literal to a CLOB field |
int |
clobDmlFile(String sql,
Object[] bind,
File f)
Utility function for writing a file to a CLOB in the database. |
void |
close()
Returns this database handle to the connection pool. |
static boolean |
contains(ResultSet rs,
String column)
Returns true if rs contains
a column named column with a non-null value. |
int |
dml(SqlStatement sql)
Executes a DML statement (insert, update, delete). |
int |
dml(String sql)
Executes a DML statement (insert, update, delete) with no bind variables. |
int |
dml(String sql,
Object[] bind)
Executes a DML statement (insert, update, delete). |
static void |
dumpBLOB(ResultSet rs,
String name,
OutputStream os)
Dumps a BLOB's contents to an OutputStream. |
void |
endTransaction()
Completes a transaction by turning on auto-commit mode. |
protected void |
finalize()
Called by the garbage collector at some point after this
goes out of scope. |
static byte[] |
getBLOB(ResultSet ors,
String name)
Returns a BLOB from a ResultSet as a byte array. |
static String |
getCLOB(ResultSet ors,
String name)
Returns a CLOB from a ResultSet as a String. |
boolean |
inTransaction()
Returns true if in a transaction. |
static void |
main(String[] args)
|
Object |
plsql(SqlStatement stmt)
Alternate form; calls plsql(stmt.getSql(), stmt.getBindValues()). |
Object |
plsql(SqlStatement stmt,
int outType)
Alternate form; calls plsql(stmt.getSql(), stmt.getBindValues(), outType). |
Object |
plsql(String sql,
Object[] bind)
Calls a SQL stored procedure, stored function, or other PL/SQL code with IN parameters but no OUT parameters. |
Object |
plsql(String sql,
Object[] bind,
int outType)
Calls a SQL stored function, or other PL/SQL code, whose first bind variable may be is an OUT parameter returning the value of the stored procedure/function. |
ResultSet |
query(SqlStatement sql)
Returns the result of a query. |
ResultSet |
query(String sql)
Returns the result of a query with no bind variables. |
ResultSet |
query(String sql,
Object[] bind)
Returns the result of a query. |
Vector |
queryToList(SqlStatement stmt)
Returns a list of Strings resulting from a single-column SQL query. |
Vector |
queryToList(String sql,
Object[] bind)
Returns a list of Strings resulting from a single-column SQL query. |
String |
queryToString(SqlStatement st)
Returns a string result from a single-row, single-column SQL query. |
String |
queryToString(SqlStatement st,
String defaultValue)
Returns a string result from a single-row, single-column SQL query. |
String |
queryToString(String sql)
Returns a string result from a single-row, single-column SQL query. |
String |
queryToString(String sql,
Object[] bind)
Returns a string result from a single-row, single-column SQL query. |
String |
queryToString(String sql,
Object[] bind,
String defaultValue)
Returns a string result from a single-row, single-column SQL query. |
String |
queryToString(String sql,
String defaultValue)
Returns a string result from a single-row, single-column SQL query. |
static void |
writeBLOB(oracle.sql.BLOB blob,
File f)
Writes a BLOB to the database from a file. |
static void |
writeBLOB(ResultSet rs,
String name,
File f)
Writes a BLOB to the database from a file. |
static void |
writeCLOB(oracle.sql.CLOB clob,
File f)
Writes a CLOB to the database from a file. |
Methods inherited from class java.lang.Object |
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
public Database() throws SQLException
public Database(ServletContext sc) throws SQLException
Method Detail |
static void()
public void close() throws SQLException
public boolean inTransaction() throws SQLException
public void beginTransaction() throws SQLException
public void endTransaction() throws SQLException
public void abortTransaction() throws SQLException
protected void finalize() throws Throwable
this
goes out of scope. Makes sure that the connection is returned
to the pool.finalize
in class Object
public ResultSet query(SqlStatement sql) throws SQLException
sql
- An object consisting of the SQL query and the
bind valuesResultSet
with the query results.public ResultSet query(String sql, Object[] bind) throws SQLException
sql
- A SQL querybind
- An array of bind valuesResultSet
with the query results.public ResultSet query(String sql) throws SQLException
sql
- A SQL queryResultSet
with the query results.public int dml(String sql) throws SQLException
sql
- A SQL querypublic int dml(String sql, Object[] bind) throws SQLException
sql
- A SQL querybind
- A list of bind valuespublic int dml(SqlStatement sql) throws SQLException
sql
- An object containing a SQL query and a list of bind
valuespublic Object plsql(String sql, Object[] bind, int outType) throws SQLException
The SQL statement will generally be of the form
begin ? := stored_function(?, ?, ?); end;
or begin stored_proc(?, ?)
.
The value returned from stored_function
will be
returned, or null will be returned for procedures.
Note that a stored function with no side effects
(inserts/updates/deletes) may be SELECT
ed from
dual
and called as a normal query, and a stored
procedure with no side effects may be executed by the dml()
method. Also, DDL can often be executed by the dml() method as
well.
sql
- An object containing the SQL codebind
- A list of bind valuesoutType
- The type of the OUT parameter (or Types.NULL if none)CallableStatement
,
java.sql.Types
public Object plsql(String sql, Object[] bind) throws SQLException
Note that a stored function with no side effects
(inserts/updates/deletes) may be SELECT
ed from
dual
and called as a normal query, and a stored
procedure with no side effects may be executed as DML.
sql
- An object containing the SQL codebind
- A list of bind valuesCallableStatement
,
java.sql.Types
public Object plsql(SqlStatement stmt) throws SQLException
public Object plsql(SqlStatement stmt, int outType) throws SQLException
public int clobDml(SqlStatement sqlst, String s) throws SQLException, IOException
sql
- SQL insert or update, with the CLOB locator
returned as an OUT parameter, as the final bind variable (e.g.,
... returning clob_column into ?
)
Note: Note that the SQL statement must be wrapped in a begin ... end block. This is a kludge; issuing a bare insert/update results in a "not enough bind values" error from Oracle, and trying to make the locator an IN/OUT parameter with setNull or setCLOB results in weird errors in the database driver.
sqlst
- The sql statement with bind varss
- The string to writepublic int blobDmlFile(String sql, Object[] bind, File f) throws SQLException, IOException
sql
- SQL insert or update, with the BLOB locator
returned as an OUT parameter, as the final bind variable (e.g.,
... returning blob_column into ?
)
Note: Note that the SQL statement must be wrapped in a begin ... end block. This is a kludge; issuing a bare insert/update results in a "not enough bind values" error from Oracle, and trying to make the locator an IN/OUT parameter with setNull or setBLOB results in weird errors in the database driver.
bind
- List of bind valsf
- The file to writepublic int blobDmlFile(SqlStatement sql, File f) throws SQLException, IOException
sql
- Object including SQL insert or update and bind values,
with the BLOB locator returned as an OUT parameter, as the final
bind variable (e.g., ... returning blob_column into ?
).
Note: Note that the SQL statement must be wrapped in a begin ... end block. This is a kludge; issuing a bare insert/update results in a "not enough bind values" error from Oracle, and trying to make the locator an IN/OUT parameter with setNull or setBLOB results in weird errors in the database driver.
f
- The file to writepublic int clobDmlFile(String sql, Object[] bind, File f) throws SQLException, IOException
sql
- SQL insert or update, with the CLOB locator
returned as an OUT parameter, as the final bind variable (e.g.,
... returning blob_column into ?
)
Note: Note that the SQL statement must be wrapped in a begin ... end block. This is a kludge; issuing a bare insert/update results in a "not enough bind values" error from Oracle, and trying to make the locator an IN/OUT parameter with setNull or setCLOB results in weird errors in the database driver.
bind
- List of bind valsf
- The file to writepublic String queryToString(String sql, String defaultValue) throws SQLException, ACSException
sql
- SQL to rundefaultValue
- Default if the query returns no rowsdefault
if the query returns no rows.public String queryToString(String sql) throws SQLException, ACSException
sql
- SQL to runACSException
- public String queryToString(String sql, Object[] bind) throws SQLException, ACSException
sql
- SQL to runbind
- List of bind valuesACSException
- public String queryToString(SqlStatement st) throws SQLException, ACSException
st
- Object with SQL statement and bind valuesACSException
- public String queryToString(SqlStatement st, String defaultValue) throws SQLException, ACSException
st
- Object with SQL statement and bind valuesdefaultValue
- Value to return if query returns no rowsdefaultValue
if no rows returned)ACSException
- public String queryToString(String sql, Object[] bind, String defaultValue) throws SQLException, ACSException
sql
- SQL to runbind
- List of bind valuesdefaultValue
- Default if the query returns no rowsdefault
if the query returns no rows.public Vector queryToList(String sql, Object[] bind) throws SQLException
sql
- SQL to runbind
- List of bind valuespublic Vector queryToList(SqlStatement stmt) throws SQLException
stmt
- An object containing a SQL statment and a
list of bind valuespublic static String getCLOB(ResultSet ors, String name) throws SQLException, IOException
ors
- The result setname
- The name of the CLOB columnpublic static byte[] getBLOB(ResultSet ors, String name) throws SQLException, IOException
ors
- The result setname
- The name of the BLOB columnpublic static void writeBLOB(ResultSet rs, String name, File f) throws SQLException, IOException
rs
- The result setname
- The name of the BLOB columnf
- The file to fill the database BLOB withpublic static void writeBLOB(oracle.sql.BLOB blob, File f) throws SQLException, IOException
blob
- The BLOB locatorf
- The file to fill the database BLOB withpublic static void writeCLOB(oracle.sql.CLOB clob, File f) throws SQLException, IOException
blob
- The CLOB locatorf
- The file to fill the database CLOB withpublic static void dumpBLOB(ResultSet rs, String name, OutputStream os) throws SQLException, IOException
rs
- The result set containing a BLOB locatorname
- The name of the BLOB columnos
- The output stream to write the BLOB contents topublic static String bindObj(Object obj)
obj
- The object that for bindingpublic static void main(String[] args)
public static boolean contains(ResultSet rs, String column)
true
if rs
contains
a column named column
with a non-null value.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |