Search in sources :

Example 31 with DBPDataSource

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

the class NavigatorHandlerObjectGoto method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    DBCExecutionContext context = null;
    DBSObject container = null;
    IWorkbenchPart activePart = HandlerUtil.getActivePart(event);
    if (activePart instanceof DBPContextProvider) {
        context = ((DBPContextProvider) activePart).getExecutionContext();
    } else if (activePart instanceof INavigatorModelView) {
        final ISelection selection = HandlerUtil.getCurrentSelection(event);
        if (selection instanceof IStructuredSelection) {
            Object element = ((IStructuredSelection) selection).getFirstElement();
            if (element instanceof DBSWrapper) {
                DBSObject object = ((DBSWrapper) element).getObject();
                if (object != null) {
                    container = object;
                    while (container instanceof DBSFolder) {
                        container = container.getParentObject();
                    }
                    DBPDataSource dataSource = object.getDataSource();
                    if (dataSource != null) {
                        context = dataSource.getDefaultContext(true);
                    }
                }
            }
        }
    }
    if (context == null) {
        DBUserInterface.getInstance().showError("Go to object", "No active datasource");
        return null;
    }
    IWorkbenchWindow workbenchWindow = HandlerUtil.getActiveWorkbenchWindow(event);
    GotoObjectDialog dialog = new GotoObjectDialog(HandlerUtil.getActiveShell(event), context, container);
    dialog.open();
    Object[] objectsToOpen = dialog.getResult();
    if (!ArrayUtils.isEmpty(objectsToOpen)) {
        Collection<DBNDatabaseNode> nodes = NavigatorHandlerObjectBase.getNodesByObjects(Arrays.asList(objectsToOpen));
        for (DBNDatabaseNode node : nodes) {
            NavigatorUtils.openNavigatorNode(node, workbenchWindow);
        }
    }
    return null;
}
Also used : DBSFolder(org.jkiss.dbeaver.model.struct.DBSFolder) IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) DBCExecutionContext(org.jkiss.dbeaver.model.exec.DBCExecutionContext) DBSWrapper(org.jkiss.dbeaver.model.struct.DBSWrapper) DBPContextProvider(org.jkiss.dbeaver.model.DBPContextProvider) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) IWorkbenchPart(org.eclipse.ui.IWorkbenchPart) ISelection(org.eclipse.jface.viewers.ISelection) GotoObjectDialog(org.jkiss.dbeaver.ui.dialogs.GotoObjectDialog) INavigatorModelView(org.jkiss.dbeaver.ui.navigator.INavigatorModelView) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)

Example 32 with DBPDataSource

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

the class DriverEditDialog method showBadConfigDialog.

public static void showBadConfigDialog(final Shell shell, final String message, final DBException error) {
    // log.debug(message);
    Runnable runnable = new Runnable() {

        @Override
        public void run() {
            DBPDataSource dataSource = error.getDataSource();
            String title = "Bad driver [" + dataSource.getContainer().getDriver().getName() + "] configuration";
            new BadDriverConfigDialog(shell, title, message == null ? title : message, error).open();
        }
    };
    DBeaverUI.syncExec(runnable);
}
Also used : DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource)

Example 33 with DBPDataSource

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

the class GenerateSQLDialog method getSQLText.

@Override
protected String getSQLText() {
    DBPDataSource dataSource = executionContext.getDataSource();
    if (dataSource instanceof SQLDataSource) {
        String lineSeparator = GeneralUtils.getDefaultLineSeparator();
        String scriptDelimiter = ((SQLDataSource) dataSource).getSQLDialect().getScriptDelimiter() + lineSeparator;
        String[] scriptLines = generateSQLScript();
        StringBuilder sql = new StringBuilder(scriptLines.length * 64);
        for (String line : scriptLines) {
            sql.append(line).append(scriptDelimiter);
        }
        // Cut last line separator
        if (sql.length() > lineSeparator.length()) {
            sql.setLength(sql.length() - lineSeparator.length());
        }
        return sql.toString();
    } else {
        return SQLUtils.generateCommentLine(dataSource, "Non-SQL data source");
    }
}
Also used : DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource) SQLDataSource(org.jkiss.dbeaver.model.sql.SQLDataSource)

Example 34 with DBPDataSource

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

the class ResultSetUtils method getAttributeValueFromClipboard.

@Nullable
public static Object getAttributeValueFromClipboard(DBDAttributeBinding attribute) throws DBCException {
    DBPDataSource dataSource = attribute.getDataSource();
    Clipboard clipboard = new Clipboard(Display.getCurrent());
    try (DBCSession session = DBUtils.openUtilSession(new VoidProgressMonitor(), dataSource, "Copy from clipboard")) {
        String strValue = (String) clipboard.getContents(TextTransfer.getInstance());
        return attribute.getValueHandler().getValueFromObject(session, attribute.getAttribute(), strValue, true);
    } finally {
        clipboard.dispose();
    }
}
Also used : DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource) Clipboard(org.eclipse.swt.dnd.Clipboard) VoidProgressMonitor(org.jkiss.dbeaver.model.runtime.VoidProgressMonitor) Nullable(org.jkiss.code.Nullable)

Example 35 with DBPDataSource

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

the class ResultSetUtils method bindAttributes.

public static void bindAttributes(@NotNull DBCSession session, @Nullable DBCResultSet resultSet, @NotNull DBDAttributeBindingMeta[] bindings, @Nullable List<Object[]> rows) throws DBException {
    final DBRProgressMonitor monitor = session.getProgressMonitor();
    final DBPDataSource dataSource = session.getDataSource();
    boolean readMetaData = dataSource.getContainer().getPreferenceStore().getBoolean(DBeaverPreferences.RESULT_SET_READ_METADATA);
    if (!readMetaData) {
        return;
    }
    boolean readReferences = dataSource.getContainer().getPreferenceStore().getBoolean(DBeaverPreferences.RESULT_SET_READ_REFERENCES);
    final Map<DBCEntityMetaData, DBSEntity> entityBindingMap = new IdentityHashMap<>();
    monitor.beginTask("Discover resultset metadata", 3);
    try {
        SQLQuery sqlQuery = null;
        DBSEntity entity = null;
        if (resultSet != null) {
            DBCStatement sourceStatement = resultSet.getSourceStatement();
            if (sourceStatement != null && sourceStatement.getStatementSource() != null) {
                DBCExecutionSource executionSource = sourceStatement.getStatementSource();
                monitor.subTask("Discover owner entity");
                DBSDataContainer dataContainer = executionSource.getDataContainer();
                if (dataContainer instanceof DBSEntity) {
                    entity = (DBSEntity) dataContainer;
                }
                DBCEntityMetaData entityMeta = null;
                if (entity == null) {
                    // Discover from entity metadata
                    Object sourceDescriptor = executionSource.getSourceDescriptor();
                    if (sourceDescriptor instanceof SQLQuery) {
                        sqlQuery = (SQLQuery) sourceDescriptor;
                        entityMeta = sqlQuery.getSingleSource();
                    }
                    if (entityMeta != null) {
                        entity = getEntityFromMetaData(monitor, dataSource, entityMeta);
                        if (entity != null) {
                            entityBindingMap.put(entityMeta, entity);
                        }
                    }
                }
            }
        }
        final Map<DBSEntity, DBDRowIdentifier> locatorMap = new IdentityHashMap<>();
        monitor.subTask("Discover attributes");
        for (DBDAttributeBindingMeta binding : bindings) {
            monitor.subTask("Discover attribute '" + binding.getName() + "'");
            DBCAttributeMetaData attrMeta = binding.getMetaAttribute();
            // We got table name and column name
            // To be editable we need this resultset contain set of columns from the same table
            // which construct any unique key
            DBSEntity attrEntity = null;
            final DBCEntityMetaData attrEntityMeta = attrMeta.getEntityMetaData();
            if (attrEntityMeta != null) {
                attrEntity = entityBindingMap.get(attrEntityMeta);
                if (attrEntity == null) {
                    if (entity != null && entity instanceof DBSTable && ((DBSTable) entity).isView()) {
                        // If this is a view then don't try to detect entity for each attribute
                        // MySQL returns source table name instead of view name. That's crazy.
                        attrEntity = entity;
                    } else {
                        attrEntity = getEntityFromMetaData(monitor, dataSource, attrEntityMeta);
                    }
                }
                if (attrEntity != null) {
                    entityBindingMap.put(attrEntityMeta, attrEntity);
                }
            }
            if (attrEntity == null) {
                attrEntity = entity;
            }
            if (attrEntity == null) {
                if (attrEntityMeta != null) {
                    log.debug("Table '" + DBUtils.getSimpleQualifiedName(attrEntityMeta.getCatalogName(), attrEntityMeta.getSchemaName(), attrEntityMeta.getEntityName()) + "' not found in metadata catalog");
                }
            } else {
                DBDPseudoAttribute pseudoAttribute = DBUtils.getPseudoAttribute(attrEntity, attrMeta.getName());
                if (pseudoAttribute != null) {
                    binding.setPseudoAttribute(pseudoAttribute);
                }
                DBSEntityAttribute tableColumn;
                if (binding.getPseudoAttribute() != null) {
                    tableColumn = binding.getPseudoAttribute().createFakeAttribute(attrEntity, attrMeta);
                } else {
                    tableColumn = attrEntity.getAttribute(monitor, attrMeta.getName());
                }
                if (sqlQuery != null) {
                    if (tableColumn != null && tableColumn.getTypeID() != attrMeta.getTypeID()) {
                        // !! Do not try to use table column handlers for custom queries if source data type
                        // differs from table data type.
                        // Query may have expressions with the same alias as underlying table column
                        // and this expression may return very different data type. It breaks fetch completely.
                        // There should be a better solution but for now let's just disable this too smart feature.
                        binding.setEntityAttribute(tableColumn, false);
                        continue;
                    }
                /*
                        final SQLSelectItem selectItem = sqlQuery.getSelectItem(attrMeta.getName());
                        if (selectItem != null && !selectItem.isPlainColumn()) {
                            // It is not a column.
                            // It maybe an expression, function or anything else
                            continue;
                        }
*/
                }
                if (tableColumn != null && binding.setEntityAttribute(tableColumn, true) && rows != null) {
                    // E.g. we fetched strings and found out that we should handle them as LOBs or enums.
                    try {
                        int pos = attrMeta.getOrdinalPosition();
                        for (Object[] row : rows) {
                            row[pos] = binding.getValueHandler().getValueFromObject(session, tableColumn, row[pos], false);
                        }
                    } catch (DBCException e) {
                        log.warn("Error resolving attribute '" + binding.getName() + "' values", e);
                    }
                }
            }
        }
        monitor.worked(1);
        // Init row identifiers
        monitor.subTask("Detect unique identifiers");
        for (DBDAttributeBindingMeta binding : bindings) {
            // monitor.subTask("Find attribute '" + binding.getName() + "' identifier");
            DBSEntityAttribute attr = binding.getEntityAttribute();
            if (attr == null) {
                continue;
            }
            DBSEntity attrEntity = attr.getParentObject();
            if (attrEntity != null) {
                DBDRowIdentifier rowIdentifier = locatorMap.get(attrEntity);
                if (rowIdentifier == null) {
                    DBSEntityReferrer entityIdentifier = getBestIdentifier(monitor, attrEntity, bindings);
                    if (entityIdentifier != null) {
                        rowIdentifier = new DBDRowIdentifier(attrEntity, entityIdentifier);
                        locatorMap.put(attrEntity, rowIdentifier);
                    }
                }
                binding.setRowIdentifier(rowIdentifier);
            }
        }
        monitor.worked(1);
        if (readReferences && rows != null) {
            monitor.subTask("Late bindings");
            // Read nested bindings
            for (DBDAttributeBinding binding : bindings) {
                binding.lateBinding(session, rows);
            }
        }
        monitor.subTask("Complete metadata load");
        // Reload attributes in row identifiers
        for (DBDRowIdentifier rowIdentifier : locatorMap.values()) {
            rowIdentifier.reloadAttributes(monitor, bindings);
        }
    } finally {
        monitor.done();
    }
}
Also used : DBSTable(org.jkiss.dbeaver.model.struct.rdb.DBSTable) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource) SQLQuery(org.jkiss.dbeaver.model.sql.SQLQuery) DBVEntityConstraint(org.jkiss.dbeaver.model.virtual.DBVEntityConstraint) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)

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