1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157 | 1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
68×
1×
1×
1×
1×
1×
1×
3×
3×
3×
1×
3×
3×
1×
3×
3×
1×
1×
1×
3×
1×
3×
9×
3×
3×
1×
1×
1×
3×
3×
3×
3×
3×
3×
3×
3×
3×
1×
6×
1×
1×
1×
| import React from 'react';
import PropTypes from 'prop-types';
import AceEditor from 'react-ace';
import 'brace/mode/sql';
import 'brace/theme/github';
import 'brace/ext/language_tools';
import ace from 'brace';
import { areArraysShallowEqual } from '../../reduxUtils';
const langTools = ace.acequire('ace/ext/language_tools');
const keywords = (
'SELECT|INSERT|UPDATE|DELETE|FROM|WHERE|AND|OR|GROUP|BY|ORDER|LIMIT|OFFSET|HAVING|AS|CASE|' +
'WHEN|ELSE|END|TYPE|LEFT|RIGHT|JOIN|ON|OUTER|DESC|ASC|UNION|CREATE|TABLE|PRIMARY|KEY|IF|' +
'FOREIGN|NOT|REFERENCES|DEFAULT|NULL|INNER|CROSS|NATURAL|DATABASE|DROP|GRANT|SUM|MAX|MIN|COUNT|' +
'AVG|DISTINCT'
);
const dataTypes = (
'INT|NUMERIC|DECIMAL|DATE|VARCHAR|CHAR|BIGINT|FLOAT|DOUBLE|BIT|BINARY|TEXT|SET|TIMESTAMP|' +
'MONEY|REAL|NUMBER|INTEGER'
);
const sqlKeywords = [].concat(keywords.split('|'), dataTypes.split('|'));
export const sqlWords = sqlKeywords.map(s => ({
name: s, value: s, score: 60, meta: 'sql',
}));
const propTypes = {
actions: PropTypes.object.isRequired,
onBlur: PropTypes.func,
sql: PropTypes.string.isRequired,
tables: PropTypes.array,
queryEditor: PropTypes.object.isRequired,
height: PropTypes.string,
hotkeys: PropTypes.arrayOf(PropTypes.shape({
key: PropTypes.string.isRequired,
descr: PropTypes.string.isRequired,
func: PropTypes.func.isRequired,
})),
onChange: PropTypes.func,
};
const defaultProps = {
onBlur: () => {},
onChange: () => {},
tables: [],
};
class AceEditorWrapper extends React.PureComponent {
constructor(props) {
super(props);
this.state = {
sql: props.sql,
selectedText: '',
};
this.onChange = this.onChange.bind(this);
}
componentDidMount() {
// Making sure no text is selected from previous mount
this.props.actions.queryEditorSetSelectedText(this.props.queryEditor, null);
this.setAutoCompleter(this.props);
}
componentWillReceiveProps(nextProps) {
if (I!areArraysShallowEqual(this.props.tables, nextProps.tables)) {
this.setAutoCompleter(nextProps);
}
if (InextProps.sql !== this.props.sql) {
this.setState({ sql: nextProps.sql });
}
}
onBlur() {
this.props.onBlur(this.state.sql);
}
onAltEnter() {
this.props.onBlur(this.state.sql);
}
onEditorLoad(editor) {
editor.commands.addCommand({
name: 'runQuery',
bindKey: { win: 'Alt-enter', mac: 'Alt-enter' },
exec: () => {
this.onAltEnter();
},
});
this.props.hotkeys.forEach((keyConfig) => {
editor.commands.addCommand({
name: keyConfig.name,
bindKey: { win: keyConfig.key, mac: keyConfig.key },
exec: keyConfig.func,
});
});
editor.$blockScrolling = Infinity; // eslint-disable-line no-param-reassign
editor.selection.on('changeSelection', () => {
const selectedText = editor.getSelectedText();
// Backspace trigger 1 character selection, ignoring
if (selectedText !== this.state.selectedText && selectedText.length !== 1) {
this.setState({ selectedText });
this.props.actions.queryEditorSetSelectedText(
this.props.queryEditor, selectedText);
}
});
}
onChange(text) {
this.setState({ sql: text });
this.props.onChange(text);
}
getCompletions(aceEditor, session, pos, prefix, callback) {
callback(null, this.state.words);
}
setAutoCompleter(props) {
// Loading table and column names as auto-completable words
let words = [];
const columns = {};
const tables = props.tables || [];
tables.forEach((t) => {
words.push({ name: t.name, value: t.name, score: 55, meta: 'table' });
const cols = t.columns || [];
cols.forEach((col) => {
columns[col.name] = null; // using an object as a unique set
});
});
words = words.concat(Object.keys(columns).map(col => (
{ name: col, value: col, score: 50, meta: 'column' }
)), sqlWords);
this.setState({ words }, () => {
const completer = {
getCompletions: this.getCompletions.bind(this),
};
if (ElangTools) {
langTools.setCompleters([completer]);
}
});
}
render() {
return (
<AceEditor
mode="sql"
theme="github"
onLoad={this.onEditorLoad.bind(this)}
onBlur={this.onBlur.bind(this)}
height={this.props.height}
onChange={this.onChange}
width="100%"
editorProps={{ $blockScrolling: true }}
enableLiveAutocompletion
value={this.state.sql}
/>
);
}
}
AceEditorWrapper.defaultProps = defaultProps;
AceEditorWrapper.propTypes = propTypes;
export default AceEditorWrapper;
|