Search in sources :

Example 16 with DBException

use of org.jkiss.dbeaver.DBException in project dbeaver by serge-rider.

the class MySQLStructureAssistant method findTableColumnsByMask.

private void findTableColumnsByMask(JDBCSession session, @Nullable final MySQLCatalog catalog, String constrNameMask, int maxResults, List<DBSObjectReference> objects) throws SQLException, DBException {
    DBRProgressMonitor monitor = session.getProgressMonitor();
    // Load columns
    try (JDBCPreparedStatement dbStat = session.prepareStatement("SELECT " + MySQLConstants.COL_TABLE_SCHEMA + "," + MySQLConstants.COL_TABLE_NAME + "," + MySQLConstants.COL_COLUMN_NAME + " FROM " + MySQLConstants.META_TABLE_COLUMNS + " WHERE " + MySQLConstants.COL_COLUMN_NAME + " LIKE ? " + (catalog == null ? "" : " AND " + MySQLConstants.COL_TABLE_SCHEMA + "=?") + " ORDER BY " + MySQLConstants.COL_COLUMN_NAME + " LIMIT " + maxResults)) {
        dbStat.setString(1, constrNameMask.toLowerCase(Locale.ENGLISH));
        if (catalog != null) {
            dbStat.setString(2, catalog.getName());
        }
        try (JDBCResultSet dbResult = dbStat.executeQuery()) {
            int tableNum = maxResults;
            while (dbResult.next() && tableNum-- > 0) {
                if (monitor.isCanceled()) {
                    break;
                }
                final String catalogName = JDBCUtils.safeGetString(dbResult, MySQLConstants.COL_TABLE_SCHEMA);
                final String tableName = JDBCUtils.safeGetString(dbResult, MySQLConstants.COL_TABLE_NAME);
                final String columnName = JDBCUtils.safeGetString(dbResult, MySQLConstants.COL_COLUMN_NAME);
                objects.add(new AbstractObjectReference(columnName, dataSource.getCatalog(catalogName), null, MySQLTableColumn.class, RelationalObjectType.TYPE_TABLE_COLUMN) {

                    @NotNull
                    @Override
                    public String getFullyQualifiedName(DBPEvaluationContext context) {
                        return DBUtils.getQuotedIdentifier(dataSource, catalogName) + '.' + DBUtils.getQuotedIdentifier(dataSource, tableName) + '.' + DBUtils.getQuotedIdentifier(dataSource, columnName);
                    }

                    @Override
                    public DBSObject resolveObject(DBRProgressMonitor monitor) throws DBException {
                        MySQLCatalog tableCatalog = catalog != null ? catalog : dataSource.getCatalog(catalogName);
                        if (tableCatalog == null) {
                            throw new DBException("Column catalog '" + catalogName + "' not found");
                        }
                        MySQLTableBase table = tableCatalog.getTableCache().getObject(monitor, tableCatalog, tableName);
                        if (table == null) {
                            throw new DBException("Column table '" + tableName + "' not found in catalog '" + tableCatalog.getName() + "'");
                        }
                        MySQLTableColumn column = table.getAttribute(monitor, columnName);
                        if (column == null) {
                            throw new DBException("Column '" + columnName + "' not found in table '" + table.getFullyQualifiedName(DBPEvaluationContext.DDL) + "'");
                        }
                        return column;
                    }
                });
            }
        }
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBPEvaluationContext(org.jkiss.dbeaver.model.DBPEvaluationContext) NotNull(org.jkiss.code.NotNull) JDBCPreparedStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) AbstractObjectReference(org.jkiss.dbeaver.model.impl.struct.AbstractObjectReference) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)

Example 17 with DBException

use of org.jkiss.dbeaver.DBException 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 18 with DBException

use of org.jkiss.dbeaver.DBException in project dbeaver by serge-rider.

the class MySQLUserEditorPrivileges method activatePart.

@Override
public synchronized void activatePart() {
    if (isLoaded) {
        return;
    }
    isLoaded = true;
    LoadingJob.createService(new DatabaseLoadService<java.util.List<MySQLPrivilege>>(MySQLMessages.editors_user_editor_privileges_service_load_privileges, getExecutionContext()) {

        @Override
        public java.util.List<MySQLPrivilege> evaluate(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
            try {
                return getDatabaseObject().getDataSource().getPrivileges(monitor);
            } catch (DBException e) {
                throw new InvocationTargetException(e);
            }
        }
    }, pageControl.createPrivilegesLoadVisualizer()).schedule();
}
Also used : DBException(org.jkiss.dbeaver.DBException) java.util(java.util) DatabaseLoadService(org.jkiss.dbeaver.model.runtime.load.DatabaseLoadService) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 19 with DBException

use of org.jkiss.dbeaver.DBException 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 20 with DBException

use of org.jkiss.dbeaver.DBException 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

DBException (org.jkiss.dbeaver.DBException)232 SQLException (java.sql.SQLException)58 JDBCSession (org.jkiss.dbeaver.model.exec.jdbc.JDBCSession)51 JDBCResultSet (org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet)50 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)43 JDBCPreparedStatement (org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement)42 ArrayList (java.util.ArrayList)37 InvocationTargetException (java.lang.reflect.InvocationTargetException)23 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)23 DBNDatabaseNode (org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)16 DBRRunnableWithProgress (org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress)14 DBPDataSource (org.jkiss.dbeaver.model.DBPDataSource)13 DBNNode (org.jkiss.dbeaver.model.navigator.DBNNode)13 GridData (org.eclipse.swt.layout.GridData)12 DBCException (org.jkiss.dbeaver.model.exec.DBCException)12 CoreException (org.eclipse.core.runtime.CoreException)11 AbstractObjectReference (org.jkiss.dbeaver.model.impl.struct.AbstractObjectReference)10 IStatus (org.eclipse.core.runtime.IStatus)9 DBSEntityAttribute (org.jkiss.dbeaver.model.struct.DBSEntityAttribute)8 XMLException (org.jkiss.utils.xml.XMLException)8