Search in sources :

Example 1 with OptTrace

use of org.apache.derby.iapi.sql.compile.OptTrace in project derby by apache.

the class OptimizerTracer method loadTool.

// /////////////////////////////////////////////////////////////////////////////////
// 
// OptionalTool BEHAVIOR
// 
// /////////////////////////////////////////////////////////////////////////////////
/**
 * <p>
 * Turns on optimizer tracing. May take optional parameters:
 * </p>
 *
 * <ul>
 * <li>xml - If the first arg is the "xml" literal, then trace output will be
 * formatted as xml.</li>
 * <li>custom, $class - If the first arg is the "custom" literal, then the next arg must be
 * the name of a class which implements org.apache.derby.iapi.sql.compile.OptTrace
 * and which has a 0-arg constructor. The 0-arg constructor is called and the resulting
 * OptTrace object is plugged in to trace the optimizer.</li>
 * </ul>
 */
public void loadTool(String... configurationParameters) throws SQLException {
    OptTrace tracer;
    if ((configurationParameters == null) || (configurationParameters.length == 0)) {
        tracer = new DefaultOptTrace();
    } else if ("xml".equals(configurationParameters[0])) {
        try {
            tracer = new XMLOptTrace();
        } catch (Throwable t) {
            throw wrap(t);
        }
    } else if ("custom".equals(configurationParameters[0])) {
        if (configurationParameters.length != 2) {
            throw wrap(MessageService.getTextMessage(SQLState.LANG_BAD_OPTIONAL_TOOL_ARGS));
        }
        String customOptTraceName = configurationParameters[1];
        try {
            ClassFactoryContext cfc = (ClassFactoryContext) getContext(ClassFactoryContext.CONTEXT_ID);
            ClassFactory classFactory = cfc.getClassFactory();
            Class<?> clazz = classFactory.loadApplicationClass(customOptTraceName);
            tracer = (OptTrace) clazz.getConstructor().newInstance();
        } catch (InstantiationException cnfe) {
            throw cantInstantiate(customOptTraceName);
        } catch (ClassNotFoundException cnfe) {
            throw cantInstantiate(customOptTraceName);
        } catch (IllegalAccessException cnfe) {
            throw cantInstantiate(customOptTraceName);
        } catch (NoSuchMethodException cnfe) {
            throw cantInstantiate(customOptTraceName);
        } catch (java.lang.reflect.InvocationTargetException cnfe) {
            throw cantInstantiate(customOptTraceName);
        } catch (Throwable t) {
            throw wrap(t);
        }
    } else {
        throw wrap(MessageService.getTextMessage(SQLState.LANG_BAD_OPTIONAL_TOOL_ARGS));
    }
    OptimizerTrace.setOptimizerTracer(tracer);
}
Also used : ClassFactory(org.apache.derby.iapi.services.loader.ClassFactory) OptTrace(org.apache.derby.iapi.sql.compile.OptTrace) ClassFactoryContext(org.apache.derby.iapi.services.loader.ClassFactoryContext)

Example 2 with OptTrace

use of org.apache.derby.iapi.sql.compile.OptTrace in project derby by apache.

the class OptimizerTracer method unloadTool.

/**
 * <p>
 * Print the optimizer trace and turn off tracing. Takes optional parameters:
 * </p>
 *
 * <ul>
 * <li><b>fileName</b> - Where to write the optimizer trace. If omitted, the trace is written to System.out.</li>
 * </ul>
 */
public void unloadTool(final String... configurationParameters) throws SQLException {
    try {
        final OptTrace tracer = OptimizerTrace.getOptimizerTracer();
        boolean needsClosing = false;
        PrintWriter pw;
        if ((configurationParameters != null) && (configurationParameters.length > 0)) {
            pw = AccessController.doPrivileged(new PrivilegedExceptionAction<PrintWriter>() {

                public PrintWriter run() throws SQLException {
                    try {
                        String fileName = configurationParameters[0];
                        File outputFile = new File(fileName);
                        if (outputFile.exists()) {
                            throw PublicAPI.wrapStandardException(StandardException.newException(SQLState.DATA_FILE_EXISTS, fileName));
                        }
                        return new PrintWriter(outputFile);
                    } catch (IOException ioe) {
                        throw new IllegalArgumentException(ioe.getMessage(), ioe);
                    }
                }
            });
            needsClosing = true;
        } else {
            pw = new PrintWriter(System.out);
        }
        if (tracer != null) {
            tracer.printToWriter(pw);
            pw.flush();
        }
        if (needsClosing) {
            pw.close();
        }
    } catch (Exception e) {
        if (e.getMessage() == null) {
            Throwable cause = e.getCause();
            if ((cause != null) && (cause instanceof SQLException)) {
                throw (SQLException) cause;
            }
        }
        throw wrap(e);
    } finally {
        OptimizerTrace.setOptimizerTracer(null);
    }
}
Also used : SQLException(java.sql.SQLException) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction) IOException(java.io.IOException) File(java.io.File) StandardException(org.apache.derby.shared.common.error.StandardException) IOException(java.io.IOException) SQLException(java.sql.SQLException) OptTrace(org.apache.derby.iapi.sql.compile.OptTrace) PrintWriter(java.io.PrintWriter)

Example 3 with OptTrace

use of org.apache.derby.iapi.sql.compile.OptTrace in project derby by apache.

the class OptimizerTrace method getOptimizerTraceOutput.

/**
 * Get the optimizer trace output for the last optimized query as a String.
 *
 * @return The optimizer trace output for the last optimized query as a String.
 *    Null will be returned if optimizer trace output is off or not supported
 *    or no trace output was found or an exception occurred.
 */
public static String getOptimizerTraceOutput() {
    String retCode = null;
    try {
        // Get the current language connection context.  This is associated
        // with the current database.
        LanguageConnectionContext lcc = ConnectionUtil.getCurrentLCC();
        OptTrace tracer = lcc.getOptimizerTracer();
        if (tracer != null) {
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            tracer.printToWriter(pw);
            pw.flush();
            sw.flush();
            retCode = sw.toString();
        }
    } catch (Throwable t) {
    // eat all exceptions, simply return null
    }
    return retCode;
}
Also used : StringWriter(java.io.StringWriter) LanguageConnectionContext(org.apache.derby.iapi.sql.conn.LanguageConnectionContext) DefaultOptTrace(org.apache.derby.impl.sql.compile.DefaultOptTrace) OptTrace(org.apache.derby.iapi.sql.compile.OptTrace) PrintWriter(java.io.PrintWriter)

Example 4 with OptTrace

use of org.apache.derby.iapi.sql.compile.OptTrace in project derby by apache.

the class OptimizerTrace method setOptimizerTrace.

/**
 * Turn default optimizer tracing on or off.
 *
 * @param onOrOff    Whether to turn optimizer tracing on (true) or off (false).
 */
public static void setOptimizerTrace(boolean onOrOff) {
    OptTrace optimizerTracer = onOrOff ? new DefaultOptTrace() : null;
    setOptimizerTracer(optimizerTracer);
}
Also used : DefaultOptTrace(org.apache.derby.impl.sql.compile.DefaultOptTrace) DefaultOptTrace(org.apache.derby.impl.sql.compile.DefaultOptTrace) OptTrace(org.apache.derby.iapi.sql.compile.OptTrace)

Aggregations

OptTrace (org.apache.derby.iapi.sql.compile.OptTrace)4 PrintWriter (java.io.PrintWriter)2 DefaultOptTrace (org.apache.derby.impl.sql.compile.DefaultOptTrace)2 File (java.io.File)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 PrivilegedExceptionAction (java.security.PrivilegedExceptionAction)1 SQLException (java.sql.SQLException)1 ClassFactory (org.apache.derby.iapi.services.loader.ClassFactory)1 ClassFactoryContext (org.apache.derby.iapi.services.loader.ClassFactoryContext)1 LanguageConnectionContext (org.apache.derby.iapi.sql.conn.LanguageConnectionContext)1 StandardException (org.apache.derby.shared.common.error.StandardException)1