Search in sources :

Example 16 with DBPDataSource

use of org.jkiss.dbeaver.model.DBPDataSource in project dbeaver by dbeaver.

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("Not connected to database");
    }
    try (JDBCSession session = DBUtils.openMetaSession(monitor, dataSource, "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()) {
                            break;
                        }
                        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 object = forObject;
                        if (object == null) {
                            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<CHILD>());
                                }
                            }
                            this.childrenCached = true;
                        }
                    } else if (!objectMap.containsKey(forObject)) {
                        cacheChildren(forObject, new ArrayList<CHILD>());
                    }
                } 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 17 with DBPDataSource

use of org.jkiss.dbeaver.model.DBPDataSource in project dbeaver by dbeaver.

the class JDBCCallableStatementImpl method findProcedure.

private static DBSProcedure findProcedure(DBCSession session, String queryString) throws DBException {
    DBPDataSource dataSource = session.getDataSource();
    if (!CommonUtils.isEmpty(queryString)) {
        Matcher matcher = EXEC_PATTERN.matcher(queryString);
        if (matcher.find()) {
            String procName = matcher.group(1);
            char divChar = 0;
            if (dataSource instanceof SQLDataSource) {
                divChar = ((SQLDataSource) dataSource).getSQLDialect().getStructSeparator();
            }
            if (procName.indexOf(divChar) != -1) {
                return findProcedureByNames(session, procName.split(String.valueOf(divChar)));
            } else {
                return findProcedureByNames(session, procName);
            }
        }
    }
    return null;
}
Also used : Matcher(java.util.regex.Matcher) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource) SQLDataSource(org.jkiss.dbeaver.model.sql.SQLDataSource)

Example 18 with DBPDataSource

use of org.jkiss.dbeaver.model.DBPDataSource in project dbeaver by dbeaver.

the class JDBCTable method readData.

@NotNull
@Override
public DBCStatistics readData(@NotNull DBCExecutionSource source, @NotNull DBCSession session, @NotNull DBDDataReceiver dataReceiver, @Nullable DBDDataFilter dataFilter, long firstRow, long maxRows, long flags) throws DBCException {
    DBCStatistics statistics = new DBCStatistics();
    boolean hasLimits = firstRow >= 0 && maxRows > 0;
    DBPDataSource dataSource = session.getDataSource();
    DBRProgressMonitor monitor = session.getProgressMonitor();
    try {
        readRequiredMeta(monitor);
    } catch (DBException e) {
        log.warn(e);
    }
    DBDPseudoAttribute rowIdAttribute = (flags & FLAG_READ_PSEUDO) != 0 ? DBUtils.getRowIdAttribute(this) : null;
    // Always use alias if we have criteria or ROWID.
    // Some criteria doesn't work without alias
    // (e.g. structured attributes in Oracle requires table alias)
    String tableAlias = null;
    if ((dataFilter != null && dataFilter.hasConditions()) || rowIdAttribute != null) {
        if (dataSource instanceof SQLDataSource) {
            if (((SQLDataSource) dataSource).getSQLDialect().supportsAliasInSelect()) {
                tableAlias = DEFAULT_TABLE_ALIAS;
            }
        }
    }
    if (rowIdAttribute != null && tableAlias == null) {
        log.warn("Can't query ROWID - table alias not supported");
        rowIdAttribute = null;
    }
    StringBuilder query = new StringBuilder(100);
    query.append("SELECT ");
    appendSelectSource(monitor, query, tableAlias, rowIdAttribute);
    query.append(" FROM ").append(getFullyQualifiedName(DBPEvaluationContext.DML));
    if (tableAlias != null) {
        // $NON-NLS-1$
        query.append(" ").append(tableAlias);
    }
    appendQueryConditions(query, tableAlias, dataFilter);
    appendQueryOrder(query, tableAlias, dataFilter);
    String sqlQuery = query.toString();
    statistics.setQueryText(sqlQuery);
    statistics.addStatementsCount();
    monitor.subTask(ModelMessages.model_jdbc_fetch_table_data);
    try (DBCStatement dbStat = DBUtils.makeStatement(source, session, DBCStatementType.SCRIPT, sqlQuery, firstRow, maxRows)) {
        if (monitor.isCanceled()) {
            return statistics;
        }
        if (dbStat instanceof JDBCStatement && maxRows > 0) {
            boolean useFetchSize = getDataSource().getContainer().getPreferenceStore().getBoolean(ModelPreferences.RESULT_SET_USE_FETCH_SIZE);
            if (useFetchSize) {
                try {
                    ((JDBCStatement) dbStat).setFetchSize(firstRow < 0 || maxRows <= 0 ? DEFAULT_READ_FETCH_SIZE : (int) (firstRow + maxRows));
                } catch (Exception e) {
                    log.warn(e);
                }
            }
        }
        long startTime = System.currentTimeMillis();
        boolean executeResult = dbStat.executeStatement();
        statistics.setExecuteTime(System.currentTimeMillis() - startTime);
        if (executeResult) {
            DBCResultSet dbResult = dbStat.openResultSet();
            if (dbResult != null && !monitor.isCanceled()) {
                try {
                    dataReceiver.fetchStart(session, dbResult, firstRow, maxRows);
                    startTime = System.currentTimeMillis();
                    long rowCount = 0;
                    while (dbResult.nextRow()) {
                        if (monitor.isCanceled() || (hasLimits && rowCount >= maxRows)) {
                            // Fetch not more than max rows
                            break;
                        }
                        dataReceiver.fetchRow(session, dbResult);
                        rowCount++;
                        if (rowCount % 100 == 0) {
                            monitor.subTask(rowCount + ModelMessages.model_jdbc__rows_fetched);
                            monitor.worked(100);
                        }
                    }
                    statistics.setFetchTime(System.currentTimeMillis() - startTime);
                    statistics.setRowsFetched(rowCount);
                } finally {
                    // First - close cursor
                    try {
                        dbResult.close();
                    } catch (Throwable e) {
                        // $NON-NLS-1$
                        log.error("Error closing result set", e);
                    }
                    // Then - signal that fetch was ended
                    try {
                        dataReceiver.fetchEnd(session, dbResult);
                    } catch (Throwable e) {
                        // $NON-NLS-1$
                        log.error("Error while finishing result set fetch", e);
                    }
                }
            }
        }
        return statistics;
    } finally {
        dataReceiver.close();
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource) SQLDataSource(org.jkiss.dbeaver.model.sql.SQLDataSource) JDBCStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement) DBException(org.jkiss.dbeaver.DBException) JDBCStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) NotNull(org.jkiss.code.NotNull)

Example 19 with DBPDataSource

use of org.jkiss.dbeaver.model.DBPDataSource in project dbeaver by dbeaver.

the class DBVModel method getRealContainer.

@Override
public DBSObjectContainer getRealContainer(DBRProgressMonitor monitor) throws DBException {
    DBPDataSource dataSource = dataSourceContainer.getDataSource();
    if (dataSource instanceof DBSObjectContainer) {
        return (DBSObjectContainer) dataSource;
    }
    log.warn("Datasource '" + dataSource + "' is not an object container");
    return null;
}
Also used : DBSObjectContainer(org.jkiss.dbeaver.model.struct.DBSObjectContainer) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource)

Example 20 with DBPDataSource

use of org.jkiss.dbeaver.model.DBPDataSource in project dbeaver by dbeaver.

the class KeepAliveJob method checkDataSourceAlive.

private void checkDataSourceAlive(final DBPDataSourceContainer dataSourceDescriptor) {
    if (!dataSourceDescriptor.isConnected()) {
        return;
    }
    final int keepAliveInterval = dataSourceDescriptor.getConnectionConfiguration().getKeepAliveInterval();
    if (keepAliveInterval <= 0) {
        return;
    }
    final String dsId = dataSourceDescriptor.getId();
    synchronized (this) {
        if (pingCache.contains(dsId)) {
            // Anyway - just skip it
            return;
        }
    }
    final DBPDataSource dataSource = dataSourceDescriptor.getDataSource();
    if (dataSource == null) {
        return;
    }
    Long lastCheckTime;
    synchronized (this) {
        lastCheckTime = checkCache.get(dsId);
    }
    if (lastCheckTime == null) {
        final Date connectTime = dataSourceDescriptor.getConnectTime();
        if (connectTime != null) {
            lastCheckTime = connectTime.getTime();
        }
    }
    if (lastCheckTime == null) {
        log.debug("Can't determine last check time for " + dsId);
        return;
    }
    long curTime = System.currentTimeMillis();
    if ((curTime - lastCheckTime) / 1000 > keepAliveInterval) {
        final PingJob pingJob = new PingJob(dataSource);
        pingJob.addJobChangeListener(new JobChangeAdapter() {

            @Override
            public void done(IJobChangeEvent event) {
                synchronized (KeepAliveJob.this) {
                    checkCache.put(dsId, System.currentTimeMillis());
                    pingCache.remove(dsId);
                }
            }
        });
        synchronized (this) {
            pingCache.add(dsId);
        }
        pingJob.schedule();
    }
}
Also used : JobChangeAdapter(org.eclipse.core.runtime.jobs.JobChangeAdapter) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource) IJobChangeEvent(org.eclipse.core.runtime.jobs.IJobChangeEvent)

Aggregations

DBPDataSource (org.jkiss.dbeaver.model.DBPDataSource)118 DBException (org.jkiss.dbeaver.DBException)43 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)22 DBPDataSourceContainer (org.jkiss.dbeaver.model.DBPDataSourceContainer)14 JDBCStatement (org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement)12 SQLException (java.sql.SQLException)10 GridData (org.eclipse.swt.layout.GridData)10 IEditorPart (org.eclipse.ui.IEditorPart)10 JDBCResultSet (org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet)10 JDBCSession (org.jkiss.dbeaver.model.exec.jdbc.JDBCSession)10 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)10 InvocationTargetException (java.lang.reflect.InvocationTargetException)9 DBCExecutionContext (org.jkiss.dbeaver.model.exec.DBCExecutionContext)9 ArrayList (java.util.ArrayList)8 DBNDatabaseNode (org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)8 IWorkbenchPart (org.eclipse.ui.IWorkbenchPart)7 SQLDataSource (org.jkiss.dbeaver.model.sql.SQLDataSource)7 ISelection (org.eclipse.jface.viewers.ISelection)6 Composite (org.eclipse.swt.widgets.Composite)5 VoidProgressMonitor (org.jkiss.dbeaver.model.runtime.VoidProgressMonitor)5