dk.netarkivet.harvester.datamodel
Class DBConnect

java.lang.Object
  extended by dk.netarkivet.harvester.datamodel.DBConnect

public class DBConnect
extends java.lang.Object

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

DBConnect

public DBConnect()
Method Detail

getDBConnection

static java.sql.Connection getDBConnection()
Get a connection to our database. Assumes that AutoCommit is true.

Returns:
a connection to our database
Throws:
IOFailure - if we cannot connect to the database (or find the driver).

selectIntValue

public static java.lang.Integer selectIntValue(java.sql.PreparedStatement s)
Execute an SQL statement and return the single int in the result set.

Parameters:
s - A prepared statement
Returns:
The integer result, or null if the result value was null.
Throws:
IOFailure - if the statement didn't result in exactly one integer,

selectIntValue

public 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. This variant takes a query string and a single string arg and combines them to form a normal query.

Parameters:
query - a query with ? for parameters
args - parameters of type string, int, long or boolean
Returns:
The integer result
Throws:
IOFailure - if the statement didn't result in exactly one integer

selectLongValue

public static java.lang.Long selectLongValue(java.sql.PreparedStatement s)
Execute an SQL statement and return the single long in the result set.

Parameters:
s - A prepared statement
Returns:
The long result, or null if the result was a null value Note that a null value is not the same as no result rows.
Throws:
IOFailure - if the statement didn't result in exactly one row with a long or null value

selectLongValue

public 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. This variant takes a query string and a single string arg and combines them to form a normal query.

Parameters:
query - a query with ? for parameters
args - parameters of type string, int, long or boolean
Returns:
The long result
Throws:
IOFailure - if the statement didn't result in exactly one long value

selectFirstLongValueIfAny

public 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.

Parameters:
query - a query with ? for parameters
args - parameters of type string, int, long or boolean
Returns:
The long result, or will return null in one of the two following cases: There is no results, or the first result is a null-value.
Throws:
IOFailure - on SQL errors.

selectStringlist

public static java.util.List<java.lang.String> selectStringlist(java.lang.String query,
                                                                java.lang.Object... args)
                                                         throws java.sql.SQLException
Execute an SQL statement and return the list of strings in its result set.

Parameters:
query - the given sql-query
args - The arguments to insert into this query
Returns:
the list of strings in its result set
Throws:
java.sql.SQLException - If this query fails

selectStringLongMap

public static java.util.Map<java.lang.String,java.lang.Long> selectStringLongMap(java.lang.String query,
                                                                                 java.lang.Object... args)
                                                                          throws java.sql.SQLException
Execute an SQL statement and return the list of strings -> id mappings in its result set.

Parameters:
query - the given sql-query
args - The arguments to insert into this query
Returns:
the list of strings -> id mappings
Throws:
java.sql.SQLException - If this query fails

selectLongList

public static java.util.List<java.lang.Long> selectLongList(java.lang.String query,
                                                            java.lang.Object... args)
                                                     throws java.sql.SQLException
Execute an SQL statement and return the list of Long-objects in its result set.

Parameters:
query - the given sql-query
args - The arguments to insert into this query
Returns:
the list of Long-objects in its resultset
Throws:
java.sql.SQLException - If this query fails

getGeneratedID

public static long getGeneratedID(java.sql.PreparedStatement s)
                           throws java.sql.SQLException
Get the automatically generated key that was created with the just-executed statement.

Parameters:
s - A statement created with Statement.RETURN_GENERATED_KEYS
Returns:
The single generated key
Throws:
java.sql.SQLException

getTableVersion

public static int getTableVersion(java.lang.String tablename)
                           throws IOFailure
Returns the version of a table according to schemaversions, or 0 for the initial, unnumbered version.

Parameters:
tablename - The name of a table in the database.
Returns:
Version of the given table.
Throws:
IOFailure - if DB table schemaversions does not exist

closeStatementIfOpen

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. Exceptions are logged as warnings, though.

Parameters:
s - a statement

setStringMaxLength

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
Set String Max Length. If contents.length() > maxSize, contents is truncated to contain the first maxSize characters of the contents, and a warning is logged.

Parameters:
s - a Prepared Statement
fieldNum - a index into the above statement
contents - the contents
maxSize - the maximum size of field: fieldName
o - the Object, which assumedly have a field named fieldName
fieldname - the name of a given field
Throws:
java.sql.SQLException - if set operation fails

setName

static void setName(java.sql.PreparedStatement s,
                    int fieldNum,
                    Named o)
             throws java.sql.SQLException
Set the name of a Named object into the given field.

Parameters:
s - a prepared statement
fieldNum - the index of the given field to be set
o - the Named object
Throws:
java.sql.SQLException
PermissionDenied - If length of o.getName() is larger than Constants.MAX_NAME_SIZE

setComments

static void setComments(java.sql.PreparedStatement s,
                        int fieldNum,
                        Named o)
                 throws java.sql.SQLException
Set the comments of a Named object into the given field of statement.

Parameters:
s - a prepared statement
fieldNum - the index of the given field to be set
o - the Named object
Throws:
java.sql.SQLException
PermissionDenied - If length of o.getComments() is larger than Constants.MAX_COMMENT_SIZE

setDateMaybeNull

static void setDateMaybeNull(java.sql.PreparedStatement s,
                             int fieldNum,
                             java.util.Date date)
                      throws java.sql.SQLException
Set the Date into the given field of a statement.

Parameters:
s - a prepared statement
fieldNum - the index of the given field to be set
date - the date (may be null)
Throws:
java.sql.SQLException

getDateMaybeNull

static java.util.Date getDateMaybeNull(java.sql.ResultSet rs,
                                       int columnIndex)
                                throws java.sql.SQLException
Get a Date from a column in the resultset. Returns null, if the value in the column is NULL.

Parameters:
rs - the resultSet
columnIndex - The given column, where the Date resides
Returns:
a Date from a column in the resultset
Throws:
java.sql.SQLException

rollbackIfNeeded

static void rollbackIfNeeded(java.sql.Connection c,
                             java.lang.String action,
                             java.lang.Object o)
Method to perform a rollback of complex DB updates. If no commit has been performed, this will undo the entire transaction, otherwise nothing will happen. This should be called in a finally block with no DB updates after the last commit. Thus exceptions while closing are ignored, but logged as warnings.

Parameters:
c - the db-connection
action - The action going on, before calling this method
o - The being acted upon by this action

setClobMaxLength

public 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
Set the CLOB maxlength. If contents.length() > maxSize, contents is truncated to contain the first maxSize characters of the contents, and a warning is logged.

Parameters:
s - a prepared statement
fieldNum - the field-index, where the contents are inserted
contents - the contents
maxSize - the maxsize for this contents
o - the Object, which assumedly have a field named fieldName
fieldName - a given field (Assumedly in Object o)
Throws:
java.sql.SQLException

setLongMaybeNull

public static void setLongMaybeNull(java.sql.PreparedStatement s,
                                    int i,
                                    java.lang.Long value)
                             throws java.sql.SQLException
Insert a long value (which could be null) into the given field of a statement.

Parameters:
s - a prepared Statement
i - the number of a given field in the prepared statement
value - the long value to insert (maybe null)
Throws:
java.sql.SQLException

setIntegerMaybeNull

public static void setIntegerMaybeNull(java.sql.PreparedStatement s,
                                       int i,
                                       java.lang.Integer value)
                                throws java.sql.SQLException
Insert an Integer in prepared statement.

Parameters:
s - a prepared statement
i - the index of the statement, where the Integer should be inserted
value - The Integer to insert
Throws:
java.sql.SQLException

getIntegerMaybeNull

public static java.lang.Integer getIntegerMaybeNull(java.sql.ResultSet rs,
                                                    int i)
                                             throws java.sql.SQLException
Get an Integer from the resultSet in column i.

Parameters:
rs - the resultset
i - the column where the wanted Integer resides
Returns:
an Integer object located in column i in the resultset
Throws:
java.sql.SQLException

getLongMaybeNull

public static java.lang.Long getLongMaybeNull(java.sql.ResultSet rs,
                                              int i)
                                       throws java.sql.SQLException
Get a Long from the resultSet in column i.

Parameters:
rs - the resultset
i - the column where the wanted Long resides
Returns:
a Long object located in column i in the resultset
Throws:
java.sql.SQLException

checkForUses

public 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.

Parameters:
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.
Throws:
PermissionDenied - if the object has usages.

getUsages

public 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.

Parameters:
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.
Returns:
A string describing the usages, or null if no usages were found.

checkTableVersion

public static void checkTableVersion(java.lang.String tablename,
                                     int desiredVersion)
Check that a database table is of the expected version.

Parameters:
tablename - The table to check.
desiredVersion - The version it should be.
Throws:
IOFailure - if the version isn't as expected.

selectStringValue

public 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. This variant takes a query string and a single string arg and combines them to form a normal query.

Parameters:
query - a query with ? for parameters
args - parameters of type string, int, long or boolean
Returns:
The string result
Throws:
IOFailure - if the statement didn't result in exactly one string value

selectAny

public static boolean selectAny(java.lang.String query,
                                java.lang.Object... args)
Execute an SQL query and return whether the result contains any rows.

Parameters:
query - a query with ? for parameters
args - parameters of type string, int, long or boolean
Returns:
True if executing the query resulted in at least one row.
Throws:
IOFailure - if there were problems with the SQL query

updateTable

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.

Parameters:
table - The table to update
newVersion - The version that the table should end up at
updates - The SQL update statements that makes the necessary updates.

makeSQLGlob

static java.lang.String makeSQLGlob(java.lang.String glob)
Translate a "normal" glob (with * and .) into SQL syntax.

Parameters:
glob - A shell-like glob string
Returns:
A string that implements glob in SQL "LIKE" constructs.