Search in sources :

Example 1 with JDBCPreparedStatement

use of org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement in project dbeaver by serge-rider.

the class ExasolCurrentUserPrivileges method verifyPriv.

private static Boolean verifyPriv(String sql, JDBCSession session) {
    JDBCPreparedStatement dbStat;
    Boolean hasPriv;
    try {
        dbStat = session.prepareStatement(C_CONNECTIONS);
        ResultSet rs = dbStat.executeQuery();
        rs.close();
        dbStat.close();
        hasPriv = true;
    } catch (SQLException e) {
        hasPriv = false;
    }
    return hasPriv;
}
Also used : JDBCPreparedStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet)

Example 2 with JDBCPreparedStatement

use of org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement in project dbeaver by serge-rider.

the class ExasolPlanAnalyser method explain.

public void explain(DBCSession session) throws DBCException {
    rootNodes = new ArrayList<>();
    JDBCSession connection = (JDBCSession) session;
    boolean oldAutoCommit = false;
    try {
        oldAutoCommit = connection.getAutoCommit();
        if (oldAutoCommit)
            connection.setAutoCommit(false);
        //alter session
        JDBCUtils.executeSQL(connection, "ALTER SESSION SET PROFILE = 'ON'");
        //execute query
        JDBCUtils.executeSQL(connection, query);
        //alter session
        JDBCUtils.executeSQL(connection, "ALTER SESSION SET PROFILE = 'OFF'");
        //rollback in case of DML
        connection.rollback();
        //alter session
        JDBCUtils.executeSQL(connection, "FLUSH STATISTICS");
        connection.commit();
        //retrieve execute info
        try (JDBCPreparedStatement stmt = connection.prepareStatement("SELECT * FROM EXA_USER_PROFILE_LAST_DAY WHERE SESSION_ID = CURRENT_SESSION AND STMT_ID = (select max(stmt_id) from EXA_USER_PROFILE_LAST_DAY where sql_text = ?)")) {
            stmt.setString(1, query);
            try (JDBCResultSet dbResult = stmt.executeQuery()) {
                while (dbResult.next()) {
                    ExasolPlanNode node = new ExasolPlanNode(null, dbResult);
                    rootNodes.add(node);
                }
            }
        }
    } catch (SQLException e) {
        throw new DBCException(e, session.getDataSource());
    } finally {
        //rollback changes because profile actually executes query and it could be INSERT/UPDATE
        try {
            connection.rollback();
            if (oldAutoCommit)
                connection.setAutoCommit(true);
        } catch (SQLException e) {
            LOG.error("Error closing plan analyser", e);
        }
    }
}
Also used : JDBCPreparedStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement) JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) SQLException(java.sql.SQLException) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet) DBCException(org.jkiss.dbeaver.model.exec.DBCException)

Example 3 with JDBCPreparedStatement

use of org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement in project dbeaver by serge-rider.

the class DerbyMetaModel method loadSequences.

@Override
public List<GenericSequence> loadSequences(@NotNull DBRProgressMonitor monitor, @NotNull GenericStructContainer container) throws DBException {
    try (JDBCSession session = DBUtils.openMetaSession(monitor, container.getDataSource(), "Read procedure definition")) {
        try (JDBCPreparedStatement dbStat = session.prepareStatement("SELECT seq.SEQUENCENAME,seq.CURRENTVALUE,seq.MINIMUMVALUE,seq.MAXIMUMVALUE,seq.INCREMENT\n" + "FROM sys.SYSSEQUENCES seq,sys.SYSSCHEMAS s\n" + "WHERE seq.SCHEMAID=s.SCHEMAID AND s.SCHEMANAME=?")) {
            dbStat.setString(1, container.getName());
            try (JDBCResultSet dbResult = dbStat.executeQuery()) {
                List<GenericSequence> result = new ArrayList<GenericSequence>();
                while (dbResult.next()) {
                    GenericSequence sequence = new GenericSequence(container, JDBCUtils.safeGetString(dbResult, 1), "", JDBCUtils.safeGetLong(dbResult, 2), JDBCUtils.safeGetLong(dbResult, 3), JDBCUtils.safeGetLong(dbResult, 4), JDBCUtils.safeGetLong(dbResult, 5));
                    result.add(sequence);
                }
                return result;
            }
        }
    } catch (SQLException e) {
        throw new DBException(e, container.getDataSource());
    }
}
Also used : JDBCPreparedStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement) JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) DBException(org.jkiss.dbeaver.DBException) SQLException(java.sql.SQLException) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet) ArrayList(java.util.ArrayList)

Example 4 with JDBCPreparedStatement

use of org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement in project dbeaver by serge-rider.

the class DB2PlanStatement method loadChildren.

// -------------
// Load children
// -------------
private void loadChildren(JDBCSession session) throws SQLException {
    mapDataObjects = new HashMap<>(32);
    try (JDBCPreparedStatement sqlStmt = session.prepareStatement(String.format(SEL_BASE_SELECT, planTableSchema, "EXPLAIN_OBJECT", "OBJECT_SCHEMA,OBJECT_NAME"))) {
        setQueryParameters(sqlStmt);
        try (JDBCResultSet res = sqlStmt.executeQuery()) {
            DB2PlanObject db2PlanObject;
            while (res.next()) {
                db2PlanObject = new DB2PlanObject(res);
                mapDataObjects.put(db2PlanObject.getNodeName(), db2PlanObject);
            }
        }
    }
    mapOperators = new HashMap<>(64);
    try (JDBCPreparedStatement sqlStmt = session.prepareStatement(String.format(SEL_BASE_SELECT, planTableSchema, "EXPLAIN_OPERATOR", "OPERATOR_ID"))) {
        setQueryParameters(sqlStmt);
        try (JDBCResultSet res = sqlStmt.executeQuery()) {
            DB2PlanOperator db2PlanOperator;
            while (res.next()) {
                db2PlanOperator = new DB2PlanOperator(session, res, this, planTableSchema);
                mapOperators.put(db2PlanOperator.getNodeName(), db2PlanOperator);
                if (db2PlanOperator.getOperatorType() == DB2PlanOperatorType.RETURN) {
                    rootNode = db2PlanOperator;
                }
            }
        }
    }
    listStreams = new ArrayList<>();
    try (JDBCPreparedStatement sqlStmt = session.prepareStatement(String.format(SEL_BASE_SELECT, planTableSchema, "EXPLAIN_STREAM", "STREAM_ID DESC"))) {
        setQueryParameters(sqlStmt);
        try (JDBCResultSet res = sqlStmt.executeQuery()) {
            while (res.next()) {
                listStreams.add(new DB2PlanStream(res, this));
            }
        }
    }
}
Also used : JDBCPreparedStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet)

Example 5 with JDBCPreparedStatement

use of org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement in project dbeaver by serge-rider.

the class DB2GranteeAuthCache method prepareObjectsStatement.

@Override
protected JDBCStatement prepareObjectsStatement(@NotNull JDBCSession session, @NotNull DB2Grantee db2Grantee) throws SQLException {
    String userType = db2Grantee.getType().name();
    String userName = db2Grantee.getName();
    String sql;
    int nbMax;
    if (db2Grantee.getDataSource().isAtLeastV9_7()) {
        sql = SQL;
        nbMax = 22;
    } else {
        sql = SQL_WITHOUT_MODULE;
        nbMax = 20;
    }
    JDBCPreparedStatement dbStat = session.prepareStatement(sql);
    for (int i = 1; i <= nbMax; ) {
        dbStat.setString(i++, userType);
        dbStat.setString(i++, userName);
    }
    return dbStat;
}
Also used : JDBCPreparedStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement)

Aggregations

JDBCPreparedStatement (org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement)100 JDBCResultSet (org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet)66 SQLException (java.sql.SQLException)49 JDBCSession (org.jkiss.dbeaver.model.exec.jdbc.JDBCSession)46 DBException (org.jkiss.dbeaver.DBException)45 ArrayList (java.util.ArrayList)18 DBCException (org.jkiss.dbeaver.model.exec.DBCException)15 NotNull (org.jkiss.code.NotNull)12 AbstractObjectReference (org.jkiss.dbeaver.model.impl.struct.AbstractObjectReference)10 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)10 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)8 DB2DataSource (org.jkiss.dbeaver.ext.db2.model.DB2DataSource)4 DB2XMLString (org.jkiss.dbeaver.ext.db2.info.DB2XMLString)3 GenericDataSource (org.jkiss.dbeaver.ext.generic.model.GenericDataSource)3 ResultSet (java.sql.ResultSet)2 DB2Parameter (org.jkiss.dbeaver.ext.db2.info.DB2Parameter)2 DB2Schema (org.jkiss.dbeaver.ext.db2.model.DB2Schema)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Clob (java.sql.Clob)1 SQLXML (java.sql.SQLXML)1