Imported Upstream version 5.3.21
[platform/upstream/libdb.git] / lang / sql / jdbc / SQLite / JDBC1 / JDBCStatement.java
1 package SQLite.JDBC1;
2
3 import java.sql.*;
4 import java.util.*;
5
6 public class JDBCStatement implements java.sql.Statement {
7
8     protected JDBCConnection conn;
9     protected JDBCResultSet rs;
10     protected int updcnt;
11     protected int maxrows = 0;
12
13     public JDBCStatement(JDBCConnection conn) {
14         this.conn = conn;
15     }
16
17     public void setFetchSize(int fetchSize) throws SQLException {
18         if (fetchSize != 1) {
19             throw new SQLException("fetch size not 1");
20         }
21     }
22
23     public int getFetchSize() throws SQLException {
24         return 1;
25     }
26
27     public int getMaxRows() throws SQLException {
28         return maxrows;
29     }
30
31     public void setMaxRows(int max) throws SQLException {
32         if (max < 0) {
33             throw new SQLException("max must be >= 0 (was " + max + ")");
34         }
35         maxrows = max;
36     }
37
38     public void setQueryTimeout(int seconds) throws SQLException {
39         conn.timeout = seconds * 1000;
40         if (conn.timeout < 0) {
41             conn.timeout = 120000;
42         } else if (conn.timeout < 1000) {
43             conn.timeout = 5000;
44         }
45     }
46
47     public int getQueryTimeout() throws SQLException {
48         return conn.timeout / 1000;
49     }
50
51     public ResultSet getResultSet() throws SQLException {
52         return rs;
53     }
54
55     ResultSet executeQuery(String sql, String args[], boolean updonly)
56         throws SQLException {
57         SQLite.TableResult tr = null;
58         if (rs != null) {
59             rs.close();
60             rs = null;
61         }
62         updcnt = -1;
63         if (conn == null || conn.db == null) {
64             throw new SQLException("stale connection");
65         }
66         int busy = 0;
67         boolean starttrans = !conn.autocommit && !conn.intrans;
68         while (true) {
69             try {
70                 if (starttrans) {
71                     conn.db.exec("BEGIN TRANSACTION", null);
72                     conn.intrans = true;
73                 }
74                 if (args == null) {
75                     if (updonly) {
76                         conn.db.exec(sql, null);
77                     } else {
78                         tr = conn.db.get_table(sql, maxrows);
79                     }
80                 } else {
81                     if (updonly) {
82                         conn.db.exec(sql, null, args);
83                     } else {
84                         tr = conn.db.get_table(sql, maxrows, args);
85                     }
86                 }
87                 updcnt = (int) conn.db.changes();
88             } catch (SQLite.Exception e) {
89                 if (conn.db.is3() &&
90                     conn.db.last_error() == SQLite.Constants.SQLITE_BUSY &&
91                     conn.busy3(conn.db, ++busy)) {
92                     try {
93                         if (starttrans && conn.intrans) {
94                             conn.db.exec("ROLLBACK", null);
95                             conn.intrans = false;
96                         }
97                     } catch (SQLite.Exception ee) {
98                     }
99                     try {
100                         int ms = 20 + busy * 10;
101                         if (ms > 1000) {
102                             ms = 1000;
103                         }
104                         synchronized (this) {
105                             this.wait(ms);
106                         }
107                     } catch (java.lang.Exception eee) {
108                     }
109                     continue;
110                 }
111                 throw new SQLException(e.toString());
112             }
113             break;
114         }
115         if (!updonly && tr == null) {
116             throw new SQLException("no result set produced");
117         }
118         if (!updonly && tr != null) {
119             rs = new JDBCResultSet(new TableResultX(tr), this);
120         }
121         return rs;
122     }
123
124     public ResultSet executeQuery(String sql) throws SQLException {
125         return executeQuery(sql, null, false);
126     }
127
128     public boolean execute(String sql) throws SQLException {
129         return executeQuery(sql) != null;
130     }
131
132     public void cancel() throws SQLException {
133         if (conn == null || conn.db == null) {
134             throw new SQLException("stale connection");
135         }
136         conn.db.interrupt();
137     }
138
139     public void clearWarnings() throws SQLException {
140     }
141
142     public Connection getConnection() throws SQLException {
143         return conn;
144     }
145
146     public void addBatch(String sql) throws SQLException {
147         throw new SQLException("not supported");
148     }
149
150     public int[] executeBatch() throws SQLException {
151         throw new SQLException("not supported");
152     }
153
154     public void clearBatch() throws SQLException {
155         throw new SQLException("not supported");
156     }
157
158     public void close() throws SQLException {
159         conn = null;
160     }
161
162     public int executeUpdate(String sql) throws SQLException {
163         executeQuery(sql, null, true);
164         return updcnt;
165     }
166
167     public int getMaxFieldSize() throws SQLException {
168         return 0;
169     }
170
171     public boolean getMoreResults() throws SQLException {
172         if (rs != null) {
173             rs.close();
174             rs = null;
175         }
176         return false;
177     }
178
179     public int getUpdateCount() throws SQLException {
180         return updcnt;
181     }
182
183     public SQLWarning getWarnings() throws SQLException {
184         return null;
185     }
186
187     public void setCursorName(String name) throws SQLException {
188         throw new SQLException("not supported");
189     }
190
191     public void setEscapeProcessing(boolean enable) throws SQLException {
192         throw new SQLException("not supported");
193     }
194
195     public void setMaxFieldSize(int max) throws SQLException {
196         throw new SQLException("not supported");
197     }
198
199 }