]> WPIA git - gigi.git/blob - src/org/cacert/gigi/database/GigiPreparedStatement.java
Fix error message
[gigi.git] / src / org / cacert / gigi / database / GigiPreparedStatement.java
1 package org.cacert.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 execute() {
53         try {
54             return target.execute();
55         } catch (SQLException e) {
56             handleSQL(e);
57             throw new Error(e);
58         }
59     }
60
61     public void setInt(int parameterIndex, int x) {
62         try {
63             target.setInt(parameterIndex, x);
64         } catch (SQLException e) {
65             handleSQL(e);
66             throw new Error(e);
67         }
68     }
69
70     public void setString(int parameterIndex, String x) {
71         try {
72             target.setString(parameterIndex, x);
73         } catch (SQLException e) {
74             handleSQL(e);
75             throw new Error(e);
76         }
77     }
78
79     public void setEnum(int parameterIndex, DBEnum x) {
80         try {
81             target.setString(parameterIndex, x.getDBName());
82         } catch (SQLException e) {
83             handleSQL(e);
84             throw new Error(e);
85         }
86     }
87
88     public void setDate(int parameterIndex, Date x) {
89         try {
90             target.setDate(parameterIndex, x);
91         } catch (SQLException e) {
92             handleSQL(e);
93             throw new Error(e);
94         }
95     }
96
97     public void setTimestamp(int parameterIndex, Timestamp x) {
98         try {
99             target.setTimestamp(parameterIndex, x);
100         } catch (SQLException e) {
101             handleSQL(e);
102             throw new Error(e);
103         }
104     }
105
106     public int lastInsertId() {
107         try {
108             ResultSet rs = target.getGeneratedKeys();
109             rs.next();
110             int id = rs.getInt(1);
111             rs.close();
112             return id;
113         } catch (SQLException e) {
114             handleSQL(e);
115             throw new Error(e);
116         }
117     }
118
119     public void setBoolean(int parameterIndex, boolean x) {
120         try {
121             target.setBoolean(parameterIndex, x);
122         } catch (SQLException e) {
123             handleSQL(e);
124             throw new Error(e);
125         }
126     }
127
128     public int getParameterCount() {
129         try {
130             return target.getParameterMetaData().getParameterCount();
131         } catch (SQLException e) {
132             throw new Error(e);
133         }
134     }
135
136     private void handleSQL(SQLException e) {
137         // TODO Auto-generated method stub
138
139     }
140
141     @Override
142     public void close() {
143         GigiResultSet r = rs;
144         if (r != null) {
145             r.close();
146         }
147         PreparedStatement tg = target;
148         target = null;
149         try {
150             DatabaseConnection.getInstance().returnStatement(tg);
151         } catch (SQLException e) {
152             throw new Error(e);
153         }
154
155     }
156
157 }