1 21 22 package org.apache.derby.impl.tools.ij; 23 24 import org.apache.derby.iapi.reference.JDBC20Translation; 25 import org.apache.derby.iapi.reference.JDBC30Translation; 26 27 import org.apache.derby.tools.JDBCDisplayUtil; 28 import org.apache.derby.iapi.tools.i18n.*; 29 30 import org.apache.derby.iapi.services.info.ProductVersionHolder; 31 import org.apache.derby.iapi.services.info.ProductGenusNames; 32 33 import org.apache.derby.iapi.error.PublicAPI; 34 import org.apache.derby.iapi.error.StandardException; 35 36 import java.util.Stack ; 37 import java.util.Hashtable ; 38 import java.util.Properties ; 39 40 import java.io.InputStream ; 41 import java.io.FileInputStream ; 42 import java.io.BufferedInputStream ; 43 import java.io.FileNotFoundException ; 44 import java.io.StringReader ; 45 import java.sql.DriverManager ; 46 import java.sql.Driver ; 47 import java.sql.Connection ; 48 import java.sql.SQLException ; 49 import java.sql.ResultSet ; 50 import java.sql.Statement ; 51 import java.sql.PreparedStatement ; 52 53 import java.lang.reflect.*; 54 55 62 public class utilMain implements java.security.PrivilegedAction { 63 64 private static final Class [] CONN_PARAM = { Integer.TYPE }; 65 private static final Object [] CONN_ARG = { new Integer (JDBC30Translation.CLOSE_CURSORS_AT_COMMIT)}; 66 67 private StatementFinder[] commandGrabber; 68 UCode_CharStream charStream; 69 ijTokenManager ijTokMgr; 70 ij ijParser; 71 ConnectionEnv[] connEnv; 72 private int currCE; 73 private final int numConnections; 74 private boolean fileInput; 75 private boolean initialFileInput; 76 private boolean mtUse; 77 private boolean firstRun = true; 78 private LocalizedOutput out = null; 79 private Properties connAttributeDefaults; 80 private Hashtable ignoreErrors; 81 85 private final boolean showErrorCode; 86 87 90 private final String ijExceptionTrace; 91 92 protected boolean isJCC; 94 97 public static final int BUFFEREDFILESIZE = 2048; 98 99 103 Stack oldGrabbers = new Stack (); 104 105 LocalizedResource langUtil = LocalizedResource.getInstance(); 106 111 utilMain(int numConnections, LocalizedOutput out) 112 throws ijFatalException 113 { 114 this(numConnections, out, (Hashtable )null); 115 } 116 117 129 public utilMain(int numConnections, LocalizedOutput out, Hashtable ignoreErrors) 130 throws ijFatalException 131 { 132 String framework_property = util.getSystemProperty("framework"); 133 134 if (framework_property != null) 135 { 136 if (framework_property.equals("DB2jNet") 137 || framework_property.equals("DB2jcc")) 138 isJCC = true; 139 } 140 143 charStream = new UCode_CharStream( 144 new StringReader (" "), 1, 1); 145 ijTokMgr = new ijTokenManager(charStream); 146 ijParser = new ij(ijTokMgr, this); 147 this.out = out; 148 this.ignoreErrors = ignoreErrors; 149 150 showErrorCode = 151 Boolean.valueOf( 152 util.getSystemProperty("ij.showErrorCode") 153 ).booleanValue(); 154 155 ijExceptionTrace = util.getSystemProperty("ij.exceptionTrace"); 156 157 this.numConnections = numConnections; 158 159 commandGrabber = new StatementFinder[numConnections]; 160 connEnv = new ConnectionEnv[numConnections]; 161 162 for (int ictr = 0; ictr < numConnections; ictr++) 163 { 164 commandGrabber[ictr] = new StatementFinder(langUtil.getNewInput(System.in)); 165 connEnv[ictr] = new ConnectionEnv(ictr, (numConnections > 1), (numConnections == 1)); 166 } 167 168 169 currCE = 0; 170 fileInput = false; 171 initialFileInput = false; 172 firstRun = true; 173 } 174 175 179 public void initFromEnvironment() 180 { 181 ijParser.initFromEnvironment(); 182 183 for (int ictr = 0; ictr < numConnections; ictr++) 184 { 185 try { 186 connEnv[ictr].init(out); 187 } catch (SQLException s) { 188 JDBCDisplayUtil.ShowException(out, s); } catch (ClassNotFoundException c) { 190 JDBCDisplayUtil.ShowException(out, c); } catch (InstantiationException i) { 192 JDBCDisplayUtil.ShowException(out, i); } catch (IllegalAccessException ia) { 194 JDBCDisplayUtil.ShowException(out, ia); } 196 } 197 } 198 199 200 208 public void go(LocalizedInput[] in, LocalizedOutput out, 209 Properties connAttributeDefaults) throws ijFatalException 210 { 211 this.out = out; 212 this.connAttributeDefaults = connAttributeDefaults; 213 214 ijParser.setConnection(connEnv[currCE], (numConnections > 1)); 215 fileInput = initialFileInput = (!in[currCE].isStandardInput()); 216 217 for (int ictr = 0; ictr < commandGrabber.length; ictr++) { 218 commandGrabber[ictr].ReInit(in[ictr]); 219 } 220 221 if (firstRun) { 222 223 InputStream versionStream = (InputStream ) java.security.AccessController.doPrivileged(this); 225 226 ProductVersionHolder ijVersion = 228 ProductVersionHolder.getProductVersionHolderFromMyEnv(versionStream); 229 230 String version; 231 if (ijVersion != null) 232 { 233 version = "" + ijVersion.getMajorVersion() + "." + 234 ijVersion.getMinorVersion(); 235 } 236 else 237 { 238 version = "?"; 239 } 240 241 out.println(langUtil.getTextMessage("IJ_IjVers30C199", version)); 242 for (int i=connEnv.length-1;i>=0;i--) { Connection c = connEnv[i].getConnection(); 244 if (c!=null) { 245 JDBCDisplayUtil.ShowWarnings(out,c); 246 } 247 } 248 firstRun = false; 249 250 boolean showNoCountForSelect = Boolean.getBoolean("ij.showNoCountForSelect"); 253 JDBCDisplayUtil.showSelectCount = !showNoCountForSelect; 254 255 boolean showNoConnectionsAtStart = Boolean.getBoolean("ij.showNoConnectionsAtStart"); 258 if (!(showNoConnectionsAtStart)) { 259 try { 260 ijResult result = ijParser.showConnectionsMethod(true); 261 displayResult(out,result,connEnv[currCE].getConnection()); 262 } catch (SQLException ex) { 263 handleSQLException(out,ex); 264 } 265 } 266 } 267 this.out = out; 268 runScriptGuts(); 269 cleanupGo(in); 270 } 271 272 279 public int goScript(Connection conn, 280 LocalizedInput in) 281 { 282 JDBCDisplayUtil.showSelectCount = false; 283 connEnv[0].addSession(conn, (String ) null); 284 fileInput = initialFileInput = !in.isStandardInput(); 285 commandGrabber[0].ReInit(in); 286 return runScriptGuts(); 287 } 288 289 295 private int runScriptGuts() { 296 297 int scriptErrorCount = 0; 298 299 boolean done = false; 300 String command = null; 301 while (!ijParser.exit && !done) { 302 try{ 303 ijParser.setConnection(connEnv[currCE], (numConnections > 1)); 304 } catch(Throwable t){ 305 } 307 308 connEnv[currCE].doPrompt(true, out); 309 try { 310 command = null; 311 out.flush(); 312 command = commandGrabber[currCE].nextStatement(); 313 314 while (command == null && ! oldGrabbers.empty()) { 317 if (fileInput) commandGrabber[currCE].close(); 319 commandGrabber[currCE] = (StatementFinder)oldGrabbers.pop(); 320 if (oldGrabbers.empty()) 321 fileInput = initialFileInput; 322 command = commandGrabber[currCE].nextStatement(); 323 } 324 325 if (command == null && oldGrabbers.empty()) { 328 done = true; 329 } 330 else { 331 boolean elapsedTimeOn = ijParser.getElapsedTimeState(); 332 long beginTime = 0; 333 long endTime; 334 335 if (fileInput) { 336 out.println(command+";"); 337 out.flush(); 338 } 339 340 charStream.ReInit(new StringReader (command), 1, 1); 341 ijTokMgr.ReInit(charStream); 342 ijParser.ReInit(ijTokMgr); 343 344 if (elapsedTimeOn) { 345 beginTime = System.currentTimeMillis(); 346 } 347 348 ijResult result = ijParser.ijStatement(); 349 displayResult(out,result,connEnv[currCE].getConnection()); 350 351 355 356 if (elapsedTimeOn) { 357 endTime = System.currentTimeMillis(); 358 out.println(langUtil.getTextMessage("IJ_ElapTime0Mil", 359 langUtil.getNumberAsString(endTime - beginTime))); 360 } 361 362 } 367 368 } catch (ParseException e) { 369 if (command != null) 370 scriptErrorCount += doCatch(command) ? 0 : 1; 371 } catch (TokenMgrError e) { 372 if (command != null) 373 scriptErrorCount += doCatch(command) ? 0 : 1; 374 } catch (SQLException e) { 375 scriptErrorCount++; 376 handleSQLException(out,e); 379 } catch (ijException e) { 380 scriptErrorCount++; 381 out.println(langUtil.getTextMessage("IJ_IjErro0",e.getMessage())); 383 doTrace(e); 384 } catch (Throwable e) { 385 scriptErrorCount++; 386 out.println(langUtil.getTextMessage("IJ_JavaErro0",e.toString())); 387 doTrace(e); 388 } 389 390 391 currCE = ++currCE % connEnv.length; 392 } 393 394 return scriptErrorCount; 395 } 396 397 403 private void cleanupGo(LocalizedInput[] in) { 404 405 try { 408 for (int i = 0; i < connEnv.length; i++) { 409 connEnv[i].removeAllSessions(); 410 } 411 } catch (SQLException se ) { 412 handleSQLException(out,se); 413 } 414 for (int i = 0; i < numConnections; i++) { 416 try { 417 in[i].close(); 418 } catch (Exception e ) { 419 out.println(langUtil.getTextMessage("IJ_CannotCloseInFile", 420 e.toString())); 421 } 422 } 423 424 427 if (ijParser.exit || (initialFileInput && !mtUse)) { 428 Driver d = null; 429 try { 430 d = DriverManager.getDriver("jdbc:derby:"); 431 } catch (Throwable e) { 432 d = null; 433 } 434 if (d!=null) { try { 436 DriverManager.getConnection("jdbc:derby:;shutdown=true"); 437 } catch (SQLException e) { 438 } 440 } 441 } 442 } 443 444 private void displayResult(LocalizedOutput out, ijResult result, Connection conn) throws SQLException { 445 if (result!=null) { 447 if (result.isConnection()) { 448 if (result.hasWarnings()) { 449 JDBCDisplayUtil.ShowWarnings(out,result.getSQLWarnings()); 450 result.clearSQLWarnings(); 451 } 452 } else if (result.isStatement()) { 453 Statement s = result.getStatement(); 454 try { 455 JDBCDisplayUtil.DisplayResults(out,s,connEnv[currCE].getConnection()); 456 } catch (SQLException se) { 457 result.closeStatement(); 458 throw se; 459 } 460 result.closeStatement(); 461 } else if (result.isNextRowOfResultSet()) { 462 ResultSet r = result.getNextRowOfResultSet(); 463 JDBCDisplayUtil.DisplayCurrentRow(out,r,connEnv[currCE].getConnection()); 464 } else if (result.isVector()) { 465 util.DisplayVector(out,result.getVector()); 466 if (result.hasWarnings()) { 467 JDBCDisplayUtil.ShowWarnings(out,result.getSQLWarnings()); 468 result.clearSQLWarnings(); 469 } 470 } else if (result.isMulti()) { 471 try { 472 util.DisplayMulti(out,(PreparedStatement )result.getStatement(),result.getResultSet(),connEnv[currCE].getConnection()); 473 } catch (SQLException se) { 474 result.closeStatement(); 475 throw se; 476 } 477 result.closeStatement(); if (result.hasWarnings()) { 479 JDBCDisplayUtil.ShowWarnings(out,result.getSQLWarnings()); 480 result.clearSQLWarnings(); 481 } 482 } else if (result.isResultSet()) { 483 ResultSet rs = result.getResultSet(); 484 try { 485 JDBCDisplayUtil.DisplayResults(out,rs,connEnv[currCE].getConnection(), result.getColumnDisplayList(), result.getColumnWidthList()); 486 } catch (SQLException se) { 487 result.closeStatement(); 488 throw se; 489 } 490 result.closeStatement(); 491 } else if (result.isException()) { 492 JDBCDisplayUtil.ShowException(out,result.getException()); 493 } 494 } 495 } 496 497 501 private boolean doCatch(String command) { 502 507 508 try { 509 boolean elapsedTimeOn = ijParser.getElapsedTimeState(); 510 long beginTime = 0; 511 long endTime; 512 513 if (elapsedTimeOn) { 514 beginTime = System.currentTimeMillis(); 515 } 516 517 ijResult result = ijParser.executeImmediate(command); 518 displayResult(out,result,connEnv[currCE].getConnection()); 519 520 521 if (elapsedTimeOn) { 522 endTime = System.currentTimeMillis(); 523 out.println(langUtil.getTextMessage("IJ_ElapTime0Mil_4", 524 langUtil.getNumberAsString(endTime - beginTime))); 525 } 526 return true; 527 528 } catch (SQLException e) { 529 handleSQLException(out,e); 532 } catch (ijException i) { 533 out.println(langUtil.getTextMessage("IJ_IjErro0_5", i.getMessage())); 534 doTrace(i); 535 } catch (ijTokenException ie) { 536 out.println(langUtil.getTextMessage("IJ_IjErro0_6", ie.getMessage())); 537 doTrace(ie); 538 } catch (Throwable t) { 539 out.println(langUtil.getTextMessage("IJ_JavaErro0_7", t.toString())); 540 doTrace(t); 541 } 542 return false; 543 } 544 545 551 private void handleSQLException(LocalizedOutput out, SQLException e) 552 throws ijFatalException 553 { 554 String errorCode; 555 String sqlState = null; 556 SQLException fatalException = null; 557 558 if (showErrorCode) { 559 errorCode = langUtil.getTextMessage("IJ_Erro0", 560 langUtil.getNumberAsString(e.getErrorCode())); 561 } 562 else { 563 errorCode = ""; 564 } 565 566 for (; e!=null; e=e.getNextException()) 567 { 568 574 if (ignoreErrors != null) 575 { 576 sqlState = e.getSQLState(); 577 if ((sqlState != null) && 578 (ignoreErrors.get(sqlState) != null)) 579 { 580 continue; 581 } 582 else 583 { 584 fatalException = e; 585 } 586 } 587 588 String st1 = JDBCDisplayUtil.mapNull(e.getSQLState(),langUtil.getTextMessage("IJ_NoSqls")); 589 String st2 = JDBCDisplayUtil.mapNull(e.getMessage(),langUtil.getTextMessage("IJ_NoMess")); 590 out.println(langUtil.getTextMessage("IJ_Erro012", st1, st2, errorCode)); 591 doTrace(e); 592 } 593 if (fatalException != null) 594 { 595 throw new ijFatalException(fatalException); 596 } 597 } 598 599 602 private void doTrace(Throwable t) { 603 if (ijExceptionTrace != null) { 604 t.printStackTrace(out); 605 } 606 out.flush(); 607 } 608 609 void newInput(String fileName) { 610 FileInputStream newFile = null; 611 try { 612 newFile = new FileInputStream (fileName); 613 } catch (FileNotFoundException e) { 614 throw ijException.fileNotFound(); 615 } 616 if (newFile == null) return; 617 618 oldGrabbers.push(commandGrabber[currCE]); 620 commandGrabber[currCE] = 621 new StatementFinder(langUtil.getNewInput(new BufferedInputStream (newFile, BUFFEREDFILESIZE))); 622 fileInput = true; 623 } 624 625 void newResourceInput(String resourceName) { 626 InputStream is = util.getResourceAsStream(resourceName); 627 if (is==null) throw ijException.resourceNotFound(); 628 oldGrabbers.push(commandGrabber[currCE]); 629 commandGrabber[currCE] = 630 new StatementFinder(langUtil.getNewEncodedInput(new BufferedInputStream (is, BUFFEREDFILESIZE), "UTF8")); 631 fileInput = true; 632 } 633 634 639 static void doPrompt(boolean newStatement, LocalizedOutput out, String tag) 640 { 641 if (newStatement) { 642 out.print("ij"+(tag==null?"":tag)+"> "); 643 } 644 else { 645 out.print("> "); 646 } 647 out.flush(); 648 } 649 650 void setMtUse(boolean b) { 651 mtUse = b; 652 } 653 654 656 657 667 Connection setHoldability(Connection conn, int holdType) 668 throws SQLException 669 { 670 try { Method sh = conn.getClass().getMethod("setHoldability", CONN_PARAM); 677 sh.invoke(conn, CONN_ARG); 678 } catch( Exception e) { 679 throw PublicAPI.wrapStandardException( StandardException.plainWrapException( e)); 680 } 681 return conn; 682 } 683 684 692 int getHoldability(Connection conn) 693 throws SQLException 694 { 695 int defaultHoldability = JDBC30Translation.HOLD_CURSORS_OVER_COMMIT; 702 try { 703 Method sh = conn.getClass().getMethod("getHoldability", null); 704 defaultHoldability = ((Integer )sh.invoke(conn, null)).intValue(); 705 } catch( Exception e) { 706 throw PublicAPI.wrapStandardException( StandardException.plainWrapException( e)); 707 } 708 return defaultHoldability; 709 } 710 711 720 Statement createStatement(Connection conn, int scrollType, int holdType) 721 throws SQLException 722 { 723 if (holdType != getHoldability(conn)) 730 { 731 throw ijException.holdCursorsNotSupported(); 732 } 733 734 Statement stmt; 735 try { 736 stmt = conn.createStatement(scrollType, JDBC20Translation.CONCUR_READ_ONLY); 737 } catch(AbstractMethodError ame) { 738 stmt = conn.createStatement(); 741 } 742 return stmt; 743 } 744 745 757 ijResult absolute(ResultSet rs, int row) 758 throws SQLException 759 { 760 boolean forwardOnly = (rs.getStatement().getResultSetType() == JDBC20Translation.TYPE_FORWARD_ONLY); 761 762 if (forwardOnly) 763 { 764 throw ijException.forwardOnlyCursor("ABSOLUTE"); 765 } 766 767 return new ijRowResult(rs, rs.absolute(row)); 769 } 770 771 783 ijResult relative(ResultSet rs, int row) 784 throws SQLException 785 { 786 boolean forwardOnly = (rs.getStatement().getResultSetType() == JDBC20Translation.TYPE_FORWARD_ONLY); 787 788 if (forwardOnly) 790 { 791 throw ijException.forwardOnlyCursor("RELATIVE"); 792 } 793 794 return new ijRowResult(rs, rs.relative(row)); 795 } 796 797 808 ijResult beforeFirst(ResultSet rs) 809 throws SQLException 810 { 811 boolean forwardOnly = (rs.getStatement().getResultSetType() == JDBC20Translation.TYPE_FORWARD_ONLY); 812 813 if (forwardOnly) 815 { 816 throw ijException.forwardOnlyCursor("BEFORE FIRST"); 817 } 818 819 rs.beforeFirst(); 820 return new ijRowResult(rs, false); 821 } 822 823 834 ijResult first(ResultSet rs) 835 throws SQLException 836 { 837 boolean forwardOnly = (rs.getStatement().getResultSetType() == JDBC20Translation.TYPE_FORWARD_ONLY); 838 839 if (forwardOnly) 841 { 842 throw ijException.forwardOnlyCursor("FIRST"); 843 } 844 845 return new ijRowResult(rs, rs.first()); 846 } 847 848 859 ijResult afterLast(ResultSet rs) 860 throws SQLException 861 { 862 boolean forwardOnly; 863 try { 864 forwardOnly = (rs.getStatement().getResultSetType() == JDBC20Translation.TYPE_FORWARD_ONLY); 865 } catch (AbstractMethodError ame) { 866 forwardOnly = true; 869 } 870 if (forwardOnly) 872 { 873 throw ijException.forwardOnlyCursor("AFTER LAST"); 874 } 875 876 rs.afterLast(); 877 return new ijRowResult(rs, false); 878 } 879 880 891 ijResult last(ResultSet rs) 892 throws SQLException 893 { 894 boolean forwardOnly = (rs.getStatement().getResultSetType() == JDBC20Translation.TYPE_FORWARD_ONLY); 895 896 if (forwardOnly) 898 { 899 throw ijException.forwardOnlyCursor("LAST"); 900 } 901 902 return new ijRowResult(rs, rs.last()); 903 } 904 905 916 ijResult previous(ResultSet rs) 917 throws SQLException 918 { 919 boolean forwardOnly = (rs.getStatement().getResultSetType() == JDBC20Translation.TYPE_FORWARD_ONLY); 920 921 if (forwardOnly) 923 { 924 throw ijException.forwardOnlyCursor("PREVIOUS"); 925 } 926 927 return new ijRowResult(rs, rs.previous()); 928 } 929 930 940 int getCurrentRowNumber(ResultSet rs) 941 throws SQLException 942 { 943 944 boolean forwardOnly = (rs.getStatement().getResultSetType() == JDBC20Translation.TYPE_FORWARD_ONLY); 945 946 if (forwardOnly) 948 { 949 throw ijException.forwardOnlyCursor("GETCURRENTROWNUMBER"); 950 } 951 952 return rs.getRow(); 953 } 954 955 Properties getConnAttributeDefaults () 956 { 957 return connAttributeDefaults; 958 } 959 960 public final Object run() { 961 return getClass().getResourceAsStream(ProductGenusNames.TOOLS_INFO); 962 } 963 } 964 | Popular Tags |