| 1 30 31 32 package org.hsqldb.jdbc; 33 34 import java.sql.CallableStatement ; 35 import java.sql.Connection ; 36 import java.sql.DatabaseMetaData ; 37 import java.sql.PreparedStatement ; 38 import java.sql.SQLException ; 39 import java.sql.SQLWarning ; 40 import java.sql.Statement ; 41 42 import java.sql.Savepoint ; 44 45 import java.util.Map ; 48 49 import java.util.Locale ; 51 52 import org.hsqldb.DatabaseManager; 53 import org.hsqldb.DatabaseURL; 54 import org.hsqldb.HSQLClientConnection; 55 import org.hsqldb.HTTPClientConnection; 56 import org.hsqldb.HsqlException; 57 import org.hsqldb.persist.HsqlProperties; 58 import org.hsqldb.Result; 59 import org.hsqldb.ResultConstants; 60 import org.hsqldb.Session; 61 import org.hsqldb.SessionInterface; 62 import org.hsqldb.Trace; 63 import org.hsqldb.lib.StringUtil; 64 65 82 403 public class jdbcConnection implements Connection { 404 405 407 411 HsqlProperties connProperties; 412 413 417 SessionInterface sessionProxy; 418 419 422 boolean isInternal; 423 424 425 protected boolean isNetConn; 426 427 430 boolean isClosed; 431 432 433 private SQLWarning rootWarning; 434 435 436 private Object rootWarning_mutex = new Object (); 437 438 443 447 448 450 497 public synchronized Statement createStatement() throws SQLException { 498 499 checkClosed(); 500 501 Statement stmt = new jdbcStatement(this, 502 jdbcResultSet.TYPE_FORWARD_ONLY); 503 504 return stmt; 505 } 506 507 555 public synchronized PreparedStatement prepareStatement(String sql) 556 throws SQLException { 557 558 PreparedStatement stmt; 559 560 checkClosed(); 561 562 try { 563 stmt = new jdbcPreparedStatement(this, sql, 564 jdbcResultSet.TYPE_FORWARD_ONLY); 565 566 return stmt; 567 } catch (HsqlException e) { 568 throw Util.sqlException(e); 569 } 570 } 571 572 614 public synchronized CallableStatement prepareCall(String sql) 615 throws SQLException { 616 617 CallableStatement stmt; 618 619 checkClosed(); 620 621 try { 622 stmt = new jdbcCallableStatement(this, sql, 623 jdbcResultSet.TYPE_FORWARD_ONLY); 624 625 return stmt; 626 } catch (HsqlException e) { 627 throw Util.sqlException(e); 628 } 629 } 630 631 706 public synchronized String nativeSQL(final String sql) 707 throws SQLException { 708 709 checkClosed(); 716 717 if (sql == null || sql.length() == 0 || sql.indexOf('{') == -1) { 719 return sql; 720 } 721 722 int state = 0; 724 int len = sql.length(); 725 int nest = 0; 726 StringBuffer sb = new StringBuffer (sql.length()); String msg; 728 729 final int outside_all = 0; 731 final int outside_escape_inside_single_quotes = 1; 732 final int outside_escape_inside_double_quotes = 2; 733 734 final int inside_escape = 3; 736 final int inside_escape_inside_single_quotes = 4; 737 final int inside_escape_inside_double_quotes = 5; 738 739 sb.append(sql); 746 747 for (int i = 0; i < len; i++) { 748 char c = sb.charAt(i); 749 750 switch (state) { 751 752 case outside_all : if (c == '\'') { 754 state = outside_escape_inside_single_quotes; 755 } else if (c == '"') { 756 state = outside_escape_inside_double_quotes; 757 } else if (c == '{') { 758 i = onStartEscapeSequence(sql, sb, i); 759 760 nest++; 762 763 state = inside_escape; 764 } 765 break; 766 767 case outside_escape_inside_single_quotes : case inside_escape_inside_single_quotes : if (c == '\'') { 770 state -= 1; 771 } 772 break; 773 774 case outside_escape_inside_double_quotes : case inside_escape_inside_double_quotes : if (c == '"') { 777 state -= 2; 778 } 779 break; 780 781 case inside_escape : if (c == '\'') { 783 state = inside_escape_inside_single_quotes; 784 } else if (c == '"') { 785 state = inside_escape_inside_double_quotes; 786 } else if (c == '}') { 787 sb.setCharAt(i, ' '); 788 789 nest--; 791 792 state = (nest == 0) ? outside_all 793 : inside_escape; 794 } else if (c == '{') { 795 i = onStartEscapeSequence(sql, sb, i); 796 797 nest++; 799 800 state = inside_escape; 801 } 802 } 803 } 804 805 return sb.toString(); 806 } 807 808 857 public synchronized void setAutoCommit(boolean autoCommit) 858 throws SQLException { 859 860 checkClosed(); 861 862 try { 863 sessionProxy.setAutoCommit(autoCommit); 864 } catch (HsqlException e) { 865 throw Util.sqlException(e); 866 } 867 } 868 869 876 public synchronized boolean getAutoCommit() throws SQLException { 877 878 checkClosed(); 879 880 try { 881 return sessionProxy.isAutoCommit(); 882 } catch (HsqlException e) { 883 throw Util.sqlException(e); 884 } 885 } 886 887 916 public synchronized void commit() throws SQLException { 917 918 checkClosed(); 919 920 try { 921 sessionProxy.commit(); 922 } catch (HsqlException e) { 923 throw Util.sqlException(e); 924 } 925 } 926 927 956 public synchronized void rollback() throws SQLException { 957 958 checkClosed(); 959 960 try { 961 sessionProxy.rollback(); 962 } catch (HsqlException e) { 963 throw Util.sqlException(e); 964 } 965 } 966 967 992 public synchronized void close() throws SQLException { 993 994 if (isInternal || isClosed) { 999 return; 1000 } 1001 1002 isClosed = true; 1003 1004 if (sessionProxy != null) { 1005 sessionProxy.close(); 1006 } 1007 1008 sessionProxy = null; 1009 rootWarning = null; 1010 connProperties = null; 1011 } 1012 1013 1019 public synchronized boolean isClosed() { 1020 return isClosed; 1021 } 1022 1023 |