Search in sources :

Example 21 with JDBCSession

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

the class PostgreRole method getPermissions.

@Override
public Collection<PostgrePermission> getPermissions(DBRProgressMonitor monitor) throws DBException {
    try (JDBCSession session = DBUtils.openMetaSession(monitor, getDataSource(), "Read role privileges")) {
        try (JDBCPreparedStatement dbStat = session.prepareStatement("SELECT * FROM information_schema.table_privileges WHERE table_catalog=? AND grantee=?")) {
            dbStat.setString(1, getDatabase().getName());
            dbStat.setString(2, getName());
            try (JDBCResultSet dbResult = dbStat.executeQuery()) {
                Map<String, List<PostgrePrivilege>> privs = new LinkedHashMap<>();
                while (dbResult.next()) {
                    PostgrePrivilege privilege = new PostgrePrivilege(dbResult);
                    String tableId = privilege.getTableSchema() + "." + privilege.getTableName();
                    List<PostgrePrivilege> privList = privs.get(tableId);
                    if (privList == null) {
                        privList = new ArrayList<>();
                        privs.put(tableId, privList);
                    }
                    privList.add(privilege);
                }
                // Pack to permission list
                List<PostgrePermission> result = new ArrayList<>(privs.size());
                for (List<PostgrePrivilege> priv : privs.values()) {
                    result.add(new PostgreRolePermission(this, priv.get(0).getTableSchema(), priv.get(0).getTableName(), priv));
                }
                Collections.sort(result);
                return result;
            }
        } catch (SQLException e) {
            throw new DBException(e, getDataSource());
        }
    }
}
Also used : JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) DBException(org.jkiss.dbeaver.DBException) SQLException(java.sql.SQLException) JDBCPreparedStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet)

Example 22 with JDBCSession

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

the class PostgreTable method getSuperInheritance.

@NotNull
public List<PostgreTableInheritance> getSuperInheritance(DBRProgressMonitor monitor) throws DBException {
    if (superTables == null) {
        try (JDBCSession session = DBUtils.openMetaSession(monitor, getDataSource(), "Load table inheritance info")) {
            try (JDBCPreparedStatement dbStat = session.prepareStatement("SELECT i.*,c.relnamespace " + "FROM pg_catalog.pg_inherits i,pg_catalog.pg_class c " + "WHERE i.inhrelid=? AND c.oid=i.inhparent " + "ORDER BY i.inhseqno")) {
                dbStat.setLong(1, getObjectId());
                try (JDBCResultSet dbResult = dbStat.executeQuery()) {
                    while (dbResult.next()) {
                        final long parentSchemaId = JDBCUtils.safeGetLong(dbResult, "relnamespace");
                        final long parentTableId = JDBCUtils.safeGetLong(dbResult, "inhparent");
                        PostgreSchema schema = getDatabase().getSchema(monitor, parentSchemaId);
                        if (schema == null) {
                            log.warn("Can't find parent table's schema '" + parentSchemaId + "'");
                            continue;
                        }
                        PostgreTableBase parentTable = schema.getTable(monitor, parentTableId);
                        if (parentTable == null) {
                            log.warn("Can't find parent table '" + parentTableId + "' in '" + schema.getName() + "'");
                            continue;
                        }
                        if (superTables == null) {
                            superTables = new ArrayList<>();
                        }
                        superTables.add(new PostgreTableInheritance(this, parentTable, JDBCUtils.safeGetInt(dbResult, "inhseqno"), true));
                    }
                }
            }
        } catch (SQLException e) {
            throw new DBCException(e, getDataSource());
        }
        if (superTables == null) {
            superTables = Collections.emptyList();
        }
    }
    return superTables;
}
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) NotNull(org.jkiss.code.NotNull)

Example 23 with JDBCSession

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

the class PostgreTableBase method getPermissions.

@Override
public Collection<PostgrePermission> getPermissions(DBRProgressMonitor monitor) throws DBException {
    try (JDBCSession session = DBUtils.openMetaSession(monitor, getDataSource(), "Read table privileges")) {
        try (JDBCPreparedStatement dbStat = session.prepareStatement("SELECT * FROM information_schema.table_privileges WHERE table_catalog=? AND table_schema=? AND table_name=?")) {
            dbStat.setString(1, getDatabase().getName());
            dbStat.setString(2, getSchema().getName());
            dbStat.setString(3, getName());
            try (JDBCResultSet dbResult = dbStat.executeQuery()) {
                Map<String, List<PostgrePrivilege>> privs = new LinkedHashMap<>();
                while (dbResult.next()) {
                    PostgrePrivilege privilege = new PostgrePrivilege(dbResult);
                    List<PostgrePrivilege> privList = privs.get(privilege.getGrantee());
                    if (privList == null) {
                        privList = new ArrayList<>();
                        privs.put(privilege.getGrantee(), privList);
                    }
                    privList.add(privilege);
                }
                // Pack to permission list
                List<PostgrePermission> result = new ArrayList<>(privs.size());
                for (List<PostgrePrivilege> priv : privs.values()) {
                    result.add(new PostgreTablePermission(this, priv.get(0).getGrantee(), priv));
                }
                Collections.sort(result);
                return result;
            }
        } catch (SQLException e) {
            throw new DBException(e, getDataSource());
        }
    }
}
Also used : JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) DBException(org.jkiss.dbeaver.DBException) SQLException(java.sql.SQLException) JDBCPreparedStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet)

Example 24 with JDBCSession

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

the class OracleUtils method getSource.

public static String getSource(DBRProgressMonitor monitor, OracleSourceObject sourceObject, boolean body, boolean insertCreateReplace) throws DBCException {
    if (sourceObject.getSourceType().isCustom()) {
        log.warn("Can't read source for custom source objects");
        return "-- ???? CUSTOM SOURCE";
    }
    final String sourceType = sourceObject.getSourceType().name();
    final OracleSchema sourceOwner = sourceObject.getSchema();
    if (sourceOwner == null) {
        log.warn("No source owner for object '" + sourceObject.getName() + "'");
        return null;
    }
    monitor.beginTask("Load sources for '" + sourceObject.getName() + "'...", 1);
    String sysViewName = OracleConstants.VIEW_DBA_SOURCE;
    if (!sourceObject.getDataSource().isViewAvailable(monitor, OracleConstants.SCHEMA_SYS, sysViewName)) {
        sysViewName = OracleConstants.VIEW_ALL_SOURCE;
    }
    try (final JDBCSession session = DBUtils.openMetaSession(monitor, sourceOwner.getDataSource(), "Load source code for " + sourceType + " '" + sourceObject.getName() + "'")) {
        try (JDBCPreparedStatement dbStat = session.prepareStatement("SELECT TEXT FROM " + OracleConstants.SCHEMA_SYS + "." + sysViewName + " " + "WHERE TYPE=? AND OWNER=? AND NAME=? " + "ORDER BY LINE")) {
            dbStat.setString(1, body ? sourceType + " BODY" : sourceType);
            dbStat.setString(2, sourceOwner.getName());
            dbStat.setString(3, sourceObject.getName());
            dbStat.setFetchSize(DBConstants.METADATA_FETCH_SIZE);
            try (JDBCResultSet dbResult = dbStat.executeQuery()) {
                StringBuilder source = null;
                int lineCount = 0;
                while (dbResult.next()) {
                    if (monitor.isCanceled()) {
                        break;
                    }
                    final String line = dbResult.getString(1);
                    if (source == null) {
                        source = new StringBuilder(200);
                    }
                    source.append(line);
                    lineCount++;
                    monitor.subTask("Line " + lineCount);
                }
                if (source == null) {
                    return null;
                }
                if (insertCreateReplace) {
                    return insertCreateReplace(sourceObject, body, source.toString());
                } else {
                    return source.toString();
                }
            }
        }
    } catch (SQLException e) {
        throw new DBCException(e, sourceOwner.getDataSource());
    } finally {
        monitor.done();
    }
}
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 25 with JDBCSession

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

the class OracleUtils method getObjectStatus.

public static boolean getObjectStatus(DBRProgressMonitor monitor, OracleStatefulObject object, OracleObjectType objectType) throws DBCException {
    try (JDBCSession session = DBUtils.openMetaSession(monitor, object.getDataSource(), "Refresh state of " + objectType.getTypeName() + " '" + object.getName() + "'")) {
        try (JDBCPreparedStatement dbStat = session.prepareStatement("SELECT STATUS FROM SYS.ALL_OBJECTS WHERE OBJECT_TYPE=? AND OWNER=? AND OBJECT_NAME=?")) {
            dbStat.setString(1, objectType.getTypeName());
            dbStat.setString(2, object.getSchema().getName());
            dbStat.setString(3, DBObjectNameCaseTransformer.transformObjectName(object, object.getName()));
            try (JDBCResultSet dbResult = dbStat.executeQuery()) {
                if (dbResult.next()) {
                    return "VALID".equals(dbResult.getString("STATUS"));
                } else {
                    log.warn(objectType.getTypeName() + " '" + object.getName() + "' not found in system dictionary");
                    return false;
                }
            }
        }
    } catch (SQLException e) {
        throw new DBCException(e, object.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)

Aggregations

JDBCSession (org.jkiss.dbeaver.model.exec.jdbc.JDBCSession)73 SQLException (java.sql.SQLException)67 DBException (org.jkiss.dbeaver.DBException)54 JDBCResultSet (org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet)54 JDBCPreparedStatement (org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement)46 DBCException (org.jkiss.dbeaver.model.exec.DBCException)16 ArrayList (java.util.ArrayList)15 JDBCStatement (org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement)7 NotNull (org.jkiss.code.NotNull)5 DBPDataSource (org.jkiss.dbeaver.model.DBPDataSource)5 DatabaseMetaData (java.sql.DatabaseMetaData)3 GenericDataSource (org.jkiss.dbeaver.ext.generic.model.GenericDataSource)3 JDBCCallableStatement (org.jkiss.dbeaver.model.exec.jdbc.JDBCCallableStatement)3 Matcher (java.util.regex.Matcher)2 DB2XMLString (org.jkiss.dbeaver.ext.db2.info.DB2XMLString)2 GenericMetaObject (org.jkiss.dbeaver.ext.generic.model.meta.GenericMetaObject)2 JDBCDatabaseMetaData (org.jkiss.dbeaver.model.exec.jdbc.JDBCDatabaseMetaData)2 Property (org.jkiss.dbeaver.model.meta.Property)2 DBSObjectType (org.jkiss.dbeaver.model.struct.DBSObjectType)2 DBSProcedureType (org.jkiss.dbeaver.model.struct.rdb.DBSProcedureType)2