Search in sources :

Example 1 with SQLParserContext

use of org.jkiss.dbeaver.model.sql.parser.SQLParserContext in project dbeaver by serge-rider.

the class SQLQueryDataContainer 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();
    // Modify query (filters + parameters)
    DBPDataSource dataSource = session.getDataSource();
    SQLQuery sqlQuery = query;
    // .trim();
    String queryText = sqlQuery.getOriginalText();
    if (dataFilter != null && dataFilter.hasFilters()) {
        String filteredQueryText = dataSource.getSQLDialect().addFiltersToQuery(session.getProgressMonitor(), dataSource, queryText, dataFilter);
        sqlQuery = new SQLQuery(dataSource, filteredQueryText, sqlQuery);
    } else {
        sqlQuery = new SQLQuery(dataSource, queryText, sqlQuery);
    }
    if (scriptContext != null) {
        SQLSyntaxManager syntaxManager = new SQLSyntaxManager();
        syntaxManager.init(dataSource);
        SQLRuleManager ruleManager = new SQLRuleManager(syntaxManager);
        ruleManager.loadRules(dataSource, false);
        SQLParserContext parserContext = new SQLParserContext(this, syntaxManager, ruleManager, new Document(query.getOriginalText()));
        sqlQuery.setParameters(SQLScriptParser.parseParameters(parserContext, 0, sqlQuery.getLength()));
        if (!scriptContext.fillQueryParameters(sqlQuery, CommonUtils.isBitSet(flags, DBSDataContainer.FLAG_REFRESH))) {
            // User canceled
            return statistics;
        }
    }
    final SQLQueryResult curResult = new SQLQueryResult(sqlQuery);
    if (firstRow > 0) {
        curResult.setRowOffset(firstRow);
    }
    statistics.setQueryText(sqlQuery.getText());
    long startTime = System.currentTimeMillis();
    try (final DBCStatement dbcStatement = DBUtils.makeStatement(source, session, DBCStatementType.SCRIPT, sqlQuery, firstRow, maxRows)) {
        DBExecUtils.setStatementFetchSize(dbcStatement, firstRow, maxRows, fetchSize);
        // Execute statement
        session.getProgressMonitor().subTask("Execute query");
        boolean hasResultSet = dbcStatement.executeStatement();
        statistics.addExecuteTime(System.currentTimeMillis() - startTime);
        statistics.addStatementsCount();
        curResult.setHasResultSet(hasResultSet);
        if (hasResultSet) {
            DBCResultSet resultSet = dbcStatement.openResultSet();
            if (resultSet != null) {
                SQLQueryResult.ExecuteResult executeResult = curResult.addExecuteResult(true);
                DBRProgressMonitor monitor = session.getProgressMonitor();
                monitor.subTask("Fetch result set");
                DBFetchProgress fetchProgress = new DBFetchProgress(session.getProgressMonitor());
                dataReceiver.fetchStart(session, resultSet, firstRow, maxRows);
                try {
                    long fetchStartTime = System.currentTimeMillis();
                    // Fetch all rows
                    while (!fetchProgress.isMaxRowsFetched(maxRows) && !fetchProgress.isCanceled() && resultSet.nextRow()) {
                        dataReceiver.fetchRow(session, resultSet);
                        fetchProgress.monitorRowFetch();
                    }
                    statistics.addFetchTime(System.currentTimeMillis() - fetchStartTime);
                } finally {
                    try {
                        resultSet.close();
                    } catch (Throwable e) {
                        log.error("Error while closing resultset", e);
                    }
                    try {
                        dataReceiver.fetchEnd(session, resultSet);
                    } catch (Throwable e) {
                        log.error("Error while handling end of result set fetch", e);
                    }
                    dataReceiver.close();
                }
                if (executeResult != null) {
                    executeResult.setRowCount(fetchProgress.getRowCount());
                }
                statistics.setRowsFetched(fetchProgress.getRowCount());
                monitor.subTask(fetchProgress.getRowCount() + " rows fetched");
            }
        } else {
            log.warn("No results returned by query execution");
        }
        try {
            curResult.addWarnings(dbcStatement.getStatementWarnings());
        } catch (Throwable e) {
            log.warn("Can't read execution warnings", e);
        }
    }
    return statistics;
}
Also used : SQLRuleManager(org.jkiss.dbeaver.model.sql.parser.SQLRuleManager) Document(org.eclipse.jface.text.Document) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) SQLParserContext(org.jkiss.dbeaver.model.sql.parser.SQLParserContext) NotNull(org.jkiss.code.NotNull)

Example 2 with SQLParserContext

use of org.jkiss.dbeaver.model.sql.parser.SQLParserContext in project dbeaver by serge-rider.

the class SQLEditorBase method reloadSyntaxRules.

public void reloadSyntaxRules() {
    // Refresh syntax
    SQLDialect dialect = getSQLDialect();
    IDocument document = getDocument();
    syntaxManager.init(dialect, getActivePreferenceStore());
    SQLRuleManager ruleManager = new SQLRuleManager(syntaxManager);
    ruleManager.loadRules(getDataSource(), SQLEditorBase.isBigScript(getEditorInput()));
    ruleScanner.refreshRules(getDataSource(), ruleManager);
    parserContext = new SQLParserContext(SQLEditorBase.this, syntaxManager, ruleManager, document != null ? document : new Document());
    if (document instanceof IDocumentExtension3) {
        IDocumentPartitioner partitioner = new FastPartitioner(new SQLPartitionScanner(getDataSource(), dialect), SQLParserPartitions.SQL_CONTENT_TYPES);
        partitioner.connect(document);
        try {
            ((IDocumentExtension3) document).setDocumentPartitioner(SQLParserPartitions.SQL_PARTITIONING, partitioner);
        } catch (Throwable e) {
            // $NON-NLS-1$
            log.warn("Error setting SQL partitioner", e);
        }
        ProjectionViewer projectionViewer = (ProjectionViewer) getSourceViewer();
        if (projectionViewer != null && projectionViewer.getAnnotationModel() != null && document.getLength() > 0) {
            // projectionViewer.getTextWidget().redraw();
            try {
                projectionViewer.reinitializeProjection();
            } catch (Throwable ex) {
                // We can catch OutOfMemory here for too big/complex documents
                // $NON-NLS-1$
                log.warn("Can't initialize SQL syntax projection", ex);
            }
        }
    }
    final IVerticalRuler verticalRuler = getVerticalRuler();
    // Update configuration
    if (getSourceViewerConfiguration() instanceof SQLEditorSourceViewerConfiguration) {
        ((SQLEditorSourceViewerConfiguration) getSourceViewerConfiguration()).onDataSourceChange();
    }
    if (verticalRuler != null) {
        verticalRuler.update();
    }
}
Also used : SQLRuleManager(org.jkiss.dbeaver.model.sql.parser.SQLRuleManager) BasicSQLDialect(org.jkiss.dbeaver.model.impl.sql.BasicSQLDialect) FastPartitioner(org.eclipse.jface.text.rules.FastPartitioner) SQLPartitionScanner(org.jkiss.dbeaver.ui.editors.sql.syntax.SQLPartitionScanner) ProjectionViewer(org.eclipse.jface.text.source.projection.ProjectionViewer) SQLParserContext(org.jkiss.dbeaver.model.sql.parser.SQLParserContext)

Example 3 with SQLParserContext

use of org.jkiss.dbeaver.model.sql.parser.SQLParserContext in project dbeaver by dbeaver.

the class SQLQueryDataContainer 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();
    // Modify query (filters + parameters)
    DBPDataSource dataSource = session.getDataSource();
    SQLQuery sqlQuery = query;
    // .trim();
    String queryText = sqlQuery.getOriginalText();
    if (dataFilter != null && dataFilter.hasFilters()) {
        String filteredQueryText = dataSource.getSQLDialect().addFiltersToQuery(session.getProgressMonitor(), dataSource, queryText, dataFilter);
        sqlQuery = new SQLQuery(dataSource, filteredQueryText, sqlQuery);
    } else {
        sqlQuery = new SQLQuery(dataSource, queryText, sqlQuery);
    }
    if (scriptContext != null) {
        SQLSyntaxManager syntaxManager = new SQLSyntaxManager();
        syntaxManager.init(dataSource);
        SQLRuleManager ruleManager = new SQLRuleManager(syntaxManager);
        ruleManager.loadRules(dataSource, false);
        SQLParserContext parserContext = new SQLParserContext(this, syntaxManager, ruleManager, new Document(query.getOriginalText()));
        sqlQuery.setParameters(SQLScriptParser.parseParameters(parserContext, 0, sqlQuery.getLength()));
        if (!scriptContext.fillQueryParameters(sqlQuery, CommonUtils.isBitSet(flags, DBSDataContainer.FLAG_REFRESH))) {
            // User canceled
            return statistics;
        }
    }
    final SQLQueryResult curResult = new SQLQueryResult(sqlQuery);
    if (firstRow > 0) {
        curResult.setRowOffset(firstRow);
    }
    statistics.setQueryText(sqlQuery.getText());
    long startTime = System.currentTimeMillis();
    try (final DBCStatement dbcStatement = DBUtils.makeStatement(source, session, DBCStatementType.SCRIPT, sqlQuery, firstRow, maxRows)) {
        DBExecUtils.setStatementFetchSize(dbcStatement, firstRow, maxRows, fetchSize);
        // Execute statement
        session.getProgressMonitor().subTask("Execute query");
        boolean hasResultSet = dbcStatement.executeStatement();
        statistics.addExecuteTime(System.currentTimeMillis() - startTime);
        statistics.addStatementsCount();
        curResult.setHasResultSet(hasResultSet);
        if (hasResultSet) {
            DBCResultSet resultSet = dbcStatement.openResultSet();
            if (resultSet != null) {
                SQLQueryResult.ExecuteResult executeResult = curResult.addExecuteResult(true);
                DBRProgressMonitor monitor = session.getProgressMonitor();
                monitor.subTask("Fetch result set");
                DBFetchProgress fetchProgress = new DBFetchProgress(session.getProgressMonitor());
                dataReceiver.fetchStart(session, resultSet, firstRow, maxRows);
                try {
                    long fetchStartTime = System.currentTimeMillis();
                    // Fetch all rows
                    while (!fetchProgress.isMaxRowsFetched(maxRows) && !fetchProgress.isCanceled() && resultSet.nextRow()) {
                        dataReceiver.fetchRow(session, resultSet);
                        fetchProgress.monitorRowFetch();
                    }
                    statistics.addFetchTime(System.currentTimeMillis() - fetchStartTime);
                } finally {
                    try {
                        resultSet.close();
                    } catch (Throwable e) {
                        log.error("Error while closing resultset", e);
                    }
                    try {
                        dataReceiver.fetchEnd(session, resultSet);
                    } catch (Throwable e) {
                        log.error("Error while handling end of result set fetch", e);
                    }
                    dataReceiver.close();
                }
                if (executeResult != null) {
                    executeResult.setRowCount(fetchProgress.getRowCount());
                }
                statistics.setRowsFetched(fetchProgress.getRowCount());
                monitor.subTask(fetchProgress.getRowCount() + " rows fetched");
            }
        } else {
            log.warn("No results returned by query execution");
        }
        try {
            curResult.addWarnings(dbcStatement.getStatementWarnings());
        } catch (Throwable e) {
            log.warn("Can't read execution warnings", e);
        }
    }
    return statistics;
}
Also used : SQLRuleManager(org.jkiss.dbeaver.model.sql.parser.SQLRuleManager) Document(org.eclipse.jface.text.Document) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) SQLParserContext(org.jkiss.dbeaver.model.sql.parser.SQLParserContext) NotNull(org.jkiss.code.NotNull)

Example 4 with SQLParserContext

use of org.jkiss.dbeaver.model.sql.parser.SQLParserContext in project dbeaver by dbeaver.

the class SQLEditorBase method reloadSyntaxRules.

public void reloadSyntaxRules() {
    // Refresh syntax
    SQLDialect dialect = getSQLDialect();
    IDocument document = getDocument();
    syntaxManager.init(dialect, getActivePreferenceStore());
    SQLRuleManager ruleManager = new SQLRuleManager(syntaxManager);
    ruleManager.loadRules(getDataSource(), SQLEditorBase.isBigScript(getEditorInput()));
    ruleScanner.refreshRules(getDataSource(), ruleManager);
    parserContext = new SQLParserContext(SQLEditorBase.this, syntaxManager, ruleManager, document != null ? document : new Document());
    if (document instanceof IDocumentExtension3) {
        IDocumentPartitioner partitioner = new FastPartitioner(new SQLPartitionScanner(getDataSource(), dialect), SQLParserPartitions.SQL_CONTENT_TYPES);
        partitioner.connect(document);
        try {
            ((IDocumentExtension3) document).setDocumentPartitioner(SQLParserPartitions.SQL_PARTITIONING, partitioner);
        } catch (Throwable e) {
            // $NON-NLS-1$
            log.warn("Error setting SQL partitioner", e);
        }
        ProjectionViewer projectionViewer = (ProjectionViewer) getSourceViewer();
        if (projectionViewer != null && projectionViewer.getAnnotationModel() != null && document.getLength() > 0) {
            // projectionViewer.getTextWidget().redraw();
            try {
                projectionViewer.reinitializeProjection();
            } catch (Throwable ex) {
                // We can catch OutOfMemory here for too big/complex documents
                // $NON-NLS-1$
                log.warn("Can't initialize SQL syntax projection", ex);
            }
        }
    }
    final IVerticalRuler verticalRuler = getVerticalRuler();
    // Update configuration
    if (getSourceViewerConfiguration() instanceof SQLEditorSourceViewerConfiguration) {
        ((SQLEditorSourceViewerConfiguration) getSourceViewerConfiguration()).onDataSourceChange();
    }
    if (verticalRuler != null) {
        verticalRuler.update();
    }
}
Also used : SQLRuleManager(org.jkiss.dbeaver.model.sql.parser.SQLRuleManager) BasicSQLDialect(org.jkiss.dbeaver.model.impl.sql.BasicSQLDialect) FastPartitioner(org.eclipse.jface.text.rules.FastPartitioner) SQLPartitionScanner(org.jkiss.dbeaver.ui.editors.sql.syntax.SQLPartitionScanner) ProjectionViewer(org.eclipse.jface.text.source.projection.ProjectionViewer) SQLParserContext(org.jkiss.dbeaver.model.sql.parser.SQLParserContext)

Aggregations

SQLParserContext (org.jkiss.dbeaver.model.sql.parser.SQLParserContext)4 SQLRuleManager (org.jkiss.dbeaver.model.sql.parser.SQLRuleManager)4 Document (org.eclipse.jface.text.Document)2 FastPartitioner (org.eclipse.jface.text.rules.FastPartitioner)2 ProjectionViewer (org.eclipse.jface.text.source.projection.ProjectionViewer)2 NotNull (org.jkiss.code.NotNull)2 BasicSQLDialect (org.jkiss.dbeaver.model.impl.sql.BasicSQLDialect)2 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)2 SQLPartitionScanner (org.jkiss.dbeaver.ui.editors.sql.syntax.SQLPartitionScanner)2