Search in sources :

Example 21 with JDBCStatement

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

the class JDBCStructCache method loadChildren.

/**
 * Reads children objects from database
 *
 * @param monitor
 *            monitor
 * @param forObject
 *            object for which to read children. If null then reads children for all objects in this container.
 * @throws org.jkiss.dbeaver.DBException
 *             on error
 */
public synchronized void loadChildren(DBRProgressMonitor monitor, OWNER owner, @Nullable final OBJECT forObject) throws DBException {
    if ((forObject == null && this.childrenCached) || (forObject != null && (!forObject.isPersisted() || isChildrenCached(forObject))) || monitor.isCanceled()) {
        return;
    }
    if (forObject == null) {
        // If we have some child objects read before that - do not clear them.
        // We have to reuse them because there could be some references in cached model
        // clearChildrenCache(null);
        super.loadObjects(monitor, owner);
    }
    DBPDataSource dataSource = owner.getDataSource();
    if (dataSource == null) {
        throw new DBException(ModelMessages.error_not_connected_to_database);
    }
    try (JDBCSession session = DBUtils.openMetaSession(monitor, owner, "Load child objects")) {
        Map<OBJECT, List<CHILD>> objectMap = new HashMap<>();
        // Load columns
        try (JDBCStatement dbStat = prepareChildrenStatement(session, owner, forObject)) {
            dbStat.setFetchSize(DBConstants.METADATA_FETCH_SIZE);
            dbStat.executeStatement();
            JDBCResultSet dbResult = dbStat.getResultSet();
            if (dbResult != null) {
                try {
                    while (dbResult.next()) {
                        if (monitor.isCanceled()) {
                            return;
                        }
                        OBJECT object = forObject;
                        if (object == null) {
                            String objectName;
                            if (objectNameColumn instanceof Number) {
                                objectName = JDBCUtils.safeGetString(dbResult, ((Number) objectNameColumn).intValue());
                            } else {
                                objectName = JDBCUtils.safeGetStringTrimmed(dbResult, objectNameColumn.toString());
                            }
                            if (objectName == null) {
                                log.debug("NULL object name in " + this);
                                continue;
                            }
                            object = super.getCachedObject(objectName);
                            if (object == null) {
                                log.debug("Object '" + objectName + "' not found in struct cache (" + getClass().getSimpleName() + ")");
                                continue;
                            }
                        }
                        if (isChildrenCached(object)) {
                            // Already read
                            continue;
                        }
                        CHILD child = fetchChild(session, owner, object, dbResult);
                        if (child == null) {
                            continue;
                        }
                        // Add to map
                        List<CHILD> children = objectMap.get(object);
                        if (children == null) {
                            children = new ArrayList<>();
                            objectMap.put(object, children);
                        }
                        children.add(child);
                    }
                    if (monitor.isCanceled()) {
                        return;
                    }
                    // All children are read. Now assign them to parents
                    for (Map.Entry<OBJECT, List<CHILD>> colEntry : objectMap.entrySet()) {
                        if (!isChildrenCached(colEntry.getKey())) {
                            // isChildrenCached may return true if the same cache was read in other thread
                            // just skip
                            cacheChildren(colEntry.getKey(), colEntry.getValue());
                        }
                    }
                    if (forObject == null) {
                        if (objectMap.isEmpty()) {
                        // Nothing was read. May be it means empty list of children
                        // but possibly this feature is not supported [JDBC: SQLite]
                        } else {
                            // Now set empty column list for other tables
                            for (OBJECT tmpObject : getAllObjects(monitor, owner)) {
                                if (!isChildrenCached(tmpObject) && !objectMap.containsKey(tmpObject)) {
                                    cacheChildren(tmpObject, new ArrayList<>());
                                }
                            }
                            this.childrenCached = true;
                        }
                    } else if (!objectMap.containsKey(forObject)) {
                        cacheChildren(forObject, new ArrayList<>());
                    }
                } finally {
                    dbResult.close();
                }
            }
        }
    } catch (SQLException ex) {
        throw new DBException(ex, dataSource);
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) 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)

Example 22 with JDBCStatement

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

the class DB2DataSource method collectObjectStatistics.

@Override
public void collectObjectStatistics(DBRProgressMonitor monitor, boolean totalSizeOnly, boolean forceRefresh) throws DBException {
    if (hasStatistics && !forceRefresh) {
        return;
    }
    try (JDBCSession session = DBUtils.openMetaSession(monitor, this, "Load schema statistics")) {
        try (JDBCStatement dbStat = session.createStatement()) {
            try (JDBCResultSet dbResult = dbStat.executeQuery("SELECT\n" + "    TABSCHEMA,\n" + "    SUM(DATA_OBJECT_P_SIZE + INDEX_OBJECT_P_SIZE + LONG_OBJECT_P_SIZE + LOB_OBJECT_P_SIZE + XML_OBJECT_P_SIZE) AS TOTAL_SIZE_IN_KB\n" + "FROM SYSIBMADM.ADMINTABINFO\n" + "GROUP BY TABSCHEMA")) {
                while (dbResult.next()) {
                    String schemaName = JDBCUtils.safeGetStringTrimmed(dbResult, 1);
                    long bytes = dbResult.getLong(2) * 1024;
                    DB2Schema schema = getSchema(monitor, schemaName);
                    if (schema != null) {
                        schema.setSchemaTotalSize(bytes);
                    }
                }
                for (DB2Schema schema : getSchemas(monitor)) {
                    if (!schema.hasStatistics()) {
                        schema.setSchemaTotalSize(0);
                    }
                }
            }
        }
    } catch (SQLException e) {
        throw new DBCException("Error reading table statistics", e);
    } finally {
        hasStatistics = true;
    }
}
Also used : JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) JDBCStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement) SQLException(java.sql.SQLException) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet) DBCException(org.jkiss.dbeaver.model.exec.DBCException) DB2XMLString(org.jkiss.dbeaver.ext.db2.info.DB2XMLString)

Example 23 with JDBCStatement

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

the class ExasolSQLDialect method initDriverSettings.

public void initDriverSettings(JDBCDataSource dataSource, JDBCDatabaseMetaData metaData) {
    super.initDriverSettings(dataSource, metaData);
    Collections.addAll(tableQueryWords, "DESC");
    try {
        JDBCSession session = DBUtils.openMetaSession(new VoidProgressMonitor(), dataSource, "");
        try (JDBCStatement stmt = session.createStatement()) {
            try (JDBCResultSet dbResult = stmt.executeQuery("/*snapshot execution*/ SELECT \"VALUE\" FROM \"$ODBCJDBC\".DB_METADATA WHERE name = 'aggregateFunctions'")) {
                if (dbResult.next()) {
                    String keyWord = dbResult.getString(1);
                    String[] aggregateFunctions = keyWord.split(",");
                    this.addExtraFunctions(aggregateFunctions);
                }
            }
            try (JDBCResultSet dbResult = stmt.executeQuery("/*snapshot execution*/ SELECT keyword FROM sys.EXA_SQL_KEYWORDS esk WHERE RESERVED")) {
                while (dbResult.next()) {
                    String keyWord = dbResult.getString("KEYWORD");
                    super.addSQLKeyword(keyWord);
                }
            }
        }
    } catch (SQLException e) {
        LOG.warn("Could not retrieve functions list from Exasol dictionary");
    }
    @SuppressWarnings("serial") ArrayList<String> value = new ArrayList<String>() {

        {
            add("KERBEROS");
            add("JDBC");
            add("BYTE");
            add("BIT");
            add("PRECEDENCE");
            add("GROUP_TEMP_DB_RAM_LIMIT");
            add("USER_TEMP_DB_RAM_LIMIT");
            add("SESSION_TEMP_DB_RAM_LIMIT");
            add("CPU_WEIGHT");
        }
    };
    this.addKeywords(value, DBPKeywordType.KEYWORD);
}
Also used : JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) JDBCStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement) SQLException(java.sql.SQLException) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet) ArrayList(java.util.ArrayList) VoidProgressMonitor(org.jkiss.dbeaver.model.runtime.VoidProgressMonitor)

Example 24 with JDBCStatement

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

the class ExasolView method read.

private void read() throws DBCException {
    if (!hasRead) {
        JDBCSession session = DBUtils.openMetaSession(new VoidProgressMonitor(), this, "Read Table Details");
        try (JDBCStatement stmt = session.createStatement()) {
            String sql = String.format("/*snapshot execution*/ SELECT VIEW_OWNER,VIEW_TEXT FROM SYS.EXA_ALL_VIEWS WHERE VIEW_SCHEMA = '%s' and VIEW_NAME = '%s'", ExasolUtils.quoteString(this.getSchema().getName()), ExasolUtils.quoteString(this.getName()));
            try (JDBCResultSet dbResult = stmt.executeQuery(sql)) {
                if (dbResult.next()) {
                    this.owner = JDBCUtils.safeGetString(dbResult, "VIEW_OWNER");
                    this.text = JDBCUtils.safeGetString(dbResult, "VIEW_TEXT");
                    this.hasRead = true;
                } else {
                    this.owner = "SYS OBJECT";
                    this.text = "-- No View Text for system objects available";
                }
                this.hasRead = true;
            }
        } catch (SQLException e) {
            throw new DBCException(e, session.getExecutionContext());
        }
    }
}
Also used : JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) JDBCStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement) SQLException(java.sql.SQLException) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet) DBCException(org.jkiss.dbeaver.model.exec.DBCException) VoidProgressMonitor(org.jkiss.dbeaver.model.runtime.VoidProgressMonitor)

Example 25 with JDBCStatement

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

the class ExasolStructureAssistant method findTableColumnsByMask.

private void findTableColumnsByMask(JDBCSession session, ExasolSchema schema, String objectNameMask, int maxResults, List<DBSObjectReference> references) throws SQLException, DBException {
    DBRProgressMonitor monitor = session.getProgressMonitor();
    // don't use parameter marks because of performance
    try (JDBCStatement dbstat = session.createStatement()) {
        try (JDBCResultSet dbResult = dbstat.executeQuery(String.format(SQL_COLS_SCHEMA, (schema == null ? "%" : ExasolUtils.quoteString(schema.getName())), ExasolUtils.quoteString(objectNameMask)))) {
            int num = maxResults;
            while (dbResult.next() && num-- > 0) {
                if (monitor.isCanceled()) {
                    break;
                }
                final String schemaName = JDBCUtils.safeGetString(dbResult, "TABLE_SCHEM");
                final String tableName = JDBCUtils.safeGetString(dbResult, "COLUMN_TABLE");
                final String columnName = JDBCUtils.safeGetString(dbResult, "COLUMN_NAME");
                references.add(new AbstractObjectReference(columnName, dataSource.getSchema(monitor, schemaName), null, ExasolTableBase.class, RelationalObjectType.TYPE_TABLE_COLUMN) {

                    @Override
                    public DBSObject resolveObject(DBRProgressMonitor monitor) throws DBException {
                        ExasolSchema tableSchema = schema != null ? schema : dataSource.getSchema(monitor, schemaName);
                        if (tableSchema == null) {
                            throw new DBException("Table schema '" + schemaName + "' not found");
                        }
                        ExasolTable table = tableSchema.getTableCache().getObject(monitor, tableSchema, tableName);
                        if (table == null) {
                            ExasolView view = tableSchema.getViewCache().getObject(monitor, tableSchema, tableName);
                            if (view == null)
                                throw new DBException("nor Table or view with name '" + tableName + "'  found in schema '" + schemaName + "'");
                            return view;
                        }
                        return table;
                    }
                });
            }
        }
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) JDBCStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement) AbstractObjectReference(org.jkiss.dbeaver.model.impl.struct.AbstractObjectReference) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)

Aggregations

JDBCStatement (org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement)70 JDBCResultSet (org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet)40 SQLException (java.sql.SQLException)32 JDBCSession (org.jkiss.dbeaver.model.exec.jdbc.JDBCSession)26 DBException (org.jkiss.dbeaver.DBException)24 NotNull (org.jkiss.code.NotNull)18 ArrayList (java.util.ArrayList)16 DBPDataSource (org.jkiss.dbeaver.model.DBPDataSource)12 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)12 DBCException (org.jkiss.dbeaver.model.exec.DBCException)10 SQLWarning (java.sql.SQLWarning)8 AbstractObjectReference (org.jkiss.dbeaver.model.impl.struct.AbstractObjectReference)8 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)8 JDBCStatementImpl (org.jkiss.dbeaver.model.impl.jdbc.exec.JDBCStatementImpl)6 VoidProgressMonitor (org.jkiss.dbeaver.model.runtime.VoidProgressMonitor)4 BigDecimal (java.math.BigDecimal)2 SQLFeatureNotSupportedException (java.sql.SQLFeatureNotSupportedException)2 SQLXML (java.sql.SQLXML)2 Tree (org.eclipse.swt.widgets.Tree)2 TreeColumn (org.eclipse.swt.widgets.TreeColumn)2