-
Notifications
You must be signed in to change notification settings - Fork 13.9k
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
[sql lab] deeper support for templating #3996
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
superset/assets/javascripts/SqlLab/components/TemplateParamsEditor.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { Badge } from 'react-bootstrap'; | ||
|
||
import AceEditor from 'react-ace'; | ||
import 'brace/mode/sql'; | ||
import 'brace/mode/json'; | ||
import 'brace/mode/html'; | ||
import 'brace/mode/markdown'; | ||
import 'brace/theme/textmate'; | ||
|
||
import ModalTrigger from '../../components/ModalTrigger'; | ||
import InfoTooltipWithTrigger from '../../components/InfoTooltipWithTrigger'; | ||
import Button from '../../components/Button'; | ||
import { t } from '../../locales'; | ||
|
||
const propTypes = { | ||
onChange: PropTypes.func, | ||
code: PropTypes.string, | ||
language: PropTypes.oneOf(['yaml', 'json']), | ||
}; | ||
|
||
const defaultProps = { | ||
label: null, | ||
description: null, | ||
onChange: () => {}, | ||
code: '{}', | ||
}; | ||
|
||
export default class TemplateParamsEditor extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
const codeText = props.code || '{}'; | ||
this.state = { | ||
codeText, | ||
parsedJSON: null, | ||
isValid: true, | ||
}; | ||
this.onChange = this.onChange.bind(this); | ||
} | ||
componentDidMount() { | ||
this.onChange(this.state.codeText); | ||
} | ||
onChange(value) { | ||
const codeText = value; | ||
let isValid; | ||
let parsedJSON = {}; | ||
try { | ||
parsedJSON = JSON.parse(value); | ||
isValid = true; | ||
} catch (e) { | ||
isValid = false; | ||
} | ||
this.setState({ parsedJSON, isValid, codeText }); | ||
if (isValid) { | ||
this.props.onChange(codeText); | ||
} else { | ||
this.props.onChange('{}'); | ||
} | ||
} | ||
renderDoc() { | ||
return ( | ||
<p> | ||
Assign a set of parameters as <code>JSON</code> bellow | ||
(example: <code>{'{"my_table": "foo"}'}</code>), | ||
and they become available | ||
in your SQL (example: <code>SELECT * FROM {'{{ my_table }}'} </code>) | ||
by using | ||
<a | ||
href="http://superset.apache.org/sqllab.html#templating-with-jinja" | ||
target="_blank" | ||
rel="noopener noreferrer" | ||
> | ||
Jinja templating | ||
</a> syntax. | ||
</p> | ||
); | ||
} | ||
renderModalBody() { | ||
return ( | ||
<div> | ||
{this.renderDoc()} | ||
<AceEditor | ||
mode={this.props.language} | ||
theme="textmate" | ||
style={{ border: '1px solid #CCC' }} | ||
minLines={25} | ||
maxLines={50} | ||
onChange={this.onChange} | ||
width="100%" | ||
editorProps={{ $blockScrolling: true }} | ||
enableLiveAutocompletion | ||
value={this.state.codeText} | ||
/> | ||
</div> | ||
); | ||
} | ||
render() { | ||
const paramCount = this.state.parsedJSON ? Object.keys(this.state.parsedJSON).length : 0; | ||
return ( | ||
<ModalTrigger | ||
modalTitle={t('Template Parameters')} | ||
triggerNode={ | ||
<Button | ||
className="m-r-5" | ||
tooltip={t('Edit template parameters')} | ||
> | ||
{`${t('parameters')} `} | ||
{paramCount > 0 && | ||
<Badge>{paramCount}</Badge> | ||
} | ||
{!this.state.isValid && | ||
<InfoTooltipWithTrigger | ||
icon="exclamation-triangle" | ||
bsStyle="danger" | ||
tooltip={t('Invalid JSON')} | ||
label="invalid-json" | ||
/> | ||
} | ||
</Button> | ||
} | ||
modalBody={this.renderModalBody(true)} | ||
/> | ||
); | ||
} | ||
} | ||
|
||
TemplateParamsEditor.propTypes = propTypes; | ||
TemplateParamsEditor.defaultProps = defaultProps; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2161,6 +2161,8 @@ def sql_json(self): | |
sql = request.form.get('sql') | ||
database_id = request.form.get('database_id') | ||
schema = request.form.get('schema') or None | ||
template_params = json.loads( | ||
request.form.get('templateParams') or '{}') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
template_params = json.loads(request.form.get('templateParams', '{}')) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The intent of the |
||
|
||
session = db.session() | ||
mydb = session.query(models.Database).filter_by(id=database_id).first() | ||
|
@@ -2212,7 +2214,9 @@ def sql_json(self): | |
try: | ||
sql_lab.get_sql_results.delay( | ||
query_id=query_id, return_results=False, | ||
store_results=not query.select_as_cta, user_name=g.user.username) | ||
store_results=not query.select_as_cta, | ||
user_name=g.user.username, | ||
template_params=template_params) | ||
except Exception as e: | ||
logging.exception(e) | ||
msg = ( | ||
|
@@ -2241,7 +2245,8 @@ def sql_json(self): | |
error_message=timeout_msg): | ||
# pylint: disable=no-value-for-parameter | ||
data = sql_lab.get_sql_results( | ||
query_id=query_id, return_results=True) | ||
query_id=query_id, return_results=True, | ||
template_params=template_params) | ||
except Exception as e: | ||
logging.exception(e) | ||
return json_error_response('{}'.format(e)) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/bellow/below/g