Search in sources :

Example 11 with JDBCStatement

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

the class ExasolTableForeignKeyCache method prepareObjectsStatement.

@SuppressWarnings("rawtypes")
@NotNull
@Override
protected JDBCStatement prepareObjectsStatement(JDBCSession session, ExasolSchema exasolSchema, ExasolTable forTable) throws SQLException {
    String sql;
    if (forTable != null) {
        sql = String.format(SQL_FK_TAB, ExasolUtils.quoteString(exasolSchema.getName()), ExasolUtils.quoteString(forTable.getName()));
    } else {
        sql = String.format(SQL_FK_ALL, ExasolUtils.quoteString(exasolSchema.getName()));
    }
    JDBCStatement dbStat = session.createStatement();
    ((JDBCStatementImpl) dbStat).setQueryString(sql);
    return dbStat;
}
Also used : JDBCStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement) JDBCStatementImpl(org.jkiss.dbeaver.model.impl.jdbc.exec.JDBCStatementImpl) NotNull(org.jkiss.code.NotNull)

Example 12 with JDBCStatement

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

the class ExasolViewCache method prepareObjectsStatement.

@SuppressWarnings("rawtypes")
@Override
protected JDBCStatement prepareObjectsStatement(@NotNull JDBCSession session, @NotNull ExasolSchema exasolSchema) throws SQLException {
    JDBCStatement dbStat = session.createStatement();
    String sql = String.format(SQL_VIEWS, ExasolUtils.quoteString(exasolSchema.getName()), ExasolUtils.quoteString(exasolSchema.getName()));
    ((JDBCStatementImpl) dbStat).setQueryString(sql);
    return dbStat;
}
Also used : JDBCStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement) JDBCStatementImpl(org.jkiss.dbeaver.model.impl.jdbc.exec.JDBCStatementImpl)

Example 13 with JDBCStatement

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

the class ExasolViewCache method prepareChildrenStatement.

@SuppressWarnings("rawtypes")
@Override
protected JDBCStatement prepareChildrenStatement(@NotNull JDBCSession session, @NotNull ExasolSchema exasolSchema, @Nullable ExasolView forView) throws SQLException {
    String sql;
    if (forView != null) {
        sql = String.format(SQL_COLS_VIEW, ExasolUtils.quoteString(exasolSchema.getName()), ExasolUtils.quoteString(forView.getName()));
    } else {
        sql = String.format(SQL_COLS_ALL, ExasolUtils.quoteString(exasolSchema.getName()));
    }
    JDBCStatement dbStat = session.createStatement();
    ((JDBCStatementImpl) dbStat).setQueryString(sql);
    return dbStat;
}
Also used : JDBCStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement) JDBCStatementImpl(org.jkiss.dbeaver.model.impl.jdbc.exec.JDBCStatementImpl)

Example 14 with JDBCStatement

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

the class ExasolStructureAssistant method searchTables.

// --------------
// Helper Classes
// --------------
private void searchTables(JDBCSession session, ExasolSchema schema, String searchObjectNameMask, List<ExasolObjectType> exasolObjectTypes, int maxResults, List<DBSObjectReference> objects, int nbResults) throws SQLException, DBException {
    String sql;
    if (schema != null) {
        sql = String.format(SQL_TABLES_SCHEMA, ExasolUtils.quoteString(schema.getName()), ExasolUtils.quoteString(searchObjectNameMask), buildTableTypes(exasolObjectTypes));
    } else {
        sql = String.format(SQL_TABLES_ALL, ExasolUtils.quoteString(searchObjectNameMask), buildTableTypes(exasolObjectTypes));
    }
    try (JDBCStatement dbStat = session.createStatement()) {
        dbStat.setFetchSize(DBConstants.METADATA_FETCH_SIZE);
        String schemaName;
        String objectName;
        ExasolSchema exasolSchema;
        ExasolObjectType objectType;
        try (JDBCResultSet dbResult = dbStat.executeQuery(sql)) {
            while (dbResult.next()) {
                if (session.getProgressMonitor().isCanceled()) {
                    break;
                }
                if (nbResults++ >= maxResults) {
                    break;
                }
                schemaName = JDBCUtils.safeGetStringTrimmed(dbResult, "TABLE_SCHEM");
                objectName = JDBCUtils.safeGetString(dbResult, "TABLE_NAME");
                exasolSchema = dataSource.getSchema(session.getProgressMonitor(), schemaName);
                if (exasolSchema == null) {
                    LOG.debug("Schema '" + schemaName + "' not found. Probably was filtered");
                    continue;
                }
                objectType = ExasolObjectType.TABLE;
                objects.add(new ExasolObjectReference(objectName, exasolSchema, objectType));
            }
        }
    }
}
Also used : ExasolObjectType(org.jkiss.dbeaver.ext.exasol.editors.ExasolObjectType) JDBCStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet)

Example 15 with JDBCStatement

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

the class JDBCCompositeCache method loadObjects.

protected synchronized void loadObjects(DBRProgressMonitor monitor, OWNER owner, PARENT forParent) throws DBException {
    synchronized (objectCache) {
        if ((forParent == null && isFullyCached()) || (forParent != null && (!forParent.isPersisted() || objectCache.containsKey(forParent)))) {
            return;
        }
    }
    // Load tables and columns first
    if (forParent == null) {
        parentCache.loadObjects(monitor, owner);
        parentCache.loadChildren(monitor, owner, null);
    }
    Map<PARENT, Map<String, ObjectInfo>> parentObjectMap = new LinkedHashMap<>();
    // Load index columns
    DBPDataSource dataSource = owner.getDataSource();
    assert (dataSource != null);
    try (JDBCSession session = DBUtils.openMetaSession(monitor, dataSource, "Load composite objects")) {
        JDBCStatement dbStat = prepareObjectsStatement(session, owner, forParent);
        dbStat.setFetchSize(DBConstants.METADATA_FETCH_SIZE);
        try {
            dbStat.executeStatement();
            JDBCResultSet dbResult = dbStat.getResultSet();
            if (dbResult != null)
                try {
                    while (dbResult.next()) {
                        if (monitor.isCanceled()) {
                            break;
                        }
                        String parentName = parentColumnName instanceof Number ? JDBCUtils.safeGetString(dbResult, ((Number) parentColumnName).intValue()) : JDBCUtils.safeGetString(dbResult, parentColumnName.toString());
                        String objectName = objectColumnName instanceof Number ? JDBCUtils.safeGetString(dbResult, ((Number) objectColumnName).intValue()) : JDBCUtils.safeGetString(dbResult, objectColumnName.toString());
                        if (CommonUtils.isEmpty(objectName)) {
                            // Use default name
                            objectName = getDefaultObjectName(dbResult, parentName);
                        }
                        if (forParent == null && CommonUtils.isEmpty(parentName)) {
                            // No parent - can't evaluate it
                            log.debug("Empty parent name in " + this);
                            continue;
                        }
                        PARENT parent = forParent;
                        if (parent == null) {
                            parent = parentCache.getObject(monitor, owner, parentName, parentType);
                            if (parent == null) {
                                log.debug("Object '" + objectName + "' owner '" + parentName + "' not found");
                                continue;
                            }
                        }
                        synchronized (objectCache) {
                            if (objectCache.containsKey(parent)) {
                                // Already cached
                                continue;
                            }
                        }
                        // Add to map
                        Map<String, ObjectInfo> objectMap = parentObjectMap.get(parent);
                        if (objectMap == null) {
                            objectMap = new TreeMap<>();
                            parentObjectMap.put(parent, objectMap);
                        }
                        ObjectInfo objectInfo = objectMap.get(objectName);
                        if (objectInfo == null) {
                            OBJECT object = fetchObject(session, owner, parent, objectName, dbResult);
                            if (object == null) {
                                // Can't fetch object
                                continue;
                            }
                            objectName = object.getName();
                            objectInfo = new ObjectInfo(object);
                            objectMap.put(objectName, objectInfo);
                        }
                        ROW_REF[] rowRef = fetchObjectRow(session, parent, objectInfo.object, dbResult);
                        if (rowRef == null || rowRef.length == 0) {
                            // At least one of rows is broken.
                            // So entire object is broken, let's just skip it.
                            objectInfo.broken = true;
                            //log.debug("Object '" + objectName + "' metadata corrupted - NULL child returned");
                            continue;
                        }
                        Collections.addAll(objectInfo.rows, rowRef);
                    }
                } finally {
                    dbResult.close();
                }
        } finally {
            dbStat.close();
        }
    } catch (SQLException ex) {
        throw new DBException(ex, dataSource);
    }
    if (monitor.isCanceled()) {
        return;
    }
    // Fill global cache
    synchronized (this) {
        synchronized (objectCache) {
            if (forParent != null || !parentObjectMap.isEmpty()) {
                if (forParent == null) {
                    // Cache global object list
                    List<OBJECT> globalCache = new ArrayList<>();
                    for (Map<String, ObjectInfo> objMap : parentObjectMap.values()) {
                        if (objMap != null) {
                            for (ObjectInfo info : objMap.values()) {
                                if (!info.broken) {
                                    globalCache.add(info.object);
                                }
                            }
                        }
                    }
                    // Save precached objects in global cache
                    for (List<OBJECT> objects : objectCache.values()) {
                        globalCache.addAll(objects);
                    }
                    // Add precached objects to global cache too
                    super.setCache(globalCache);
                    this.invalidateObjects(monitor, owner, new CacheIterator());
                }
            }
            // All objects are read. Now assign them to parents
            for (Map.Entry<PARENT, Map<String, ObjectInfo>> colEntry : parentObjectMap.entrySet()) {
                if (colEntry.getValue() == null || objectCache.containsKey(colEntry.getKey())) {
                    // Do not overwrite this object's cache
                    continue;
                }
                Collection<ObjectInfo> objectInfos = colEntry.getValue().values();
                ArrayList<OBJECT> objects = new ArrayList<>(objectInfos.size());
                for (ObjectInfo objectInfo : objectInfos) {
                    objectInfo.needsCaching = true;
                    objects.add(objectInfo.object);
                }
                objectCache.put(colEntry.getKey(), objects);
            }
            // Now set empty object list for other parents
            if (forParent == null) {
                for (PARENT tmpParent : parentCache.getTypedObjects(monitor, owner, parentType)) {
                    if (!parentObjectMap.containsKey(tmpParent) && !objectCache.containsKey(tmpParent)) {
                        objectCache.put(tmpParent, new ArrayList<OBJECT>());
                    }
                }
            } else if (!parentObjectMap.containsKey(forParent) && !objectCache.containsKey(forParent)) {
                objectCache.put(forParent, new ArrayList<OBJECT>());
            }
        }
        // Cache children lists (we do it in the end because children caching may operate with other model objects)
        for (Map.Entry<PARENT, Map<String, ObjectInfo>> colEntry : parentObjectMap.entrySet()) {
            for (ObjectInfo objectInfo : colEntry.getValue().values()) {
                if (objectInfo.needsCaching) {
                    cacheChildren(monitor, objectInfo.object, objectInfo.rows);
                }
            }
        }
        for (Map.Entry<PARENT, Map<String, ObjectInfo>> colEntry : parentObjectMap.entrySet()) {
            for (ObjectInfo objectInfo : colEntry.getValue().values()) {
                if (objectInfo.needsCaching) {
                    cacheChildren2(monitor, objectInfo.object, objectInfo.rows);
                }
            }
        }
    }
}
Also used : JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) DBException(org.jkiss.dbeaver.DBException) SQLException(java.sql.SQLException) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource) JDBCStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet)

Aggregations

JDBCStatement (org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement)18 JDBCResultSet (org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet)10 SQLException (java.sql.SQLException)9 DBException (org.jkiss.dbeaver.DBException)7 JDBCSession (org.jkiss.dbeaver.model.exec.jdbc.JDBCSession)7 DBPDataSource (org.jkiss.dbeaver.model.DBPDataSource)6 JDBCStatementImpl (org.jkiss.dbeaver.model.impl.jdbc.exec.JDBCStatementImpl)6 NotNull (org.jkiss.code.NotNull)4 ArrayList (java.util.ArrayList)2 DBCException (org.jkiss.dbeaver.model.exec.DBCException)2 SQLWarning (java.sql.SQLWarning)1 Tree (org.eclipse.swt.widgets.Tree)1 TreeColumn (org.eclipse.swt.widgets.TreeColumn)1 TreeItem (org.eclipse.swt.widgets.TreeItem)1 Nullable (org.jkiss.code.Nullable)1 ExasolObjectType (org.jkiss.dbeaver.ext.exasol.editors.ExasolObjectType)1 ExasolServerSession (org.jkiss.dbeaver.ext.exasol.model.app.ExasolServerSession)1 PostgreObject (org.jkiss.dbeaver.ext.postgresql.model.PostgreObject)1 DBCResultSet (org.jkiss.dbeaver.model.exec.DBCResultSet)1 DBCStatement (org.jkiss.dbeaver.model.exec.DBCStatement)1