const $ = window.$ = require('jquery'); import React from 'react'; import { bindActionCreators } from 'redux'; import { connect } from 'react-redux'; import * as Actions from '../actions'; import shortid from 'shortid'; import Select from 'react-select'; import { Label, Button } from 'react-bootstrap'; import TableElement from './TableElement'; class SqlEditorTopToolbar extends React.Component { constructor(props) { super(props); this.state = { databaseLoading: false, databaseOptions: [], schemaLoading: false, schemaOptions: [], tableLoading: false, tableOptions: [], }; } componentWillMount() { this.fetchDatabaseOptions(); this.fetchSchemas(); this.fetchTables(); } resetState() { this.props.actions.resetState(); } fetchTables(dbId, schema) { const actualDbId = dbId || this.props.queryEditor.dbId; if (actualDbId) { const actualSchema = schema || this.props.queryEditor.schema; this.setState({ tableLoading: true }); this.setState({ tableOptions: [] }); const url = `/caravel/tables/${actualDbId}/${actualSchema}`; $.get(url, (data) => { let tableOptions = data.tables.map((s) => ({ value: s, label: s })); const views = data.views.map((s) => ({ value: s, label: '[view] ' + s })); tableOptions = [...tableOptions, ...views]; this.setState({ tableOptions }); this.setState({ tableLoading: false }); }); } } changeSchema(schemaOpt) { const schema = (schemaOpt) ? schemaOpt.value : null; this.props.actions.queryEditorSetSchema(this.props.queryEditor, schema); this.fetchTables(this.props.queryEditor.dbId, schema); } fetchSchemas(dbId) { const actualDbId = dbId || this.props.queryEditor.dbId; if (actualDbId) { this.setState({ schemaLoading: true }); const url = `/databasetablesasync/api/read?_flt_0_id=${actualDbId}`; $.get(url, (data) => { const schemas = data.result[0].all_schema_names; const schemaOptions = schemas.map((s) => ({ value: s, label: s })); this.setState({ schemaOptions }); this.setState({ schemaLoading: false }); }); } } changeDb(db) { const val = (db) ? db.value : null; this.setState({ schemaOptions: [] }); this.props.actions.queryEditorSetDb(this.props.queryEditor, val); if (!(db)) { this.setState({ tableOptions: [] }); return; } this.fetchTables(val, this.props.queryEditor.schema); this.fetchSchemas(val); } fetchDatabaseOptions() { this.setState({ databaseLoading: true }); const url = '/databaseasync/api/read?_flt_0_expose_in_sqllab=1'; $.get(url, (data) => { const options = data.result.map((db) => ({ value: db.id, label: db.database_name })); this.props.actions.setDatabases(data.result); this.setState({ databaseOptions: options }); this.setState({ databaseLoading: false }); // Auto select if only one option if (options.length === 1) { this.changeDb(options[0]); } }); } closePopover(ref) { this.refs[ref].hide(); } changeTable(tableOpt) { const tableName = tableOpt.value; const qe = this.props.queryEditor; const url = `/caravel/table/${qe.dbId}/${tableName}/${qe.schema}/`; $.get(url, (data) => { this.props.actions.addTable({ id: shortid.generate(), dbId: this.props.queryEditor.dbId, queryEditorId: this.props.queryEditor.id, name: data.name, schema: qe.schema, columns: data.columns, expanded: true, }); }) .fail(() => { this.props.actions.addAlert({ msg: 'Error occurred while fetching metadata', bsStyle: 'danger', }); }); } render() { let networkAlert = null; if (!this.props.networkOn) { networkAlert =
; } const tables = this.props.tables.filter((t) => (t.queryEditorId === this.props.queryEditor.id)); const shouldShowReset = window.location.search === '?reset=1'; return (