const $ = require('jquery'); import { now } from '../../modules/dates'; import React from 'react'; import { Button, ButtonGroup, Col, FormGroup, InputGroup, Form, FormControl, DropdownButton, Label, MenuItem, OverlayTrigger, Row, Tooltip, } from 'react-bootstrap'; import AceEditor from 'react-ace'; import 'brace/mode/sql'; import 'brace/theme/github'; import 'brace/ext/language_tools'; import { bindActionCreators } from 'redux'; import { connect } from 'react-redux'; import * as Actions from '../actions'; import shortid from 'shortid'; import ButtonWithTooltip from './ButtonWithTooltip'; import SouthPane from './SouthPane'; import Timer from './Timer'; import SqlEditorLeft from './SqlEditorLeft'; class SqlEditor extends React.Component { constructor(props) { super(props); this.state = { autorun: props.queryEditor.autorun, sql: props.queryEditor.sql, ctas: '', }; } componentDidMount() { this.onMount(); } onMount() { if (this.state.autorun) { this.setState({ autorun: false }); this.props.actions.queryEditorSetAutorun(this.props.queryEditor, false); this.startQuery(); } } runQuery() { this.startQuery(); } startQuery(runAsync = false, ctas = false) { const that = this; const query = { dbId: this.props.queryEditor.dbId, id: shortid.generate(), progress: 0, sql: this.props.queryEditor.sql, sqlEditorId: this.props.queryEditor.id, startDttm: now(), state: 'running', tab: this.props.queryEditor.title, }; if (runAsync) { query.state = 'pending'; } // Execute the Query that.props.actions.startQuery(query); const sqlJsonUrl = '/caravel/sql_json/'; const sqlJsonRequest = { async: runAsync, client_id: query.id, database_id: this.props.queryEditor.dbId, json: true, schema: this.props.queryEditor.schema, select_as_cta: ctas, sql: this.props.queryEditor.sql, sql_editor_id: this.props.queryEditor.id, tab: this.props.queryEditor.title, tmp_table_name: this.state.ctas, }; $.ajax({ type: 'POST', dataType: 'json', url: sqlJsonUrl, data: sqlJsonRequest, success(results) { if (!runAsync) { that.props.actions.querySuccess(query, results); } }, error(err) { let msg; try { msg = err.responseJSON.error; } catch (e) { msg = (err.responseText) ? err.responseText : e; } if (typeof(msg) !== 'string') { msg = JSON.stringify(msg); } that.props.actions.queryFailed(query, msg); }, }); } stopQuery() { this.props.actions.stopQuery(this.props.latestQuery); } createTableAs() { this.startQuery(true, true); } textChange(text) { this.setState({ sql: text }); this.props.actions.queryEditorSetSql(this.props.queryEditor, text); } addWorkspaceQuery() { this.props.actions.addWorkspaceQuery({ id: shortid.generate(), sql: this.state.sql, dbId: this.props.queryEditor.dbId, schema: this.props.queryEditor.schema, title: this.props.queryEditor.title, }); } ctasChange() {} visualize() {} ctasChanged(event) { this.setState({ ctas: event.target.value }); } render() { let runButtons = ( ); if (this.props.latestQuery && this.props.latestQuery.state === 'running') { runButtons = ( ); } const rightButtons = (   } > export to .csv export to .json ); let limitWarning = null; const rowLimit = 1000; if (this.props.latestQuery && this.props.latestQuery.rows === rowLimit) { const tooltip = ( It appears that the number of rows in the query results displayed was limited on the server side to the {rowLimit} limit. ); limitWarning = ( ); } let ctasControls; if (this.props.database && this.props.database.allow_ctas) { ctasControls = ( ); } const editorBottomBar = (
{runButtons} {ctasControls}
{limitWarning} {rightButtons}
); return (
{editorBottomBar}
); } } SqlEditor.propTypes = { actions: React.PropTypes.object, database: React.PropTypes.object, latestQuery: React.PropTypes.object, queryEditor: React.PropTypes.object, }; SqlEditor.defaultProps = { }; function mapStateToProps() { return {}; } function mapDispatchToProps(dispatch) { return { actions: bindActionCreators(Actions, dispatch), }; } export default connect(mapStateToProps, mapDispatchToProps)(SqlEditor);