aboutsummaryrefslogtreecommitdiff
path: root/src/uk/org/ury/database/DatabaseDriver.java
blob: bd70c8f1c035ae38dcf02206fc807fd0406111fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package uk.org.ury.database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

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.
 *
 * @author Matt Windsor
 *
 */

public class DatabaseDriver
{ 
  /* The JDBC path used to connect to the URY database. */
  private String DATABASE_PATH = "jdbc:postgresql://4574.co.uk/membership";
  
  /* The database connection. */
  private Connection conn;
  
  
  /**
   * Construct a new DatabaseDriver with the given user class.
   * 
   * @param userclass  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 (UserClass userclass) 
    throws MissingCredentialsException, ConnectionFailureException
  { 
    DatabaseLogin login = null;
    
    login = DatabaseLogin.getLoginFromFile (userclass.configName + ".txt");
    
    try
      {
        connect (login);
      }
    catch (SQLException e)
      {
        throw new ConnectionFailureException (e.getMessage ());
      }

  }
  
  
  /**
   * Connect to the URY database.
   * 
   * @param login  The login tuple to use for the connection.   
   * 
   * @throws       SQLException if the database connection failed.
   */
  
  private void
  connect (DatabaseLogin login) throws SQLException
  {
    if (login == null)
      throw new IllegalArgumentException ("Supplied null login.");
    
    if (login.getUsername () == null)
      throw new IllegalArgumentException ("Login has no associated username.");
    
    if (login.getPassword () == null)
      throw new IllegalArgumentException ("Login has no associated password.");
    
    
    conn = DriverManager.getConnection (DATABASE_PATH,
                                        login.getUsername (),
                                        login.getPassword ());
  }
  
  
  /**
   * 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.
   */
  
  public ResultSet
  executeQuery (String sql, int fetchSize)
  {
    try 
      {
        Statement st = conn.createStatement ();
        st.setFetchSize (fetchSize);
        
        return st.executeQuery (sql);
      }
    catch (SQLException e)
      {
        e.printStackTrace ();
        return null;
      }
  }

  
  /**
   * 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 ();
  }
}