]> WPIA git - gigi.git/blob - src/org/cacert/gigi/database/GigiPreparedStatement.java
Merge "upd: Reduce Boilerplate in translated SprintfCommands"
[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 setDate(int parameterIndex, Date x) {
80         try {
81             target.setDate(parameterIndex, x);
82         } catch (SQLException e) {
83             handleSQL(e);
84             throw new Error(e);
85         }
86     }
87
88     public void setTimestamp(int parameterIndex, Timestamp x) {
89         try {
90             target.setTimestamp(parameterIndex, x);
91         } catch (SQLException e) {
92             handleSQL(e);
93             throw new Error(e);
94         }
95     }
96
97     public int lastInsertId() {
98         try {
99             ResultSet rs = target.getGeneratedKeys();
100             rs.next();
101             int id = rs.getInt(1);
102             rs.close();
103             return id;
104         } catch (SQLException e) {
105             handleSQL(e);
106             throw new Error(e);
107         }
108     }
109
110     public void setBoolean(int parameterIndex, boolean x) {
111         try {
112             target.setBoolean(parameterIndex, x);
113         } catch (SQLException e) {
114             handleSQL(e);
115             throw new Error(e);
116         }
117     }
118
119     private void handleSQL(SQLException e) {
120         // TODO Auto-generated method stub
121
122     }
123
124     @Override
125     public void close() {
126         GigiResultSet r = rs;
127         if (r != null) {
128             r.close();
129         }
130         PreparedStatement tg = target;
131         target = null;
132         try {
133             DatabaseConnection.getInstance().returnStatement(tg);
134         } catch (SQLException e) {
135             throw new Error(e);
136         }
137
138     }
139
140 }