Search in sources :

Example 16 with SQLConnection

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

Example 17 with SQLConnection

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

the class ExplainAction2 method run.

public void run() {
    try {
        Session session;
        SQLConnection connection;
        Statement stmt;
        ResultSet rs;
        session = getSession();
        if (session == null)
            return;
        connection = null;
        stmt = null;
        rs = null;
        boolean createPlanTable;
        boolean notFoundTable;
        connection = session.grabConnection();
        Statement st = connection.createStatement();
        createPlanTable = false;
        notFoundTable = true;
        try {
            rs = st.executeQuery("select statement_id from plan_table");
            notFoundTable = false;
            rs.close();
            rs = null;
        } catch (SQLException _ex) {
            createPlanTable = MessageDialog.openQuestion(null, Messages.getString("oracle.editor.actions.explain.notFound.Title"), Messages.getString("oracle.editor.actions.explain.notFound"));
        }
        st.close();
        st = null;
        if (notFoundTable && !createPlanTable) {
            if (rs != null)
                try {
                    rs.close();
                } catch (SQLException e) {
                    SQLExplorerPlugin.error("Cannot close result set", e);
                }
            if (stmt != null)
                try {
                    stmt.close();
                } catch (SQLException e) {
                    SQLExplorerPlugin.error("Cannot close statement", e);
                }
            if (connection != null)
                session.releaseConnection(connection);
            return;
        }
        try {
            if (notFoundTable && createPlanTable) {
                st = connection.createStatement();
                st.execute("CREATE TABLE PLAN_TABLE (  STATEMENT_ID                    VARCHAR2(30), TIMESTAMP                       DATE,  REMARKS                         VARCHAR2(80),  OPERATION                       VARCHAR2(30),  OPTIONS                         VARCHAR2(30),  OBJECT_NODE                     VARCHAR2(128),  OBJECT_OWNER                    VARCHAR2(30),  OBJECT_NAME                     VARCHAR2(30),  OBJECT_INSTANCE                 NUMBER(38),  OBJECT_TYPE                     VARCHAR2(30),  OPTIMIZER                       VARCHAR2(255),  SEARCH_COLUMNS                  NUMBER,  ID                              NUMBER(38),  PARENT_ID                       NUMBER(38),  POSITION                        NUMBER(38),  COST                            NUMBER(38),  CARDINALITY                     NUMBER(38),  BYTES                           NUMBER(38),  OTHER_TAG                       VARCHAR2(255),  PARTITION_START                 VARCHAR2(255),  PARTITION_STOP                  VARCHAR2(255),  PARTITION_ID                    NUMBER(38),  OTHER                           LONG,  DISTRIBUTION                    VARCHAR2(30))");
                st.close();
                st = null;
            }
            QueryParser qt = session.getDatabaseProduct().getQueryParser(_editor.getSQLToBeExecuted(), _editor.getSQLLineNumber());
            qt.parse();
            (new ExplainExecution(_editor, qt)).schedule();
        } catch (SQLException e) {
            SQLExplorerPlugin.error("Error creating explain plan", e);
        } catch (ParserException e) {
            SQLExplorerPlugin.error("Cannot parse query", e);
        }
        if (rs != null)
            try {
                rs.close();
            } catch (SQLException e) {
                SQLExplorerPlugin.error("Cannot close result set", e);
            }
        if (stmt != null)
            try {
                stmt.close();
            } catch (SQLException e) {
                SQLExplorerPlugin.error("Cannot close statement", e);
            }
        if (connection != null)
            session.releaseConnection(connection);
        if (rs != null)
            try {
                rs.close();
            } catch (SQLException e) {
                SQLExplorerPlugin.error("Cannot close result set", e);
            }
        if (stmt != null)
            try {
                stmt.close();
            } catch (SQLException e) {
                SQLExplorerPlugin.error("Cannot close statement", e);
            }
        if (connection != null)
            session.releaseConnection(connection);
    } catch (Exception e) {
    }
}
Also used : ParserException(net.sourceforge.sqlexplorer.parsers.ParserException) QueryParser(net.sourceforge.sqlexplorer.parsers.QueryParser) SQLException(java.sql.SQLException) Statement(java.sql.Statement) SQLConnection(net.sourceforge.sqlexplorer.dbproduct.SQLConnection) ResultSet(java.sql.ResultSet) SQLException(java.sql.SQLException) ParserException(net.sourceforge.sqlexplorer.parsers.ParserException) Session(net.sourceforge.sqlexplorer.dbproduct.Session)

Aggregations

SQLConnection (net.sourceforge.sqlexplorer.dbproduct.SQLConnection)17 SQLException (java.sql.SQLException)7 User (net.sourceforge.sqlexplorer.dbproduct.User)7 Alias (net.sourceforge.sqlexplorer.dbproduct.Alias)6 ResultSet (java.sql.ResultSet)5 Statement (java.sql.Statement)5 DataSet (net.sourceforge.sqlexplorer.dataset.DataSet)4 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)4 PreparedStatement (java.sql.PreparedStatement)3 Iterator (java.util.Iterator)3 LinkedHashSet (java.util.LinkedHashSet)3 Session (net.sourceforge.sqlexplorer.dbproduct.Session)3 INode (net.sourceforge.sqlexplorer.dbstructure.nodes.INode)2 ParserException (net.sourceforge.sqlexplorer.parsers.ParserException)2 QueryParser (net.sourceforge.sqlexplorer.parsers.QueryParser)2 MessageDialogWithToggle (org.eclipse.jface.dialogs.MessageDialogWithToggle)2 File (java.io.File)1 FileReader (java.io.FileReader)1 IOException (java.io.IOException)1 DatabaseMetaData (java.sql.DatabaseMetaData)1