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.java29
-rw-r--r--src/uk/org/ury/database/DatabaseItem.java30
2 files changed, 44 insertions, 15 deletions
diff --git a/src/uk/org/ury/database/DatabaseDriver.java b/src/uk/org/ury/database/DatabaseDriver.java
index a7db32e..7c71827 100644
--- a/src/uk/org/ury/database/DatabaseDriver.java
+++ b/src/uk/org/ury/database/DatabaseDriver.java
@@ -35,7 +35,8 @@ public class DatabaseDriver
* Construct a new DatabaseDriver with the given user class.
*
* @param config The config with login details.
- * @param userclass The user class to log in to the database with.
+ *
+ * @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).
@@ -49,7 +50,8 @@ public class DatabaseDriver
*/
public
- DatabaseDriver (ConfigReader config, UserClass type) throws MissingCredentialsException, ConnectionFailureException
+ DatabaseDriver (ConfigReader config, UserClass type)
+ throws MissingCredentialsException, ConnectionFailureException
{
try
{
@@ -73,7 +75,8 @@ public class DatabaseDriver
*/
private void
- connect (ConfigReader config, UserClass type) throws SQLException
+ connect (ConfigReader config, UserClass type)
+ throws SQLException
{
if (config == null)
throw new IllegalArgumentException ("Supplied null config.");
@@ -113,23 +116,18 @@ public class DatabaseDriver
* @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
{
- try
- {
- Statement st = conn.createStatement ();
- st.setFetchSize (fetchSize);
+ Statement st = conn.createStatement ();
+ st.setFetchSize (fetchSize);
- return st.executeQuery (sql);
- }
- catch (SQLException e)
- {
- e.printStackTrace ();
- return null;
- }
+ return st.executeQuery (sql);
}
@@ -154,7 +152,8 @@ public class DatabaseDriver
*/
public ResultSet
- executeQuery (String sql, Object[] params, int fetchSize) throws SQLException
+ executeQuery (String sql, Object[] params, int fetchSize)
+ throws SQLException
{
PreparedStatement st = conn.prepareStatement (sql);
diff --git a/src/uk/org/ury/database/DatabaseItem.java b/src/uk/org/ury/database/DatabaseItem.java
index 4f6d34d..f151dbf 100644
--- a/src/uk/org/ury/database/DatabaseItem.java
+++ b/src/uk/org/ury/database/DatabaseItem.java
@@ -1,8 +1,11 @@
package uk.org.ury.database;
+import java.util.ArrayList;
+import java.util.List;
import java.util.Map;
import uk.org.ury.database.exceptions.MissingPropertyException;
+import uk.org.ury.server.protocol.Directive;
/**
@@ -21,6 +24,7 @@ public abstract class DatabaseItem<E, T>
{
private Map<E, T> properties;
+
/**
* Construct a new item from an existing list of properties.
*
@@ -67,4 +71,30 @@ public abstract class DatabaseItem<E, T>
else
throw new MissingPropertyException (property.toString ());
}
+
+
+ /**
+ * Map down the item into a server response body.
+ *
+ * This relies on E and T having meaningful toString methods.
+ *
+ * @return a list of lines representing the response.
+ */
+
+ public List<String>
+ asResponse ()
+ {
+ // TODO: Fan out implementation details into separate class
+
+ List<String> response = new ArrayList<String> ();
+
+ for (E property : properties.keySet ())
+ {
+ if (properties.get (property) != null)
+ response.add (property.toString () + ": "
+ + properties.get (property).toString ());
+ }
+
+ return response;
+ }
}