Search in sources :

Example 31 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 32 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)

Example 33 with JDBCSession

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

the class OracleTablePhysical method getPartitionInfo.

@PropertyGroup
@LazyProperty(cacheValidator = PartitionInfoValidator.class)
public PartitionInfo getPartitionInfo(DBRProgressMonitor monitor) throws DBException {
    if (partitionInfo == null && partitioned) {
        try (final JDBCSession session = DBUtils.openMetaSession(monitor, getDataSource(), "Load partitioning info")) {
            try (JDBCPreparedStatement dbStat = session.prepareStatement("SELECT * FROM ALL_PART_TABLES WHERE OWNER=? AND TABLE_NAME=?")) {
                dbStat.setString(1, getContainer().getName());
                dbStat.setString(2, getName());
                try (JDBCResultSet dbResult = dbStat.executeQuery()) {
                    if (dbResult.next()) {
                        partitionInfo = new PartitionInfo(monitor, this.getDataSource(), dbResult);
                    }
                }
            }
        } catch (SQLException e) {
            throw new DBException(e, getDataSource());
        }
    }
    return partitionInfo;
}
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 34 with JDBCSession

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

the class FireBirdUtils method getProcedureSource.

public static String getProcedureSource(DBRProgressMonitor monitor, GenericProcedure procedure) throws DBException {
    try (JDBCSession session = DBUtils.openMetaSession(monitor, procedure.getDataSource(), "Load procedure source code")) {
        DatabaseMetaData fbMetaData = session.getOriginal().getMetaData();
        String source = (String) fbMetaData.getClass().getMethod("getProcedureSourceCode", String.class).invoke(fbMetaData, procedure.getName());
        if (CommonUtils.isEmpty(source)) {
            return null;
        }
        return getProcedureSourceWithHeader(monitor, procedure, source);
    } catch (SQLException e) {
        throw new DBException("Can't read source code of procedure '" + procedure.getName() + "'", e);
    } catch (Exception e) {
        log.debug(e);
        return null;
    }
}
Also used : JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) DBException(org.jkiss.dbeaver.DBException) SQLException(java.sql.SQLException) DatabaseMetaData(java.sql.DatabaseMetaData) SQLException(java.sql.SQLException) DBException(org.jkiss.dbeaver.DBException)

Example 35 with JDBCSession

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

the class ExasolDataSource method initialize.

// -----------------------
// Initialization/Structure
// -----------------------
@Override
public void initialize(@NotNull DBRProgressMonitor monitor) throws DBException {
    super.initialize(monitor);
    try (JDBCSession session = DBUtils.openMetaSession(monitor, this, "Load data source meta info")) {
        // First try to get active schema from special register 'CURRENT
        // SCHEMA'
        this.activeSchemaName = determineActiveSchema(session);
        this.exasolCurrentUserPrivileges = new ExasolCurrentUserPrivileges(monitor, session, this);
        this.driverMajorVersion = session.getMetaData().getDriverMajorVersion();
    } catch (SQLException e) {
        LOG.warn("Error reading active schema", e);
    }
    String schemaSQL = "select b.object_name,b.owner,b.created,b.object_comment from EXA_ALL_OBJECTS b  " + "inner join EXA_SCHEMAS s on b.object_name = s.schema_name  where b.object_type = 'SCHEMA' ";
    if (exasolCurrentUserPrivileges.getatLeastV6()) {
        //additional where clause to filter virtual schemas
        schemaSQL += " and not schema_is_virtual ";
        //build virtual schema cache for >V6 databases
        virtualSchemaCache = new JDBCObjectSimpleCache<>(ExasolVirtualSchema.class, "select" + "	SCHEMA_NAME as OBJECT_NAME," + "	SCHEMA_OWNER AS OWNER," + "	ADAPTER_SCRIPT," + "	LAST_REFRESH," + "	LAST_REFRESH_BY," + "	ADAPTER_NOTES," + "	OBJECT_COMMENT," + "	CREATED" + " from" + "		EXA_VIRTUAL_SCHEMAS s" + "	INNER JOIN" + "		EXA_ALL_OBJECTS o" + "	ON" + "		o.OBJECT_NAME = s.SCHEMA_NAME AND" + "		o.OBJECT_TYPE = 'SCHEMA'");
    }
    schemaSQL += " union all select distinct SCHEMA_NAME as \"OBJECT_NAME\", 'SYS' as owner, cast(null as timestamp) as created, '' as \"OBJECT_COMMENT\" from SYS.EXA_SYSCAT " + "order by b.object_name";
    schemaCache = new JDBCObjectSimpleCache<>(ExasolSchema.class, schemaSQL);
    try {
        this.dataTypeCache.getAllObjects(monitor, this);
    } catch (DBException e) {
        LOG.warn("Error reading types info", e);
        this.dataTypeCache.setCache(Collections.<ExasolDataType>emptyList());
    }
    if (exasolCurrentUserPrivileges.getUserIsAuthorizedForUsers()) {
        this.userCache = new JDBCObjectSimpleCache<>(ExasolUser.class, "select * from EXA_DBA_USERS ORDER BY USER_NAME");
    }
    if (exasolCurrentUserPrivileges.getUserIsAuthorizedForRoles()) {
        this.roleCache = new JDBCObjectSimpleCache<>(ExasolRole.class, "SELECT * FROM EXA_DBA_ROLES ORDER BY ROLE_NAME");
    }
    if (exasolCurrentUserPrivileges.getUserIsAuthorizedForConnections()) {
        this.connectionCache = new JDBCObjectSimpleCache<>(ExasolConnection.class, "SELECT * FROM EXA_DBA_CONNECTIONS ORDER BY CONNECTION_NAME");
    }
    if (exasolCurrentUserPrivileges.getUserIsAuthorizedForConnectionPrivs()) {
        this.connectionGrantCache = new JDBCObjectSimpleCache<>(ExasolConnectionGrant.class, "SELECT c.*,P.ADMIN_OPTION,P.GRANTEE FROM SYS.EXA_DBA_CONNECTION_PRIVS P " + "INNER JOIN SYS.EXA_DBA_CONNECTIONS C on P.GRANTED_CONNECTION = C.CONNECTION_NAME ORDER BY P.GRANTEE,C.CONNECTION_NAME ");
    }
    if (exasolCurrentUserPrivileges.getUserIsAuthorizedForObjectPrivs()) {
        this.baseTableGrantCache = new JDBCObjectSimpleCache<>(ExasolBaseObjectGrant.class, "SELECT " + "	OBJECT_SCHEMA," + "	OBJECT_TYPE," + "	GRANTEE," + "	OBJECT_NAME," + "	GROUP_CONCAT(" + "		DISTINCT PRIVILEGE" + "	ORDER BY" + "		OBJECT_SCHEMA," + "		OBJECT_NAME" + "		SEPARATOR '|'" + "	) as PRIVS " + " FROM" + "	SYS.EXA_DBA_OBJ_PRIVS P" + " GROUP BY" + "	OBJECT_SCHEMA," + "	OBJECT_TYPE," + "	GRANTEE," + "	OBJECT_NAME ORDER BY GRANTEE,OBJECT_SCHEMA,OBJECT_TYPE,OBJECT_NAME");
    }
    if (exasolCurrentUserPrivileges.getUserIsAuthorizedForSystemPrivs()) {
        this.systemGrantCache = new JDBCObjectSimpleCache<>(ExasolSystemGrant.class, "SELECT GRANTEE,PRIVILEGE,ADMIN_OPTION FROM SYS.EXA_DBA_SYS_PRIVS ORDER BY GRANTEE,PRIVILEGE");
    }
    if (exasolCurrentUserPrivileges.getUserIsAuthorizedForRolePrivs()) {
        this.roleGrantCache = new JDBCObjectSimpleCache<>(ExasolRoleGrant.class, "select r.*,p.ADMIN_OPTION,p.GRANTEE from EXA_DBA_ROLES r " + "INNER JOIN  EXA_DBA_ROLE_PRIVS p ON p.GRANTED_ROLE = r.ROLE_NAME ORDER BY P.GRANTEE,R.ROLE_NAME");
    }
}
Also used : JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) DBException(org.jkiss.dbeaver.DBException) ExasolConnectionGrant(org.jkiss.dbeaver.ext.exasol.manager.security.ExasolConnectionGrant) SQLException(java.sql.SQLException) ExasolBaseObjectGrant(org.jkiss.dbeaver.ext.exasol.manager.security.ExasolBaseObjectGrant) ExasolSystemGrant(org.jkiss.dbeaver.ext.exasol.manager.security.ExasolSystemGrant) ExasolUser(org.jkiss.dbeaver.ext.exasol.manager.security.ExasolUser) ExasolRole(org.jkiss.dbeaver.ext.exasol.manager.security.ExasolRole) ExasolRoleGrant(org.jkiss.dbeaver.ext.exasol.manager.security.ExasolRoleGrant)

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