📄 ij.java
字号:
/* Generated By:JavaCC: Do not edit this line. ij.java */package org.apache.derby.impl.tools.ij;import org.apache.derby.iapi.reference.JDBC20Translation;import org.apache.derby.iapi.reference.JDBC30Translation;import org.apache.derby.tools.JDBCDisplayUtil;import org.apache.derby.iapi.tools.i18n.LocalizedInput;import org.apache.derby.iapi.tools.i18n.LocalizedResource;import org.apache.derby.iapi.services.info.JVMInfo;import org.apache.derby.tools.URLCheck;import java.lang.reflect.*;import java.sql.Connection;import java.sql.DatabaseMetaData;import java.sql.DriverManager;import java.sql.Statement;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.ResultSetMetaData;import java.sql.SQLException;import java.sql.SQLWarning;import java.util.Hashtable;import java.util.Properties;import java.util.StringTokenizer;import java.io.IOException;import java.util.Vector;import java.util.Enumeration;import java.util.Locale;/** This parser works on a statement-at-a-time basis. It maintains a connection environment that is set by the caller and contains a list of connections for the current thread/ij session. Multi-user frameworks that use this parser tend to maintain multiple connectionEnv's and pass in the current one to set ij up. A connectionEnv has a default connection in use, and the ij connect/set connection/disconnect commands are used to change the current connection. Each connection has associated with it a list of prepared statements and cursors, created by the ij prepare and get cursor statements and manipulated by additional ij statements. To enable multiple display modes, this parser will not output anything, but will return objects that the caller can then display. This means the caller is responsible for displaying thrown exceptions and also SQLWarnings. So, our return value is the JDBC object upon which warnings will be hung, i.e. the one manipulated by the statement, if any. If there is no object to display, then a null is returned. @author ames */class ij implements ijConstants { static final String PROTOCOL_PROPERTY = "ij.protocol"; static final String USER_PROPERTY = "ij.user"; static final String PASSWORD_PROPERTY = "ij.password"; static final String FRAMEWORK_PROPERTY = "framework"; boolean elapsedTime = false; Connection theConnection = null; ConnectionEnv currentConnEnv = null; xaAbstractHelper xahelper = null; boolean exit = false; utilMain utilInstance = null; Hashtable ignoreErrors = null; String protocol = null; // the (single) unnamed protocol Hashtable namedProtocols; /** * A constructor that understands the local state that needs to be * initialized. * * @param tm The token manager to use * @param utilInstance The util to use */ ij(ijTokenManager tm, utilMain utilInstance) { this(tm); this.utilInstance = utilInstance; // load all protocols specified via properties // Properties p = System.getProperties(); protocol = p.getProperty(PROTOCOL_PROPERTY); String framework_property = p.getProperty(FRAMEWORK_PROPERTY); if (ij.JDBC20X() && ij.JTA() && ij.JNDI()) { try { xahelper = (xaAbstractHelper) Class.forName("org.apache.derby.impl.tools.ij.xaHelper").newInstance(); xahelper.setFramework(framework_property); } catch (Exception e) { } } namedProtocols = new Hashtable(); String prefix = PROTOCOL_PROPERTY + "."; for (Enumeration e = p.propertyNames(); e.hasMoreElements(); ) { String key = (String)e.nextElement(); if (key.startsWith(prefix)) { String name = key.substring(prefix.length()); installProtocol(name.toUpperCase(Locale.ENGLISH), p.getProperty(key)); } } } /** * Return whether or not JDBC 2.0 (and greater) extension classes can be loaded * * @return true if JDBC 2.0 (and greater) extension classes can be loaded */ private static boolean JDBC20X() { try { Class.forName("javax.sql.DataSource"); Class.forName("javax.sql.ConnectionPoolDataSource"); Class.forName("javax.sql.PooledConnection"); Class.forName("javax.sql.XAConnection"); Class.forName("javax.sql.XADataSource"); } catch(ClassNotFoundException cnfe) { return false; } return true; } /** * Return whether or not JTA classes can be loaded * * @return true if JTA classes can be loaded */ private static boolean JTA() { try { Class.forName("javax.transaction.xa.Xid"); Class.forName("javax.transaction.xa.XAResource"); Class.forName("javax.transaction.xa.XAException"); } catch(ClassNotFoundException cnfe) { return false; } return true; } /** * Return whether or not JNDI extension classes can be loaded * * @return true if JNDI extension classes can be loaded */ private static boolean JNDI() { try { Class.forName("javax.naming.spi.Resolver"); Class.forName("javax.naming.Referenceable"); Class.forName("javax.naming.directory.Attribute"); } catch(ClassNotFoundException cnfe) { return false; } return true; }// FIXME: caller has to deal with ignoreErrors and handleSQLException behavior /** Add the warnings of wTail to the end of those of wHead. */ SQLWarning appendWarnings(SQLWarning wHead, SQLWarning wTail) { if (wHead == null) return wTail; if (wHead.getNextException() == null) { wHead.setNextException(wTail); } else { appendWarnings(wHead.getNextWarning(), wTail); } return wHead; } /** * Get the "elapsedTime state". */ boolean getElapsedTimeState() { return elapsedTime; } /** this removes the outside quotes from the string. it will also swizzle the special characters into their actual characters, like '' for ', etc. */ String stringValue(String s) { String result = s.substring(1,s.length()-1); char quotes = '\''; int index; /* Find the first occurrence of adjacent quotes. */ index = result.indexOf(quotes); /* Replace each occurrence with a single quote and begin the * search for the next occurrence from where we left off. */ while (index != -1) { result = result.substring(0, index + 1) + result.substring(index + 2); index = result.indexOf(quotes, index + 1); } return result; } void installProtocol(String name, String value) { try { // `value' is a JDBC protocol; // we load the "driver" in the prototypical // manner, it will register itself with // the DriverManager. util.loadDriverIfKnown(value); } catch (ClassNotFoundException e) { throw ijException.classNotFoundForProtocol(value); } catch (IllegalArgumentException e) { throw ijException.classNotFoundForProtocol(value); } catch (IllegalAccessException e) { throw ijException.classNotFoundForProtocol(value); } catch (InstantiationException e) { throw ijException.classNotFoundForProtocol(value); } if (name == null) protocol = value; else namedProtocols.put(name, value); } void haveConnection() { JDBCDisplayUtil.checkNotNull(theConnection, "connection"); } /** We do not reuse statement objects at all, because some systems require you to close the object to release resources (JBMS), while others will not let you reuse the statement object once it is closed (WebLogic). If you want to reuse statement objects, you need to use the ij PREPARE and EXECUTE statements. @param stmt the statement **/ ijResult executeImmediate(String stmt) throws SQLException { Statement aStatement = null; try { long beginTime = 0; long endTime = 0; boolean cleanUpStmt = false; haveConnection(); aStatement = theConnection.createStatement(); // for JCC - remove comments at the beginning of the statement // and trim; do the same for Derby Clients that have versions // earlier than 10.2. if (currentConnEnv != null) { boolean trimForDNC = currentConnEnv.getSession().getIsDNC(); if (trimForDNC) { // we're using the Derby Client, but we only want to trim // if the version is earlier than 10.2. DatabaseMetaData dbmd = theConnection.getMetaData(); int majorVersion = dbmd.getDriverMajorVersion(); if ((majorVersion > 10) || ((majorVersion == 10) && (dbmd.getDriverMinorVersion() > 1))) { // 10.2 or later, so don't trim/remove comments. trimForDNC = false; } } if (currentConnEnv.getSession().getIsJCC() || trimForDNC) { // remove comments and trim. int nextline; while(stmt.startsWith("--")) { nextline = stmt.indexOf('\n')+1; stmt = stmt.substring(nextline); } stmt = stmt.trim(); } } aStatement.execute(stmt); // FIXME: display results. return start time. return new ijStatementResult(aStatement,true); } catch (SQLException e) { if (aStatement!=null) // free the resource aStatement.close(); throw e; } } ijResult quit() throws SQLException { exit = true; if (getExpect()) { // report stats // FIXME: replace with MVC... // FIXME: this is a kludgy way to quiet /0 and make 0/0=1... int numExpectOr1 = (numExpect==0?1:numExpect); int numPassOr1 = (numPass==numExpect && numPass==0)?1:numPass; int numFailOr1 = (numFail==numExpect && numFail==0)?1:numFail; int numUnxOr1 = (numUnx==numExpect && numUnx==0)?1:numUnx; LocalizedResource.OutputWriter().println(LocalizedResource.getMessage("IJ_TestsRun0Pass12Fail34", new Object[]{ LocalizedResource.getNumber(numExpect), LocalizedResource.getNumber(100*(numPassOr1/numExpectOr1)), LocalizedResource.getNumber(100*(numFailOr1/numExpectOr1))})); if (numUnx > 0) { LocalizedResource.OutputWriter().println(); LocalizedResource.OutputWriter().println(LocalizedResource.getMessage("IJ_UnexpResulUnx01", LocalizedResource.getNumber(numUnx), LocalizedResource.getNumber(100*(numUnxOr1/numExpectOr1)))); } } currentConnEnv.removeAllSessions(); theConnection = null; return null; } /** Async execution wants to return results off-cycle. We want to control their output, and so will hold it up until it is requested with a WAIT FOR asyncName statement. WAIT FOR will return the results of the async statement once they are ready. Note that using a select only waits for the execute to complete; the logic to step through the result set is in the caller. **/ ijResult executeAsync(String stmt, String name) { AsyncStatement as = new AsyncStatement(theConnection, stmt);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -