Search in sources :

Example 71 with JDBCPreparedStatement

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

the class OracleStructureAssistant method searchAllObjects.

private void searchAllObjects(final JDBCSession session, final OracleSchema schema, String objectNameMask, DBSObjectType[] objectTypes, boolean caseSensitive, int maxResults, List<DBSObjectReference> objects) throws SQLException, DBException {
    StringBuilder objectTypeClause = new StringBuilder(100);
    final List<OracleObjectType> oracleObjectTypes = new ArrayList<>(objectTypes.length + 2);
    for (DBSObjectType objectType : objectTypes) {
        if (objectType instanceof OracleObjectType) {
            oracleObjectTypes.add((OracleObjectType) objectType);
            if (objectType == OracleObjectType.PROCEDURE) {
                oracleObjectTypes.add(OracleObjectType.FUNCTION);
            } else if (objectType == OracleObjectType.TABLE) {
                oracleObjectTypes.add(OracleObjectType.VIEW);
                oracleObjectTypes.add(OracleObjectType.MATERIALIZED_VIEW);
            }
        }
    }
    for (OracleObjectType objectType : oracleObjectTypes) {
        if (objectTypeClause.length() > 0)
            objectTypeClause.append(",");
        objectTypeClause.append("'").append(objectType.getTypeName()).append("'");
    }
    if (objectTypeClause.length() == 0) {
        return;
    }
    // Always search for synonyms
    objectTypeClause.append(",'").append(OracleObjectType.SYNONYM.getTypeName()).append("'");
    // Seek for objects (join with public synonyms)
    try (JDBCPreparedStatement dbStat = session.prepareStatement("SELECT " + OracleUtils.getSysCatalogHint((OracleDataSource) session.getDataSource()) + " DISTINCT OWNER,OBJECT_NAME,OBJECT_TYPE FROM (SELECT OWNER,OBJECT_NAME,OBJECT_TYPE FROM ALL_OBJECTS WHERE " + "OBJECT_TYPE IN (" + objectTypeClause + ") AND OBJECT_NAME LIKE ? " + (schema == null ? "" : " AND OWNER=?") + "UNION ALL\n" + "SELECT " + OracleUtils.getSysCatalogHint((OracleDataSource) session.getDataSource()) + " O.OWNER,O.OBJECT_NAME,O.OBJECT_TYPE\n" + "FROM ALL_SYNONYMS S,ALL_OBJECTS O\n" + "WHERE O.OWNER=S.TABLE_OWNER AND O.OBJECT_NAME=S.TABLE_NAME AND S.OWNER='PUBLIC' AND S.SYNONYM_NAME LIKE ?)" + "\nORDER BY OBJECT_NAME")) {
        if (!caseSensitive) {
            objectNameMask = objectNameMask.toUpperCase();
        }
        dbStat.setString(1, objectNameMask);
        if (schema != null) {
            dbStat.setString(2, schema.getName());
        }
        dbStat.setString(schema != null ? 3 : 2, objectNameMask);
        dbStat.setFetchSize(DBConstants.METADATA_FETCH_SIZE);
        try (JDBCResultSet dbResult = dbStat.executeQuery()) {
            while (objects.size() < maxResults && dbResult.next()) {
                if (session.getProgressMonitor().isCanceled()) {
                    break;
                }
                final String schemaName = JDBCUtils.safeGetString(dbResult, "OWNER");
                final String objectName = JDBCUtils.safeGetString(dbResult, "OBJECT_NAME");
                final String objectTypeName = JDBCUtils.safeGetString(dbResult, "OBJECT_TYPE");
                final OracleObjectType objectType = OracleObjectType.getByType(objectTypeName);
                if (objectType != null && objectType != OracleObjectType.SYNONYM && objectType.isBrowsable() && oracleObjectTypes.contains(objectType)) {
                    OracleSchema objectSchema = dataSource.getSchema(session.getProgressMonitor(), schemaName);
                    if (objectSchema == null) {
                        log.debug("Schema '" + schemaName + "' not found. Probably was filtered");
                        continue;
                    }
                    objects.add(new AbstractObjectReference(objectName, objectSchema, null, objectType.getTypeClass(), objectType) {

                        @Override
                        public DBSObject resolveObject(DBRProgressMonitor monitor) throws DBException {
                            OracleSchema tableSchema = (OracleSchema) getContainer();
                            DBSObject object = objectType.findObject(session.getProgressMonitor(), tableSchema, objectName);
                            if (object == null) {
                                throw new DBException(objectTypeName + " '" + objectName + "' not found in schema '" + tableSchema.getName() + "'");
                            }
                            return object;
                        }
                    });
                }
            }
        }
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) JDBCPreparedStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement) AbstractObjectReference(org.jkiss.dbeaver.model.impl.struct.AbstractObjectReference) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)

Example 72 with JDBCPreparedStatement

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

the class OracleStructureAssistant method findConstraintsByMask.

private void findConstraintsByMask(JDBCSession session, final OracleSchema schema, String constrNameMask, DBSObjectType[] objectTypes, int maxResults, List<DBSObjectReference> objects) throws SQLException, DBException {
    DBRProgressMonitor monitor = session.getProgressMonitor();
    List<DBSObjectType> objectTypesList = Arrays.asList(objectTypes);
    final boolean hasFK = objectTypesList.contains(OracleObjectType.FOREIGN_KEY);
    final boolean hasConstraints = objectTypesList.contains(OracleObjectType.CONSTRAINT);
    // Load tables
    try (JDBCPreparedStatement dbStat = session.prepareStatement("SELECT " + OracleUtils.getSysCatalogHint((OracleDataSource) session.getDataSource()) + " OWNER, TABLE_NAME, CONSTRAINT_NAME, CONSTRAINT_TYPE\n" + "FROM SYS.ALL_CONSTRAINTS\n" + "WHERE CONSTRAINT_NAME like ?" + (!hasFK ? " AND CONSTRAINT_TYPE<>'R'" : "") + (schema != null ? " AND OWNER=?" : ""))) {
        dbStat.setString(1, constrNameMask);
        if (schema != null) {
            dbStat.setString(2, schema.getName());
        }
        try (JDBCResultSet dbResult = dbStat.executeQuery()) {
            int tableNum = maxResults;
            while (dbResult.next() && tableNum-- > 0) {
                if (monitor.isCanceled()) {
                    break;
                }
                final String schemaName = JDBCUtils.safeGetString(dbResult, OracleConstants.COL_OWNER);
                final String tableName = JDBCUtils.safeGetString(dbResult, OracleConstants.COL_TABLE_NAME);
                final String constrName = JDBCUtils.safeGetString(dbResult, OracleConstants.COL_CONSTRAINT_NAME);
                final String constrType = JDBCUtils.safeGetString(dbResult, OracleConstants.COL_CONSTRAINT_TYPE);
                final DBSEntityConstraintType type = OracleTableConstraint.getConstraintType(constrType);
                objects.add(new AbstractObjectReference(constrName, dataSource.getSchema(session.getProgressMonitor(), schemaName), null, type == DBSEntityConstraintType.FOREIGN_KEY ? OracleTableForeignKey.class : OracleTableConstraint.class, type == DBSEntityConstraintType.FOREIGN_KEY ? OracleObjectType.FOREIGN_KEY : OracleObjectType.CONSTRAINT) {

                    @Override
                    public DBSObject resolveObject(DBRProgressMonitor monitor) throws DBException {
                        OracleSchema tableSchema = schema != null ? schema : dataSource.getSchema(monitor, schemaName);
                        if (tableSchema == null) {
                            throw new DBException("Constraint schema '" + schemaName + "' not found");
                        }
                        OracleTable table = tableSchema.getTable(monitor, tableName);
                        if (table == null) {
                            throw new DBException("Constraint table '" + tableName + "' not found in catalog '" + tableSchema.getName() + "'");
                        }
                        DBSObject constraint = null;
                        if (hasFK && type == DBSEntityConstraintType.FOREIGN_KEY) {
                            constraint = table.getForeignKey(monitor, constrName);
                        }
                        if (hasConstraints && type != DBSEntityConstraintType.FOREIGN_KEY) {
                            constraint = table.getConstraint(monitor, constrName);
                        }
                        if (constraint == null) {
                            throw new DBException("Constraint '" + constrName + "' not found in table '" + table.getFullyQualifiedName(DBPEvaluationContext.DDL) + "'");
                        }
                        return constraint;
                    }
                });
            }
        }
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) JDBCPreparedStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement) AbstractObjectReference(org.jkiss.dbeaver.model.impl.struct.AbstractObjectReference) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)

Example 73 with JDBCPreparedStatement

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

the class OracleTableBase method loadColumnComments.

void loadColumnComments(DBRProgressMonitor monitor) {
    try {
        try (JDBCSession session = DBUtils.openMetaSession(monitor, getDataSource(), "Load table column comments")) {
            try (JDBCPreparedStatement stat = session.prepareStatement("SELECT COLUMN_NAME,COMMENTS FROM SYS.ALL_COL_COMMENTS cc WHERE CC.OWNER=? AND cc.TABLE_NAME=?")) {
                stat.setString(1, getSchema().getName());
                stat.setString(2, getName());
                try (JDBCResultSet resultSet = stat.executeQuery()) {
                    while (resultSet.next()) {
                        String colName = resultSet.getString(1);
                        String colComment = resultSet.getString(2);
                        OracleTableColumn col = getAttribute(monitor, colName);
                        if (col == null) {
                            log.warn("Column '" + colName + "' not found in table '" + getFullyQualifiedName(DBPEvaluationContext.DDL) + "'");
                        } else {
                            col.setComment(CommonUtils.notEmpty(colComment));
                        }
                    }
                }
            }
        }
        for (OracleTableColumn col : getAttributes(monitor)) {
            col.cacheComment();
        }
    } catch (Exception e) {
        log.warn("Error fetching table '" + getName() + "' column comments", e);
    }
}
Also used : JDBCPreparedStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement) JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet) SQLException(java.sql.SQLException) DBException(org.jkiss.dbeaver.DBException)

Example 74 with JDBCPreparedStatement

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

the class MySQLStructureAssistant method findConstraintsByMask.

private void findConstraintsByMask(JDBCSession session, @Nullable final MySQLCatalog catalog, String constrNameMask, int maxResults, List<DBSObjectReference> objects) throws SQLException, DBException {
    DBRProgressMonitor monitor = session.getProgressMonitor();
    // Load constraints
    try (JDBCPreparedStatement dbStat = session.prepareStatement("SELECT " + MySQLConstants.COL_TABLE_SCHEMA + "," + MySQLConstants.COL_TABLE_NAME + "," + MySQLConstants.COL_CONSTRAINT_NAME + "," + MySQLConstants.COL_CONSTRAINT_TYPE + " FROM " + MySQLConstants.META_TABLE_TABLE_CONSTRAINTS + " WHERE " + MySQLConstants.COL_CONSTRAINT_NAME + " LIKE ? " + (catalog == null ? "" : " AND " + MySQLConstants.COL_TABLE_SCHEMA + "=?") + " ORDER BY " + MySQLConstants.COL_CONSTRAINT_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 constrName = JDBCUtils.safeGetString(dbResult, MySQLConstants.COL_CONSTRAINT_NAME);
                final String constrType = JDBCUtils.safeGetString(dbResult, MySQLConstants.COL_CONSTRAINT_TYPE);
                final boolean isFK = MySQLConstants.CONSTRAINT_FOREIGN_KEY.equals(constrType);
                objects.add(new AbstractObjectReference(constrName, dataSource.getCatalog(catalogName), null, isFK ? MySQLTableForeignKey.class : MySQLTableConstraint.class, RelationalObjectType.TYPE_CONSTRAINT) {

                    @Override
                    public DBSObject resolveObject(DBRProgressMonitor monitor) throws DBException {
                        MySQLCatalog tableCatalog = catalog != null ? catalog : dataSource.getCatalog(catalogName);
                        if (tableCatalog == null) {
                            throw new DBException("Constraint catalog '" + catalogName + "' not found");
                        }
                        MySQLTable table = tableCatalog.getTable(monitor, tableName);
                        if (table == null) {
                            throw new DBException("Constraint table '" + tableName + "' not found in catalog '" + tableCatalog.getName() + "'");
                        }
                        DBSObject constraint;
                        if (isFK) {
                            constraint = table.getAssociation(monitor, constrName);
                        } else {
                            constraint = table.getConstraint(monitor, constrName);
                        }
                        if (constraint == null) {
                            throw new DBException("Constraint '" + constrName + "' not found in table '" + table.getFullyQualifiedName(DBPEvaluationContext.DDL) + "'");
                        }
                        return constraint;
                    }
                });
            }
        }
    }
}
Also used : JDBCPreparedStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement) DBException(org.jkiss.dbeaver.DBException) 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 75 with JDBCPreparedStatement

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

the class MySQLStructureAssistant method findProceduresByMask.

private void findProceduresByMask(JDBCSession session, @Nullable final MySQLCatalog catalog, String procNameMask, int maxResults, List<DBSObjectReference> objects) throws SQLException, DBException {
    DBRProgressMonitor monitor = session.getProgressMonitor();
    // Load procedures
    try (JDBCPreparedStatement dbStat = session.prepareStatement("SELECT " + MySQLConstants.COL_ROUTINE_SCHEMA + "," + MySQLConstants.COL_ROUTINE_NAME + " FROM " + MySQLConstants.META_TABLE_ROUTINES + " WHERE " + MySQLConstants.COL_ROUTINE_NAME + " LIKE ? " + (catalog == null ? "" : " AND " + MySQLConstants.COL_ROUTINE_SCHEMA + "=?") + " ORDER BY " + MySQLConstants.COL_ROUTINE_NAME + " LIMIT " + maxResults)) {
        dbStat.setString(1, procNameMask.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_ROUTINE_SCHEMA);
                final String procName = JDBCUtils.safeGetString(dbResult, MySQLConstants.COL_ROUTINE_NAME);
                objects.add(new AbstractObjectReference(procName, dataSource.getCatalog(catalogName), null, MySQLProcedure.class, RelationalObjectType.TYPE_PROCEDURE) {

                    @Override
                    public DBSObject resolveObject(DBRProgressMonitor monitor) throws DBException {
                        MySQLCatalog procCatalog = catalog != null ? catalog : dataSource.getCatalog(catalogName);
                        if (procCatalog == null) {
                            throw new DBException("Procedure catalog '" + catalogName + "' not found");
                        }
                        MySQLProcedure procedure = procCatalog.getProcedure(monitor, procName);
                        if (procedure == null) {
                            throw new DBException("Procedure '" + procName + "' not found in catalog '" + procCatalog.getName() + "'");
                        }
                        return procedure;
                    }
                });
            }
        }
    }
}
Also used : JDBCPreparedStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement) DBException(org.jkiss.dbeaver.DBException) 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)

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