|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectdk.netarkivet.harvester.datamodel.DBConnect
public class DBConnect
Logic to connect with the harvest definition database. This also defines basic logic for checking versions of tables. The statements to create the tables are now in scripts/sql/createfullhddb.sql
Constructor Summary | |
---|---|
DBConnect()
|
Method Summary | |
---|---|
static void |
checkForUses(java.lang.String select,
java.lang.Object victim,
java.lang.Object... args)
Check whether an object is used otherwhere in the database. |
static void |
checkTableVersion(java.lang.String tablename,
int desiredVersion)
Check that a database table is of the expected version. |
(package private) 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. |
(package private) static java.util.Date |
getDateMaybeNull(java.sql.ResultSet rs,
int columnIndex)
Get a Date from a column in the resultset. |
(package private) static java.sql.Connection |
getDBConnection()
Get a connection to our database. |
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.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.lang.String select,
java.lang.Object victim,
java.lang.Object... args)
Return a description of where an object is used otherwhere in the database, or null. |
(package private) static java.lang.String |
makeSQLGlob(java.lang.String glob)
Translate a "normal" glob (with * and .) into SQL syntax. |
(package private) 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.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.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.PreparedStatement s)
Execute an SQL statement and return the single int in the result set. |
static java.lang.Integer |
selectIntValue(java.lang.String query,
java.lang.Object... args)
Execute an SQL statement and return the single int in the result set. |
static java.util.List<java.lang.Long> |
selectLongList(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.PreparedStatement s)
Execute an SQL statement and return the single long in the result set. |
static java.lang.Long |
selectLongValue(java.lang.String query,
java.lang.Object... args)
Execute an SQL statement and return the single long in the result set. |
static java.util.List<java.lang.String> |
selectStringlist(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.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.lang.String query,
java.lang.Object... args)
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. |
(package private) static void |
setComments(java.sql.PreparedStatement s,
int fieldNum,
Named o)
Set the comments of a Named object into the given field of statement. |
(package private) 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. |
(package private) static void |
setName(java.sql.PreparedStatement s,
int fieldNum,
Named o)
Set the name of a Named object into the given field. |
(package private) 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. |
protected static void |
updateTable(java.lang.String table,
int newVersion,
java.lang.String... updates)
Update a table by executing all the statements in the updates String array. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public DBConnect()
Method Detail |
---|
static java.sql.Connection getDBConnection()
IOFailure
- if we cannot connect to the database (or find the
driver).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.lang.String query, java.lang.Object... args)
query
- a query with ? for parametersargs
- 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.lang.String query, java.lang.Object... args)
query
- a query with ? for parametersargs
- 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.lang.String query, java.lang.Object... args)
query
- a query with ? for parametersargs
- parameters of type string, int, long or boolean
IOFailure
- on SQL errors.public static java.util.List<java.lang.String> selectStringlist(java.lang.String query, java.lang.Object... args) throws java.sql.SQLException
query
- the given sql-queryargs
- The arguments to insert into this query
java.sql.SQLException
- If this query failspublic static java.util.Map<java.lang.String,java.lang.Long> selectStringLongMap(java.lang.String query, java.lang.Object... args) throws java.sql.SQLException
query
- the given sql-queryargs
- The arguments to insert into this query
java.sql.SQLException
- If this query failspublic static java.util.List<java.lang.Long> selectLongList(java.lang.String query, java.lang.Object... args) throws java.sql.SQLException
query
- the given sql-queryargs
- The arguments to insert into this query
java.sql.SQLException
- If this query failspublic static long getGeneratedID(java.sql.PreparedStatement s) throws java.sql.SQLException
s
- A statement created with Statement.RETURN_GENERATED_KEYS
java.sql.SQLException
public static int getTableVersion(java.lang.String tablename) throws IOFailure
tablename
- The name of a table in the database.
IOFailure
- if DB table schemaversions does not existstatic void closeStatementIfOpen(java.sql.PreparedStatement s)
s
- a statementstatic 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 failsstatic void setName(java.sql.PreparedStatement s, int fieldNum, Named o) throws java.sql.SQLException
s
- a prepared statementfieldNum
- the index of the given field to be seto
- the Named object
java.sql.SQLException
PermissionDenied
- If length of o.getName() is larger than
Constants.MAX_NAME_SIZEstatic void setComments(java.sql.PreparedStatement s, int fieldNum, Named o) throws java.sql.SQLException
s
- a prepared statementfieldNum
- the index of the given field to be seto
- the Named object
java.sql.SQLException
PermissionDenied
- If length of o.getComments() is larger than
Constants.MAX_COMMENT_SIZEstatic 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
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
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 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
public 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
public 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
java.sql.SQLException
public 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
public 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
public static void checkForUses(java.lang.String select, java.lang.Object victim, java.lang.Object... args)
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.
PermissionDenied
- if the object has usages.public static java.lang.String getUsages(java.lang.String select, java.lang.Object victim, java.lang.Object... args)
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.lang.String tablename, int desiredVersion)
tablename
- The table to check.desiredVersion
- The version it should be.
IOFailure
- if the version isn't as expected.public static java.lang.String selectStringValue(java.lang.String query, java.lang.Object... args)
query
- a query with ? for parametersargs
- parameters of type string, int, long or boolean
IOFailure
- if the statement didn't result in exactly one string valuepublic static boolean selectAny(java.lang.String query, java.lang.Object... args)
query
- a query with ? for parametersargs
- parameters of type string, int, long or boolean
IOFailure
- if there were problems with the SQL queryprotected static void updateTable(java.lang.String table, int newVersion, java.lang.String... updates)
table
- The table to updatenewVersion
- The version that the table should end up atupdates
- The SQL update statements that makes the necessary
updates.static java.lang.String makeSQLGlob(java.lang.String glob)
glob
- A shell-like glob string
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |