Search in sources :

Example 21 with DBPDataSource

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

the class SearchDataQuery method run.

@Override
public IStatus run(IProgressMonitor monitor) throws OperationCanceledException {
    try {
        String searchString = params.getSearchString();
        // monitor.subTask("Collect tables");
        Set<DBPDataSource> dataSources = new HashSet<>();
        for (DBSDataContainer searcher : params.sources) {
            dataSources.add(searcher.getDataSource());
        }
        // Search
        DBNModel dbnModel = DBeaverCore.getInstance().getNavigatorModel();
        monitor.beginTask("Search \"" + searchString + "\" in " + params.sources.size() + " table(s) / " + dataSources.size() + " database(s)", params.sources.size());
        try {
            for (DBSDataContainer dataContainer : params.sources) {
                if (monitor.isCanceled()) {
                    break;
                }
                String objectName = DBUtils.getObjectFullName(dataContainer, DBPEvaluationContext.DML);
                DBNDatabaseNode node = dbnModel.findNode(dataContainer);
                if (node == null) {
                    log.warn("Can't find tree node for object \"" + objectName + "\"");
                    continue;
                }
                monitor.subTask(objectName);
                DBPDataSource dataSource = dataContainer.getDataSource();
                if (dataSource == null) {
                    log.warn("Object \"" + objectName + "\" not connected");
                    continue;
                }
                SearchTableMonitor searchMonitor = new SearchTableMonitor();
                try (DBCSession session = DBUtils.openUtilSession(searchMonitor, dataSource, "Search rows in " + objectName)) {
                    TestDataReceiver dataReceiver = new TestDataReceiver(searchMonitor);
                    try {
                        findRows(session, dataContainer, dataReceiver);
                    } catch (DBCException e) {
                        // Search failed in some container - just write an error in log.
                        // We don't want to break whole search because of one single table.
                        log.error("Fulltext search failed in '" + dataContainer.getName() + "'", e);
                    }
                    if (dataReceiver.rowCount > 0) {
                        SearchDataObject object = new SearchDataObject(node, dataReceiver.rowCount, dataReceiver.filter);
                        searchResult.addObjects(Collections.singletonList(object));
                    }
                }
                monitor.worked(1);
            }
        } finally {
            monitor.done();
        }
        return Status.OK_STATUS;
    } catch (Exception e) {
        return GeneralUtils.makeExceptionStatus(e);
    }
}
Also used : DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) DBException(org.jkiss.dbeaver.DBException) DBNModel(org.jkiss.dbeaver.model.navigator.DBNModel) DBSDataContainer(org.jkiss.dbeaver.model.struct.DBSDataContainer) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)

Example 22 with DBPDataSource

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

the class ExplainPlanViewer method explainQueryPlan.

public void explainQueryPlan(DBCExecutionContext executionContext, SQLQuery query) throws DBCException {
    this.executionContext = executionContext;
    this.query = query;
    if (this.executionContext != null) {
        DBPDataSource dataSource = executionContext.getDataSource();
        planner = DBUtils.getAdapter(DBCQueryPlanner.class, dataSource);
    } else {
        planner = null;
    }
    planTree.clearListData();
    refreshPlanAction.setEnabled(false);
    if (planner == null) {
        throw new DBCException("This datasource doesn't support execution plans");
    }
    if (planTree.isLoading()) {
        UIUtils.showMessageBox(getControl().getShell(), "Can't explain plan", "Explain plan already running", SWT.ICON_ERROR);
        return;
    }
    sqlText.setText(query.getText());
    planTree.init(this.executionContext, planner, query.getText());
    planTree.loadData();
    refreshPlanAction.setEnabled(true);
    toggleViewAction.setEnabled(true);
}
Also used : DBCQueryPlanner(org.jkiss.dbeaver.model.exec.plan.DBCQueryPlanner) DBCException(org.jkiss.dbeaver.model.exec.DBCException) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource)

Example 23 with DBPDataSource

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

the class DebugCore method resolveDatabaseContext.

public static Map<String, Object> resolveDatabaseContext(DBSObject databaseObject) {
    Map<String, Object> result = new HashMap<String, Object>();
    if (databaseObject == null) {
        return result;
    }
    DBPDataSource dataSource = databaseObject.getDataSource();
    if (dataSource == null) {
        return result;
    }
    DBGResolver finder = Adapters.adapt(dataSource.getContainer(), DBGResolver.class);
    if (finder == null) {
        return result;
    }
    Map<String, Object> context = finder.resolveContext(databaseObject);
    result.putAll(context);
    return result;
}
Also used : DBGResolver(org.jkiss.dbeaver.debug.DBGResolver) HashMap(java.util.HashMap) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource)

Example 24 with DBPDataSource

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

the class PostgreDebugAdapterFactory method getAdapter.

@SuppressWarnings("unchecked")
@Override
public <T> T getAdapter(Object adaptableObject, Class<T> adapterType) {
    if (adapterType == DBGController.class) {
        if (adaptableObject instanceof DBPDataSourceContainer) {
            DBPDataSourceContainer sourceContainer = (DBPDataSourceContainer) adaptableObject;
            DBPDriver driver = sourceContainer.getDriver();
            if (driver == null) {
                return null;
            }
            DBPDataSourceProvider dataSourceProvider = driver.getDataSourceProvider();
            if (dataSourceProvider instanceof PostgreDataSourceProvider) {
                PostgreDebugController postgreDebugController = new PostgreDebugController(sourceContainer);
                return (T) postgreDebugController;
            }
        }
    } else if (adapterType == DBGResolver.class) {
        if (adaptableObject instanceof DBPDataSourceContainer) {
            DBPDataSourceContainer sourceContainer = (DBPDataSourceContainer) adaptableObject;
            DBPDataSource dataSource = sourceContainer.getDataSource();
            if (dataSource instanceof PostgreDataSource) {
                PostgreDataSource postgeDataSource = (PostgreDataSource) dataSource;
                return (T) new PostgreResolver(postgeDataSource);
            }
        }
    }
    return null;
}
Also used : PostgreDataSource(org.jkiss.dbeaver.ext.postgresql.model.PostgreDataSource) DBGResolver(org.jkiss.dbeaver.debug.DBGResolver) DBPDriver(org.jkiss.dbeaver.model.connection.DBPDriver) PostgreDataSourceProvider(org.jkiss.dbeaver.ext.postgresql.PostgreDataSourceProvider) PostgreDebugController(org.jkiss.dbeaver.ext.postgresql.debug.internal.impl.PostgreDebugController) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource) DBPDataSourceContainer(org.jkiss.dbeaver.model.DBPDataSourceContainer) DBPDataSourceProvider(org.jkiss.dbeaver.model.DBPDataSourceProvider)

Example 25 with DBPDataSource

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

the class PrefPageSQLFormat method createPreferenceContent.

@Override
protected Control createPreferenceContent(Composite parent) {
    Composite composite = UIUtils.createPlaceholder(parent, 2, 5);
    // Autoclose
    {
        Composite acGroup = UIUtils.createControlGroup(composite, CoreMessages.pref_page_sql_format_group_auto_close, 1, GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING, 0);
        acSingleQuotesCheck = UIUtils.createCheckbox(acGroup, CoreMessages.pref_page_sql_format_label_single_quotes, false);
        acDoubleQuotesCheck = UIUtils.createCheckbox(acGroup, CoreMessages.pref_page_sql_format_label_double_quotes, false);
        acBracketsCheck = UIUtils.createCheckbox(acGroup, CoreMessages.pref_page_sql_format_label_brackets, false);
    }
    {
        // Formatting
        Composite afGroup = UIUtils.createControlGroup(composite, CoreMessages.pref_page_sql_format_group_auto_format, 1, GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING, 0);
        afKeywordCase = UIUtils.createCheckbox(afGroup, CoreMessages.pref_page_sql_format_label_convert_keyword_case, CoreMessages.pref_page_sql_format_label_convert_keyword_case_tip, false, 1);
        afExtractFromSource = UIUtils.createCheckbox(afGroup, CoreMessages.pref_page_sql_format_label_extract_sql_from_source_code, CoreMessages.pref_page_sql_format_label_extract_sql_from_source_code_tip, false, 1);
    }
    Composite formatterGroup = UIUtils.createControlGroup(composite, CoreMessages.pref_page_sql_format_group_formatter, 1, GridData.FILL_BOTH, 0);
    ((GridData) formatterGroup.getLayoutData()).horizontalSpan = 2;
    {
        Composite formatterPanel = UIUtils.createPlaceholder(formatterGroup, 2);
        formatterPanel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        formatterSelector = UIUtils.createLabelCombo(formatterPanel, CoreMessages.pref_page_sql_format_label_formatter, SWT.DROP_DOWN | SWT.READ_ONLY);
        formatters = SQLFormatterConfigurationRegistry.getInstance().getFormatters();
        for (SQLFormatterDescriptor formatterDesc : formatters) {
            formatterSelector.add(DBPIdentifierCase.capitalizeCaseName(formatterDesc.getLabel()));
        }
        formatterSelector.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                showFormatterSettings();
                performApply();
            }
        });
        formatterSelector.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING));
    }
    // Default formatter settings
    {
        defaultGroup = UIUtils.createPlaceholder(formatterGroup, 2, 0);
        defaultGroup.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING));
        keywordCaseCombo = UIUtils.createLabelCombo(defaultGroup, CoreMessages.pref_page_sql_format_label_keyword_case, SWT.DROP_DOWN | SWT.READ_ONLY);
        keywordCaseCombo.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING));
        keywordCaseCombo.add("Database");
        for (DBPIdentifierCase c : DBPIdentifierCase.values()) {
            keywordCaseCombo.add(DBPIdentifierCase.capitalizeCaseName(c.name()));
        }
        keywordCaseCombo.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                performApply();
            }
        });
    }
    // External formatter
    {
        externalGroup = UIUtils.createPlaceholder(formatterGroup, 2, 5);
        externalGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.HORIZONTAL_ALIGN_BEGINNING));
        externalCmdText = UIUtils.createLabelText(externalGroup, CoreMessages.pref_page_sql_format_label_external_command_line, "");
        externalCmdText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        UIUtils.installContentProposal(externalCmdText, new TextContentAdapter(), new SimpleContentProposalProvider(new String[] { GeneralUtils.variablePattern(SQLFormatterExternal.VAR_FILE) }));
        UIUtils.setContentProposalToolTip(externalCmdText, CoreMessages.pref_page_sql_format_label_external_set_content_tool_tip, SQLFormatterExternal.VAR_FILE);
        externalUseFile = UIUtils.createLabelCheckbox(externalGroup, CoreMessages.pref_page_sql_format_label_external_use_temp_file, CoreMessages.pref_page_sql_format_label_external_use_temp_file_tip + GeneralUtils.variablePattern(SQLFormatterExternal.VAR_FILE), false);
        externalTimeout = UIUtils.createLabelSpinner(externalGroup, CoreMessages.pref_page_sql_format_label_external_exec_timeout, CoreMessages.pref_page_sql_format_label_external_exec_timeout_tip, 100, 100, 10000);
    }
    {
        // SQL preview
        Composite previewGroup = new Composite(formatterGroup, SWT.BORDER);
        previewGroup.setLayoutData(new GridData(GridData.FILL_BOTH));
        previewGroup.setLayout(new FillLayout());
        sqlViewer = new SQLEditorBase() {

            @Override
            public DBCExecutionContext getExecutionContext() {
                final DBPDataSourceContainer container = getDataSourceContainer();
                if (container != null) {
                    final DBPDataSource dataSource = container.getDataSource();
                    if (dataSource != null) {
                        return dataSource.getDefaultContext(false);
                    }
                }
                return null;
            }
        };
        try {
            try (final InputStream sqlStream = getClass().getResourceAsStream(FORMAT_FILE_NAME)) {
                final String sqlText = ContentUtils.readToString(sqlStream, GeneralUtils.DEFAULT_ENCODING);
                IEditorSite subSite = new SubEditorSite(DBeaverUI.getActiveWorkbenchWindow().getActivePage().getActivePart().getSite());
                StringEditorInput sqlInput = new StringEditorInput("SQL preview", sqlText, true, GeneralUtils.getDefaultFileEncoding());
                sqlViewer.init(subSite, sqlInput);
            }
        } catch (Exception e) {
            log.error(e);
        }
        sqlViewer.createPartControl(previewGroup);
        Object text = sqlViewer.getAdapter(Control.class);
        if (text instanceof StyledText) {
            ((StyledText) text).setWordWrap(true);
        }
        sqlViewer.reloadSyntaxRules();
        previewGroup.addDisposeListener(new DisposeListener() {

            @Override
            public void widgetDisposed(DisposeEvent e) {
                sqlViewer.dispose();
            }
        });
    }
    return composite;
}
Also used : SQLFormatterDescriptor(org.jkiss.dbeaver.registry.sql.SQLFormatterDescriptor) DisposeListener(org.eclipse.swt.events.DisposeListener) StyledText(org.eclipse.swt.custom.StyledText) InputStream(java.io.InputStream) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) FillLayout(org.eclipse.swt.layout.FillLayout) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource) TextContentAdapter(org.eclipse.jface.fieldassist.TextContentAdapter) DisposeEvent(org.eclipse.swt.events.DisposeEvent) StringEditorInput(org.jkiss.dbeaver.ui.editors.StringEditorInput) SubEditorSite(org.jkiss.dbeaver.ui.editors.SubEditorSite) SimpleContentProposalProvider(org.eclipse.jface.fieldassist.SimpleContentProposalProvider) SQLEditorBase(org.jkiss.dbeaver.ui.editors.sql.SQLEditorBase) GridData(org.eclipse.swt.layout.GridData) SelectionEvent(org.eclipse.swt.events.SelectionEvent) DBPIdentifierCase(org.jkiss.dbeaver.model.DBPIdentifierCase) DBPDataSourceContainer(org.jkiss.dbeaver.model.DBPDataSourceContainer) IEditorSite(org.eclipse.ui.IEditorSite)

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