2014-08-18 17:45:43 +04:00
|
|
|
// CodeMirror, copyright (c) by Marijn Haverbeke and others
|
|
|
|
// Distributed under an MIT license: http://codemirror.net/LICENSE
|
|
|
|
|
|
|
|
(function(mod) {
|
|
|
|
if (typeof exports == "object" && typeof module == "object") // CommonJS
|
|
|
|
mod(require("../../lib/codemirror"), require("../../mode/sql/sql"));
|
|
|
|
else if (typeof define == "function" && define.amd) // AMD
|
|
|
|
define(["../../lib/codemirror", "../../mode/sql/sql"], mod);
|
|
|
|
else // Plain browser env
|
|
|
|
mod(CodeMirror);
|
|
|
|
})(function(CodeMirror) {
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var tables;
|
2014-10-27 10:49:58 +03:00
|
|
|
var defaultTable;
|
2014-08-18 17:45:43 +04:00
|
|
|
var keywords;
|
|
|
|
var CONS = {
|
|
|
|
QUERY_DIV: ";",
|
|
|
|
ALIAS_KEYWORD: "AS"
|
|
|
|
};
|
|
|
|
var Pos = CodeMirror.Pos;
|
|
|
|
|
|
|
|
function getKeywords(editor) {
|
|
|
|
var mode = editor.doc.modeOption;
|
2014-08-30 07:26:14 +04:00
|
|
|
if (mode === "sql") mode = "text/x-sql";
|
2014-08-18 17:45:43 +04:00
|
|
|
return CodeMirror.resolveMode(mode).keywords;
|
|
|
|
}
|
|
|
|
|
2015-03-24 12:27:39 +03:00
|
|
|
function getText(item) {
|
|
|
|
return typeof item == "string" ? item : item.text;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getItem(list, item) {
|
|
|
|
if (!list.slice) return list[item];
|
|
|
|
for (var i = list.length - 1; i >= 0; i--) if (getText(list[i]) == item)
|
|
|
|
return list[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
function shallowClone(object) {
|
|
|
|
var result = {};
|
|
|
|
for (var key in object) if (object.hasOwnProperty(key))
|
|
|
|
result[key] = object[key];
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-08-18 17:45:43 +04:00
|
|
|
function match(string, word) {
|
|
|
|
var len = string.length;
|
2015-03-24 12:27:39 +03:00
|
|
|
var sub = getText(word).substr(0, len);
|
2014-08-18 17:45:43 +04:00
|
|
|
return string.toUpperCase() === sub.toUpperCase();
|
|
|
|
}
|
|
|
|
|
|
|
|
function addMatches(result, search, wordlist, formatter) {
|
2014-08-30 07:26:14 +04:00
|
|
|
for (var word in wordlist) {
|
|
|
|
if (!wordlist.hasOwnProperty(word)) continue;
|
2015-03-24 12:27:39 +03:00
|
|
|
if (wordlist.slice) word = wordlist[word];
|
|
|
|
|
|
|
|
if (match(search, word)) result.push(formatter(word));
|
2014-08-18 17:45:43 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-24 12:27:39 +03:00
|
|
|
function cleanName(name) {
|
|
|
|
// Get rid name from backticks(`) and preceding dot(.)
|
|
|
|
if (name.charAt(0) == ".") {
|
|
|
|
name = name.substr(1);
|
|
|
|
}
|
|
|
|
return name.replace(/`/g, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
function insertBackticks(name) {
|
|
|
|
var nameParts = getText(name).split(".");
|
|
|
|
for (var i = 0; i < nameParts.length; i++)
|
|
|
|
nameParts[i] = "`" + nameParts[i] + "`";
|
|
|
|
var escaped = nameParts.join(".");
|
|
|
|
if (typeof name == "string") return escaped;
|
|
|
|
name = shallowClone(name);
|
|
|
|
name.text = escaped;
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2014-12-30 12:45:45 +03:00
|
|
|
function nameCompletion(cur, token, result, editor) {
|
2015-03-24 12:27:39 +03:00
|
|
|
// Try to complete table, colunm names and return start position of completion
|
|
|
|
var useBacktick = false;
|
|
|
|
var nameParts = [];
|
|
|
|
var start = token.start;
|
|
|
|
var cont = true;
|
|
|
|
while (cont) {
|
|
|
|
cont = (token.string.charAt(0) == ".");
|
|
|
|
useBacktick = useBacktick || (token.string.charAt(0) == "`");
|
|
|
|
|
|
|
|
start = token.start;
|
|
|
|
nameParts.unshift(cleanName(token.string));
|
|
|
|
|
|
|
|
token = editor.getTokenAt(Pos(cur.line, token.start));
|
|
|
|
if (token.string == ".") {
|
|
|
|
cont = true;
|
|
|
|
token = editor.getTokenAt(Pos(cur.line, token.start));
|
2014-10-27 10:49:58 +03:00
|
|
|
}
|
|
|
|
}
|
2015-03-24 12:27:39 +03:00
|
|
|
|
|
|
|
// Try to complete table names
|
|
|
|
var string = nameParts.join(".");
|
|
|
|
addMatches(result, string, tables, function(w) {
|
|
|
|
return useBacktick ? insertBackticks(w) : w;
|
|
|
|
});
|
|
|
|
|
|
|
|
// Try to complete columns from defaultTable
|
|
|
|
addMatches(result, string, defaultTable, function(w) {
|
|
|
|
return useBacktick ? insertBackticks(w) : w;
|
|
|
|
});
|
|
|
|
|
|
|
|
// Try to complete columns
|
|
|
|
string = nameParts.pop();
|
|
|
|
var table = nameParts.join(".");
|
|
|
|
|
|
|
|
// Check if table is available. If not, find table by Alias
|
|
|
|
if (!getItem(tables, table))
|
|
|
|
table = findTableByAlias(table, editor);
|
|
|
|
|
|
|
|
var columns = getItem(tables, table);
|
|
|
|
if (columns && columns.columns)
|
|
|
|
columns = columns.columns;
|
|
|
|
|
|
|
|
if (columns) {
|
|
|
|
addMatches(result, string, columns, function(w) {
|
|
|
|
if (typeof w == "string") {
|
|
|
|
w = table + "." + w;
|
|
|
|
} else {
|
|
|
|
w = shallowClone(w);
|
|
|
|
w.text = table + "." + w.text;
|
|
|
|
}
|
|
|
|
return useBacktick ? insertBackticks(w) : w;
|
|
|
|
});
|
2014-10-27 10:49:58 +03:00
|
|
|
}
|
2015-03-24 12:27:39 +03:00
|
|
|
|
|
|
|
return start;
|
2014-08-18 17:45:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
function eachWord(lineText, f) {
|
2014-08-30 07:26:14 +04:00
|
|
|
if (!lineText) return;
|
2014-08-18 17:45:43 +04:00
|
|
|
var excepted = /[,;]/g;
|
2014-08-30 07:26:14 +04:00
|
|
|
var words = lineText.split(" ");
|
|
|
|
for (var i = 0; i < words.length; i++) {
|
|
|
|
f(words[i]?words[i].replace(excepted, '') : '');
|
2014-08-18 17:45:43 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-30 07:26:14 +04:00
|
|
|
function convertCurToNumber(cur) {
|
2014-08-18 17:45:43 +04:00
|
|
|
// max characters of a line is 999,999.
|
2014-08-30 07:26:14 +04:00
|
|
|
return cur.line + cur.ch / Math.pow(10, 6);
|
2014-08-18 17:45:43 +04:00
|
|
|
}
|
|
|
|
|
2014-08-30 07:26:14 +04:00
|
|
|
function convertNumberToCur(num) {
|
|
|
|
return Pos(Math.floor(num), +num.toString().split('.').pop());
|
2014-08-18 17:45:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
function findTableByAlias(alias, editor) {
|
|
|
|
var doc = editor.doc;
|
|
|
|
var fullQuery = doc.getValue();
|
|
|
|
var aliasUpperCase = alias.toUpperCase();
|
|
|
|
var previousWord = "";
|
|
|
|
var table = "";
|
|
|
|
var separator = [];
|
|
|
|
var validRange = {
|
2014-08-30 07:26:14 +04:00
|
|
|
start: Pos(0, 0),
|
|
|
|
end: Pos(editor.lastLine(), editor.getLineHandle(editor.lastLine()).length)
|
2014-08-18 17:45:43 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
//add separator
|
2014-08-30 07:26:14 +04:00
|
|
|
var indexOfSeparator = fullQuery.indexOf(CONS.QUERY_DIV);
|
|
|
|
while(indexOfSeparator != -1) {
|
|
|
|
separator.push(doc.posFromIndex(indexOfSeparator));
|
|
|
|
indexOfSeparator = fullQuery.indexOf(CONS.QUERY_DIV, indexOfSeparator+1);
|
2014-08-18 17:45:43 +04:00
|
|
|
}
|
2014-08-30 07:26:14 +04:00
|
|
|
separator.unshift(Pos(0, 0));
|
|
|
|
separator.push(Pos(editor.lastLine(), editor.getLineHandle(editor.lastLine()).text.length));
|
2014-08-18 17:45:43 +04:00
|
|
|
|
2014-08-30 07:26:14 +04:00
|
|
|
//find valid range
|
2014-08-18 17:45:43 +04:00
|
|
|
var prevItem = 0;
|
2014-08-30 07:26:14 +04:00
|
|
|
var current = convertCurToNumber(editor.getCursor());
|
|
|
|
for (var i=0; i< separator.length; i++) {
|
|
|
|
var _v = convertCurToNumber(separator[i]);
|
|
|
|
if (current > prevItem && current <= _v) {
|
|
|
|
validRange = { start: convertNumberToCur(prevItem), end: convertNumberToCur(_v) };
|
2014-08-18 17:45:43 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
prevItem = _v;
|
|
|
|
}
|
|
|
|
|
|
|
|
var query = doc.getRange(validRange.start, validRange.end, false);
|
|
|
|
|
2014-08-30 07:26:14 +04:00
|
|
|
for (var i = 0; i < query.length; i++) {
|
2014-08-18 17:45:43 +04:00
|
|
|
var lineText = query[i];
|
2014-08-30 07:26:14 +04:00
|
|
|
eachWord(lineText, function(word) {
|
2014-08-18 17:45:43 +04:00
|
|
|
var wordUpperCase = word.toUpperCase();
|
2015-03-24 12:27:39 +03:00
|
|
|
if (wordUpperCase === aliasUpperCase && getItem(tables, previousWord))
|
|
|
|
table = previousWord;
|
|
|
|
if (wordUpperCase !== CONS.ALIAS_KEYWORD)
|
2014-08-18 17:45:43 +04:00
|
|
|
previousWord = word;
|
|
|
|
});
|
2014-08-30 07:26:14 +04:00
|
|
|
if (table) break;
|
2014-08-18 17:45:43 +04:00
|
|
|
}
|
|
|
|
return table;
|
|
|
|
}
|
|
|
|
|
2014-08-30 07:26:14 +04:00
|
|
|
CodeMirror.registerHelper("hint", "sql", function(editor, options) {
|
2014-08-18 17:45:43 +04:00
|
|
|
tables = (options && options.tables) || {};
|
2014-10-27 10:49:58 +03:00
|
|
|
var defaultTableName = options && options.defaultTable;
|
2015-03-24 12:27:39 +03:00
|
|
|
defaultTable = defaultTableName && getItem(tables, defaultTableName);
|
2014-08-18 17:45:43 +04:00
|
|
|
keywords = keywords || getKeywords(editor);
|
2014-10-27 10:49:58 +03:00
|
|
|
|
2015-03-24 12:27:39 +03:00
|
|
|
if (defaultTableName && !defaultTable)
|
|
|
|
defaultTable = findTableByAlias(defaultTableName, editor);
|
|
|
|
|
|
|
|
defaultTable = defaultTable || [];
|
|
|
|
|
|
|
|
if (defaultTable.columns)
|
|
|
|
defaultTable = defaultTable.columns;
|
|
|
|
|
2014-08-18 17:45:43 +04:00
|
|
|
var cur = editor.getCursor();
|
|
|
|
var result = [];
|
2014-08-30 07:26:14 +04:00
|
|
|
var token = editor.getTokenAt(cur), start, end, search;
|
2014-12-30 12:45:45 +03:00
|
|
|
if (token.end > cur.ch) {
|
|
|
|
token.end = cur.ch;
|
|
|
|
token.string = token.string.slice(0, cur.ch - token.start);
|
|
|
|
}
|
|
|
|
|
2014-10-27 10:49:58 +03:00
|
|
|
if (token.string.match(/^[.`\w@]\w*$/)) {
|
2014-08-30 07:26:14 +04:00
|
|
|
search = token.string;
|
|
|
|
start = token.start;
|
|
|
|
end = token.end;
|
|
|
|
} else {
|
|
|
|
start = end = cur.ch;
|
|
|
|
search = "";
|
|
|
|
}
|
2014-10-27 10:49:58 +03:00
|
|
|
if (search.charAt(0) == "." || search.charAt(0) == "`") {
|
2015-03-24 12:27:39 +03:00
|
|
|
start = nameCompletion(cur, token, result, editor);
|
2014-08-18 17:45:43 +04:00
|
|
|
} else {
|
2014-08-30 07:26:14 +04:00
|
|
|
addMatches(result, search, tables, function(w) {return w;});
|
2014-10-27 10:49:58 +03:00
|
|
|
addMatches(result, search, defaultTable, function(w) {return w;});
|
2014-08-30 07:26:14 +04:00
|
|
|
addMatches(result, search, keywords, function(w) {return w.toUpperCase();});
|
2014-08-18 17:45:43 +04:00
|
|
|
}
|
|
|
|
|
2014-08-30 07:26:14 +04:00
|
|
|
return {list: result, from: Pos(cur.line, start), to: Pos(cur.line, end)};
|
|
|
|
});
|
2014-08-18 17:45:43 +04:00
|
|
|
});
|