Search in sources :

Example 6 with Message

use of net.sourceforge.sqlexplorer.plugin.editors.Message in project tdq-studio-se by Talend.

the class AbstractSQLExecution method logException.

/**
 * Handles a SQLException by parsing the message and populating the messages tab;
 * where error messages from the server are numbered, they start relative to the
 * line number of the query that was sent; lineNoOffset is added to each line
 * number so that they relate to the line in SQLEditor
 * @param e The exception
 * @param query The Query that triggered the exception
 * @param positionEditor Whether to reposition the text caret of the editor to the first message
 */
protected void logException(SQLException e, Query query, boolean positionEditor) throws SQLException {
    DatabaseProduct product = _session.getDatabaseProduct();
    if (product == null)
        return;
    final Collection<Message> messages = product.getErrorMessages(_connection, e, query.getLineNo() - 1);
    if (messages == null)
        return;
    for (Message message : messages) {
        int lineNo = message.getLineNo();
        lineNo = queryParser.adjustLineNo(lineNo);
        message.setLineNo(lineNo);
        message.setSql(query.getQuerySql());
    }
    addMessages(messages);
    if (positionEditor) {
        final Shell shell = getEditor().getSite().getShell();
        shell.getDisplay().asyncExec(new Runnable() {

            public void run() {
                if (messages.size() > 0) {
                    Message msg = messages.iterator().next();
                    getEditor().setCursorPosition(msg.getLineNo(), msg.getCharNo());
                }
            }
        });
    }
}
Also used : Shell(org.eclipse.swt.widgets.Shell) Message(net.sourceforge.sqlexplorer.plugin.editors.Message) DatabaseProduct(net.sourceforge.sqlexplorer.dbproduct.DatabaseProduct)

Example 7 with Message

use of net.sourceforge.sqlexplorer.plugin.editors.Message in project tdq-studio-se by Talend.

the class AbstractSQLExecution method logException.

/**
 * Handles a SQLException by parsing the message and populating the messages tab;
 * where error messages from the server are numbered, they start relative to the
 * line number of the query that was sent; lineNoOffset is added to each line
 * number so that they relate to the line in SQLEditor
 * @param e
 */
protected void logException(SQLException e, String sql) throws SQLException {
    Collection<Message> messages = _session.getDatabaseProduct().getErrorMessages(_connection, e, 0);
    if (messages == null)
        return;
    for (Message message : messages) {
        int lineNo = message.getLineNo();
        lineNo = queryParser.adjustLineNo(lineNo);
        message.setLineNo(lineNo);
        message.setSql(sql);
    }
    addMessages(messages);
}
Also used : Message(net.sourceforge.sqlexplorer.plugin.editors.Message)

Example 8 with Message

use of net.sourceforge.sqlexplorer.plugin.editors.Message in project tdq-studio-se by Talend.

the class BatchJob method run.

@Override
protected IStatus run(IProgressMonitor monitor) {
    monitor.beginTask(Messages.getString("BatchJob.ExecutingScripts"), files.size());
    DatabaseProduct product = user.getAlias().getDriver().getDatabaseProduct();
    SQLConnection connection = null;
    try {
        if (session == null)
            session = user.createSession();
        connection = session.grabConnection();
        int index = 0;
        for (File file : files) {
            if (monitor.isCanceled())
                break;
            monitor.worked(index++);
            monitor.subTask(file.getName());
            _logger.fatal(file.getAbsolutePath());
            String sql = null;
            try {
                char[] buffer = new char[(int) file.length() + 10];
                FileReader reader = new FileReader(file);
                int length = reader.read(buffer);
                reader.close();
                if (length < 0 || length >= buffer.length) {
                    SQLExplorerPlugin.error("Cannot read from file " + file.getAbsolutePath());
                    continue;
                }
                // Normalise this to have standard \n in strings.  \r confuses Oracle and
                // isn't normally needed internally anyway
                StringBuffer sb = new StringBuffer(new String(buffer, 0, length));
                buffer = null;
                for (int i = 0; i < sb.length(); i++) {
                    if (sb.charAt(i) == '\r') {
                        sb.deleteCharAt(i);
                        i--;
                    }
                }
                sql = sb.toString();
                sb = null;
            } catch (IOException e) {
                SQLExplorerPlugin.error("Cannot read from file " + file.getAbsolutePath(), e);
                continue;
            }
            QueryParser parser = product.getQueryParser(sql, 1);
            parser.parse();
            for (Query query : parser) {
                DatabaseProduct.ExecutionResults results = null;
                try {
                    results = product.executeQuery(connection, query, -1);
                    DataSet dataSet;
                    while ((dataSet = results.nextDataSet()) != null) {
                        LinkedList<Message> messages = new LinkedList<Message>();
                        Collection<Message> messagesTmp = session.getDatabaseProduct().getErrorMessages(connection, query);
                        if (messagesTmp != null)
                            messages.addAll(messagesTmp);
                        messagesTmp = session.getDatabaseProduct().getServerMessages(connection);
                        if (messagesTmp != null)
                            messages.addAll(messagesTmp);
                        for (Message msg : messages) msg.setLineNo(parser.adjustLineNo(msg.getLineNo()));
                        for (Message message : messages) {
                            _logger.fatal(message.getSql());
                        }
                    }
                } catch (SQLException e) {
                    _logger.fatal(e.getMessage());
                } finally {
                    try {
                        if (results != null) {
                            results.close();
                            results = null;
                        }
                    } catch (SQLException e) {
                    // Nothing
                    }
                }
            }
        }
        monitor.done();
    } catch (SQLException e) {
        SQLExplorerPlugin.error(e);
    } catch (ParserException e) {
        SQLExplorerPlugin.error(e);
    } finally {
        if (connection != null)
            session.releaseConnection(connection);
    }
    return new Status(IStatus.OK, getClass().getName(), IStatus.OK, Messages.getString("BatchJob.Success"), null);
}
Also used : Status(org.eclipse.core.runtime.Status) IStatus(org.eclipse.core.runtime.IStatus) ParserException(net.sourceforge.sqlexplorer.parsers.ParserException) Query(net.sourceforge.sqlexplorer.parsers.Query) Message(net.sourceforge.sqlexplorer.plugin.editors.Message) DataSet(net.sourceforge.sqlexplorer.dataset.DataSet) SQLException(java.sql.SQLException) SQLConnection(net.sourceforge.sqlexplorer.dbproduct.SQLConnection) IOException(java.io.IOException) LinkedList(java.util.LinkedList) DatabaseProduct(net.sourceforge.sqlexplorer.dbproduct.DatabaseProduct) QueryParser(net.sourceforge.sqlexplorer.parsers.QueryParser) FileReader(java.io.FileReader) File(java.io.File)

Aggregations

Message (net.sourceforge.sqlexplorer.plugin.editors.Message)8 LinkedList (java.util.LinkedList)4 SQLException (java.sql.SQLException)3 DatabaseProduct (net.sourceforge.sqlexplorer.dbproduct.DatabaseProduct)3 Query (net.sourceforge.sqlexplorer.parsers.Query)3 DataSet (net.sourceforge.sqlexplorer.dataset.DataSet)2 File (java.io.File)1 FileReader (java.io.FileReader)1 IOException (java.io.IOException)1 DataSetTable (net.sourceforge.sqlexplorer.dataset.DataSetTable)1 SQLConnection (net.sourceforge.sqlexplorer.dbproduct.SQLConnection)1 ParserException (net.sourceforge.sqlexplorer.parsers.ParserException)1 QueryParser (net.sourceforge.sqlexplorer.parsers.QueryParser)1 ResultsTab (net.sourceforge.sqlexplorer.sqleditor.results.ResultsTab)1 IStatus (org.eclipse.core.runtime.IStatus)1 Status (org.eclipse.core.runtime.Status)1 MessageDialogWithToggle (org.eclipse.jface.dialogs.MessageDialogWithToggle)1 GridData (org.eclipse.swt.layout.GridData)1 GridLayout (org.eclipse.swt.layout.GridLayout)1 Composite (org.eclipse.swt.widgets.Composite)1