Path: blob/master/web-gui/buildyourownbotnet/assets/js/codemirror/addon/hint/sql-hint.js
2069 views
(function () {1"use strict";23var tables;4var keywords;5var CONS = {6QUERY_DIV: ";",7ALIAS_KEYWORD: "AS"8};910function getKeywords(editor) {11var mode = editor.doc.modeOption;12if(mode === "sql") mode = "text/x-sql";13return CodeMirror.resolveMode(mode).keywords;14}1516function match(string, word) {17var len = string.length;18var sub = word.substr(0, len);19return string.toUpperCase() === sub.toUpperCase();20}2122function addMatches(result, search, wordlist, formatter) {23for(var word in wordlist) {24if(!wordlist.hasOwnProperty(word)) continue;25if(Array.isArray(wordlist)) {26word = wordlist[word];27}28if(match(search, word)) {29result.push(formatter(word));30}31}32}3334function columnCompletion(result, editor) {35var cur = editor.getCursor();36var token = editor.getTokenAt(cur);37var string = token.string.substr(1);38var prevCur = CodeMirror.Pos(cur.line, token.start);39var table = editor.getTokenAt(prevCur).string;40if( !tables.hasOwnProperty( table ) ){41table = findTableByAlias(table, editor);42}43var columns = tables[table];44if(!columns) {45return;46}47addMatches(result, string, columns,48function(w) {return "." + w;});49}5051function eachWord(lineText, f) {52if( !lineText ){return;}53var excepted = /[,;]/g;54var words = lineText.split( " " );55for( var i = 0; i < words.length; i++ ){56f( words[i]?words[i].replace( excepted, '' ) : '' );57}58}5960function convertCurToNumber( cur ){61// max characters of a line is 999,999.62return cur.line + cur.ch / Math.pow( 10, 6 );63}6465function convertNumberToCur( num ){66return CodeMirror.Pos(Math.floor( num ), +num.toString().split( '.' ).pop());67}6869function findTableByAlias(alias, editor) {70var doc = editor.doc;71var fullQuery = doc.getValue();72var aliasUpperCase = alias.toUpperCase();73var previousWord = "";74var table = "";75var separator = [];76var validRange = {77start: CodeMirror.Pos( 0, 0 ),78end: CodeMirror.Pos( editor.lastLine(), editor.getLineHandle( editor.lastLine() ).length )79};8081//add separator82var indexOfSeparator = fullQuery.indexOf( CONS.QUERY_DIV );83while( indexOfSeparator != -1 ){84separator.push( doc.posFromIndex(indexOfSeparator));85indexOfSeparator = fullQuery.indexOf( CONS.QUERY_DIV, indexOfSeparator+1);86}87separator.unshift( CodeMirror.Pos( 0, 0 ) );88separator.push( CodeMirror.Pos( editor.lastLine(), editor.getLineHandle( editor.lastLine() ).text.length ) );8990//find valieRange91var prevItem = 0;92var current = convertCurToNumber( editor.getCursor() );93for( var i=0; i< separator.length; i++){94var _v = convertCurToNumber( separator[i] );95if( current > prevItem && current <= _v ){96validRange = { start: convertNumberToCur( prevItem ), end: convertNumberToCur( _v ) };97break;98}99prevItem = _v;100}101102var query = doc.getRange(validRange.start, validRange.end, false);103104for(var i=0; i < query.length; i++){105var lineText = query[i];106eachWord( lineText, function( word ){107var wordUpperCase = word.toUpperCase();108if( wordUpperCase === aliasUpperCase && tables.hasOwnProperty( previousWord ) ){109table = previousWord;110}111if( wordUpperCase !== CONS.ALIAS_KEYWORD ){112previousWord = word;113}114});115if( table ){ break; }116}117return table;118}119120function sqlHint(editor, options) {121tables = (options && options.tables) || {};122keywords = keywords || getKeywords(editor);123var cur = editor.getCursor();124var token = editor.getTokenAt(cur);125var result = [];126var search = token.string.trim();127128addMatches(result, search, keywords,129function(w) {return w.toUpperCase();});130131addMatches(result, search, tables,132function(w) {return w;});133134if(search.lastIndexOf('.') === 0) {135columnCompletion(result, editor);136}137138return {139list: result,140from: CodeMirror.Pos(cur.line, token.start),141to: CodeMirror.Pos(cur.line, token.end)142};143}144CodeMirror.registerHelper("hint", "sql", sqlHint);145})();146147148