use of org.jkiss.dbeaver.model.sql.parser.SQLRuleManager 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;
}
use of org.jkiss.dbeaver.model.sql.parser.SQLRuleManager 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;
}
use of org.jkiss.dbeaver.model.sql.parser.SQLRuleManager 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();
}
}
use of org.jkiss.dbeaver.model.sql.parser.SQLRuleManager 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();
}
}
Aggregations