Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow execution of highlighted subquery #3288

Merged
merged 9 commits into from
Jan 20, 2019
Merged
14 changes: 13 additions & 1 deletion client/app/components/QueryEditor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class QueryEditor extends React.Component {
queryExecuting: PropTypes.bool.isRequired,
saveQuery: PropTypes.func.isRequired,
updateQuery: PropTypes.func.isRequired,
updateHighlightedQuery: PropTypes.func.isRequired,
listenForResize: PropTypes.func.isRequired,
listenForEditorCommand: PropTypes.func.isRequired,
};
Expand All @@ -75,6 +76,7 @@ class QueryEditor extends React.Component {
liveAutocompleteDisabled: false,
// XXX temporary while interfacing with angular
queryText: props.queryText,
highlightedQueryText: null,
};

const schemaCompleter = {
Expand Down Expand Up @@ -129,6 +131,7 @@ class QueryEditor extends React.Component {

onLoad = (editor) => {
// Release Cmd/Ctrl+L to the browser
this.editor = editor;
chang marked this conversation as resolved.
Show resolved Hide resolved
editor.commands.bindKey('Cmd+L', null);
editor.commands.bindKey('Ctrl+P', null);
editor.commands.bindKey('Ctrl+L', null);
Expand Down Expand Up @@ -181,6 +184,14 @@ class QueryEditor extends React.Component {
});
};

updateSelectedQuery = (selection) => {
const doc = this.editor.getSession().doc;
const rawHighlightedQueryText = doc.getTextRange(selection.getRange());
const highlightedQueryText = (rawHighlightedQueryText.length > 1) ? rawHighlightedQueryText : null;
this.setState({ highlightedQueryText });
this.props.updateHighlightedQuery(highlightedQueryText);
}

updateQuery = (queryText) => {
this.props.updateQuery(queryText);
this.setState({ queryText });
Expand Down Expand Up @@ -229,6 +240,7 @@ class QueryEditor extends React.Component {
onLoad={this.onLoad}
onPaste={this.onPaste}
onChange={this.updateQuery}
onSelectionChange={this.updateSelectedQuery}
/>
</div>

Expand Down Expand Up @@ -292,7 +304,7 @@ class QueryEditor extends React.Component {
data-test="ExecuteButton"
>
<span className="zmdi zmdi-play" />
<span className="hidden-xs m-l-5">Execute</span>
<span className="hidden-xs m-l-5">{ (this.state.highlightedQueryText == null) ? 'Execute' : 'Execute Selected' }</span>
</button>
</Tooltip>
</div>
Expand Down
1 change: 1 addition & 0 deletions client/app/pages/queries/query.html
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ <h3>
listen-for-editor-command="listenForEditorCommand"
save-query="saveQuery"
update-query="updateQuery"
update-highlighted-query="updateHighlightedQuery"
add-new-parameter="addNewParameter"
data-data-source="dataSource"
data-data-sources="dataSources"></query-editor>
Expand Down
17 changes: 12 additions & 5 deletions client/app/pages/queries/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function QueryViewCtrl(
DataSource,
Visualization,
) {
function getQueryResult(maxAge) {
function getQueryResult(maxAge, highlightedQueryText) {
if (maxAge === undefined) {
maxAge = $location.search().maxAge;
}
Expand All @@ -36,7 +36,7 @@ function QueryViewCtrl(
}

$scope.showLog = false;
$scope.queryResult = $scope.query.getQueryResult(maxAge);
$scope.queryResult = $scope.query.getQueryResult(maxAge, highlightedQueryText);
}

function getDataSourceId() {
Expand Down Expand Up @@ -105,6 +105,10 @@ function QueryViewCtrl(
getSchema();
}

$scope.updateHighlightedQuery = (highlightedQueryText) => {
$scope.highlightedQueryText = highlightedQueryText;
};

$scope.executeQuery = () => {
if (!$scope.canExecuteQuery()) {
return;
Expand All @@ -114,7 +118,7 @@ function QueryViewCtrl(
return;
}

getQueryResult(0);
getQueryResult(0, $scope.highlightedQueryText);
$scope.lockButton(true);
$scope.cancelling = false;
Events.record('execute', 'query', $scope.query.id);
Expand Down Expand Up @@ -372,8 +376,11 @@ function QueryViewCtrl(
}

if (status === 'done') {
$scope.query.latest_query_data_id = $scope.queryResult.getId();
$scope.query.queryResult = $scope.queryResult;
const ranSelectedQuery = $scope.query.query !== $scope.queryResult.query;
if (!ranSelectedQuery) {
$scope.query.latest_query_data_id = $scope.queryResult.getId();
$scope.query.queryResult = $scope.queryResult;
}

Notifications.showNotification('Redash', `${$scope.query.name} updated.`);
} else if (status === 'failed') {
Expand Down
4 changes: 2 additions & 2 deletions client/app/services/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -431,12 +431,12 @@ function QueryResource(
return this.getParameters().isRequired();
};

Query.prototype.getQueryResult = function getQueryResult(maxAge) {
Query.prototype.getQueryResult = function getQueryResult(maxAge, highlightedQueryText) {
if (!this.query) {
return new QueryResultError("Can't execute empty query.");
}
const queryText = this.query;

const queryText = (highlightedQueryText == null) ? this.query : highlightedQueryText;
chang marked this conversation as resolved.
Show resolved Hide resolved
const parameters = this.getParameters();
const missingParams = parameters.getMissing();

Expand Down