Search in sources :

Example 11 with DBSObjectContainer

use of org.jkiss.dbeaver.model.struct.DBSObjectContainer in project dbeaver by dbeaver.

the class SQLEntityResolver method resolveTables.

static void resolveTables(DBRProgressMonitor monitor, DBCExecutionContext executionContext, TemplateContext context, List<DBSEntity> entities) throws DBException {
    TemplateVariable schemaVariable = ((SQLContext) context).getTemplateVariable(SQLContainerResolver.VAR_NAME_SCHEMA);
    TemplateVariable catalogVariable = ((SQLContext) context).getTemplateVariable(SQLContainerResolver.VAR_NAME_CATALOG);
    String catalogName = catalogVariable == null ? null : catalogVariable.getDefaultValue();
    String schemaName = schemaVariable == null ? null : schemaVariable.getDefaultValue();
    DBSObjectContainer objectContainer = DBUtils.getAdapter(DBSObjectContainer.class, executionContext.getDataSource());
    if (objectContainer == null) {
        return;
    }
    if (!CommonUtils.isEmpty(catalogName) || !CommonUtils.isEmpty(schemaName)) {
        // Find container for specified schema/catalog
        objectContainer = (DBSObjectContainer) DBUtils.getObjectByPath(monitor, objectContainer, catalogName, schemaName, null);
    } else {
        DBSObjectSelector objectSelector = DBUtils.getAdapter(DBSObjectSelector.class, executionContext.getDataSource());
        if (objectSelector != null) {
            objectContainer = DBUtils.getAdapter(DBSObjectContainer.class, objectSelector.getDefaultObject());
        }
    }
    if (objectContainer != null) {
        makeProposalsFromChildren(monitor, objectContainer, entities);
    }
}
Also used : DBSObjectContainer(org.jkiss.dbeaver.model.struct.DBSObjectContainer) TemplateVariable(org.eclipse.jface.text.templates.TemplateVariable) DBSObjectSelector(org.jkiss.dbeaver.model.struct.DBSObjectSelector)

Example 12 with DBSObjectContainer

use of org.jkiss.dbeaver.model.struct.DBSObjectContainer in project dbeaver by dbeaver.

the class ConnectionPageGeneral method activatePage.

@Override
public void activatePage() {
    if (connectionNameText != null) {
        ConnectionPageSettings settings = wizard.getPageSettings();
        String newName;
        if (settings != null) {
            DBPConnectionConfiguration connectionInfo = settings.getActiveDataSource().getConnectionConfiguration();
            // $NON-NLS-1$
            newName = dataSourceDescriptor == null ? "" : settings.getActiveDataSource().getName();
            if (CommonUtils.isEmpty(newName)) {
                newName = connectionInfo.getDatabaseName();
                if (CommonUtils.isEmpty(newName)) {
                    newName = connectionInfo.getHostName();
                }
                if (CommonUtils.isEmpty(newName)) {
                    newName = connectionInfo.getUrl();
                }
                if (CommonUtils.isEmpty(newName)) {
                    newName = CoreMessages.dialog_connection_wizard_final_default_new_connection_name;
                }
                // $NON-NLS-1$
                StringTokenizer st = new StringTokenizer(newName, "/\\:,?=%$#@!^&*()");
                while (st.hasMoreTokens()) {
                    newName = st.nextToken();
                }
                if (!CommonUtils.isEmpty(settings.getDriver().getCategory())) {
                    // $NON-NLS-1$
                    newName = settings.getDriver().getCategory() + " - " + newName;
                } else {
                    // $NON-NLS-1$
                    newName = settings.getDriver().getName() + " - " + newName;
                }
                newName = CommonUtils.truncateString(newName, 50);
            }
        } else {
            newName = wizard.getSelectedDriver().getName();
        }
        if (CommonUtils.isEmpty(connectionNameText.getText()) || !connectionNameChanged) {
            if (newName != null) {
                connectionNameText.setText(newName);
            }
            connectionNameChanged = false;
        }
    }
    if (dataSourceDescriptor != null) {
        if (!activated) {
            // Get settings from data source descriptor
            final DBPConnectionConfiguration conConfig = dataSourceDescriptor.getConnectionConfiguration();
            connectionTypeCombo.select(conConfig.getConnectionType());
            dataSourceFolder = dataSourceDescriptor.getFolder();
            if (dataSourceDescriptor.getFolder() == null) {
                connectionFolderCombo.select(0);
            } else {
                connectionFolderCombo.select(dataSourceFolder);
            }
            savePasswordCheck.setSelection(dataSourceDescriptor.isSavePassword());
            autocommit.setSelection(dataSourceDescriptor.isDefaultAutoCommit());
            showSystemObjects.setSelection(dataSourceDescriptor.isShowSystemObjects());
            showUtilityObjects.setSelection(dataSourceDescriptor.isShowUtilityObjects());
            readOnlyConnection.setSelection(dataSourceDescriptor.isConnectionReadOnly());
            isolationLevel.add("");
            DataSourceDescriptor originalDataSource = getWizard().getOriginalDataSource();
            if (originalDataSource != null && originalDataSource.isConnected()) {
                DBPDataSource dataSource = originalDataSource.getDataSource();
                isolationLevel.setEnabled(!autocommit.getSelection());
                supportedLevels.clear();
                DBPTransactionIsolation defaultLevel = dataSourceDescriptor.getActiveTransactionsIsolation();
                for (DBPTransactionIsolation level : CommonUtils.safeCollection(dataSource.getInfo().getSupportedTransactionsIsolation())) {
                    if (!level.isEnabled())
                        continue;
                    isolationLevel.add(level.getTitle());
                    supportedLevels.add(level);
                    if (level.equals(defaultLevel)) {
                        isolationLevel.select(isolationLevel.getItemCount() - 1);
                    }
                }
                if (dataSource instanceof DBSObjectContainer) {
                    new SchemaReadJob((DBSObjectContainer) dataSource).schedule();
                }
            } else {
                isolationLevel.setEnabled(false);
            }
            defaultSchema.setText(CommonUtils.notEmpty(conConfig.getBootstrap().getDefaultObjectName()));
            keepAliveInterval.setSelection(conConfig.getKeepAliveInterval());
            if (dataSourceDescriptor.getDescription() != null) {
                descriptionText.setText(dataSourceDescriptor.getDescription());
            }
            activated = true;
        }
    } else {
        if (eventsButton != null) {
            eventsButton.setFont(getFont());
            DataSourceDescriptor dataSource = getActiveDataSource();
            for (DBPConnectionEventType eventType : dataSource.getConnectionConfiguration().getDeclaredEvents()) {
                if (dataSource.getConnectionConfiguration().getEvent(eventType).isEnabled()) {
                    eventsButton.setFont(boldFont);
                    break;
                }
            }
        }
        // Default settings
        savePasswordCheck.setSelection(true);
        connectionTypeCombo.select(0);
        autocommit.setSelection((connectionTypeCombo.getItem(0)).isAutocommit());
        if (dataSourceFolder != null) {
            connectionFolderCombo.select(dataSourceFolder);
        } else {
            connectionFolderCombo.select(0);
        }
        showSystemObjects.setSelection(true);
        showUtilityObjects.setSelection(false);
        readOnlyConnection.setSelection(false);
        isolationLevel.setEnabled(false);
        defaultSchema.setText("");
    }
    if (savePasswordCheck != null) {
    // savePasswordCheck.setEnabled();
    }
    long features = wizard.getSelectedDriver().getDataSourceProvider().getFeatures();
    for (FilterInfo filterInfo : filters) {
        if (DBSCatalog.class.isAssignableFrom(filterInfo.type)) {
            enableFilter(filterInfo, (features & DBPDataSourceProvider.FEATURE_CATALOGS) != 0);
        } else if (DBSSchema.class.isAssignableFrom(filterInfo.type)) {
            enableFilter(filterInfo, (features & DBPDataSourceProvider.FEATURE_SCHEMAS) != 0);
        } else {
            enableFilter(filterInfo, true);
        }
    }
    filtersGroup.layout();
}
Also used : DBPConnectionEventType(org.jkiss.dbeaver.model.connection.DBPConnectionEventType) DBSSchema(org.jkiss.dbeaver.model.struct.rdb.DBSSchema) DBPConnectionConfiguration(org.jkiss.dbeaver.model.connection.DBPConnectionConfiguration) StringTokenizer(java.util.StringTokenizer) DBSObjectContainer(org.jkiss.dbeaver.model.struct.DBSObjectContainer) DataSourceDescriptor(org.jkiss.dbeaver.registry.DataSourceDescriptor)

Example 13 with DBSObjectContainer

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

the class EditForeignKeyPage method createSchemaSelector.

private void createSchemaSelector(Composite tableGroup) throws DBException {
    // Here is a trick - we need to find schema/catalog container node and list its children
    DBNDatabaseNode schemaContainerNode = null;
    for (DBNNode node = ownerTableNode.getParentNode(); node != null; node = node.getParentNode()) {
        if (node instanceof DBNDatabaseNode) {
            DBSObject nodeObject = ((DBNDatabaseNode) node).getObject();
            if (nodeObject instanceof DBSSchema || nodeObject instanceof DBSCatalog) {
                if (node.getParentNode() instanceof DBNDatabaseNode) {
                    schemaContainerNode = (DBNDatabaseNode) node.getParentNode();
                    break;
                }
            }
        }
    }
    if (schemaContainerNode != null) {
        ILabelProvider labelProvider = new LabelProvider() {

            @Override
            public Image getImage(Object element) {
                return DBeaverIcons.getImage(((DBNDatabaseNode) element).getNodeIcon());
            }

            @Override
            public String getText(Object element) {
                return ((DBNDatabaseNode) element).getNodeName();
            }
        };
        boolean isSchema = (ownTable.getParentObject() instanceof DBSSchema);
        DBPDataSourceInfo dsInfo = ownTable.getDataSource().getInfo();
        UIUtils.createControlLabel(tableGroup, isSchema ? dsInfo.getSchemaTerm() : dsInfo.getCatalogTerm());
        final CSmartCombo<DBNDatabaseNode> schemaCombo = new CSmartCombo<>(tableGroup, SWT.BORDER, labelProvider);
        schemaCombo.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING));
        DBNDatabaseNode selectedNode = null;
        for (DBNNode node : schemaContainerNode.getChildren(VoidProgressMonitor.INSTANCE)) {
            if (node instanceof DBNDatabaseNode && ((DBNDatabaseNode) node).getObject() instanceof DBSObjectContainer) {
                schemaCombo.addItem((DBNDatabaseNode) node);
                if (((DBNDatabaseNode) node).getObject() == ownTable.getParentObject()) {
                    selectedNode = (DBNDatabaseNode) node;
                }
            }
        }
        if (selectedNode != null) {
            schemaCombo.select(selectedNode);
        }
        schemaCombo.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                // Here is another trick
                // We need to find table container node
                // This node is a child of schema node and has the same meta as our original table parent node
                DBNDatabaseNode newContainerNode = null;
                DBXTreeNode tableContainerMeta = ((DBNDatabaseNode) ownerTableNode.getParentNode()).getMeta();
                DBNDatabaseNode schemaNode = schemaCombo.getSelectedItem();
                if (schemaNode.getMeta() == tableContainerMeta) {
                    newContainerNode = schemaNode;
                } else {
                    try {
                        for (DBNNode child : schemaNode.getChildren(VoidProgressMonitor.INSTANCE)) {
                            if (child instanceof DBNDatabaseNode && ((DBNDatabaseNode) child).getMeta() == tableContainerMeta) {
                                newContainerNode = (DBNDatabaseNode) child;
                                break;
                            }
                        }
                    } catch (DBException e1) {
                        log.debug(e1);
                    // Shouldn't be here
                    }
                }
                if (newContainerNode != null) {
                    tableList.setRootNode(newContainerNode);
                    tableList.loadData();
                }
            }
        });
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBNNode(org.jkiss.dbeaver.model.navigator.DBNNode) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBXTreeNode(org.jkiss.dbeaver.model.navigator.meta.DBXTreeNode) CSmartCombo(org.jkiss.dbeaver.ui.controls.CSmartCombo) GridData(org.eclipse.swt.layout.GridData) DBSObjectContainer(org.jkiss.dbeaver.model.struct.DBSObjectContainer) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)

Example 14 with DBSObjectContainer

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

the class SQLCompletionProcessor method computeCompletionProposals.

@Override
public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) {
    final SQLCompletionAnalyzer.CompletionRequest request = new SQLCompletionAnalyzer.CompletionRequest(editor, documentOffset, simpleMode);
    SQLWordPartDetector wordDetector = request.wordDetector = new SQLWordPartDetector(viewer.getDocument(), editor.getSyntaxManager(), documentOffset);
    request.wordPart = wordDetector.getWordPart();
    if (lookupTemplates) {
        return makeTemplateProposals(viewer, request);
    }
    request.queryType = null;
    String searchPrefix = request.wordPart;
    {
        final String prevKeyWord = wordDetector.getPrevKeyWord();
        if (!CommonUtils.isEmpty(prevKeyWord)) {
            if (editor.getSyntaxManager().getDialect().isEntityQueryWord(prevKeyWord)) {
                // TODO: its an ugly hack. Need a better way
                if (SQLConstants.KEYWORD_INTO.equals(prevKeyWord) && !CommonUtils.isEmpty(wordDetector.getPrevWords()) && ("(".equals(wordDetector.getPrevDelimiter()) || ",".equals(wordDetector.getPrevDelimiter()))) {
                    request.queryType = QueryType.COLUMN;
                } else {
                    request.queryType = QueryType.TABLE;
                }
            } else if (editor.getSyntaxManager().getDialect().isAttributeQueryWord(prevKeyWord)) {
                request.queryType = QueryType.COLUMN;
                if (!request.simpleMode && CommonUtils.isEmpty(request.wordPart) && wordDetector.getPrevDelimiter().equals(ALL_COLUMNS_PATTERN)) {
                    wordDetector.moveToDelimiter();
                    searchPrefix = ALL_COLUMNS_PATTERN;
                }
            }
        }
    }
    request.wordPart = searchPrefix;
    if (request.wordPart != null) {
        if (editor.getDataSource() != null) {
            ProposalSearchJob searchJob = new ProposalSearchJob(request);
            searchJob.schedule();
            // Wait until job finished
            UIUtils.waitJobCompletion(searchJob);
        }
    }
    if (!CommonUtils.isEmpty(request.wordPart)) {
        // Keyword assist
        List<String> matchedKeywords = editor.getSyntaxManager().getDialect().getMatchedKeywords(request.wordPart);
        if (!request.simpleMode) {
            // Sort using fuzzy match
            Collections.sort(matchedKeywords, new Comparator<String>() {

                @Override
                public int compare(String o1, String o2) {
                    return TextUtils.fuzzyScore(o1, request.wordPart) - TextUtils.fuzzyScore(o2, request.wordPart);
                }
            });
        }
        for (String keyWord : matchedKeywords) {
            DBPKeywordType keywordType = editor.getSyntaxManager().getDialect().getKeywordType(keyWord);
            if (keywordType != null) {
                request.proposals.add(SQLCompletionAnalyzer.createCompletionProposal(request, keyWord, keyWord, keywordType, null, false, null));
            }
        }
    }
    // Remove duplications
    for (int i = 0; i < request.proposals.size(); i++) {
        SQLCompletionProposal proposal = request.proposals.get(i);
        for (int j = i + 1; j < request.proposals.size(); ) {
            SQLCompletionProposal proposal2 = request.proposals.get(j);
            if (proposal.getDisplayString().equals(proposal2.getDisplayString())) {
                request.proposals.remove(j);
            } else {
                j++;
            }
        }
    }
    DBSObject selectedObject = DBUtils.getActiveInstanceObject(editor.getDataSource());
    boolean hideDups = editor.getActivePreferenceStore().getBoolean(SQLPreferenceConstants.HIDE_DUPLICATE_PROPOSALS) && selectedObject != null;
    if (hideDups) {
        for (int i = 0; i < request.proposals.size(); i++) {
            SQLCompletionProposal proposal = request.proposals.get(i);
            for (int j = 0; j < request.proposals.size(); ) {
                SQLCompletionProposal proposal2 = request.proposals.get(j);
                if (i != j && proposal.hasStructObject() && proposal2.hasStructObject() && CommonUtils.equalObjects(proposal.getObject().getName(), proposal2.getObject().getName()) && proposal.getObjectContainer() == selectedObject) {
                    request.proposals.remove(j);
                } else {
                    j++;
                }
            }
        }
    }
    if (hideDups) {
        if (selectedObject instanceof DBSObjectContainer) {
        //List<ICompletionProposal>
        }
    }
    return ArrayUtils.toArray(ICompletionProposal.class, request.proposals);
}
Also used : DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBSObjectContainer(org.jkiss.dbeaver.model.struct.DBSObjectContainer)

Example 15 with DBSObjectContainer

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

the class SQLEntityResolver method resolveTables.

static void resolveTables(DBRProgressMonitor monitor, DBCExecutionContext executionContext, TemplateContext context, List<DBSEntity> entities) throws DBException {
    TemplateVariable schemaVariable = ((SQLContext) context).getTemplateVariable(SQLContainerResolver.VAR_NAME_SCHEMA);
    TemplateVariable catalogVariable = ((SQLContext) context).getTemplateVariable(SQLContainerResolver.VAR_NAME_CATALOG);
    String catalogName = catalogVariable == null ? null : catalogVariable.getDefaultValue();
    String schemaName = schemaVariable == null ? null : schemaVariable.getDefaultValue();
    DBSObjectContainer objectContainer = DBUtils.getAdapter(DBSObjectContainer.class, executionContext.getDataSource());
    if (objectContainer == null) {
        return;
    }
    if (!CommonUtils.isEmpty(catalogName) || !CommonUtils.isEmpty(schemaName)) {
        // Find container for specified schema/catalog
        objectContainer = (DBSObjectContainer) DBUtils.getObjectByPath(monitor, objectContainer, catalogName, schemaName, null);
    } else {
        DBSObjectSelector objectSelector = DBUtils.getAdapter(DBSObjectSelector.class, executionContext.getDataSource());
        if (objectSelector != null) {
            objectContainer = DBUtils.getAdapter(DBSObjectContainer.class, objectSelector.getDefaultObject());
        }
    }
    if (objectContainer != null) {
        makeProposalsFromChildren(monitor, objectContainer, entities);
    }
}
Also used : DBSObjectContainer(org.jkiss.dbeaver.model.struct.DBSObjectContainer) TemplateVariable(org.eclipse.jface.text.templates.TemplateVariable) DBSObjectSelector(org.jkiss.dbeaver.model.struct.DBSObjectSelector)

Aggregations

DBSObjectContainer (org.jkiss.dbeaver.model.struct.DBSObjectContainer)21 DBException (org.jkiss.dbeaver.DBException)9 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)9 DBNDatabaseNode (org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)5 DBXTreeNode (org.jkiss.dbeaver.model.navigator.meta.DBXTreeNode)5 DBSObjectSelector (org.jkiss.dbeaver.model.struct.DBSObjectSelector)4 DataSourceDescriptor (org.jkiss.dbeaver.registry.DataSourceDescriptor)4 GridData (org.eclipse.swt.layout.GridData)3 DBNNode (org.jkiss.dbeaver.model.navigator.DBNNode)3 DBSSchema (org.jkiss.dbeaver.model.struct.rdb.DBSSchema)3 CSmartCombo (org.jkiss.dbeaver.ui.controls.CSmartCombo)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 ExecutionException (org.eclipse.core.commands.ExecutionException)2 IStatus (org.eclipse.core.runtime.IStatus)2 BadLocationException (org.eclipse.jface.text.BadLocationException)2 TemplateVariable (org.eclipse.jface.text.templates.TemplateVariable)2 Point (org.eclipse.swt.graphics.Point)2 DBPDataSource (org.jkiss.dbeaver.model.DBPDataSource)2 DBPDataSourceInfo (org.jkiss.dbeaver.model.DBPDataSourceInfo)2 DBPSystemObject (org.jkiss.dbeaver.model.DBPSystemObject)2