Search in sources :

Example 46 with DBPDataSource

use of org.jkiss.dbeaver.model.DBPDataSource in project dbeaver by serge-rider.

the class GroupingResultsDecorator method getEmptyDataDescription.

@Override
public String getEmptyDataDescription() {
    DBPDataSource dataSource = container.getResultSetController().getDataContainer().getDataSource();
    if (dataSource == null) {
        return ResultSetMessages.results_decorator_no_connected_to_db;
    }
    SQLDialect dialect = SQLUtils.getDialectFromDataSource(dataSource);
    /*if (dialect == null) {
            return NLS.bind(ResultSetMessages.results_decorator_grouping_is_not_supported, dataSource.getContainer().getDriver().getFullName());
        } else */
    {
        if (container.getGroupAttributes().isEmpty()) {
            return ResultSetMessages.results_decorator_drag_and_drop_results_column;
        } else {
            return ResultSetMessages.results_decorator_grouping_attempt_failed;
        }
    }
}
Also used : SQLDialect(org.jkiss.dbeaver.model.sql.SQLDialect) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource)

Example 47 with DBPDataSource

use of org.jkiss.dbeaver.model.DBPDataSource in project dbeaver by serge-rider.

the class TransformerSettingsDialog method detectTransformers.

private void detectTransformers() {
    final DBPDataSource dataSource = viewer.getDataSource();
    DBVEntityAttribute vAttr = vEntity.getVirtualAttribute(currentAttribute, false);
    DBVTransformSettings settings = vAttr == null ? null : DBVUtils.getTransformSettings(vAttr, false);
    if (dataSource != null && settings != null && !CommonUtils.isEmpty(settings.getCustomTransformer())) {
        transformer = dataSource.getContainer().getPlatform().getValueHandlerRegistry().getTransformer(settings.getCustomTransformer());
    } else {
        transformer = null;
    }
    transformerList = DBWorkbench.getPlatform().getValueHandlerRegistry().findTransformers(currentAttribute.getDataSource(), currentAttribute, null);
}
Also used : DBVEntityAttribute(org.jkiss.dbeaver.model.virtual.DBVEntityAttribute) DBVTransformSettings(org.jkiss.dbeaver.model.virtual.DBVTransformSettings) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource)

Example 48 with DBPDataSource

use of org.jkiss.dbeaver.model.DBPDataSource in project dbeaver by serge-rider.

the class JDBCObjectLookupCache method reloadObject.

protected OBJECT reloadObject(@NotNull DBRProgressMonitor monitor, @NotNull OWNER owner, @Nullable OBJECT object, @Nullable String objectName) throws DBException {
    DBPDataSource dataSource = owner.getDataSource();
    if (dataSource == null) {
        throw new DBException(ModelMessages.error_not_connected_to_database);
    }
    try (JDBCSession session = DBUtils.openMetaSession(monitor, owner, object == null ? "Load object '" + objectName + "' from " + owner.getName() : "Reload object '" + object + "' from " + owner.getName())) {
        try (JDBCStatement dbStat = prepareLookupStatement(session, owner, object, objectName)) {
            dbStat.setFetchSize(1);
            dbStat.executeStatement();
            JDBCResultSet dbResult = dbStat.getResultSet();
            if (dbResult != null) {
                try {
                    if (dbResult.next()) {
                        return fetchObject(session, owner, dbResult);
                    }
                } finally {
                    dbResult.close();
                }
            }
            return null;
        }
    } catch (SQLException ex) {
        throw new DBException(ex, dataSource);
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) 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) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource)

Example 49 with DBPDataSource

use of org.jkiss.dbeaver.model.DBPDataSource 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 50 with DBPDataSource

use of org.jkiss.dbeaver.model.DBPDataSource in project dbeaver by serge-rider.

the class DatabaseLazyEditorInput method initializeRealInput.

public IDatabaseEditorInput initializeRealInput(final DBRProgressMonitor monitor) throws DBException {
    // Get the node path.
    if (project != null) {
        dataSourceContainer = project.getDataSourceRegistry().getDataSource(dataSourceId);
    }
    if (dataSourceContainer == null) {
        // $NON-NLS-2$
        log.error("Can't find data source '" + dataSourceId + "'");
        return null;
    }
    if (project == null) {
        project = dataSourceContainer.getRegistry().getProject();
    }
    final DBNModel navigatorModel = DBWorkbench.getPlatform().getNavigatorModel();
    navigatorModel.ensureProjectLoaded(project);
    // dataSourceContainer, project, nodePath, nodeName, activePageId, activeFolderId
    DBPDataSource dataSource;
    while (!dataSourceContainer.isConnected()) {
        try {
            dataSourceContainer.connect(monitor, true, true);
        } catch (final DBException e) {
            // Connection error
            final Integer result = new UITask<Integer>() {

                @Override
                protected Integer runTask() {
                    ConnectionLostDialog clDialog = new ConnectionLostDialog(UIUtils.getActiveWorkbenchShell(), dataSourceContainer, e, "Close");
                    return clDialog.open();
                }
            }.execute();
            if (result == IDialogConstants.STOP_ID) {
                // Close editor
                return null;
            } else if (result == IDialogConstants.RETRY_ID) {
                continue;
            } else {
                return new ErrorEditorInput(GeneralUtils.makeExceptionStatus(e), navigatorModel.getNodeByObject(dataSourceContainer));
            }
        }
        break;
    }
    try {
        dataSource = dataSourceContainer.getDataSource();
        if (dataSource == null) {
            throw new DBException("Connection to '" + dataSourceContainer.getName() + "' canceled");
        }
        final DBNNode[] editorNodeResult = new DBNNode[1];
        DBExecUtils.tryExecuteRecover(monitor, dataSource, param -> {
            try {
                DBNDataSource dsNode = (DBNDataSource) navigatorModel.getNodeByObject(monitor, this.dataSourceContainer, true);
                if (dsNode == null) {
                    throw new DBException("Datasource '" + this.dataSourceContainer.getName() + "' navigator node not found");
                }
                dsNode.initializeNode(monitor, null);
                editorNodeResult[0] = navigatorModel.getNodeByPath(monitor, project, nodePath);
            } catch (Exception e) {
                throw new InvocationTargetException(e);
            }
        });
        DBNNode node = editorNodeResult[0];
        if (node == null) {
            throw new DBException("Navigator node '" + nodePath + "' not found");
        }
        if (node instanceof DBNDatabaseNode) {
            DatabaseNodeEditorInput realInput = new DatabaseNodeEditorInput((DBNDatabaseNode) node);
            realInput.setDefaultFolderId(activeFolderId);
            realInput.setDefaultPageId(activePageId);
            return realInput;
        } else {
            throw new DBException("Database node has bad type: " + node.getClass().getName());
        }
    } catch (DBException e) {
        return new ErrorEditorInput(GeneralUtils.makeExceptionStatus(e), navigatorModel.getNodeByObject(dataSourceContainer));
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) UITask(org.jkiss.dbeaver.ui.UITask) ConnectionLostDialog(org.jkiss.dbeaver.ui.dialogs.ConnectionLostDialog) DBNNode(org.jkiss.dbeaver.model.navigator.DBNNode) DBNDataSource(org.jkiss.dbeaver.model.navigator.DBNDataSource) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBException(org.jkiss.dbeaver.DBException) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBNModel(org.jkiss.dbeaver.model.navigator.DBNModel) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)

Aggregations

DBPDataSource (org.jkiss.dbeaver.model.DBPDataSource)154 DBException (org.jkiss.dbeaver.DBException)57 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)28 DBPDataSourceContainer (org.jkiss.dbeaver.model.DBPDataSourceContainer)19 InvocationTargetException (java.lang.reflect.InvocationTargetException)15 DBCExecutionContext (org.jkiss.dbeaver.model.exec.DBCExecutionContext)15 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)14 ArrayList (java.util.ArrayList)12 GridData (org.eclipse.swt.layout.GridData)12 JDBCStatement (org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement)12 SQLException (java.sql.SQLException)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 IWorkbenchPart (org.eclipse.ui.IWorkbenchPart)8 DBCExecutionContextDefaults (org.jkiss.dbeaver.model.exec.DBCExecutionContextDefaults)8 DBNDatabaseNode (org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)8 DBSCatalog (org.jkiss.dbeaver.model.struct.rdb.DBSCatalog)8 DBSSchema (org.jkiss.dbeaver.model.struct.rdb.DBSSchema)8 ISelection (org.eclipse.jface.viewers.ISelection)6