Search in sources :

Example 1 with DatabaseProduct

use of net.sourceforge.sqlexplorer.dbproduct.DatabaseProduct in project tdq-studio-se by Talend.

the class SQLExecution method doExecution.

protected void doExecution(IProgressMonitor monitor) throws Exception {
    int numErrors = 0;
    SQLException lastSQLException = null;
    try {
        long overallUpdateCount = 0;
        long overallStartTime = System.currentTimeMillis();
        for (Query query : getQueryParser()) {
            if (monitor.isCanceled())
                break;
            if (getEditor().isClosed())
                break;
            // Get the next bit of SQL to run and store it as "current"
            if (query == null)
                break;
            String querySQL = query.getQuerySql().toString();
            if (querySQL == null)
                continue;
            // Initialise
            setProgressMessage(Messages.getString("SQLResultsView.Executing"));
            final long startTime = System.currentTimeMillis();
            // Run it
            DatabaseProduct.ExecutionResults results = null;
            try {
                DatabaseProduct product = getEditor().getSession().getDatabaseProduct();
                try {
                    results = product.executeQuery(_connection, query, _maxRows);
                } catch (RuntimeException e) {
                    throw new SQLException(e.getMessage());
                }
                final long endTime = System.currentTimeMillis();
                DataSet dataSet;
                boolean checkedForMessages = false;
                while ((dataSet = results.nextDataSet()) != null) {
                    // update sql result
                    SQLResult sqlResult = new SQLResult();
                    sqlResult.setQuery(query);
                    sqlResult.setDataSet(dataSet);
                    sqlResult.setExecutionTimeMillis(endTime - startTime);
                    // Save successfull query
                    SQLExplorerPlugin.getDefault().getSQLHistory().addSQL(querySQL, _session);
                    if (monitor.isCanceled())
                        return;
                    checkForMessages(query);
                    checkedForMessages = true;
                    // show results..
                    displayResults(sqlResult);
                }
                overallUpdateCount += results.getUpdateCount();
                if (!checkedForMessages)
                    checkForMessages(query);
                debugLogQuery(query, null);
            } catch (final SQLException e) {
                debugLogQuery(query, e);
                boolean stopOnError = SQLExplorerPlugin.getDefault().getPreferenceStore().getBoolean(IConstants.STOP_ON_ERROR);
                logException(e, query, stopOnError);
                closeStatement();
                hasMessages = true;
                if (stopOnError) {
                    errorDialog(Messages.getString("SQLResultsView.Error.Title"), e.getMessage());
                    return;
                }
                numErrors++;
                lastSQLException = e;
            } finally {
                try {
                    if (results != null) {
                        results.close();
                        results = null;
                    }
                } catch (SQLException e) {
                // Nothing
                }
            }
        }
        if (!hasMessages || SQLExplorerPlugin.getDefault().getPreferenceStore().getBoolean(IConstants.LOG_SUCCESS_MESSAGES)) {
            long overallTime = System.currentTimeMillis() - overallStartTime;
            String message = Long.toString(overallUpdateCount) + " " + Messages.getString("SQLEditor.Update.Prefix") + " " + Long.toString(overallTime) + " " + Messages.getString("SQLEditor.Update.Postfix");
            addMessage(new Message(Message.Status.STATUS, getQueryParser().adjustLineNo(1), 0, "", message));
        }
    } catch (Exception e) {
        closeStatement();
        throw e;
    }
    if (numErrors == 1)
        throw lastSQLException;
    else if (numErrors > 1 && SQLExplorerPlugin.getDefault().getPreferenceStore().getBoolean(IConstants.CONFIRM_BOOL_SHOW_DIALOG_ON_QUERY_ERROR))
        getEditor().getSite().getShell().getDisplay().asyncExec(new Runnable() {

            public void run() {
                MessageDialogWithToggle dialog = MessageDialogWithToggle.openInformation(getEditor().getSite().getShell(), Messages.getString("SQLExecution.Error.Title"), Messages.getString("SQLExecution.Error.Message"), Messages.getString("SQLExecution.Error.Toggle"), false, null, null);
                if (dialog.getToggleState() && dialog.getReturnCode() == IDialogConstants.OK_ID)
                    SQLExplorerPlugin.getDefault().getPluginPreferences().setValue(IConstants.CONFIRM_BOOL_SHOW_DIALOG_ON_QUERY_ERROR, false);
            }
        });
}
Also used : Query(net.sourceforge.sqlexplorer.parsers.Query) Message(net.sourceforge.sqlexplorer.plugin.editors.Message) SQLException(java.sql.SQLException) DataSet(net.sourceforge.sqlexplorer.dataset.DataSet) SQLException(java.sql.SQLException) DatabaseProduct(net.sourceforge.sqlexplorer.dbproduct.DatabaseProduct) MessageDialogWithToggle(org.eclipse.jface.dialogs.MessageDialogWithToggle)

Example 2 with DatabaseProduct

use of net.sourceforge.sqlexplorer.dbproduct.DatabaseProduct 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 3 with DatabaseProduct

use of net.sourceforge.sqlexplorer.dbproduct.DatabaseProduct 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

DatabaseProduct (net.sourceforge.sqlexplorer.dbproduct.DatabaseProduct)3 Message (net.sourceforge.sqlexplorer.plugin.editors.Message)3 SQLException (java.sql.SQLException)2 DataSet (net.sourceforge.sqlexplorer.dataset.DataSet)2 Query (net.sourceforge.sqlexplorer.parsers.Query)2 File (java.io.File)1 FileReader (java.io.FileReader)1 IOException (java.io.IOException)1 LinkedList (java.util.LinkedList)1 SQLConnection (net.sourceforge.sqlexplorer.dbproduct.SQLConnection)1 ParserException (net.sourceforge.sqlexplorer.parsers.ParserException)1 QueryParser (net.sourceforge.sqlexplorer.parsers.QueryParser)1 IStatus (org.eclipse.core.runtime.IStatus)1 Status (org.eclipse.core.runtime.Status)1 MessageDialogWithToggle (org.eclipse.jface.dialogs.MessageDialogWithToggle)1 Shell (org.eclipse.swt.widgets.Shell)1