]> WPIA git - gigi.git/blob - src/club/wpia/gigi/database/GigiPreparedStatement.java
fix: ResultSet.getDate is often wrong as it fetches day-precision times
[gigi.git] / src / club / wpia / gigi / database / GigiPreparedStatement.java
1 package club.wpia.gigi.database;
2
3 import java.sql.Date;
4 import java.sql.PreparedStatement;
5 import java.sql.ResultSet;
6 import java.sql.SQLException;
7 import java.sql.Timestamp;
8
9 public class GigiPreparedStatement implements AutoCloseable {
10
11     private PreparedStatement target;
12
13     private GigiResultSet rs;
14
15     protected GigiPreparedStatement(PreparedStatement preparedStatement) {
16         target = preparedStatement;
17     }
18
19     public GigiPreparedStatement(String stmt) {
20         this(stmt, false);
21     }
22
23     public GigiPreparedStatement(String stmt, boolean scroll) {
24         try {
25             target = DatabaseConnection.getInstance().prepareInternal(stmt, scroll);
26         } catch (SQLException e) {
27             throw new Error(e);
28         }
29     }
30
31     public GigiResultSet executeQuery() {
32         try {
33             return rs = new GigiResultSet(target.executeQuery());
34         } catch (SQLException e) {
35             handleSQL(e);
36             throw new Error(e);
37         }
38     }
39
40     public void executeUpdate() {
41         try {
42             int updated = target.executeUpdate();
43             if (updated != 1) {
44                 throw new Error("FATAL: multiple or no data updated: " + updated);
45             }
46         } catch (SQLException e) {
47             handleSQL(e);
48             throw new Error(e);
49         }
50     }
51
52     public boolean executeMaybeUpdate() {
53         try {
54             int updated = target.executeUpdate();
55             if (updated > 1) {
56                 throw new Error("More than one record (" + updated + ") updated.");
57             }
58             return updated == 1;
59         } catch (SQLException e) {
60             handleSQL(e);
61             throw new Error(e);
62         }
63     }
64
65     public boolean execute() {
66         try {
67             return target.execute();
68         } catch (SQLException e) {
69             handleSQL(e);
70             throw new Error(e);
71         }
72     }
73
74     public void setInt(int parameterIndex, int x) {
75         try {
76             target.setInt(parameterIndex, x);
77         } catch (SQLException e) {
78             handleSQL(e);
79             throw new Error(e);
80         }
81     }
82
83     public void setString(int parameterIndex, String x) {
84         try {
85             target.setString(parameterIndex, x);
86         } catch (SQLException e) {
87             handleSQL(e);
88             throw new Error(e);
89         }
90     }
91
92     public void setEnum(int parameterIndex, DBEnum x) {
93         try {
94             target.setString(parameterIndex, x.getDBName());
95         } catch (SQLException e) {
96             handleSQL(e);
97             throw new Error(e);
98         }
99     }
100
101     public void setDate(int parameterIndex, Date x) {
102         try {
103             target.setDate(parameterIndex, x);
104         } catch (SQLException e) {
105             handleSQL(e);
106             throw new Error(e);
107         }
108     }
109
110     public void setTimestamp(int parameterIndex, Timestamp x) {
111         try {
112             target.setTimestamp(parameterIndex, x);
113         } catch (SQLException e) {
114             handleSQL(e);
115             throw new Error(e);
116         }
117     }
118
119     public int lastInsertId() {
120         try {
121             ResultSet rs = target.getGeneratedKeys();
122             rs.next();
123             int id = rs.getInt(1);
124             rs.close();
125             return id;
126         } catch (SQLException e) {
127             handleSQL(e);
128             throw new Error(e);
129         }
130     }
131
132     public void setBoolean(int parameterIndex, boolean x) {
133         try {
134             target.setBoolean(parameterIndex, x);
135         } catch (SQLException e) {
136             handleSQL(e);
137             throw new Error(e);
138         }
139     }
140
141     public int getParameterCount() {
142         try {
143             return target.getParameterMetaData().getParameterCount();
144         } catch (SQLException e) {
145             throw new Error(e);
146         }
147     }
148
149     private void handleSQL(SQLException e) {
150         // TODO Auto-generated method stub
151
152     }
153
154     @Override
155     public void close() {
156         GigiResultSet r = rs;
157         if (r != null) {
158             r.close();
159         }
160         PreparedStatement tg = target;
161         target = null;
162         try {
163             DatabaseConnection.getInstance().returnStatement(tg);
164         } catch (SQLException e) {
165             throw new Error(e);
166         }
167
168     }
169
170 }