]> WPIA git - gigi.git/blob - src/org/cacert/gigi/database/GigiPreparedStatement.java
fix: SQL change database call pattern
[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             if (scroll) {
26                 target = DatabaseConnection.getInstance().prepareInternalScrollable(stmt);
27             } else {
28                 target = DatabaseConnection.getInstance().prepareInternal(stmt);
29             }
30         } catch (SQLException e) {
31             throw new Error(e);
32         }
33     }
34
35     public GigiResultSet executeQuery() {
36         try {
37             return rs = new GigiResultSet(target.executeQuery());
38         } catch (SQLException e) {
39             handleSQL(e);
40             throw new Error(e);
41         }
42     }
43
44     public void executeUpdate() {
45         try {
46             int updated = target.executeUpdate();
47             if (updated != 1) {
48                 throw new Error("FATAL: multiple or no data updated: " + updated);
49             }
50         } catch (SQLException e) {
51             handleSQL(e);
52             throw new Error(e);
53         }
54     }
55
56     public boolean execute() {
57         try {
58             return target.execute();
59         } catch (SQLException e) {
60             handleSQL(e);
61             throw new Error(e);
62         }
63     }
64
65     public void setInt(int parameterIndex, int x) {
66         try {
67             target.setInt(parameterIndex, x);
68         } catch (SQLException e) {
69             handleSQL(e);
70             throw new Error(e);
71         }
72     }
73
74     public void setString(int parameterIndex, String x) {
75         try {
76             target.setString(parameterIndex, x);
77         } catch (SQLException e) {
78             handleSQL(e);
79             throw new Error(e);
80         }
81     }
82
83     public void setDate(int parameterIndex, Date x) {
84         try {
85             target.setDate(parameterIndex, x);
86         } catch (SQLException e) {
87             handleSQL(e);
88             throw new Error(e);
89         }
90     }
91
92     public void setTimestamp(int parameterIndex, Timestamp x) {
93         try {
94             target.setTimestamp(parameterIndex, x);
95         } catch (SQLException e) {
96             handleSQL(e);
97             throw new Error(e);
98         }
99     }
100
101     public int lastInsertId() {
102         try {
103             ResultSet rs = target.getGeneratedKeys();
104             rs.next();
105             int id = rs.getInt(1);
106             rs.close();
107             return id;
108         } catch (SQLException e) {
109             handleSQL(e);
110             throw new Error(e);
111         }
112     }
113
114     public void setBoolean(int parameterIndex, boolean x) {
115         try {
116             target.setBoolean(parameterIndex, x);
117         } catch (SQLException e) {
118             handleSQL(e);
119             throw new Error(e);
120         }
121     }
122
123     private void handleSQL(SQLException e) {
124         // TODO Auto-generated method stub
125
126     }
127
128     @Override
129     public void close() {
130         GigiResultSet r = rs;
131         if (r != null) {
132             r.close();
133         }
134         PreparedStatement tg = target;
135         target = null;
136         DatabaseConnection.getInstance().returnStatement(tg);
137
138     }
139
140 }