|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object dk.netarkivet.common.utils.DBUtils
public class DBUtils
Various database related utilities.
Constructor Summary | |
---|---|
DBUtils()
|
Method Summary | |
---|---|
static void |
checkTableVersion(java.sql.Connection connection,
java.lang.String tablename,
int desiredVersion)
Check that a database table is of the expected version. |
static void |
closeStatementIfOpen(java.sql.PreparedStatement s)
Close a statement, if not closed already Note: This does not throw any a SQLException, because it is always called inside a finally-clause. |
static void |
executeSQL(java.sql.Connection connection,
java.lang.String... updates)
Update a database by executing all the statements in the updates String array. |
static java.util.Date |
getDateMaybeNull(java.sql.ResultSet rs,
int columnIndex)
Get a Date from a column in the resultset. |
static long |
getGeneratedID(java.sql.PreparedStatement s)
Get the automatically generated key that was created with the just-executed statement. |
static java.lang.Integer |
getIntegerMaybeNull(java.sql.ResultSet rs,
int i)
Get an Integer from the resultSet in column i. |
static java.lang.Long |
getLongMaybeNull(java.sql.ResultSet rs,
int i)
Get a Long from the resultSet in column i. |
static int |
getTableVersion(java.sql.Connection connection,
java.lang.String tablename)
Returns the version of a table according to schemaversions, or 0 for the initial, unnumbered version. |
static java.lang.String |
getUsages(java.sql.Connection connection,
java.lang.String select,
java.lang.Object victim,
java.lang.Object... args)
Return a description of where an object is used elsewhere in the database, or null. |
static java.lang.String |
makeSQLGlob(java.lang.String glob)
Translate a "normal" glob (with * and .) into SQL syntax. |
static java.sql.PreparedStatement |
prepareStatement(java.sql.Connection c,
java.lang.String query,
java.lang.Object... args)
Prepare a statement given a query string and some args. |
static void |
rollbackIfNeeded(java.sql.Connection c,
java.lang.String action,
java.lang.Object o)
Method to perform a rollback of complex DB updates. |
static boolean |
selectAny(java.sql.Connection connection,
java.lang.String query,
java.lang.Object... args)
Execute an SQL query and return whether the result contains any rows. |
static java.lang.Long |
selectFirstLongValueIfAny(java.sql.Connection connection,
java.lang.String query,
java.lang.Object... args)
Execute an SQL statement and return the first long in the result set, or null if resultset is empty. |
static java.lang.Integer |
selectIntValue(java.sql.Connection connection,
java.lang.String query,
java.lang.Object... args)
Execute an SQL statement and return the single int in the result set. |
static java.lang.Integer |
selectIntValue(java.sql.PreparedStatement s)
Execute an SQL statement and return the single integer in the result set. |
static java.util.List<java.lang.Long> |
selectLongList(java.sql.Connection connection,
java.lang.String query,
java.lang.Object... args)
Execute an SQL statement and return the list of Long-objects in its result set. |
static java.lang.Long |
selectLongValue(java.sql.Connection connection,
java.lang.String query,
java.lang.Object... args)
Execute an SQL statement and return the single long in the result set. |
static java.lang.Long |
selectLongValue(java.sql.PreparedStatement s)
Execute an SQL statement and return the single long in the result set. |
static java.util.List<java.lang.String> |
selectStringList(java.sql.Connection connection,
java.lang.String query,
java.lang.Object... args)
Execute an SQL statement and return the list of strings in its result set. |
static java.util.Map<java.lang.String,java.lang.Long> |
selectStringLongMap(java.sql.Connection connection,
java.lang.String query,
java.lang.Object... args)
Execute an SQL statement and return the list of strings -> id mappings in its result set. |
static java.lang.String |
selectStringValue(java.sql.Connection connection,
java.lang.String query,
java.lang.Object... args)
Execute an SQL statement and return the single string in the result set. |
static java.lang.String |
selectStringValue(java.sql.PreparedStatement s)
Execute an SQL statement and return the single string in the result set. |
static void |
setClobMaxLength(java.sql.PreparedStatement s,
int fieldNum,
java.lang.String contents,
long maxSize,
java.lang.Object o,
java.lang.String fieldName)
Set the CLOB maxlength. |
static void |
setComments(java.sql.PreparedStatement s,
int fieldNum,
Named o,
int maxFieldSize)
Set the comments of a Named object into the given field of statement. |
static void |
setDateMaybeNull(java.sql.PreparedStatement s,
int fieldNum,
java.util.Date date)
Set the Date into the given field of a statement. |
static void |
setIntegerMaybeNull(java.sql.PreparedStatement s,
int i,
java.lang.Integer value)
Insert an Integer in prepared statement. |
static void |
setLongMaybeNull(java.sql.PreparedStatement s,
int i,
java.lang.Long value)
Insert a long value (which could be null) into the given field of a statement. |
static void |
setName(java.sql.PreparedStatement s,
int fieldNum,
Named o,
int maxFieldSize)
Set the name of a Named object into the given field. |
static void |
setStringMaxLength(java.sql.PreparedStatement s,
int fieldNum,
java.lang.String contents,
int maxSize,
java.lang.Object o,
java.lang.String fieldname)
Set String Max Length. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public DBUtils()
Method Detail |
---|
public static java.lang.Integer selectIntValue(java.sql.PreparedStatement s)
s
- A prepared statement
IOFailure
- if the statement didn't result in exactly one integer.public static java.lang.Integer selectIntValue(java.sql.Connection connection, java.lang.String query, java.lang.Object... args)
connection
- connection to database.query
- a query with ? for parameters (must not be null or
empty string)args
- parameters of type string, int, long or boolean
IOFailure
- if the statement didn't result in exactly one integerpublic static java.lang.Long selectLongValue(java.sql.PreparedStatement s)
s
- A prepared statement
IOFailure
- if the statement didn't result in exactly one row with
a long or null valuepublic static java.lang.Long selectLongValue(java.sql.Connection connection, java.lang.String query, java.lang.Object... args)
connection
- connection to database.query
- a query with ? for parameters (must not be null or
empty string)args
- parameters of type string, int, long or boolean
IOFailure
- if the statement didn't result in exactly one long
valuepublic static java.lang.Long selectFirstLongValueIfAny(java.sql.Connection connection, java.lang.String query, java.lang.Object... args)
connection
- connection to database.query
- a query with ? for parameters (must not be null
or empty string)args
- parameters of type string, int, long or boolean
IOFailure
- on SQL errors.public static java.sql.PreparedStatement prepareStatement(java.sql.Connection c, java.lang.String query, java.lang.Object... args) throws java.sql.SQLException
c
- a Database connectionquery
- a query string (must not be null or empty)args
- some args to insert into this query string (must not be null)
java.sql.SQLException
- If unable to prepare a statement
ArgumentNotValid
- If unable to handle type of one the args, or
the arguments are either null or an empty String.public static java.util.List<java.lang.String> selectStringList(java.sql.Connection connection, java.lang.String query, java.lang.Object... args)
connection
- connection to the database.query
- the given sql-query (must not be null or empty)args
- The arguments to insert into this query (must not be null)
IOFailure
- If this query failspublic static java.util.Map<java.lang.String,java.lang.Long> selectStringLongMap(java.sql.Connection connection, java.lang.String query, java.lang.Object... args) throws java.sql.SQLException
connection
- connection to the database.query
- the given sql-query (must not be null or empty string)args
- The arguments to insert into this query
java.sql.SQLException
- If this query failspublic static java.util.List<java.lang.Long> selectLongList(java.sql.Connection connection, java.lang.String query, java.lang.Object... args)
connection
- connection to the database.query
- the given sql-query (must not be null or empty string)args
- The arguments to insert into this query
public static long getGeneratedID(java.sql.PreparedStatement s) throws java.sql.SQLException
s
- A statement created with Statement.RETURN_GENERATED_KEYS
java.sql.SQLException
- If a database access error occurs or
the PreparedStatement is closed, or the JDBC driver does not support
the setGeneratedKeys() methodpublic static int getTableVersion(java.sql.Connection connection, java.lang.String tablename) throws IOFailure
connection
- connection to the database.tablename
- The name of a table in the database.
IOFailure
- if DB table schemaversions does not existpublic static void setStringMaxLength(java.sql.PreparedStatement s, int fieldNum, java.lang.String contents, int maxSize, java.lang.Object o, java.lang.String fieldname) throws java.sql.SQLException
s
- a Prepared StatementfieldNum
- a index into the above statementcontents
- the contentsmaxSize
- the maximum size of field: fieldNameo
- the Object, which assumedly have a field named fieldNamefieldname
- the name of a given field
java.sql.SQLException
- if set operation failspublic static void setComments(java.sql.PreparedStatement s, int fieldNum, Named o, int maxFieldSize) throws java.sql.SQLException
s
- a prepared statementfieldNum
- the index of the given field to be seto
- the Named objectmaxFieldSize
- max size of the comments field
java.sql.SQLException
- If any trouble accessing the database during
the operation
PermissionDenied
- If length of o.getComments() is larger than
Constants.MAX_COMMENT_SIZEpublic static void setName(java.sql.PreparedStatement s, int fieldNum, Named o, int maxFieldSize) throws java.sql.SQLException
s
- a prepared statementfieldNum
- the index of the given field to be seto
- the Named objectmaxFieldSize
- max size of the name field
java.sql.SQLException
- If any trouble accessing the database during
the operation
PermissionDenied
- If length of o.getName() is larger than
Constants.MAX_NAME_SIZEpublic static void setDateMaybeNull(java.sql.PreparedStatement s, int fieldNum, java.util.Date date) throws java.sql.SQLException
s
- a prepared statementfieldNum
- the index of the given field to be setdate
- the date (may be null)
java.sql.SQLException
- If any trouble accessing the database during
the operationpublic static java.util.Date getDateMaybeNull(java.sql.ResultSet rs, int columnIndex) throws java.sql.SQLException
rs
- the resultSetcolumnIndex
- The given column, where the Date resides
java.sql.SQLException
- If columnIndex does not correspond to a
parameter marker in the ResultSet, or a database access error
occurs or this method is called on a closed ResultSetpublic static void rollbackIfNeeded(java.sql.Connection c, java.lang.String action, java.lang.Object o)
c
- the db-connectionaction
- The action going on, before calling this methodo
- The Object being acted upon by this actionpublic static void setClobMaxLength(java.sql.PreparedStatement s, int fieldNum, java.lang.String contents, long maxSize, java.lang.Object o, java.lang.String fieldName) throws java.sql.SQLException
s
- a prepared statementfieldNum
- the field-index, where the contents are insertedcontents
- the contentsmaxSize
- the maxsize for this contentso
- the Object, which assumedly have a field named fieldNamefieldName
- a given field (Assumedly in Object o)
java.sql.SQLException
- If fieldNum does not correspond to a
parameter marker in the PreparedStatement, or a database access error
occurs or this method is called on a closed PreparedStatementpublic static void setLongMaybeNull(java.sql.PreparedStatement s, int i, java.lang.Long value) throws java.sql.SQLException
s
- a prepared Statementi
- the number of a given field in the prepared statementvalue
- the long value to insert (maybe null)
java.sql.SQLException
- If i does not correspond to a
parameter marker in the PreparedStatement, or a database access error
occurs or this method is called on a closed PreparedStatementpublic static void setIntegerMaybeNull(java.sql.PreparedStatement s, int i, java.lang.Integer value) throws java.sql.SQLException
s
- a prepared statementi
- the index of the statement, where the Integer should be insertedvalue
- The Integer to insert (maybe null)
java.sql.SQLException
- If i does not correspond to a
parameter marker in the PreparedStatement, or a database access error
occurs or this method is called on a closed PreparedStatementpublic static java.lang.Integer getIntegerMaybeNull(java.sql.ResultSet rs, int i) throws java.sql.SQLException
rs
- the resultseti
- the column where the wanted Integer resides
java.sql.SQLException
- If the columnIndex is not valid, or a database
access error occurs or this method is called on a closed result setpublic static java.lang.Long getLongMaybeNull(java.sql.ResultSet rs, int i) throws java.sql.SQLException
rs
- the resultseti
- the column where the wanted Long resides
java.sql.SQLException
- If the columnIndex is not valid, or a database
access error occurs or this method is called on a closed result setpublic static java.lang.String getUsages(java.sql.Connection connection, java.lang.String select, java.lang.Object victim, java.lang.Object... args)
connection
- connection to the database.select
- A select statement finding the names of other uses. The
statement should result in exactly one column of string values.victim
- The object being used.args
- Any objects that may be used to prepare the select statement.
public static void checkTableVersion(java.sql.Connection connection, java.lang.String tablename, int desiredVersion)
connection
- connection to the database.tablename
- The table to check.desiredVersion
- The version it should be.
IllegalState
- if the version isn't as expected.public static java.lang.String selectStringValue(java.sql.Connection connection, java.lang.String query, java.lang.Object... args)
connection
- connection to the database.query
- a query with ? for parameters (must not be null or
an empty string)args
- parameters of type string, int, long or boolean
IOFailure
- if the statement didn't result in exactly one string
valuepublic static java.lang.String selectStringValue(java.sql.PreparedStatement s)
s
- A prepared statement
IOFailure
- if the statement didn't result in exactly one row with
a string or null valuepublic static boolean selectAny(java.sql.Connection connection, java.lang.String query, java.lang.Object... args)
connection
- connection to the database.query
- a query with ? for parameters (must not be null or
an empty String)args
- parameters of type string, int, long or boolean
IOFailure
- if there were problems with the SQL querypublic static java.lang.String makeSQLGlob(java.lang.String glob)
glob
- A shell-like glob string (must not be null)
public static void executeSQL(java.sql.Connection connection, java.lang.String... updates)
connection
- connection to the database.updates
- The SQL statements that makes the necessary
updates.
IOFailure
- in case of problems in interacting with the databasepublic static void closeStatementIfOpen(java.sql.PreparedStatement s)
s
- a statement
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |