Search in sources :

Example 1 with SQLConnection

use of net.sourceforge.squirrel_sql.fw.sql.SQLConnection in project tdi-studio-se by Talend.

the class OracleExplainPlanAction method run.

/*
     * (non-Javadoc)
     * 
     * @see org.talend.sqlbuilder.actions.AbstractEditorAction#run()
     */
//$NON-NLS-1$
@SuppressWarnings("unchecked")
@Override
public void run() {
    RepositoryNode node = editor.getRepositoryNode();
    SessionTreeNodeManager nodeManager = new SessionTreeNodeManager();
    SessionTreeNode runNode = null;
    try {
        runNode = nodeManager.getSessionTreeNode(node, editor.getDialog().getSelectedContext());
    } catch (Exception e) {
        //$NON-NLS-1$
        MessageDialog.openError(null, Messages.getString("AbstractSQLExecution.Executing.Error"), e.getMessage());
        //$NON-NLS-1$
        SqlBuilderPlugin.log(Messages.getString("OracleExplainPlanAction.logMessage1"), e);
        return;
    }
    Preferences prefs = SqlBuilderPlugin.getDefault().getPluginPreferences();
    String queryDelimiter = prefs.getString(IConstants.QUERY_DELIMITER);
    String alternateDelimiter = prefs.getString(IConstants.ALTERNATE_DELIMITER);
    String commentDelimiter = prefs.getString(IConstants.COMMENT_DELIMITER);
    QueryTokenizer qt = new QueryTokenizer(getSQLToBeExecuted(), queryDelimiter, alternateDelimiter, commentDelimiter);
    List queryStrings = new ArrayList();
    while (qt.hasQuery()) {
        final String querySql = qt.nextQuery();
        // ignore commented lines.
        if (!querySql.startsWith("--")) {
            //$NON-NLS-1$
            queryStrings.add(querySql);
        }
    }
    // check if we can run explain plans
    try {
        Statement st = runNode.getInteractiveConnection().createStatement();
        boolean createPlanTable = false;
        boolean notFoundTable = true;
        try {
            //$NON-NLS-1$
            ResultSet rs = st.executeQuery("select statement_id from plan_table");
            notFoundTable = false;
            rs.close();
        } catch (Throwable e) {
            createPlanTable = MessageDialog.openQuestion(null, //$NON-NLS-1$
            Messages.getString("oracle.editor.actions.explain.notFound.Title"), //$NON-NLS-1$
            Messages.getString("oracle.editor.actions.explain.notFound"));
        } finally {
            try {
                st.close();
            } catch (Throwable e) {
                //$NON-NLS-1$
                SqlBuilderPlugin.log(Messages.getString("OracleExplainPlanAction.logMessage2"), e);
            }
        }
        if (notFoundTable && !createPlanTable) {
            return;
        }
        if (notFoundTable && createPlanTable) {
            SQLConnection conn = runNode.getInteractiveConnection();
            st = conn.createStatement();
            try {
                st.execute(createPlanTableScript);
                if (!conn.getAutoCommit()) {
                    conn.commit();
                }
            } catch (Throwable e) {
                //$NON-NLS-1$
                SqlBuilderPlugin.log(Messages.getString("OracleExplainPlanAction.logMessage2"), e);
                //$NON-NLS-1$
                MessageDialog.openError(//$NON-NLS-1$
                null, //$NON-NLS-1$
                Messages.getString("oracle.editor.actions.explain.createError.Title"), //$NON-NLS-1$
                Messages.getString("oracle.editor.actions.explain.createError"));
                try {
                    st.close();
                } catch (Throwable e1) {
                    //$NON-NLS-1$
                    SqlBuilderPlugin.log(Messages.getString("OracleExplainPlanAction.logMessage3"), e1);
                }
                return;
            }
            try {
                st.close();
            } catch (Throwable e) {
                //$NON-NLS-1$
                SqlBuilderPlugin.log(Messages.getString("OracleExplainPlanAction.logMessage3"), e);
            }
        }
    } catch (Exception e) {
        //$NON-NLS-1$
        SqlBuilderPlugin.log(Messages.getString("OracleExplainPlanAction.logMessage4"), e);
    }
    try {
        while (!queryStrings.isEmpty()) {
            String querySql = (String) queryStrings.remove(0);
            if (querySql != null) {
                resultDisplayer.addSQLExecution(new OracleExplainPlanExecution(querySql, runNode));
            }
        }
    } catch (Exception e) {
        //$NON-NLS-1$
        SqlBuilderPlugin.log(Messages.getString("OracleExplainPlanAction.logMessage5"), e);
    }
}
Also used : Statement(java.sql.Statement) SessionTreeNode(org.talend.sqlbuilder.sessiontree.model.SessionTreeNode) SQLConnection(net.sourceforge.squirrel_sql.fw.sql.SQLConnection) ArrayList(java.util.ArrayList) RepositoryNode(org.talend.repository.model.RepositoryNode) QueryTokenizer(org.talend.sqlbuilder.util.QueryTokenizer) SessionTreeNodeManager(org.talend.sqlbuilder.dbstructure.SessionTreeNodeManager) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) List(java.util.List) Preferences(org.eclipse.core.runtime.Preferences)

Example 2 with SQLConnection

use of net.sourceforge.squirrel_sql.fw.sql.SQLConnection in project tdi-studio-se by Talend.

the class AbstractSQLSourceTab method getSource.

public String getSource() {
    String source = null;
    SQLConnection connection = getNode().getSession().getInteractiveConnection();
    ResultSet rs = null;
    Statement stmt = null;
    PreparedStatement pStmt = null;
    int timeOut = SqlBuilderPlugin.getDefault().getPluginPreferences().getInt(IConstants.INTERACTIVE_QUERY_TIMEOUT);
    try {
        Object[] params = getSQLParameters();
        if (params == null || params.length == 0) {
            // use normal statement
            stmt = connection.createStatement();
            stmt.setQueryTimeout(timeOut);
            rs = stmt.executeQuery(getSQL());
        } else {
            // use prepared statement
            pStmt = connection.prepareStatement(getSQL());
            pStmt.setQueryTimeout(timeOut);
            for (int i = 0; i < params.length; i++) {
                if (params[i] instanceof String) {
                    pStmt.setString(i + 1, (String) params[i]);
                } else if (params[i] instanceof Integer) {
                    pStmt.setInt(i + 1, ((Integer) params[i]).intValue());
                } else if (params[i] instanceof String) {
                    pStmt.setLong(i + 1, ((Long) params[i]).longValue());
                }
            }
            rs = pStmt.executeQuery();
        }
        //$NON-NLS-1$
        source = "";
        while (rs.next()) {
            source = source + rs.getString(1);
        }
        rs.close();
    } catch (Exception e) {
        //$NON-NLS-1$
        SqlBuilderPlugin.log(Messages.getString("AbstractSQLSourceTab.logMessage1") + getNode().getName(), e);
    } finally {
        if (stmt != null) {
            try {
                stmt.close();
            } catch (Exception e) {
                //$NON-NLS-1$
                SqlBuilderPlugin.log(Messages.getString("AbstractSQLSourceTab.logMessage2"), e);
            }
        }
        if (pStmt != null) {
            try {
                pStmt.close();
            } catch (Exception e) {
                //$NON-NLS-1$
                SqlBuilderPlugin.log(Messages.getString("AbstractSQLSourceTab.logMessage2"), e);
            }
        }
    }
    return source;
}
Also used : Statement(java.sql.Statement) PreparedStatement(java.sql.PreparedStatement) SQLConnection(net.sourceforge.squirrel_sql.fw.sql.SQLConnection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 3 with SQLConnection

use of net.sourceforge.squirrel_sql.fw.sql.SQLConnection in project tdi-studio-se by Talend.

the class AbstractSQLTab method getDataSet.

public final DataSet getDataSet() throws Exception {
    DataSet dataSet = null;
    int timeOut = SqlBuilderPlugin.getDefault().getPluginPreferences().getInt(IConstants.INTERACTIVE_QUERY_TIMEOUT);
    SQLConnection connection = getNode().getSession().getInteractiveConnection();
    ResultSet rs = null;
    Statement stmt = null;
    PreparedStatement pStmt = null;
    try {
        Object[] params = getSQLParameters();
        if (params == null || params.length == 0) {
            // use normal statement
            stmt = connection.createStatement();
            stmt.setQueryTimeout(timeOut);
            rs = stmt.executeQuery(getSQL());
        } else {
            // use prepared statement
            pStmt = connection.prepareStatement(getSQL());
            pStmt.setQueryTimeout(timeOut);
            for (int i = 0; i < params.length; i++) {
                if (params[i] instanceof String) {
                    pStmt.setString(i + 1, (String) params[i]);
                } else if (params[i] instanceof Integer) {
                    pStmt.setInt(i + 1, ((Integer) params[i]).intValue());
                } else if (params[i] instanceof String) {
                    pStmt.setLong(i + 1, ((Long) params[i]).longValue());
                }
            }
            rs = pStmt.executeQuery();
        }
        dataSet = new DataSet(null, rs, null);
        rs.close();
    } catch (Exception e) {
        //$NON-NLS-1$
        SqlBuilderPlugin.log(Messages.getString("AbstractSQLTab.logMessage1") + getNode().getName(), e);
    } finally {
        if (stmt != null) {
            try {
                stmt.close();
            } catch (Exception e) {
                //$NON-NLS-1$
                SqlBuilderPlugin.log(Messages.getString("AbstractSQLTab.logMessage2"), e);
            }
        }
        if (pStmt != null) {
            try {
                pStmt.close();
            } catch (Exception e) {
                //$NON-NLS-1$
                SqlBuilderPlugin.log(Messages.getString("AbstractSQLTab.logMessage2"), e);
            }
        }
    }
    return dataSet;
}
Also used : DataSet(org.talend.sqlbuilder.dataset.dataset.DataSet) Statement(java.sql.Statement) PreparedStatement(java.sql.PreparedStatement) SQLConnection(net.sourceforge.squirrel_sql.fw.sql.SQLConnection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 4 with SQLConnection

use of net.sourceforge.squirrel_sql.fw.sql.SQLConnection in project tdi-studio-se by Talend.

the class SessionTreeNodeUtils method createSQLConnection.

private static List createSQLConnection(DatabaseConnection con, String selectedContext, IMetadataConnection iMetadataConnection) throws Exception {
    // bug 17980
    ExtractMetaDataUtils extractMeta = ExtractMetaDataUtils.getInstance();
    List list = extractMeta.getConnection(iMetadataConnection.getDbType(), iMetadataConnection.getUrl(), iMetadataConnection.getUsername(), iMetadataConnection.getPassword(), iMetadataConnection.getDatabase(), iMetadataConnection.getSchema(), iMetadataConnection.getDriverClass(), iMetadataConnection.getDriverJarPath(), iMetadataConnection.getDbVersionString(), iMetadataConnection.getAdditionalParams());
    SQLConnection sqlConnection = new SQLConnection(extractMeta.getConn(), null);
    if (sqlConnection != null) {
        list.add(sqlConnection);
    }
    return list;
}
Also used : SQLConnection(net.sourceforge.squirrel_sql.fw.sql.SQLConnection) ExtractMetaDataUtils(org.talend.core.model.metadata.builder.database.ExtractMetaDataUtils) ArrayList(java.util.ArrayList) List(java.util.List)

Example 5 with SQLConnection

use of net.sourceforge.squirrel_sql.fw.sql.SQLConnection in project tdi-studio-se by Talend.

the class SessionTreeNodeUtils method disposeConnections.

/**
     * disposeConnections.
     */
private static void disposeConnections() {
    SQLConnection connection = null;
    for (Iterator it = connections.iterator(); it.hasNext(); ) {
        connection = (SQLConnection) it.next();
        try {
            connection.close();
        } catch (Exception e) {
            //$NON-NLS-1$
            SqlBuilderPlugin.log(Messages.getString("SessionTreeNodeUtils.logMessage"), e);
        }
    }
    connections.clear();
}
Also used : SQLConnection(net.sourceforge.squirrel_sql.fw.sql.SQLConnection) Iterator(java.util.Iterator)

Aggregations

SQLConnection (net.sourceforge.squirrel_sql.fw.sql.SQLConnection)9 ResultSet (java.sql.ResultSet)5 Statement (java.sql.Statement)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 PreparedStatement (java.sql.PreparedStatement)3 SessionTreeNode (org.talend.sqlbuilder.sessiontree.model.SessionTreeNode)3 Preferences (org.eclipse.core.runtime.Preferences)2 IMetadataConnection (org.talend.core.model.metadata.IMetadataConnection)2 RepositoryNode (org.talend.repository.model.RepositoryNode)2 SessionTreeNodeManager (org.talend.sqlbuilder.dbstructure.SessionTreeNodeManager)2 QueryTokenizer (org.talend.sqlbuilder.util.QueryTokenizer)2 Connection (java.sql.Connection)1 Iterator (java.util.Iterator)1 ISQLAlias (net.sourceforge.squirrel_sql.fw.sql.ISQLAlias)1 DatabaseConnection (org.talend.core.model.metadata.builder.connection.DatabaseConnection)1 DriverShim (org.talend.core.model.metadata.builder.database.DriverShim)1 ExtractMetaDataUtils (org.talend.core.model.metadata.builder.database.ExtractMetaDataUtils)1 DataSet (org.talend.sqlbuilder.dataset.dataset.DataSet)1 SessionTreeModel (org.talend.sqlbuilder.sessiontree.model.SessionTreeModel)1