View Javadoc

1   package ca.spaz.sql;
2   
3   import java.sql.*;
4   
5   /***
6    * Base class for SQL command generators
7    * 
8    * @author davidson
9    */
10  public abstract class SQLStatement {
11  
12      public final void execute(Connection con) throws SQLException {
13          if (!isExecuteSupported()) {
14              throw new UnsupportedOperationException("Execute not supported on "
15                      + getClass().getName());
16          }
17          doExecute(con);
18      }
19  
20      protected void doExecute(Connection con) throws SQLException { }
21  
22      public final boolean isExecuteSupported() {
23          return executeSupport;
24      }
25  
26      public final ResultSet executeQuery(Connection con) throws SQLException {
27          if (!isQuerySupported()) {
28              throw new UnsupportedOperationException("Query not supported on "
29                      + getClass().getName());
30          }
31          return doExecuteQuery(con);
32      }
33  
34      protected ResultSet doExecuteQuery(Connection con) throws SQLException {
35          return null;
36      }
37  
38      public final boolean isQuerySupported() {
39          return querySupport;
40      }
41  
42      protected String table;
43  
44      private boolean querySupport;
45  
46      private boolean executeSupport;
47  
48      protected SQLStatement(String table, boolean querySupport,
49              boolean executeSupport) {
50          this.table = table;
51          this.querySupport = querySupport;
52          this.executeSupport = executeSupport;
53      }
54  
55      public String getTableName() {
56          return table;
57      }
58  
59      static String escape(String s) {
60          return s.replaceAll("//'", "//'//'");
61      }
62  
63      protected abstract String getQueryString();
64  
65      public String toString() {
66          return getQueryString();
67      }
68      
69      /***
70       * Ensure that correct classes (with respect to toString()) ge
71       * passed on
72       * @param o the class to check.
73       * @return a valid class for SQL
74       */
75      static Object fixClass(Object o) {
76         if (o instanceof java.util.Date) {
77            o = new java.sql.Date(((java.util.Date)o).getTime());
78         }
79         return o;
80      }
81      
82  
83  }