Search in sources :

Example 6 with DBCStatistics

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

the class GroupingResultsContainer method rebuildGrouping.

public void rebuildGrouping() throws DBException {
    if (groupAttributes.isEmpty() || groupFunctions.isEmpty()) {
        groupingViewer.showEmptyPresentation();
        return;
    }
    DBCStatistics statistics = presentation.getController().getModel().getStatistics();
    if (statistics == null) {
        throw new DBException("No main query - can't perform grouping");
    }
    DBPDataSource dataSource = dataContainer.getDataSource();
    if (dataSource == null) {
        throw new DBException("No active datasource");
    }
    SQLDialect dialect = SQLUtils.getDialectFromDataSource(dataSource);
    SQLSyntaxManager syntaxManager = new SQLSyntaxManager();
    syntaxManager.init(dialect, presentation.getController().getPreferenceStore());
    String queryText = statistics.getQueryText();
    if (queryText == null || queryText.isEmpty()) {
        DBSDataContainer dataContainer = presentation.getController().getDataContainer();
        if (dataContainer != null) {
            queryText = dataContainer.getName();
        } else {
            throw new DBException("Empty data container");
        }
    }
    for (String delimiter : syntaxManager.getStatementDelimiters()) {
        while (queryText.endsWith(delimiter)) {
            queryText = queryText.substring(0, queryText.length() - delimiter.length());
        }
    }
    StringBuilder sql = new StringBuilder();
    sql.append("SELECT ");
    for (int i = 0; i < groupAttributes.size(); i++) {
        if (i > 0)
            sql.append(", ");
        sql.append(DBUtils.getQuotedIdentifier(dataSource, groupAttributes.get(i)));
    }
    for (String func : groupFunctions) {
        sql.append(", ").append(func);
    }
    sql.append(" FROM (\n");
    sql.append(queryText);
    sql.append(") src");
    sql.append("\nGROUP BY ");
    for (int i = 0; i < groupAttributes.size(); i++) {
        if (i > 0)
            sql.append(", ");
        sql.append(DBUtils.getQuotedIdentifier(dataSource, groupAttributes.get(i)));
    }
    boolean isDefaultGrouping = groupFunctions.size() == 1 && groupFunctions.get(0).equals(DEFAULT_FUNCTION);
    boolean isShowDuplicatesOnly = dataSource.getContainer().getPreferenceStore().getBoolean(ResultSetPreferences.RS_GROUPING_SHOW_DUPLICATES_ONLY);
    if (isDefaultGrouping && isShowDuplicatesOnly) {
        sql.append("\nHAVING ").append(DEFAULT_FUNCTION).append(" > 1");
    }
    dataContainer.setGroupingQuery(sql.toString());
    DBDDataFilter dataFilter;
    if (presentation.getController().getModel().isMetadataChanged()) {
        dataFilter = new DBDDataFilter();
    } else {
        dataFilter = new DBDDataFilter(groupingViewer.getModel().getDataFilter());
    }
    String defaultSorting = dataSource.getContainer().getPreferenceStore().getString(ResultSetPreferences.RS_GROUPING_DEFAULT_SORTING);
    if (!CommonUtils.isEmpty(defaultSorting) && isDefaultGrouping) {
        if (dialect.supportsOrderByIndex()) {
            // By default sort by count in desc order
            int countPosition = groupAttributes.size() + 1;
            StringBuilder orderBy = new StringBuilder();
            orderBy.append(countPosition).append(" ").append(defaultSorting);
            for (int i = 0; i < groupAttributes.size(); i++) {
                orderBy.append(",").append(i + 1);
            }
            dataFilter.setOrder(orderBy.toString());
        } else {
            dataFilter.setOrder(groupFunctions.get(groupFunctions.size() - 1) + " " + defaultSorting);
        }
    }
    groupingViewer.setDataFilter(dataFilter, true);
// groupingViewer.refresh();
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBDDataFilter(org.jkiss.dbeaver.model.data.DBDDataFilter) SQLDialect(org.jkiss.dbeaver.model.sql.SQLDialect) SQLSyntaxManager(org.jkiss.dbeaver.model.sql.SQLSyntaxManager) DBCStatistics(org.jkiss.dbeaver.model.exec.DBCStatistics) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource) DBSDataContainer(org.jkiss.dbeaver.model.struct.DBSDataContainer)

Example 7 with DBCStatistics

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

the class WMIClass method readData.

@NotNull
@Override
public DBCStatistics readData(@NotNull DBCExecutionSource source, @NotNull DBCSession session, @NotNull DBDDataReceiver dataReceiver, DBDDataFilter dataFilter, long firstRow, long maxRows, long flags, int fetchSize) throws DBCException {
    DBCStatistics statistics = new DBCStatistics();
    try {
        long startTime = System.currentTimeMillis();
        WMIObjectCollectorSink sink = new WMIObjectCollectorSink(session.getProgressMonitor(), getNamespace().getService(), firstRow, maxRows);
        getNamespace().getService().enumInstances(getName(), sink, WMIConstants.WBEM_FLAG_SHALLOW);
        statistics.setExecuteTime(System.currentTimeMillis() - startTime);
        startTime = System.currentTimeMillis();
        sink.waitForFinish();
        WMIResultSet resultSet = new WMIResultSet(session, this, sink.getObjectList());
        long resultCount = 0;
        try {
            dataReceiver.fetchStart(session, resultSet, firstRow, maxRows);
            while (resultSet.nextRow()) {
                resultCount++;
                dataReceiver.fetchRow(session, resultSet);
            }
        } finally {
            try {
                dataReceiver.fetchEnd(session, resultSet);
            } catch (DBCException e) {
                // $NON-NLS-1$
                log.error("Error while finishing result set fetch", e);
            }
            resultSet.close();
            dataReceiver.close();
        }
        statistics.setFetchTime(System.currentTimeMillis() - startTime);
        statistics.setRowsFetched(resultCount);
        return statistics;
    } catch (WMIException e) {
        throw new DBCException(e, session.getExecutionContext());
    }
}
Also used : DBCException(org.jkiss.dbeaver.model.exec.DBCException) DBCStatistics(org.jkiss.dbeaver.model.exec.DBCStatistics) NotNull(org.jkiss.code.NotNull)

Example 8 with DBCStatistics

use of org.jkiss.dbeaver.model.exec.DBCStatistics in project dbeaver by dbeaver.

the class GroupingResultsContainer method rebuildGrouping.

public void rebuildGrouping() throws DBException {
    if (groupAttributes.isEmpty() || groupFunctions.isEmpty()) {
        groupingViewer.showEmptyPresentation();
        return;
    }
    DBCStatistics statistics = presentation.getController().getModel().getStatistics();
    if (statistics == null) {
        throw new DBException("No main query - can't perform grouping");
    }
    DBPDataSource dataSource = dataContainer.getDataSource();
    if (dataSource == null) {
        throw new DBException("No active datasource");
    }
    SQLDialect dialect = SQLUtils.getDialectFromDataSource(dataSource);
    SQLSyntaxManager syntaxManager = new SQLSyntaxManager();
    syntaxManager.init(dialect, presentation.getController().getPreferenceStore());
    String queryText = statistics.getQueryText();
    if (queryText == null || queryText.isEmpty()) {
        DBSDataContainer dataContainer = presentation.getController().getDataContainer();
        if (dataContainer != null) {
            queryText = dataContainer.getName();
        } else {
            throw new DBException("Empty data container");
        }
    }
    for (String delimiter : syntaxManager.getStatementDelimiters()) {
        while (queryText.endsWith(delimiter)) {
            queryText = queryText.substring(0, queryText.length() - delimiter.length());
        }
    }
    StringBuilder sql = new StringBuilder();
    sql.append("SELECT ");
    for (int i = 0; i < groupAttributes.size(); i++) {
        if (i > 0)
            sql.append(", ");
        sql.append(DBUtils.getQuotedIdentifier(dataSource, groupAttributes.get(i)));
    }
    for (String func : groupFunctions) {
        sql.append(", ").append(func);
    }
    sql.append(" FROM (\n");
    sql.append(queryText);
    sql.append(") src");
    sql.append("\nGROUP BY ");
    for (int i = 0; i < groupAttributes.size(); i++) {
        if (i > 0)
            sql.append(", ");
        sql.append(DBUtils.getQuotedIdentifier(dataSource, groupAttributes.get(i)));
    }
    boolean isDefaultGrouping = groupFunctions.size() == 1 && groupFunctions.get(0).equals(DEFAULT_FUNCTION);
    boolean isShowDuplicatesOnly = dataSource.getContainer().getPreferenceStore().getBoolean(ResultSetPreferences.RS_GROUPING_SHOW_DUPLICATES_ONLY);
    if (isDefaultGrouping && isShowDuplicatesOnly) {
        sql.append("\nHAVING ").append(DEFAULT_FUNCTION).append(" > 1");
    }
    dataContainer.setGroupingQuery(sql.toString());
    DBDDataFilter dataFilter;
    if (presentation.getController().getModel().isMetadataChanged()) {
        dataFilter = new DBDDataFilter();
    } else {
        dataFilter = new DBDDataFilter(groupingViewer.getModel().getDataFilter());
    }
    String defaultSorting = dataSource.getContainer().getPreferenceStore().getString(ResultSetPreferences.RS_GROUPING_DEFAULT_SORTING);
    if (!CommonUtils.isEmpty(defaultSorting) && isDefaultGrouping) {
        if (dialect.supportsOrderByIndex()) {
            // By default sort by count in desc order
            int countPosition = groupAttributes.size() + 1;
            StringBuilder orderBy = new StringBuilder();
            orderBy.append(countPosition).append(" ").append(defaultSorting);
            for (int i = 0; i < groupAttributes.size(); i++) {
                orderBy.append(",").append(i + 1);
            }
            dataFilter.setOrder(orderBy.toString());
        } else {
            dataFilter.setOrder(groupFunctions.get(groupFunctions.size() - 1) + " " + defaultSorting);
        }
    }
    groupingViewer.setDataFilter(dataFilter, true);
// groupingViewer.refresh();
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBDDataFilter(org.jkiss.dbeaver.model.data.DBDDataFilter) SQLDialect(org.jkiss.dbeaver.model.sql.SQLDialect) SQLSyntaxManager(org.jkiss.dbeaver.model.sql.SQLSyntaxManager) DBCStatistics(org.jkiss.dbeaver.model.exec.DBCStatistics) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource) DBSDataContainer(org.jkiss.dbeaver.model.struct.DBSDataContainer)

Aggregations

DBCStatistics (org.jkiss.dbeaver.model.exec.DBCStatistics)8 NotNull (org.jkiss.code.NotNull)6 DBCException (org.jkiss.dbeaver.model.exec.DBCException)4 DBSDataContainer (org.jkiss.dbeaver.model.struct.DBSDataContainer)4 DBException (org.jkiss.dbeaver.DBException)2 DBPDataSource (org.jkiss.dbeaver.model.DBPDataSource)2 DBDDataFilter (org.jkiss.dbeaver.model.data.DBDDataFilter)2 SQLDialect (org.jkiss.dbeaver.model.sql.SQLDialect)2 SQLSyntaxManager (org.jkiss.dbeaver.model.sql.SQLSyntaxManager)2