import React from 'react'; import PropTypes from 'prop-types'; import throttle from 'lodash.throttle'; import { Col, FormGroup, InputGroup, Form, FormControl, Label, OverlayTrigger, Row, Tooltip, Collapse, } from 'react-bootstrap'; import SplitPane from 'react-split-pane'; import Button from '../../components/Button'; import TemplateParamsEditor from './TemplateParamsEditor'; import SouthPane from './SouthPane'; import SaveQuery from './SaveQuery'; import Timer from '../../components/Timer'; import SqlEditorLeftBar from './SqlEditorLeftBar'; import AceEditorWrapper from './AceEditorWrapper'; import { STATE_BSSTYLE_MAP } from '../constants'; import RunQueryActionButton from './RunQueryActionButton'; import { t } from '../../locales'; const propTypes = { actions: PropTypes.object.isRequired, height: PropTypes.string.isRequired, database: PropTypes.object, latestQuery: PropTypes.object, tables: PropTypes.array.isRequired, editorQueries: PropTypes.array.isRequired, dataPreviewQueries: PropTypes.array.isRequired, queryEditor: PropTypes.object.isRequired, hideLeftBar: PropTypes.bool, }; const defaultProps = { database: null, latestQuery: null, hideLeftBar: false, }; class SqlEditor extends React.PureComponent { constructor(props) { super(props); this.state = { autorun: props.queryEditor.autorun, ctas: '', }; this.onResize = this.onResize.bind(this); this.throttledResize = throttle(this.onResize, 250); } componentWillMount() { if (this.state.autorun) { this.setState({ autorun: false }); this.props.actions.queryEditorSetAutorun(this.props.queryEditor, false); this.startQuery(); } } componentDidMount() { this.onResize(); window.addEventListener('resize', this.throttledResize); } componentWillUnmount() { window.removeEventListener('resize', this.throttledResize); } onResize() { const height = this.sqlEditorHeight(); this.setState({ editorPaneHeight: this.props.queryEditor.height, southPaneHeight: height - this.props.queryEditor.height, height, }); if (this.refs.ace.clientHeight) { this.props.actions.persistEditorHeight(this.props.queryEditor, this.refs.ace.clientHeight); } } setQueryEditorSql(sql) { this.props.actions.queryEditorSetSql(this.props.queryEditor, sql); } runQuery(runAsync = false) { if (!this.props.queryEditor.sql) { return; } let effectiveRunAsync = runAsync; if (!this.props.database.allow_run_sync) { effectiveRunAsync = true; } this.startQuery(effectiveRunAsync); } startQuery(runAsync = false, ctas = false) { const qe = this.props.queryEditor; const query = { dbId: qe.dbId, sql: qe.selectedText ? qe.selectedText : qe.sql, sqlEditorId: qe.id, tab: qe.title, schema: qe.schema, tempTableName: ctas ? this.state.ctas : '', templateParams: qe.templateParams, runAsync, ctas, }; this.props.actions.runQuery(query); this.props.actions.setActiveSouthPaneTab('Results'); } stopQuery() { this.props.actions.postStopQuery(this.props.latestQuery); } createTableAs() { this.startQuery(true, true); } ctasChanged(event) { this.setState({ ctas: event.target.value }); } sqlEditorHeight() { // quick hack to make the white bg of the tab stretch full height. const tabNavHeight = 40; const navBarHeight = 56; const mysteryVerticalHeight = 50; return window.innerHeight - tabNavHeight - navBarHeight - mysteryVerticalHeight; } renderEditorBottomBar() { let ctasControls; if (this.props.database && this.props.database.allow_ctas) { const ctasToolTip = t('Create table as with query results'); ctasControls = ( ); } const qe = this.props.queryEditor; let limitWarning = null; if (this.props.latestQuery && this.props.latestQuery.limit_reached) { const tooltip = ( It appears that the number of rows in the query results displayed was limited on the server side to the {this.props.latestQuery.rows} limit. ); limitWarning = ( ); } return (
{ctasControls}
{ this.props.actions.queryEditorSetTemplateParams(qe, params); }} code={qe.templateParams} /> {limitWarning} {this.props.latestQuery && }
); } render() { const height = this.sqlEditorHeight(); const defaultNorthHeight = this.props.queryEditor.height || 200; return (
{this.renderEditorBottomBar()}
); } } SqlEditor.defaultProps = defaultProps; SqlEditor.propTypes = propTypes; export default SqlEditor;