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