forked from apache/superset
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[sqllab] add support for Jinja templating (apache#1426)
* [sqllab] add support for Jinja templating * Adressing comments * Presto macros * Progress * Addressing coments
- Loading branch information
1 parent
8c5e495
commit 5944643
Showing
20 changed files
with
444 additions
and
65 deletions.
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
109 changes: 79 additions & 30 deletions
109
caravel/assets/javascripts/SqlLab/components/HighlightedSql.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 |
---|---|---|
@@ -1,44 +1,93 @@ | ||
import React from 'react'; | ||
import { Well } from 'react-bootstrap'; | ||
import SyntaxHighlighter from 'react-syntax-highlighter'; | ||
import { github } from 'react-syntax-highlighter/dist/styles'; | ||
import ModalTrigger from '../../components/ModalTrigger'; | ||
|
||
const HighlightedSql = (props) => { | ||
const sql = props.sql || ''; | ||
let lines = sql.split('\n'); | ||
if (lines.length >= props.maxLines) { | ||
lines = lines.slice(0, props.maxLines); | ||
lines.push('{...}'); | ||
const defaultProps = { | ||
maxWidth: 50, | ||
maxLines: 5, | ||
shrink: false, | ||
}; | ||
|
||
const propTypes = { | ||
sql: React.PropTypes.string.isRequired, | ||
rawSql: React.PropTypes.string, | ||
maxWidth: React.PropTypes.number, | ||
maxLines: React.PropTypes.number, | ||
shrink: React.PropTypes.bool, | ||
}; | ||
|
||
class HighlightedSql extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
this.state = { | ||
modalBody: null, | ||
}; | ||
} | ||
let shownSql = sql; | ||
if (props.shrink) { | ||
shownSql = lines.map((line) => { | ||
shrinkSql() { | ||
const props = this.props; | ||
const sql = props.sql || ''; | ||
let lines = sql.split('\n'); | ||
if (lines.length >= props.maxLines) { | ||
lines = lines.slice(0, props.maxLines); | ||
lines.push('{...}'); | ||
} | ||
return lines.map((line) => { | ||
if (line.length > props.maxWidth) { | ||
return line.slice(0, props.maxWidth) + '{...}'; | ||
} | ||
return line; | ||
}) | ||
.join('\n'); | ||
} | ||
return ( | ||
<div> | ||
<SyntaxHighlighter language="sql" style={github}> | ||
{shownSql} | ||
</SyntaxHighlighter> | ||
</div> | ||
); | ||
}; | ||
|
||
HighlightedSql.defaultProps = { | ||
maxWidth: 60, | ||
maxLines: 6, | ||
shrink: false, | ||
}; | ||
|
||
HighlightedSql.propTypes = { | ||
sql: React.PropTypes.string, | ||
maxWidth: React.PropTypes.number, | ||
maxLines: React.PropTypes.number, | ||
shrink: React.PropTypes.bool, | ||
}; | ||
triggerNode() { | ||
const props = this.props; | ||
let shownSql = props.shrink ? this.shrinkSql(props.sql) : props.sql; | ||
return ( | ||
<Well> | ||
<SyntaxHighlighter language="sql" style={github}> | ||
{shownSql} | ||
</SyntaxHighlighter> | ||
</Well>); | ||
} | ||
generateModal() { | ||
const props = this.props; | ||
let rawSql; | ||
if (props.rawSql && props.rawSql !== this.props.sql) { | ||
rawSql = ( | ||
<div> | ||
<h4>Raw SQL</h4> | ||
<SyntaxHighlighter language="sql" style={github}> | ||
{props.rawSql} | ||
</SyntaxHighlighter> | ||
</div> | ||
); | ||
} | ||
this.setState({ | ||
modalBody: ( | ||
<div> | ||
<h4>Source SQL</h4> | ||
<SyntaxHighlighter language="sql" style={github}> | ||
{this.props.sql} | ||
</SyntaxHighlighter> | ||
{rawSql} | ||
</div> | ||
), | ||
}); | ||
} | ||
render() { | ||
return ( | ||
<ModalTrigger | ||
modalTitle="SQL" | ||
triggerNode={this.triggerNode()} | ||
modalBody={this.state.modalBody} | ||
beforeOpen={this.generateModal.bind(this)} | ||
/> | ||
); | ||
} | ||
} | ||
HighlightedSql.propTypes = propTypes; | ||
HighlightedSql.defaultProps = defaultProps; | ||
|
||
export default HighlightedSql; |
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 |
---|---|---|
|
@@ -254,3 +254,7 @@ div.tablePopover:hover { | |
a.Link { | ||
cursor: pointer; | ||
} | ||
.QueryTable .well { | ||
padding: 3px 5px; | ||
margin: 3px 5px; | ||
} |
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
23 changes: 15 additions & 8 deletions
23
caravel/assets/spec/javascripts/sqllab/HighlightedSql_spec.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 |
---|---|---|
@@ -1,26 +1,33 @@ | ||
import React from 'react'; | ||
import HighlightedSql from '../../../javascripts/SqlLab/components/HighlightedSql'; | ||
import ModalTrigger from '../../../javascripts/components/ModalTrigger'; | ||
import SyntaxHighlighter from 'react-syntax-highlighter'; | ||
import { shallow } from 'enzyme'; | ||
import { mount, shallow } from 'enzyme'; | ||
import { describe, it } from 'mocha'; | ||
import { expect } from 'chai'; | ||
|
||
|
||
describe('HighlightedSql', () => { | ||
const sql = "SELECT * FROM test WHERE something='fkldasjfklajdslfkjadlskfjkldasjfkladsjfkdjsa'"; | ||
it('renders', () => { | ||
expect(React.isValidElement(<HighlightedSql />)).to.equal(true); | ||
}); | ||
it('renders with props', () => { | ||
expect(React.isValidElement(<HighlightedSql sql={sql} />)) | ||
.to.equal(true); | ||
}); | ||
it('renders a SyntaxHighlighter', () => { | ||
it('renders a ModalTrigger', () => { | ||
const wrapper = shallow(<HighlightedSql sql={sql} />); | ||
expect(wrapper.find(SyntaxHighlighter)).to.have.length(1); | ||
expect(wrapper.find(ModalTrigger)).to.have.length(1); | ||
}); | ||
it('renders a SyntaxHighlighter while using shrink', () => { | ||
it('renders a ModalTrigger while using shrink', () => { | ||
const wrapper = shallow(<HighlightedSql sql={sql} shrink maxWidth={20} />); | ||
expect(wrapper.find(SyntaxHighlighter)).to.have.length(1); | ||
expect(wrapper.find(ModalTrigger)).to.have.length(1); | ||
}); | ||
it('renders two SyntaxHighlighter in modal', () => { | ||
const wrapper = mount( | ||
<HighlightedSql sql={sql} rawSql="SELECT * FORM foo" shrink maxWidth={5} />); | ||
const well = wrapper.find('.well'); | ||
expect(well).to.have.length(1); | ||
well.simulate('click'); | ||
const modalBody = mount(wrapper.state().modalBody); | ||
expect(modalBody.find(SyntaxHighlighter)).to.have.length(2); | ||
}); | ||
}); |
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
Oops, something went wrong.