import React from 'react'; import { connect } from 'react-redux'; import { bindActionCreators } from 'redux'; import * as Actions from '../actions'; import { ButtonGroup, Well } from 'react-bootstrap'; import shortid from 'shortid'; import CopyToClipboard from '../../components/CopyToClipboard'; import Link from './Link'; import ModalTrigger from '../../components/ModalTrigger'; const propTypes = { table: React.PropTypes.object, queryEditor: React.PropTypes.object, actions: React.PropTypes.object, }; const defaultProps = { table: null, actions: {}, }; class TableElement extends React.Component { popSelectStar() { const qe = { id: shortid.generate(), title: this.props.table.name, dbId: this.props.table.dbId, autorun: true, sql: this.props.table.selectStar, }; this.props.actions.addQueryEditor(qe); } collapseTable(e) { e.preventDefault(); this.props.actions.collapseTable(this.props.table); } expandTable(e) { e.preventDefault(); this.props.actions.expandTable(this.props.table); } removeTable() { this.props.actions.removeTable(this.props.table); } dataPreviewModal() { const query = { dbId: this.props.queryEditor.dbId, sql: this.props.table.selectStar, tableName: this.props.table.name, sqlEditorId: null, tab: '', runAsync: false, ctas: false, }; this.props.actions.runQuery(query); } render() { const table = this.props.table; let metadata = null; let buttonToggle; let header; if (table.partitions) { let partitionQuery; let partitionClipBoard; if (table.partitions.partitionQuery) { partitionQuery = table.partitions.partitionQuery; const tt = 'Copy partition query to clipboard'; partitionClipBoard = ( } /> ); } let latest = []; for (const k in table.partitions.latest) { latest.push(`${k}=${table.partitions.latest[k]}`); } latest = latest.join('/'); header = (
latest partition: {latest} {partitionClipBoard}
); } if (table.expanded) { buttonToggle = ( { this.collapseTable(e); }} > {table.name} ); metadata = (
{header}
{table.columns.map((col) => { let name = col.name; if (col.indexed) { name = {col.name}; } return (
{name}
{col.type}
); })}
); } else { buttonToggle = ( { this.expandTable(e); }} > {table.name} ); } let keyLink; if (table.indexes && table.indexes.length > 0) { keyLink = ( Keys for table {table.name} } modalBody={
{JSON.stringify(table.indexes, null, 4)}
} triggerNode={ } /> ); } return (
{buttonToggle}
{keyLink} } text={table.selectStar} shouldShowText={false} tooltipText="Copy SELECT statement to clipboard" />
{metadata}
); } } TableElement.propTypes = propTypes; TableElement.defaultProps = defaultProps; function mapDispatchToProps(dispatch) { return { actions: bindActionCreators(Actions, dispatch), }; } export default connect(null, mapDispatchToProps)(TableElement); export { TableElement };