Debian Codegraph @

lang/sql/jdbc/SQLite/JDBC2/JDBCStatement.java

raw
1 package SQLite.JDBC2;
2
3 import java.sql.BatchUpdateException;
4 import java.sql.Connection;
5 import java.sql.ResultSet;
6 import java.sql.SQLException;
7 import java.sql.SQLWarning;
8 import java.sql.Statement;
9 import java.util.ArrayList;
10
11 public class JDBCStatement implements java.sql.Statement {
12
13 protected JDBCConnection conn;
14 protected JDBCResultSet rs;
15 protected int updcnt;
16 protected int maxrows = 0;
17 private ArrayList batch;
18
19 public JDBCStatement(JDBCConnection conn) {
20 this.conn = conn;
21 this.updcnt = 0;
22 this.rs = null;
23 this.batch = null;
24 }
25
26 public void setFetchSize(int fetchSize) throws SQLException {
27 if (fetchSize != 1) {
28 throw new SQLException("fetch size not 1");
29 }
30 }
31
32 public int getFetchSize() throws SQLException {
33 return 1;
34 }
35
36 public int getMaxRows() throws SQLException {
37 return maxrows;
38 }
39
40 public void setMaxRows(int max) throws SQLException {
41 if (max < 0) {
42 throw new SQLException("max must be >= 0 (was " + max + ")");
43 }
44 maxrows = max;
45 }
46
47 public void setFetchDirection(int fetchDirection) throws SQLException {
48 throw new SQLException("not supported");
49 }
50
51 public int getFetchDirection() throws SQLException {
52 return ResultSet.FETCH_UNKNOWN;
53 }
54
55 public int getResultSetConcurrency() throws SQLException {
56 return ResultSet.CONCUR_READ_ONLY;
57 }
58
59 public int getResultSetType() throws SQLException {
60 return ResultSet.TYPE_SCROLL_INSENSITIVE;
61 }
62
63 public void setQueryTimeout(int seconds) throws SQLException {
64 conn.timeout = seconds * 1000;
65 if (conn.timeout < 0) {
66 conn.timeout = 120000;
67 } else if (conn.timeout < 1000) {
68 conn.timeout = 5000;
69 }
70 }
71
72 public int getQueryTimeout() throws SQLException {
73 return conn.timeout / 1000;
74 }
75
76 public ResultSet getResultSet() throws SQLException {
77 return rs;
78 }
79
80 ResultSet executeQuery(String sql, String args[], boolean updonly)
81 throws SQLException {
82 SQLite.TableResult tr = null;
83 if (rs != null) {
84 rs.close();
85 rs = null;
86 }
87 updcnt = -1;
88 if (conn == null || conn.db == null) {
89 throw new SQLException("stale connection");
90 }
91 int busy = 0;
92 boolean starttrans = !conn.autocommit && !conn.intrans;
93 while (true) {
94 try {
95 if (starttrans) {
96 conn.db.exec("BEGIN TRANSACTION", null);
97 conn.intrans = true;
98 }
99 if (args == null) {
100 if (updonly) {
101 conn.db.exec(sql, null);
102 } else {
103 tr = conn.db.get_table(sql, maxrows);
104 }
105 } else {
106 if (updonly) {
107 conn.db.exec(sql, null, args);
108 } else {
109 tr = conn.db.get_table(sql, maxrows, args);
110 }
111 }
112 updcnt = (int) conn.db.changes();
113 } catch (SQLite.Exception e) {
114 if (conn.db.is3() &&
115 conn.db.last_error() == SQLite.Constants.SQLITE_BUSY &&
116 conn.busy3(conn.db, ++busy)) {
117 try {
118 if (starttrans && conn.intrans) {
119 conn.db.exec("ROLLBACK", null);
120 conn.intrans = false;
121 }
122 } catch (SQLite.Exception ee) {
123 }
124 try {
125 int ms = 20 + busy * 10;
126 if (ms > 1000) {
127 ms = 1000;
128 }
129 synchronized (this) {
130 this.wait(ms);
131 }
132 } catch (java.lang.Exception eee) {
133 }
134 continue;
135 }
136 throw new SQLException(e.toString());
137 }
138 break;
139 }
140 if (!updonly && tr == null) {
141 throw new SQLException("no result set produced");
142 }
143 if (!updonly && tr != null) {
144 rs = new JDBCResultSet(new TableResultX(tr), this);
145 }
146 return rs;
147 }
148
149 public ResultSet executeQuery(String sql) throws SQLException {
150 return executeQuery(sql, null, false);
151 }
152
153 public boolean execute(String sql) throws SQLException {
154 return executeQuery(sql) != null;
155 }
156
157 public void cancel() throws SQLException {
158 if (conn == null || conn.db == null) {
159 throw new SQLException("stale connection");
160 }
161 conn.db.interrupt();
162 }
163
164 public void clearWarnings() throws SQLException {
165 }
166
167 public Connection getConnection() throws SQLException {
168 return conn;
169 }
170
171 public void addBatch(String sql) throws SQLException {
172 if (batch == null) {
173 batch = new ArrayList(1);
174 }
175 batch.add(sql);
176 }
177
178 public int[] executeBatch() throws SQLException {
179 if (batch == null) {
180 return new int[0];
181 }
182 int[] ret = new int[batch.size()];
183 for (int i = 0; i < ret.length; i++) {
184 ret[i] = EXECUTE_FAILED;
185 }
186 int errs = 0;
187 for (int i = 0; i < ret.length; i++) {
188 try {
189 execute(batch.get(i));
190 ret[i] = updcnt;
191 } catch (SQLException e) {
192 ++errs;
193 }
194 }
195 if (errs > 0) {
196 throw new BatchUpdateException("batch failed", ret);
197 }
198 return ret;
199 }
200
201 public void clearBatch() throws SQLException {
202 if (batch != null) {
203 batch.clear();
204 batch = null;
205 }
206 }
207
208 public void close() throws SQLException {
209 clearBatch();
210 conn = null;
211 }
212
213 public int executeUpdate(String sql) throws SQLException {
214 executeQuery(sql, null, true);
215 return updcnt;
216 }
217
218 public int getMaxFieldSize() throws SQLException {
219 return 0;
220 }
221
222 public boolean getMoreResults() throws SQLException {
223 if (rs != null) {
224 rs.close();
225 rs = null;
226 }
227 return false;
228 }
229
230 public int getUpdateCount() throws SQLException {
231 return updcnt;
232 }
233
234 public SQLWarning getWarnings() throws SQLException {
235 return null;
236 }
237
238 public void setCursorName(String name) throws SQLException {
239 throw new SQLException("not supported");
240 }
241
242 public void setEscapeProcessing(boolean enable) throws SQLException {
243 throw new SQLException("not supported");
244 }
245
246 public void setMaxFieldSize(int max) throws SQLException {
247 throw new SQLException("not supported");
248 }
249
250 }