aboutsummaryrefslogtreecommitdiff
path: root/src/uk/org/ury/database
diff options
context:
space:
mode:
Diffstat (limited to 'src/uk/org/ury/database')
-rw-r--r--src/uk/org/ury/database/DatabaseDriver.java298
-rw-r--r--src/uk/org/ury/database/DatabaseItem.java152
-rw-r--r--src/uk/org/ury/database/UserClass.java46
-rw-r--r--src/uk/org/ury/database/exceptions/package.html11
-rw-r--r--src/uk/org/ury/database/package.html15
5 files changed, 261 insertions, 261 deletions
diff --git a/src/uk/org/ury/database/DatabaseDriver.java b/src/uk/org/ury/database/DatabaseDriver.java
index 7c71827..e48b0a1 100644
--- a/src/uk/org/ury/database/DatabaseDriver.java
+++ b/src/uk/org/ury/database/DatabaseDriver.java
@@ -11,162 +11,156 @@ import uk.org.ury.config.ConfigReader;
import uk.org.ury.database.exceptions.ConnectionFailureException;
import uk.org.ury.database.exceptions.MissingCredentialsException;
-
/**
- * A database connection manager that connects to the URY databases
- * using suitably privileged accounts, and handles the processing
- * of SQL queries.
- *
+ * A database connection manager that connects to the URY databases using
+ * suitably privileged accounts, and handles the processing of SQL queries.
+ *
* @author Matt Windsor
* @author Nathan Lasseter
- *
+ *
*/
+public class DatabaseDriver {
+ /* The JDBC path used to connect to the URY database. */
+ private String DATABASE_PATH = "jdbc:postgresql://";
+
+ /* The database connection. */
+ private Connection conn;
+
+ /**
+ * Construct a new DatabaseDriver with the given user class.
+ *
+ * @param config
+ * The config with login details.
+ *
+ * @param type
+ * The user class to log in to the database with.
+ *
+ * @throws IllegalArgumentException
+ * if the user class is not supported (this should not happen).
+ *
+ * @throws MissingCredentialsException
+ * if the user class login credentials could not be loaded.
+ *
+ * @throws ConnectionFailureException
+ * if the database backend failed to connect to the database
+ * server.
+ */
+ public DatabaseDriver(ConfigReader config, UserClass type)
+ throws MissingCredentialsException, ConnectionFailureException {
+ try {
+ connect(config, type);
+ } catch (SQLException e) {
+ throw new ConnectionFailureException(e.getMessage());
+ }
+
+ }
+
+ /**
+ * Connect to the URY database.
+ *
+ * @param config
+ * The config to use for the connection.
+ * @param type
+ * The access level of the connection
+ *
+ * @throws SQLException
+ * if the database connection failed.
+ */
+ private void connect(ConfigReader config, UserClass type)
+ throws SQLException {
+ if (config == null)
+ throw new IllegalArgumentException("Supplied null config.");
+
+ if (config.getDatabase().getHost() == null)
+ throw new IllegalArgumentException("config has no associated host.");
+
+ if (config.getDatabase().getDb() == null)
+ throw new IllegalArgumentException(
+ "config has no associated database.");
+
+ DATABASE_PATH = DATABASE_PATH + config.getDatabase().getHost() + "/"
+ + config.getDatabase().getDb();
+
+ if (type == UserClass.READ_ONLY) {
+ if (config.getRoAuth().getUser() == null)
+ throw new IllegalArgumentException(
+ "config has no associated username.");
+ if (config.getRoAuth().getPass() == null)
+ throw new IllegalArgumentException(
+ "config has no associated password.");
+ conn = DriverManager.getConnection(DATABASE_PATH, config
+ .getRoAuth().getUser(), config.getRoAuth().getPass());
+ } else if (type == UserClass.READ_WRITE) {
+ if (config.getRwAuth().getUser() == null)
+ throw new IllegalArgumentException(
+ "config has no associated username.");
+ if (config.getRwAuth().getPass() == null)
+ throw new IllegalArgumentException(
+ "config has no associated password.");
+ conn = DriverManager.getConnection(DATABASE_PATH, config
+ .getRwAuth().getUser(), config.getRwAuth().getPass());
+ }
+ }
+
+ /**
+ * Execute an unprepared SQL statement with no arguments.
+ *
+ * @param sql
+ * The SQL statement to execute.
+ * @param fetchSize
+ * The maximum number of query rows to return.
+ *
+ * @return the JDBC results set.
+ *
+ * @throws SQLException
+ * if a SQL error occurs.
+ */
+ public ResultSet executeQuery(String sql, int fetchSize)
+ throws SQLException {
+ Statement st = conn.createStatement();
+ st.setFetchSize(fetchSize);
+
+ return st.executeQuery(sql);
+ }
+
+ /**
+ * Perform a SQL statement with arguments.
+ *
+ * This accepts an array of parameter objects, which must each either be
+ * String or Integer objects. The objects will be used sequentially to fill
+ * in '?' placeholders in the query text.
+ *
+ * @param sql
+ * The SQL statement to execute.
+ * @param params
+ * A list of parameter objects.
+ * @param fetchSize
+ * The maximum number of query rows to return.
+ *
+ * @return the set of results from the query.
+ *
+ * @throws IllegalArgumentException
+ * if any of the parameters is unsupported by the database as a
+ * statement parameter.
+ *
+ * @throws SQLException
+ * if a SQL error occurs.
+ */
+ public ResultSet executeQuery(String sql, Object[] params, int fetchSize)
+ throws SQLException {
+ PreparedStatement st = conn.prepareStatement(sql);
+
+ st.setFetchSize(fetchSize);
+
+ for (int i = 0; i < params.length; i++)
+ if (params[i] instanceof String)
+ st.setString(i + 1, (String) params[i]);
+ else if (params[i] instanceof Integer)
+ st.setInt(i + 1, (Integer) params[i]);
+ else
+ throw new IllegalArgumentException("Unsupported parameter #"
+ + (i + 1));
-public class DatabaseDriver
-{
- /* The JDBC path used to connect to the URY database. */
- private String DATABASE_PATH = "jdbc:postgresql://";
-
- /* The database connection. */
- private Connection conn;
-
-
- /**
- * Construct a new DatabaseDriver with the given user class.
- *
- * @param config The config with login details.
- *
- * @param type The user class to log in to the database with.
- *
- * @throws IllegalArgumentException if the user class is
- * not supported (this should not happen).
- *
- * @throws MissingCredentialsException if the user class
- * login credentials could not be loaded.
- *
- * @throws ConnectionFailureException if the database
- * backend failed to connect to the database
- * server.
- */
-
- public
- DatabaseDriver (ConfigReader config, UserClass type)
- throws MissingCredentialsException, ConnectionFailureException
- {
- try
- {
- connect (config, type);
- }
- catch (SQLException e)
- {
- throw new ConnectionFailureException (e.getMessage ());
- }
-
- }
-
-
- /**
- * Connect to the URY database.
- *
- * @param config The config to use for the connection.
- * @param type The access level of the connection
- *
- * @throws SQLException if the database connection failed.
- */
-
- private void
- connect (ConfigReader config, UserClass type)
- throws SQLException
- {
- if (config == null)
- throw new IllegalArgumentException ("Supplied null config.");
-
- if (config.getDatabase().getHost() == null)
- throw new IllegalArgumentException ("config has no associated host.");
-
- if (config.getDatabase().getDb() == null)
- throw new IllegalArgumentException ("config has no associated database.");
-
- DATABASE_PATH = DATABASE_PATH + config.getDatabase().getHost() + "/" + config.getDatabase().getDb();
-
- if(type == UserClass.READ_ONLY) {
- if(config.getRoAuth().getUser() == null)
- throw new IllegalArgumentException ("config has no associated username.");
- if(config.getRoAuth().getPass() == null)
- throw new IllegalArgumentException ("config has no associated password.");
- conn = DriverManager.getConnection (DATABASE_PATH,
- config.getRoAuth().getUser(),
- config.getRoAuth().getPass());
- } else if(type == UserClass.READ_WRITE) {
- if(config.getRwAuth().getUser() == null)
- throw new IllegalArgumentException ("config has no associated username.");
- if(config.getRwAuth().getPass() == null)
- throw new IllegalArgumentException ("config has no associated password.");
- conn = DriverManager.getConnection (DATABASE_PATH,
- config.getRwAuth().getUser(),
- config.getRwAuth().getPass());
+ return st.executeQuery();
}
- }
-
-
- /**
- * Execute an unprepared SQL statement with no arguments.
- *
- * @param sql The SQL statement to execute.
- * @param fetchSize The maximum number of query rows to return.
- *
- * @return the JDBC results set.
- *
- * @throws SQLException if a SQL error occurs.
- */
-
- public ResultSet
- executeQuery (String sql, int fetchSize)
- throws SQLException
- {
- Statement st = conn.createStatement ();
- st.setFetchSize (fetchSize);
-
- return st.executeQuery (sql);
- }
-
-
- /**
- * Perform a SQL statement with arguments.
- *
- * This accepts an array of parameter objects, which must each
- * either be String or Integer objects. The objects will be used
- * sequentially to fill in '?' placeholders in the query text.
- *
- * @param sql The SQL statement to execute.
- * @param params A list of parameter objects.
- * @param fetchSize The maximum number of query rows to return.
- *
- * @return the set of results from the query.
- *
- * @throws IllegalArgumentException if any of the
- * parameters is unsupported by the database as
- * a statement parameter.
- *
- * @throws SQLException if a SQL error occurs.
- */
-
- public ResultSet
- executeQuery (String sql, Object[] params, int fetchSize)
- throws SQLException
- {
- PreparedStatement st = conn.prepareStatement (sql);
-
- st.setFetchSize (50);
-
- for (int i = 0; i < params.length; i++)
- if (params[i] instanceof String)
- st.setString (i + 1, (String) params[i]);
- else if (params[i] instanceof Integer)
- st.setInt (i + 1, (Integer) params[i]);
- else
- throw new IllegalArgumentException ("Unsupported parameter #" + (i + 1));
-
- return st.executeQuery ();
- }
}
diff --git a/src/uk/org/ury/database/DatabaseItem.java b/src/uk/org/ury/database/DatabaseItem.java
index 54b2ef7..bac23a5 100644
--- a/src/uk/org/ury/database/DatabaseItem.java
+++ b/src/uk/org/ury/database/DatabaseItem.java
@@ -5,94 +5,76 @@ import java.util.Map;
import uk.org.ury.database.exceptions.MissingPropertyException;
-
/**
- * An abstract class presenting a template for objects serving as
- * a data structure for collections of properties retrieved from
- * a SQL database.
- *
- * @param E The enumeration type used as the property list.
+ * An abstract class presenting a template for objects serving as a data
+ * structure for collections of properties retrieved from a SQL database.
+ *
+ * @param E
+ * The enumeration type used as the property list.
*
- * @param T The type of datum stored for each property.
+ * @param T
+ * The type of datum stored for each property.
*
- * @author Matt Windsor
+ * @author Matt Windsor
*/
+public abstract class DatabaseItem<E, T> {
+ private Map<E, T> properties;
+
+ /**
+ * Construct a new item from an existing list of properties.
+ *
+ * @param properties
+ * The map of properties that the new item will inherit.
+ */
+ public DatabaseItem(Map<E, T> properties) {
+ this.properties = properties;
+ }
+
+ /**
+ * Check whether a property has been set in the item.
+ *
+ * @return true if the property has been set; false otherwise.
+ */
+
+ public boolean has(E property) {
+ return properties.containsKey(property);
+ }
+
+ /**
+ * Query this item for a property.
+ *
+ * @param property
+ * The property to query.
+ *
+ * @return The property, if it exists.
+ *
+ * @throws MissingPropertyException
+ * if the property does not exist.
+ */
+ public T get(E property) throws MissingPropertyException {
+ if (properties.containsKey(property))
+ return properties.get(property);
+ else
+ throw new MissingPropertyException(property.toString());
+ }
+
+ /**
+ * Retrieve a map of string representations of the properties.
+ *
+ * This relies on E and T having meaningful toString methods.
+ *
+ * @return a list of lines representing the response.
+ */
+ public Map<String, String> asResponse() {
+ // TODO: Fan out implementation details into separate class
+ Map<String, String> response = new HashMap<String, String>();
+
+ for (E property : properties.keySet()) {
+ if (properties.get(property) != null)
+ response.put(property.toString(), properties.get(property)
+ .toString());
+ }
-public abstract class DatabaseItem<E, T>
-{
- private Map<E, T> properties;
-
-
- /**
- * Construct a new item from an existing list of properties.
- *
- * @param properties The map of properties that the new item will
- * inherit.
- */
-
- public
- DatabaseItem (Map<E, T> properties)
- {
- this.properties = properties;
- }
-
-
- /**
- * Check whether a property has been set in the item.
- *
- * @return true if the property has been set; false otherwise.
- */
-
- public boolean
- has (E property)
- {
- return properties.containsKey (property);
- }
-
-
- /**
- * Query this item for a property.
- *
- * @param property The property to query.
- *
- * @return The property, if it exists.
- *
- * @throws MissingPropertyException if the property does
- * not exist.
- */
-
- public T
- get (E property) throws MissingPropertyException
- {
- if (properties.containsKey (property))
- return properties.get (property);
- else
- throw new MissingPropertyException (property.toString ());
- }
-
-
- /**
- * Retrieve a map of string representations of the properties.
- *
- * This relies on E and T having meaningful toString methods.
- *
- * @return a list of lines representing the response.
- */
-
- public Map<String, String>
- asResponse ()
- {
- // TODO: Fan out implementation details into separate class
-
- Map<String, String> response = new HashMap<String, String> ();
-
- for (E property : properties.keySet ())
- {
- if (properties.get (property) != null)
- response.put (property.toString (),
- properties.get (property).toString ());
- }
-
- return response;
- }
+ return response;
+ }
}
diff --git a/src/uk/org/ury/database/UserClass.java b/src/uk/org/ury/database/UserClass.java
index 79af61a..48cb2d3 100644
--- a/src/uk/org/ury/database/UserClass.java
+++ b/src/uk/org/ury/database/UserClass.java
@@ -3,38 +3,36 @@
*/
package uk.org.ury.database;
-
/**
* The various user classes of the database driver.
*
- * These refer to various users in the database proper, and thus
- * grant various levels of permission to the program.
+ * These refer to various users in the database proper, and thus grant various
+ * levels of permission to the program.
+ *
+ * Please use the least privileged user class that works. For most cases,
+ * READ_ONLY should work perfectly.
*
- * Please use the least privileged user class that works. For most
- * cases, READ_ONLY should work perfectly.
+ * @author Matt Windsor
*
- * @author Matt Windsor
- *
*/
-public enum UserClass
- {
- // Constant configName
- READ_ONLY ("read_only"),
- READ_WRITE ("read_write");
-
-
+public enum UserClass {
+ // Constant configName
+ READ_ONLY ("read_only"),
+ READ_WRITE ("read_write");
+
/**
- * The name of the tag in the configuration file that contains
- * the credentials for this user class.
+ * The name of the tag in the configuration file that contains the
+ * credentials for this user class.
*/
-
public String configName;
-
-
- private
- UserClass (String configName)
- {
- this.configName = configName;
+
+ /**
+ * Constructs a new UserClass.
+ *
+ * @param configName The name of the user class in the config.
+ */
+ private UserClass(String configName) {
+ this.configName = configName;
}
- }
+}
diff --git a/src/uk/org/ury/database/exceptions/package.html b/src/uk/org/ury/database/exceptions/package.html
new file mode 100644
index 0000000..b5e7cac
--- /dev/null
+++ b/src/uk/org/ury/database/exceptions/package.html
@@ -0,0 +1,11 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
+"http://www.w3.org/TR/html4/strict.dtd">
+
+<HTML>
+ <HEAD>
+ <TITLE>uk.org.ury.database.exceptions</TITLE>
+ </HEAD>
+ <BODY>
+ <P>Exceptions thrown by the database services classes.</P>
+ </BODY>
+</HTML> \ No newline at end of file
diff --git a/src/uk/org/ury/database/package.html b/src/uk/org/ury/database/package.html
new file mode 100644
index 0000000..2b138b8
--- /dev/null
+++ b/src/uk/org/ury/database/package.html
@@ -0,0 +1,15 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
+"http://www.w3.org/TR/html4/strict.dtd">
+
+<HTML>
+ <HEAD>
+ <TITLE>uk.org.ury.database</TITLE>
+ </HEAD>
+ <BODY>
+ <P>Database services for the URY Presenter Suite.</P>
+ <P>The classes provided within this package are expected to be
+ used by the back-end through utility classes, and <EM>not</EM>
+ by the frontend, which should use the server API to indirectly
+ query the database.</P>
+ </BODY>
+</HTML> \ No newline at end of file