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