Search in sources :

Example 6 with SQLConnection

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

the class DB2ExplainPlanAction 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("DB2ExplainPlanAction.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);
    final 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 queryno from SYSTOOLS.EXPLAIN_STATEMENT");
            notFoundTable = false;
            rs.close();
        } catch (Throwable e) {
            createPlanTable = MessageDialog.openQuestion(null, //$NON-NLS-1$
            Messages.getString("db2.editor.actions.explain.notFound.Title"), //$NON-NLS-1$
            Messages.getString("db2.editor.actions.explain.notFound"));
        } finally {
            try {
                st.close();
            } catch (Throwable e) {
                //$NON-NLS-1$
                SqlBuilderPlugin.log(Messages.getString("DB2ExplainPlanAction.logMessage2"), e);
            }
        }
        if (notFoundTable && !createPlanTable) {
            return;
        }
        if (notFoundTable && createPlanTable) {
            SQLConnection conn = runNode.getInteractiveConnection();
            st = conn.createStatement();
            try {
                st.execute(createPlanScript1);
                st.execute(createPlanScript2);
                st.execute(createPlanScript3);
                st.execute(createPlanScript4);
                st.execute(createPlanScript5);
                st.execute(createPlanScript6);
                st.execute(createPlanScript7);
                if (!conn.getAutoCommit()) {
                    conn.commit();
                }
            } catch (Throwable e) {
                //$NON-NLS-1$
                SqlBuilderPlugin.log(Messages.getString("DB2ExplainPlanAction.logMessage3"), e);
                //$NON-NLS-1$
                MessageDialog.openError(//$NON-NLS-1$
                null, //$NON-NLS-1$
                Messages.getString("db2.editor.actions.explain.createError.Title"), //$NON-NLS-1$
                Messages.getString("db2.editor.actions.explain.createError"));
                try {
                    st.close();
                } catch (Throwable e1) {
                    //$NON-NLS-1$
                    SqlBuilderPlugin.log(Messages.getString("DB2ExplainPlanAction.logMessage2"), e1);
                }
                return;
            }
            try {
                st.close();
            } catch (Throwable e) {
                //$NON-NLS-1$
                SqlBuilderPlugin.log(Messages.getString("DB2ExplainPlanAction.logMessage2"), e);
            }
        }
    } catch (Exception e) {
        //$NON-NLS-1$
        SqlBuilderPlugin.log(Messages.getString("DB2ExplainPlanAction.logMessage4"), e);
    }
    try {
        while (!queryStrings.isEmpty()) {
            String querySql = (String) queryStrings.remove(0);
            if (querySql != null) {
                resultDisplayer.addSQLExecution(new DB2ExplainPlanExecution(querySql, runNode));
            }
        }
    } catch (Exception e) {
        //$NON-NLS-1$
        SqlBuilderPlugin.log(Messages.getString("DB2ExplainPlanAction.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 7 with SQLConnection

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

the class SessionTreeNodeUtils method createSQLConnection.

/**
     * DOC qianbing Comment method "createSQLConnection".
     * 
     * @param dbType database Type
     * @param url url
     * @param userName userName
     * @param password password
     * @return SQLConnection
     * @throws Exception Exception
     */
protected static SQLConnection createSQLConnection(String dbType, String url, String userName, String password) throws Exception {
    Class.forName(ExtractMetaDataUtils.getInstance().getDriverClassByDbType(dbType)).newInstance();
    ExtractMetaDataUtils.getInstance().checkDBConnectionTimeout();
    Connection connection = DriverManager.getConnection(url, userName, password);
    SQLConnection sqlConnection = new SQLConnection(connection, null);
    return sqlConnection;
}
Also used : SQLConnection(net.sourceforge.squirrel_sql.fw.sql.SQLConnection) Connection(java.sql.Connection) DatabaseConnection(org.talend.core.model.metadata.builder.connection.DatabaseConnection) SQLConnection(net.sourceforge.squirrel_sql.fw.sql.SQLConnection) IMetadataConnection(org.talend.core.model.metadata.IMetadataConnection)

Example 8 with SQLConnection

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

the class SessionTreeNodeUtils method getSessionTreeNode.

public static SessionTreeNode getSessionTreeNode(DatabaseConnection dbconnection, RepositoryNode repositoryNode, String selectedContext) throws Exception {
    // hyWang add for bug 0007014
    IMetadataConnection iMetadataConnection = null;
    iMetadataConnection = ConvertionHelper.convert(dbconnection, false, selectedContext);
    String url = dbconnection.getURL();
    if (url == null || url.equals("")) {
        url = iMetadataConnection.getUrl();
    }
    // bug 17980
    SQLConnection connection = null;
    DriverShim wapperDriver = null;
    List list = createSQLConnection(dbconnection, selectedContext, iMetadataConnection);
    if (list != null && list.size() > 0) {
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i) instanceof SQLConnection) {
                connection = (SQLConnection) list.get(i);
            }
            if (list.get(i) instanceof DriverShim) {
                wapperDriver = (DriverShim) list.get(i);
            }
        }
    }
    ISQLAlias alias = createSQLAlias("Repository Name", url, dbconnection.getUsername(), //$NON-NLS-1$
    dbconnection.getRawPassword(), // fix bug for 7014,added by hyWang
    dbconnection.getSID() == null || dbconnection.getSID().length() == 0 ? (dbconnection.getDatasourceName() == null || //$NON-NLS-1$  
    dbconnection.getDatasourceName().length() == 0 ? //$NON-NLS-1$  
    "Database" : dbconnection.getDatasourceName()) : dbconnection.getSID());
    SessionTreeModel stm = new SessionTreeModel();
    SessionTreeNode session;
    if (wapperDriver != null && (iMetadataConnection.getDriverClass().equals(EDatabase4DriverClassName.JAVADB_EMBEDED.getDriverClass()) || iMetadataConnection.getDbType().equals(EDatabaseTypeName.JAVADB_EMBEDED.getDisplayName()) || iMetadataConnection.getDbType().equals(EDatabaseTypeName.JAVADB_DERBYCLIENT.getDisplayName()) || iMetadataConnection.getDbType().equals(EDatabaseTypeName.JAVADB_JCCJDBC.getDisplayName()) || iMetadataConnection.getDbType().equals(EDatabaseTypeName.HSQLDB_IN_PROGRESS.getDisplayName()))) {
        session = stm.createSessionTreeNode(new SQLConnection[] { connection, connection }, alias, null, dbconnection.getRawPassword(), repositoryNode, wapperDriver);
    } else {
        session = stm.createSessionTreeNode(new SQLConnection[] { connection, connection }, alias, null, dbconnection.getRawPassword(), repositoryNode);
    }
    return session;
}
Also used : SQLConnection(net.sourceforge.squirrel_sql.fw.sql.SQLConnection) SessionTreeNode(org.talend.sqlbuilder.sessiontree.model.SessionTreeNode) IMetadataConnection(org.talend.core.model.metadata.IMetadataConnection) ArrayList(java.util.ArrayList) List(java.util.List) DriverShim(org.talend.core.model.metadata.builder.database.DriverShim) SessionTreeModel(org.talend.sqlbuilder.sessiontree.model.SessionTreeModel) ISQLAlias(net.sourceforge.squirrel_sql.fw.sql.ISQLAlias)

Example 9 with SQLConnection

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

the class AbstractSQLFolderNode method loadChildren.

/**
     * Load Children.
     */
public final void loadChildren() {
    SQLConnection connection = 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();
        }
        while (rs.next()) {
            String name = rs.getString(1);
            if (!isExcludedByFilter(name)) {
                ObjectNode node = new ObjectNode(name, getChildType(), this, pimage);
                addChildNode(node);
            }
        }
        rs.close();
    } catch (Exception e) {
        //$NON-NLS-1$
        SqlBuilderPlugin.log(Messages.getString("AbstractSQLFolderNode.logMessage1") + getName(), e);
    } finally {
        if (stmt != null) {
            try {
                stmt.close();
            } catch (Exception e) {
                //$NON-NLS-1$
                SqlBuilderPlugin.log(Messages.getString("AbstractSQLFolderNode.logMessage2"), e);
            }
        }
        if (pStmt != null) {
            try {
                pStmt.close();
            } catch (Exception e) {
                //$NON-NLS-1$
                SqlBuilderPlugin.log(Messages.getString("AbstractSQLFolderNode.logMessage2"), e);
            }
        }
    }
}
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)

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