1 package com.quadcap.jdbc; 2 3 40 41 import java.io.IOException ; 42 43 import java.sql.SQLException ; 44 import java.sql.SQLWarning ; 45 46 import antlr.RecognitionException; 47 import antlr.TokenStreamException; 48 49 import com.quadcap.sql.Database; 50 import com.quadcap.sql.Session; 51 import com.quadcap.sql.SQLParser; 52 import com.quadcap.sql.Stmt; 53 54 import com.quadcap.util.ConfigNumber; 55 import com.quadcap.util.Debug; 56 import com.quadcap.util.Util; 57 58 69 public class Statement implements java.sql.Statement { 70 84 static final ConfigNumber trace = 85 ConfigNumber.find("qed.trace.Statement", "0"); 86 87 Connection conn; 88 com.quadcap.sql.Connection qConn = null; 89 Session session = null; 90 ResultSet rs = null; 91 int updateCount = -1; 92 protected boolean escapeProcessing = true; 93 int maxRows = -1; 94 95 98 public Statement(Connection conn) throws SQLException , IOException { 99 this.conn = conn; 100 this.qConn = conn.getConnection(); 101 this.session = qConn.createSession(); 102 if (trace.bit(0)) { 104 Debug.println(toString() + " created"); 105 } 106 } 108 109 113 public Statement(Connection conn, int resultSetType, 114 int resultSetConcurrency) 115 throws SQLException , IOException 116 { 117 this(conn); 118 this.resultSetType = resultSetType; 119 this.resultSetConcurrency = resultSetConcurrency; 120 } 121 122 125 public void finalize() throws Throwable { 126 try { 127 close(); 128 } catch (Throwable t) {} 129 super.finalize(); 130 } 131 132 137 public void addBatch(String sql) throws SQLException { 138 throw new SQLException ("not implemented", "0A000"); 139 } 140 141 144 public void cancel() throws SQLException { 145 } 146 147 152 public void clearBatch() throws SQLException { 153 throw new SQLException ("not implemented", "0A000"); 154 } 155 156 161 public void clearWarnings() { 162 } 163 164 167 public void close() { 168 if (trace.bit(0)) { 170 Debug.println(toString() + ".close()"); 171 } 172 if (session != null) { 174 try { 175 session.close(); 176 } catch (SQLException e) { 177 } catch (IOException e) { 178 } finally { 179 session = null; 180 qConn = null; 181 } 182 } 183 } 184 185 public String toString() { 187 return "Statement[" + session == null ? "" : session.toString() + "]"; 188 } 189 191 201 public boolean execute(String sql) throws SQLException { 202 if (trace.bit(1)) { 204 Debug.println(toString() + ".execute(" + sql + ")"); 205 } 206 if (this.qConn == null || this.qConn.isClosed()) { 208 throw new SQLException ("Connection closed"); 209 } 210 211 if (this.rs != null) { 212 this.rs.close(); 213 this.rs = null; 214 } 215 this.updateCount = -1; 216 217 session.makeTransaction(); 218 SQLParser p = new SQLParser(session, sql, escapeProcessing); 219 String auth = qConn.getAuth(); 220 String res = ""; 222 try { 224 try { 225 Stmt s = p.statement(); 226 if (s == null) { 227 throw new SQLException ("Parse error", "42000"); 228 } 229 session.doStatement(s); 230 this.rs = (ResultSet)session.getResultSet(this); 231 if (rs != null) { 232 res = "true"; 234 return true; 236 } else { 237 this.updateCount = session.getUpdateCount(); 238 res = String.valueOf(updateCount); 240 return false; 242 } 243 } catch (IOException e) { 244 res = e.toString(); 246 Debug.print(e); 248 session.endStatement(true); 249 throw new SQLException (e.toString(), "Q0012"); 250 } catch (RecognitionException e) { 251 Debug.print(e); 253 Debug.println("SQL: " + sql); 254 res = e.toString(); 255 session.endStatement(true); 257 SQLException se = new SQLException ("Statment: " + sql); 258 SQLException s2 = new SQLException (e.toString(), "42000"); 259 s2.setNextException(se); 260 throw s2; 261 } catch (TokenStreamException e) { 262 res = e.toString(); 264 session.endStatement(true); 266 SQLException se = new SQLException ("Statment: " + sql); 267 SQLException s2 = new SQLException (e.toString(), "42000"); 268 s2.setNextException(se); 269 throw s2; 270 } catch (SQLException e) { 271 res = e.toString(); 273 session.endStatement(true); 275 throw e; 276 } catch (Throwable e) { 277 res = e.toString(); 279 Debug.println("Throwable: " + e); 280 Debug.print(e); 281 session.endStatement(true); 283 throw new SQLException (e.toString(), "Q0013"); 284 } 285 } catch (IOException e) { 286 Debug.print(e); 287 throw new SQLException (e.toString(), "Q0014"); 288 } finally { 289 298 session.getConnection().setAuth(auth, null); 301 if (trace.bit(2)) { 303 Debug.println("Statement.execute(" + sql + ") = " + res); 304 } 305 } 307 } 308 309 314 public int[] executeBatch() throws SQLException { 315 throw new SQLException ("not implemented", "0A000"); 316 } 317 318 319 329 public java.sql.ResultSet executeQuery(String sql) throws SQLException { 330 if (execute(sql)) { 331 return this.rs; 332 } else { 333 return null; 334 } 335 } 336 337 346 public int executeUpdate(String sql) throws SQLException { 347 if (!execute(sql)) { 348 return this.updateCount; 349 } else { 350 return 0; 351 } 352 } 353 354 360 public java.sql.Connection getConnection() { 361 return conn; 362 } 363 364 369 public int getFetchSize() { 370 return 1; 371 } 372 373 378 public int getMaxFieldSize() { 379 return 0; 380 } 381 382 387 public int getMaxRows() { 388 return 0; 389 } 390 391 400 public boolean getMoreResults() throws SQLException { 401 if (rs != null) rs.close(); 402 this.updateCount = -1; 403 return false; 404 } 405 406 412 public int getQueryTimeout() { 413 return 0; 414 } 415 416 426 public java.sql.ResultSet getResultSet() { 427 return rs; 428 } 429 430 437 public int getUpdateCount() { 438 return updateCount; 439 } 440 441 447 public SQLWarning getWarnings() { 448 return null; 449 } 450 451 456 public void setCursorName(String name) throws SQLException { 457 throw new SQLException ("not implemented", "0A000"); 458 } 459 460 466 public void setEscapeProcessing(boolean enable) { 467 escapeProcessing = enable; 468 } 469 470 474 public void setFetchDirection(int dir) throws SQLException { 475 } 476 477 482 public void setFetchSize(int x) throws SQLException { 483 } 484 485 490 public void setMaxFieldSize(int x) throws SQLException { 491 throw new SQLException ("not implemented", "0A000"); 492 } 493 494 498 public void setMaxRows(int x) throws SQLException { 499 maxRows = x; 500 } 501 502 507 public void setQueryTimeout(int x) throws SQLException { 508 throw new SQLException ("not implemented", "0A000"); 509 } 510 511 int resultSetType = ResultSet.TYPE_SCROLL_INSENSITIVE; 512 int resultSetConcurrency = ResultSet.CONCUR_UPDATABLE; 513 514 520 public int getResultSetConcurrency() { 521 return resultSetConcurrency; 522 } 523 524 530 public int getResultSetType() { 531 return resultSetType; 532 } 533 534 540 public int getFetchDirection() { 541 return ResultSet.FETCH_FORWARD; 542 } 543 544 571 public boolean getMoreResults(int current) throws SQLException { 572 if (current == KEEP_CURRENT_RESULT) { 573 throw new SQLException ("JDBC 3.0 feature KEEP_CURRENT_RESULT " + 574 "not implemented"); 575 } 576 return getMoreResults(); 577 } 578 579 591 public java.sql.ResultSet getGeneratedKeys() throws SQLException { 592 throw new SQLException ("JDBC3.0 feature not implemented"); 593 } 594 595 618 public int executeUpdate(String sql, int autoGeneratedKeys) 619 throws SQLException 620 { 621 if (autoGeneratedKeys == RETURN_GENERATED_KEYS) { 622 throw new SQLException ("JDBC3.0 feature not implemented"); 623 } 624 return executeUpdate(sql); 625 } 626 627 646 public int executeUpdate(String sql, int columnIndexes[]) 647 throws SQLException 648 { 649 return executeUpdate(sql); 650 } 651 652 669 public int executeUpdate(String sql, String columnNames[]) 670 throws SQLException 671 { 672 return executeUpdate(sql); 673 } 674 675 711 public boolean execute(String sql, int autoGeneratedKeys) 712 throws SQLException 713 { 714 return execute(sql); 715 } 716 717 752 public boolean execute(String sql, int columnIndexes[]) 753 throws SQLException 754 { 755 return execute(sql); 756 } 757 758 794 public boolean execute(String sql, String columnNames[]) 795 throws SQLException 796 { 797 return execute(sql); 798 } 799 800 810 public int getResultSetHoldability() throws SQLException { 811 return ResultSet.CLOSE_CURSORS_AT_COMMIT; 812 } 813 815 } 816 | Popular Tags |