|
12 | 12 | "use strict"; |
13 | 13 |
|
14 | 14 | var tables; |
| 15 | + var defaultTable; |
15 | 16 | var keywords; |
16 | 17 | var CONS = { |
17 | 18 | QUERY_DIV: ";", |
|
43 | 44 | } |
44 | 45 | } |
45 | 46 |
|
46 | | - function columnCompletion(result, editor) { |
| 47 | + function nameCompletion(result, editor) { |
47 | 48 | var cur = editor.getCursor(); |
48 | 49 | var token = editor.getTokenAt(cur); |
| 50 | + var useBacktick = (token.string.charAt(0) == "`"); |
49 | 51 | var string = token.string.substr(1); |
50 | | - var prevCur = Pos(cur.line, token.start); |
51 | | - var table = editor.getTokenAt(prevCur).string; |
52 | | - if (!tables.hasOwnProperty(table)) |
53 | | - table = findTableByAlias(table, editor); |
54 | | - var columns = tables[table]; |
55 | | - if (!columns) return; |
56 | | - |
57 | | - addMatches(result, string, columns, function(w) {return "." + w;}); |
| 52 | + var prevToken = editor.getTokenAt(Pos(cur.line, token.start)); |
| 53 | + if (token.string.charAt(0) == "." || prevToken.string == "."){ |
| 54 | + //Suggest colunm names |
| 55 | + if (prevToken.string == ".") { |
| 56 | + var prevToken = editor.getTokenAt(Pos(cur.line, token.start - 1)); |
| 57 | + } |
| 58 | + var table = prevToken.string; |
| 59 | + //Check if backtick is used in table name. If yes, use it for columns too. |
| 60 | + var useBacktickTable = false; |
| 61 | + if (table.match(/`/g)) { |
| 62 | + useBacktickTable = true; |
| 63 | + table = table.replace(/`/g, ""); |
| 64 | + } |
| 65 | + //Check if table is available. If not, find table by Alias |
| 66 | + if (!tables.hasOwnProperty(table)) |
| 67 | + table = findTableByAlias(table, editor); |
| 68 | + var columns = tables[table]; |
| 69 | + if (!columns) return; |
| 70 | + |
| 71 | + if (useBacktick) { |
| 72 | + addMatches(result, string, columns, function(w) {return "`" + w + "`";}); |
| 73 | + } |
| 74 | + else if(useBacktickTable) { |
| 75 | + addMatches(result, string, columns, function(w) {return ".`" + w + "`";}); |
| 76 | + } |
| 77 | + else { |
| 78 | + addMatches(result, string, columns, function(w) {return "." + w;}); |
| 79 | + } |
| 80 | + } |
| 81 | + else { |
| 82 | + //Suggest table names or colums in defaultTable |
| 83 | + while (token.start && string.charAt(0) == ".") { |
| 84 | + token = editor.getTokenAt(Pos(cur.line, token.start - 1)); |
| 85 | + string = token.string + string; |
| 86 | + } |
| 87 | + if (useBacktick) { |
| 88 | + addMatches(result, string, tables, function(w) {return "`" + w + "`";}); |
| 89 | + addMatches(result, string, defaultTable, function(w) {return "`" + w + "`";}); |
| 90 | + } |
| 91 | + else { |
| 92 | + addMatches(result, string, tables, function(w) {return w;}); |
| 93 | + addMatches(result, string, defaultTable, function(w) {return w;}); |
| 94 | + } |
| 95 | + } |
58 | 96 | } |
59 | 97 |
|
60 | 98 | function eachWord(lineText, f) { |
|
128 | 166 |
|
129 | 167 | CodeMirror.registerHelper("hint", "sql", function(editor, options) { |
130 | 168 | tables = (options && options.tables) || {}; |
| 169 | + var defaultTableName = options && options.defaultTable; |
| 170 | + defaultTable = (defaultTableName && tables[defaultTableName] || []); |
131 | 171 | keywords = keywords || getKeywords(editor); |
| 172 | + |
132 | 173 | var cur = editor.getCursor(); |
133 | 174 | var result = []; |
134 | 175 | var token = editor.getTokenAt(cur), start, end, search; |
135 | | - if (token.string.match(/^[.\w@]\w*$/)) { |
| 176 | + if (token.string.match(/^[.`\w@]\w*$/)) { |
136 | 177 | search = token.string; |
137 | 178 | start = token.start; |
138 | 179 | end = token.end; |
139 | 180 | } else { |
140 | 181 | start = end = cur.ch; |
141 | 182 | search = ""; |
142 | 183 | } |
143 | | - if (search.charAt(0) == ".") { |
144 | | - columnCompletion(result, editor); |
145 | | - if (!result.length) { |
146 | | - while (start && search.charAt(0) == ".") { |
147 | | - token = editor.getTokenAt(Pos(cur.line, token.start - 1)); |
148 | | - start = token.start; |
149 | | - search = token.string + search; |
150 | | - } |
151 | | - addMatches(result, search, tables, function(w) {return w;}); |
152 | | - } |
| 184 | + if (search.charAt(0) == "." || search.charAt(0) == "`") { |
| 185 | + nameCompletion(result, editor); |
153 | 186 | } else { |
154 | 187 | addMatches(result, search, tables, function(w) {return w;}); |
| 188 | + addMatches(result, search, defaultTable, function(w) {return w;}); |
155 | 189 | addMatches(result, search, keywords, function(w) {return w.toUpperCase();}); |
156 | 190 | } |
157 | 191 |
|
|
0 commit comments