Search in sources :

Example 6 with JDBCPreparedStatement

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

the class MySQLUser method getGrants.

public List<MySQLGrant> getGrants(DBRProgressMonitor monitor) throws DBException {
    if (this.grants != null) {
        return this.grants;
    }
    if (!isPersisted()) {
        this.grants = new ArrayList<>();
        return this.grants;
    }
    try (JDBCSession session = DBUtils.openMetaSession(monitor, getDataSource(), "Read catalog privileges")) {
        try (JDBCPreparedStatement dbStat = session.prepareStatement("SHOW GRANTS FOR " + getFullName())) {
            try (JDBCResultSet dbResult = dbStat.executeQuery()) {
                List<MySQLGrant> grants = new ArrayList<>();
                while (dbResult.next()) {
                    List<MySQLPrivilege> privileges = new ArrayList<>();
                    boolean allPrivilegesFlag = false;
                    boolean grantOption = false;
                    String catalog = null;
                    String table = null;
                    String grantString = CommonUtils.notEmpty(JDBCUtils.safeGetString(dbResult, 1)).trim().toUpperCase(Locale.ENGLISH);
                    if (grantString.endsWith(" WITH GRANT OPTION")) {
                        //privileges.add(getDataSource().getPrivilege(monitor, MySQLPrivilege.GRANT_PRIVILEGE));
                        grantOption = true;
                    }
                    String privString;
                    Matcher matcher = MySQLGrant.TABLE_GRANT_PATTERN.matcher(grantString);
                    if (matcher.find()) {
                        privString = matcher.group(1);
                        catalog = matcher.group(2);
                        table = matcher.group(3);
                    } else {
                        matcher = MySQLGrant.GLOBAL_GRANT_PATTERN.matcher(grantString);
                        if (matcher.find()) {
                            privString = matcher.group(1);
                        } else {
                            log.warn("Can't parse GRANT string: " + grantString);
                            continue;
                        }
                    }
                    StringTokenizer st = new StringTokenizer(privString, ",");
                    while (st.hasMoreTokens()) {
                        String privName = st.nextToken().trim();
                        if (privName.equalsIgnoreCase(MySQLPrivilege.ALL_PRIVILEGES)) {
                            allPrivilegesFlag = true;
                            continue;
                        }
                        MySQLPrivilege priv = getDataSource().getPrivilege(monitor, privName);
                        if (priv == null) {
                            log.warn("Can't find privilege '" + privName + "'");
                        } else {
                            privileges.add(priv);
                        }
                    }
                    grants.add(new MySQLGrant(this, privileges, catalog, table, allPrivilegesFlag, grantOption));
                }
                this.grants = grants;
                return this.grants;
            }
        }
    } catch (SQLException e) {
        throw new DBException(e, getDataSource());
    }
}
Also used : JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) DBException(org.jkiss.dbeaver.DBException) Matcher(java.util.regex.Matcher) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) JDBCPreparedStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement) StringTokenizer(java.util.StringTokenizer) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet)

Example 7 with JDBCPreparedStatement

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

the class MySQLView method loadAdditionalInfo.

private void loadAdditionalInfo(DBRProgressMonitor monitor) throws DBCException {
    if (!isPersisted() || getContainer().isSystem()) {
        additionalInfo.loaded = true;
        return;
    }
    try (JDBCSession session = DBUtils.openMetaSession(monitor, getDataSource(), "Load table status")) {
        try (JDBCPreparedStatement dbStat = session.prepareStatement("SELECT * FROM " + MySQLConstants.META_TABLE_VIEWS + " WHERE " + MySQLConstants.COL_TABLE_SCHEMA + "=? AND " + MySQLConstants.COL_TABLE_NAME + "=?")) {
            dbStat.setString(1, getContainer().getName());
            dbStat.setString(2, getName());
            try (JDBCResultSet dbResult = dbStat.executeQuery()) {
                if (dbResult.next()) {
                    try {
                        additionalInfo.setCheckOption(CheckOption.valueOf(JDBCUtils.safeGetString(dbResult, MySQLConstants.COL_CHECK_OPTION)));
                    } catch (IllegalArgumentException e) {
                        log.warn(e);
                    }
                    additionalInfo.setDefiner(JDBCUtils.safeGetString(dbResult, MySQLConstants.COL_DEFINER));
                    additionalInfo.setDefinition(SQLUtils.formatSQL(getDataSource(), JDBCUtils.safeGetString(dbResult, MySQLConstants.COL_VIEW_DEFINITION)));
                    additionalInfo.setUpdatable("YES".equals(JDBCUtils.safeGetString(dbResult, MySQLConstants.COL_IS_UPDATABLE)));
                }
                additionalInfo.loaded = true;
            }
        }
    } catch (SQLException e) {
        throw new DBCException(e, getDataSource());
    }
}
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 8 with JDBCPreparedStatement

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

the class MySQLPlanAnalyser method explain.

public void explain(DBCSession session) throws DBCException {
    String plainQuery = SQLUtils.stripComments(SQLUtils.getDialectFromObject(session.getDataSource()), query).toUpperCase();
    if (!plainQuery.startsWith("SELECT")) {
        throw new DBCException("Only SELECT statements could produce execution plan");
    }
    JDBCSession connection = (JDBCSession) session;
    try {
        try (JDBCPreparedStatement dbStat = connection.prepareStatement("EXPLAIN EXTENDED " + query)) {
            try (JDBCResultSet dbResult = dbStat.executeQuery()) {
                rootNodes = new ArrayList<>();
                while (dbResult.next()) {
                    MySQLPlanNode node = new MySQLPlanNode(null, dbResult);
                    rootNodes.add(node);
                }
            }
        }
    } catch (SQLException e) {
        throw new DBCException(e, session.getDataSource());
    }
}
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 9 with JDBCPreparedStatement

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

the class MySQLDataSource method loadParameters.

private List<MySQLParameter> loadParameters(DBRProgressMonitor monitor, boolean status, boolean global) throws DBException {
    try (JDBCSession session = DBUtils.openMetaSession(monitor, this, "Load status")) {
        try (JDBCPreparedStatement dbStat = session.prepareStatement("SHOW " + (global ? "GLOBAL " : "") + (status ? "STATUS" : "VARIABLES"))) {
            try (JDBCResultSet dbResult = dbStat.executeQuery()) {
                List<MySQLParameter> parameters = new ArrayList<>();
                while (dbResult.next()) {
                    MySQLParameter parameter = new MySQLParameter(this, JDBCUtils.safeGetString(dbResult, "variable_name"), JDBCUtils.safeGetString(dbResult, "value"));
                    parameters.add(parameter);
                }
                return parameters;
            }
        }
    } catch (SQLException ex) {
        throw new DBException(ex, this);
    }
}
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)

Example 10 with JDBCPreparedStatement

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

the class MySQLDataSource method loadUsers.

private List<MySQLUser> loadUsers(DBRProgressMonitor monitor) throws DBException {
    try (JDBCSession session = DBUtils.openMetaSession(monitor, this, "Load users")) {
        try (JDBCPreparedStatement dbStat = session.prepareStatement("SELECT * FROM mysql.user ORDER BY user")) {
            try (JDBCResultSet dbResult = dbStat.executeQuery()) {
                List<MySQLUser> userList = new ArrayList<>();
                while (dbResult.next()) {
                    MySQLUser user = new MySQLUser(this, dbResult);
                    userList.add(user);
                }
                return userList;
            }
        }
    } catch (SQLException ex) {
        throw new DBException(ex, this);
    }
}
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)

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