1 24 25 package org.objectweb.cjdbc.driver; 26 27 import java.sql.BatchUpdateException ; 28 import java.sql.ResultSet ; 29 import java.sql.SQLException ; 30 import java.sql.SQLWarning ; 31 import java.util.Vector ; 32 33 import org.objectweb.cjdbc.common.exceptions.NotImplementedException; 34 import org.objectweb.cjdbc.common.sql.AbstractWriteRequest; 35 import org.objectweb.cjdbc.common.sql.AlterRequest; 36 import org.objectweb.cjdbc.common.sql.CreateRequest; 37 import org.objectweb.cjdbc.common.sql.DeleteRequest; 38 import org.objectweb.cjdbc.common.sql.DropRequest; 39 import org.objectweb.cjdbc.common.sql.InsertRequest; 40 import org.objectweb.cjdbc.common.sql.SelectRequest; 41 import org.objectweb.cjdbc.common.sql.StoredProcedure; 42 import org.objectweb.cjdbc.common.sql.UpdateRequest; 43 44 63 public class Statement implements java.sql.Statement 64 { 65 66 protected Connection connection = null; 67 68 69 protected Vector batch = null; 70 71 72 private SQLWarning warnings = null; 73 74 75 protected ResultSet result = null; 76 77 78 protected int updateCount = -1; 79 80 81 private int timeout = 0; 82 83 84 private int fetchSize = 0; 85 86 private String cursorName; 87 88 89 private int resultSetType = ResultSet.TYPE_FORWARD_ONLY; 90 91 92 private int resultSetConcurrency = ResultSet.CONCUR_READ_ONLY; 93 94 95 private int maxFieldSize = 0; 96 97 98 private int maxRows = 0; 99 100 104 private int fetchDirection = ResultSet.FETCH_FORWARD; 105 106 109 protected boolean escapeProcessing = true; 110 111 112 protected ResultSet generatedKeys = null; 113 protected int generatedKeysFlag = java.sql.Statement.NO_GENERATED_KEYS; 114 115 120 public Statement(Connection c) 121 { 122 connection = c; 123 } 124 125 131 public synchronized void addBatch(String sql) throws SQLException 132 { 133 if (batch == null) 134 batch = new Vector (); 135 batch.addElement(sql.trim()); 136 } 137 138 144 public void cancel() throws SQLException 145 { 146 throw new NotImplementedException("cancel()"); 147 } 148 149 154 public void clearBatch() throws SQLException 155 { 156 if (batch != null) 157 batch.removeAllElements(); 158 } 159 160 166 public void clearWarnings() throws SQLException 167 { 168 warnings = null; 169 } 170 171 181 public int[] executeBatch() throws BatchUpdateException 182 { 183 if (batch == null || batch.isEmpty()) 184 return new int[0]; 185 186 int size = batch.size(); 187 int[] batchResult = new int[size]; 188 int i = 0; 189 190 try 191 { 192 for (i = 0; i < size; i++) 193 batchResult[i] = this.executeUpdate((String ) batch.elementAt(i)); 194 return batchResult; 195 } 196 catch (SQLException e) 197 { 198 String message = "Batch failed for request " + i + ": " 199 + batch.elementAt(i) + " (" + e + ")"; 200 201 int[] updateCounts = new int[i]; 202 System.arraycopy(batchResult, 0, updateCounts, 0, i); 203 204 throw new BatchUpdateException (message, updateCounts); 205 } 206 finally 207 { 208 batch.removeAllElements(); 209 } 210 } 211 212 223 public void close() throws SQLException 224 { 225 if (result != null) 227 result.close(); 228 229 result = null; 231 connection = null; 232 } 233 234 241 public boolean execute(String sql) throws SQLException 242 { 243 int start = 0; 244 try 245 { 246 while (sql.charAt(start) == '(') 248 start++; 249 } 250 catch (IndexOutOfBoundsException e) 251 { 252 start = 0; 255 } 256 257 if (sql.regionMatches(true, start, "select", 0, 6) 258 || (sql.regionMatches(true, start, "{call", 0, 5)) 259 || (sql.regionMatches(true, start, "values", 0, 6))) 260 { 261 result = executeQuery(sql); 262 return true; 263 } 264 else 265 { 266 updateCount = executeUpdate(sql); 267 return false; 268 } 269 } 270 271 278 public java.sql.ResultSet executeQuery(String sql) throws SQLException 279 { 280 return executeQuery(null, sql.trim()); 281 } 282 283 293 protected java.sql.ResultSet executeQuery(String sqlSkeleton, String sqlQuery) 294 throws SQLException 295 { 296 if (isClosed()) 297 { 298 throw new SQLException ("Unable to execute query on a closed statement"); 299 } 300 updateCount = -1; if (result != null) 302 { result.close(); 304 result = null; 305 } 306 307 if (sqlQuery.regionMatches(true, 0, "{call", 0, 5)) 308 { 309 StoredProcedure proc = new StoredProcedure(sqlQuery, escapeProcessing, 310 timeout, Connection.LINE_SEPARATOR, true ); 311 if (connection.controllerNeedsSqlSkeleton || !connection.isDriverProcessed()) 312 proc.setSqlSkeleton(sqlSkeleton); 313 proc.setMaxRows(maxRows); 314 proc.setFetchSize(fetchSize); 315 proc.setCursorName(cursorName); 316 result = connection.execReadStoredProcedure(proc); 317 } 318 else 319 { 320 SelectRequest request = new SelectRequest(sqlQuery, escapeProcessing, 321 timeout, Connection.LINE_SEPARATOR); 322 if (connection.controllerNeedsSqlSkeleton || !connection.isDriverProcessed()) 323 request.setSqlSkeleton(sqlSkeleton); 324 request.setMaxRows(maxRows); 325 request.setFetchSize(fetchSize); 326 request.setCursorName(cursorName); 327 result = connection.execReadRequest(request); 328 } 329 330 if (result instanceof DriverResultSet) 331 ((DriverResultSet) result).setStatement(this); 332 return result; 333 } 334 335 343 public int executeUpdate(String sql) throws SQLException 344 { 345 return executeUpdateWithSkeleton(null, sql.trim()); 346 } 347 348 358 protected int executeUpdateWithSkeleton(String sqlSkeleton, String sqlQuery) 359 throws SQLException 360 { 361 if (isClosed()) 362 { 363 throw new SQLException ("Unable to execute query on a closed statement"); 364 } 365 if (result != null) 366 { result.close(); 368 result = null; 369 } 370 371 String lower = sqlQuery.substring(0, 374 6 < sqlQuery.length() ? 6 : sqlQuery.length()).toLowerCase(); 375 AbstractWriteRequest request; 376 if (lower.equals("insert")) 377 request = new InsertRequest(sqlQuery, escapeProcessing, timeout, 378 Connection.LINE_SEPARATOR, 379 (Statement.RETURN_GENERATED_KEYS == generatedKeysFlag) ); 380 else if (lower.equals("update")) 381 request = new UpdateRequest(sqlQuery, escapeProcessing, timeout, 382 Connection.LINE_SEPARATOR); 383 else if (lower.equals("delete")) 384 request = new DeleteRequest(sqlQuery, escapeProcessing, timeout, 385 Connection.LINE_SEPARATOR); 386 else if (lower.startsWith("create")) 387 request = new CreateRequest(sqlQuery, escapeProcessing, timeout, 388 Connection.LINE_SEPARATOR); 389 else if (lower.startsWith("drop")) 390 request = new DropRequest(sqlQuery, escapeProcessing, timeout, 391 Connection.LINE_SEPARATOR); 392 else if (lower.startsWith("alter")) 393 request = new AlterRequest(sqlQuery, escapeProcessing, timeout, 394 Connection.LINE_SEPARATOR); 395 else if (lower.startsWith("{call")) 396 { StoredProcedure proc = new StoredProcedure(sqlQuery, escapeProcessing, 398 timeout, Connection.LINE_SEPARATOR, 399 (Statement.RETURN_GENERATED_KEYS == generatedKeysFlag) ); 400 if (connection.controllerNeedsSqlSkeleton || !connection.isDriverProcessed()) 401 proc.setSqlSkeleton(sqlSkeleton); 402 updateCount = connection.execWriteStoredProcedure(proc); 403 return updateCount; 404 } 405 else if (lower.startsWith("}call")) 406 { StoredProcedure proc = new StoredProcedure("{" + sqlQuery.substring(1), 409 escapeProcessing, timeout, Connection.LINE_SEPARATOR, 410 (Statement.RETURN_GENERATED_KEYS == generatedKeysFlag) ); 411 if (connection.controllerNeedsSqlSkeleton || !connection.isDriverProcessed()) 412 proc.setSqlSkeleton(sqlSkeleton); 413 updateCount = connection.execWriteStoredProcedure(proc); 414 return updateCount; 415 } 416 else 417 throw new SQLException ( 418 "executeUpdate only accepts statements starting with insert/update/delete/create/drop/{call (" 419 + sqlQuery + ")"); 420 421 if (connection.controllerNeedsSqlSkeleton || !connection.isDriverProcessed()) 422 request.setSqlSkeleton(sqlSkeleton); 423 424 if (generatedKeysFlag == Statement.RETURN_GENERATED_KEYS) 425 { generatedKeys = connection.execWriteRequestWithKeys(request); 427 428 434 return 1; 435 } 436 else 437 { updateCount = connection.execWriteRequest(request); 439 return updateCount; 440 } 441 } 442 443 449 public java.sql.Connection getConnection() throws SQLException 450 { 451 return connection; 452 } 453 454 457 public int getFetchDirection() throws SQLException 458 { 459 return fetchDirection; 460 } 461 462 465 public int getFetchSize() throws SQLException 466 { 467 return fetchSize; 468 } 469 470 482 public int getMaxFieldSize() throws SQLException 483 { 484 return maxFieldSize; 485 } 486 487 495 public int getMaxRows() throws SQLException 496 { 497 return maxRows; 498 } 499 500 507 public boolean getMoreResults() throws SQLException 508 { 509 if (result != null) 510 result.close(); 511 updateCount = -1; 512 return false; 513 } 514 515 523 public int getQueryTimeout() throws SQLException 524 { 525 return timeout; 526 } 527 528 534 public java.sql.ResultSet getResultSet() throws SQLException 535 { 536 return result; 537 } 538 539 545 public int getResultSetConcurrency() throws SQLException 546 { 547 return resultSetConcurrency; 548 } 549 550 557 public int getResultSetType() throws SQLException 558 { 559 return resultSetType; 560 } 561 562 570 public int getUpdateCount() throws SQLException 571 { 572 return updateCount; 573 } 574 575 590 public SQLWarning getWarnings() throws SQLException 591 { 592 return warnings; 593 } 594 595 606 public void setCursorName(String name) throws SQLException 607 { 608 cursorName = name; 609 } 610 611 618 public void setEscapeProcessing(boolean enable) throws SQLException 619 { 620 escapeProcessing = enable; 621 } 622 623 626 public void setFetchDirection(int direction) throws SQLException 627 { 628 if ((direction == ResultSet.FETCH_FORWARD) 629 || (direction == ResultSet.FETCH_REVERSE) 630 || (direction == ResultSet.FETCH_UNKNOWN)) 631 this.fetchDirection = direction; 632 else 633 throw new SQLException ("Unsupported direction " + direction 634 + " in setFetchDirection"); 635 } 636 637 644 public void setFetchSize(int rows) throws SQLException 645 { 646 if (rows < 0 647 || 0 < maxRows && maxRows < rows) 649 { 650 throw new SQLException ("Invalid fetch size value: " + rows); 651 } 652 655 fetchSize = rows; 656 } 657 658 665 public void setMaxFieldSize(int max) throws SQLException 666 { 667 if (max < 0) 668 { 669 throw new SQLException ("Invalid max field size value: " + max); 670 } 671 maxFieldSize = max; 672 } 673 674 682 public void setMaxRows(int max) throws SQLException 683 { 684 if (max < 0) 685 { 686 throw new SQLException ("Invalid max rows limit: " + max); 687 } 688 maxRows = max; 690 } 691 692 700 public void setQueryTimeout(int seconds) throws SQLException 701 { 702 if (seconds < 0) 703 { 704 throw new SQLException ("Invalid query timeout value: " + seconds); 705 } 706 timeout = seconds; 707 } 708 709 713 public void setResultSetConcurrency(int value) throws SQLException 714 { 715 switch (value) 716 { 717 case ResultSet.CONCUR_READ_ONLY : 718 case ResultSet.CONCUR_UPDATABLE : 719 resultSetConcurrency = value; 720 break; 721 default : 722 throw new SQLException ("Invalid ResultSet " + "concurrency mode: " 723 + value); 724 } 725 } 726 727 731 public void setResultSetType(int value) throws SQLException 732 { 733 switch (value) 734 { 735 case ResultSet.TYPE_FORWARD_ONLY : 736 case ResultSet.TYPE_SCROLL_INSENSITIVE : 737 resultSetType = value; 738 break; 739 case ResultSet.TYPE_SCROLL_SENSITIVE : 740 throw new SQLException ( 741 "TYPE_SCROLL_SENSITIVE is not a supported ResultSet type"); 742 default : 743 throw new SQLException ("Invalid ResultSet type"); 744 } 745 } 746 747 749 785 public boolean getMoreResults(int current) throws SQLException 786 { 787 throw new NotImplementedException("getMoreResults"); 788 } 789 790 802 public java.sql.ResultSet getGeneratedKeys() throws SQLException 803 { 804 return generatedKeys; 805 } 806 807 827 public int executeUpdate(String sql, int autoGeneratedKeys) 828 throws SQLException 829 { 830 generatedKeysFlag = autoGeneratedKeys; 831 return executeUpdate(sql); 832 } 833 834 852 public int executeUpdate(String sql, int[] columnIndexes) throws SQLException 853 { 854 throw new NotImplementedException("executeUpdate"); 855 } 856 857 874 public int executeUpdate(String sql, String [] columnNames) 875 throws SQLException 876 { 877 throw new NotImplementedException("executeUpdate"); 878 } 879 880 913 public boolean execute(String sql, int autoGeneratedKeys) throws SQLException 914 { 915 generatedKeysFlag = autoGeneratedKeys; 916 return execute(sql); 917 } 918 919 952 public boolean execute(String sql, int[] columnIndexes) throws SQLException 953 { 954 throw new NotImplementedException("execute"); 955 } 956 957 990 public boolean execute(String sql, String [] columnNames) throws SQLException 991 { 992 throw new NotImplementedException("execute"); 993 } 994 995 1004 public int getResultSetHoldability() throws SQLException 1005 { 1006 throw new NotImplementedException("getResultSetHoldability"); 1007 } 1008 1009 1014 private boolean isClosed() 1015 { 1016 return (connection == null); 1017 } 1018} | Popular Tags |