Skip to content

Commit

Permalink
Added cache prop to ResultSet (#1552)
Browse files Browse the repository at this point in the history
* Added cache prop to ResultSet for distiguishing query result and datapreview results

* small nit

* Only cache results.data locally
  • Loading branch information
vera-liu authored Nov 7, 2016
1 parent 97ded32 commit 4014a48
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
21 changes: 15 additions & 6 deletions caravel/assets/javascripts/SqlLab/components/ResultSet.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const propTypes = {
searchText: React.PropTypes.string,
showSql: React.PropTypes.bool,
visualize: React.PropTypes.bool,
cache: React.PropTypes.bool,
};
const defaultProps = {
search: true,
Expand All @@ -22,6 +23,7 @@ const defaultProps = {
csv: true,
searchText: '',
actions: {},
cache: false,
};


Expand All @@ -31,16 +33,16 @@ class ResultSet extends React.PureComponent {
this.state = {
searchText: '',
showModal: false,
results: null,
data: [],
};
}
componentWillReceiveProps(nextProps) {
// when new results comes in, save them locally and clear in store
if ((!nextProps.query.cached)
if (this.props.cache && (!nextProps.query.cached)
&& nextProps.query.results
&& nextProps.query.results.data.length > 0) {
this.setState(
{ results: nextProps.query.results },
{ data: nextProps.query.results.data },
this.clearQueryResults(nextProps.query)
);
}
Expand Down Expand Up @@ -125,7 +127,14 @@ class ResultSet extends React.PureComponent {
}
render() {
const query = this.props.query;
const results = (this.props.query.cached) ? this.state.results : query.results;
const results = query.results;
let data;
if (this.props.cache && query.cached) {
data = this.state.data;
} else {
data = results ? results.data : [];
}

let sql;

if (this.props.showSql) {
Expand Down Expand Up @@ -165,7 +174,7 @@ class ResultSet extends React.PureComponent {
</Alert>
</div>);
} else if (query.state === 'success') {
if (results && results.data && results.data.length > 0) {
if (results && data && data.length > 0) {
return (
<div>
<VisualizeModal
Expand All @@ -177,7 +186,7 @@ class ResultSet extends React.PureComponent {
{sql}
<div className="ResultSet">
<Table
data={results.data}
data={data}
columns={results.columns.map((col) => col.name)}
sortable
className="table table-condensed table-bordered"
Expand Down
2 changes: 1 addition & 1 deletion caravel/assets/javascripts/SqlLab/components/SouthPane.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class SouthPane extends React.PureComponent {
eventKey={query.id}
key={query.id}
>
<ResultSet query={query} visualize={false} csv={false} actions={props.actions} />
<ResultSet query={query} visualize={false} csv={false} actions={props.actions} cache />
</Tab>
));

Expand Down
4 changes: 3 additions & 1 deletion caravel/assets/javascripts/SqlLab/reducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,9 @@ export const sqlLabReducer = function (state, action) {
return alterInObject(state, 'queries', action.query, { state: 'stopped' });
},
[actions.CLEAR_QUERY_RESULTS]() {
return alterInObject(state, 'queries', action.query, { results: [], cached: true });
const newResults = Object.assign({}, action.query.results);
newResults.data = [];
return alterInObject(state, 'queries', action.query, { results: newResults, cached: true });
},
[actions.REQUEST_QUERY_RESULTS]() {
return alterInObject(state, 'queries', action.query, { state: 'fetching' });
Expand Down

0 comments on commit 4014a48

Please sign in to comment.