(function(a){if(typeof exports=="object"&&typeof module=="object"){a(require("../../lib/codemirror"),require("../../mode/sql/sql"))}else{if(typeof define=="function"&&define.amd){define(["../../lib/codemirror","../../mode/sql/sql"],a)}else{a(CodeMirror)}}})(function(p){var h;var s;var i;var j={QUERY_DIV:";",ALIAS_KEYWORD:"AS"};var n=p.Pos,o=p.cmpPos;function l(v){return Object.prototype.toString.call(v)=="[object Array]"}function d(v){var w=v.doc.modeOption;if(w==="sql"){w="text/x-sql"}return p.resolveMode(w).keywords}function a(v){return typeof v=="string"?v:v.text}function b(v,w){if(l(w)){w={columns:w}}if(!w.text){w.text=v}return w}function q(w){var v={};if(l(w)){for(var y=w.length-1;y>=0;y--){var z=w[y];v[a(z).toUpperCase()]=b(a(z),z)}}else{if(w){for(var x in w){v[x.toUpperCase()]=b(x,w[x])}}}return v}function c(v){return h[v.toUpperCase()]}function g(w){var v={};for(var x in w){if(w.hasOwnProperty(x)){v[x]=w[x]}}return v}function e(w,y){var v=w.length;var x=a(y).substr(0,v);return w.toUpperCase()===x.toUpperCase()}function f(v,y,z,x){if(l(z)){for(var w=0;w<z.length;w++){if(e(y,z[w])){v.push(x(z[w]))}}}else{for(var A in z){if(z.hasOwnProperty(A)){var B=z[A];if(!B||B===true){B=A}else{B=B.displayText?{text:B.text,displayText:B.displayText}:B.text}if(e(y,B)){v.push(x(B))}}}}}function k(v){if(v.charAt(0)=="."){v=v.substr(1)}return v.replace(/`/g,"")}function r(w){var v=a(w).split(".");for(var x=0;x<v.length;x++){v[x]="`"+v[x]+"`"}var y=v.join(".");if(typeof w=="string"){return y}w=g(w);w.text=y;return w}function t(F,y,H,D){var E=false;var z=[];var v=y.start;var I=true;while(I){I=(y.string.charAt(0)==".");E=E||(y.string.charAt(0)=="`");v=y.start;z.unshift(k(y.string));y=D.getTokenAt(n(F.line,y.start));if(y.string=="."){I=true;y=D.getTokenAt(n(F.line,y.start))}}var C=z.join(".");f(H,C,h,function(J){return E?r(J):J});f(H,C,s,function(J){return E?r(J):J});C=z.pop();var G=z.join(".");var B=false;var A=G;if(!c(G)){var w=G;G=m(G,D);if(G!==w){B=true}}var x=c(G);if(x&&x.columns){x=x.columns}if(x){f(H,C,x,function(J){var K=G;if(B==true){K=A}if(typeof J=="string"){J=K+"."+J}else{J=g(J);J.text=K+"."+J.text}return E?r(J):J})}return v}function u(w,y){if(!w){return}var v=/[,;]/g;var z=w.split(" ");for(var x=0;x<z.length;x++){y(z[x]?z[x].replace(v,""):"")}}function m(A,B){var I=B.doc;var w=I.getValue();var v=A.toUpperCase();var C="";var J="";var z=[];var H={start:n(0,0),end:n(B.lastLine(),B.getLineHandle(B.lastLine()).length)};var y=w.indexOf(j.QUERY_DIV);while(y!=-1){z.push(I.posFromIndex(y));y=w.indexOf(j.QUERY_DIV,y+1)}z.unshift(n(0,0));z.push(n(B.lastLine(),B.getLineHandle(B.lastLine()).text.length));var G=null;var D=B.getCursor();for(var x=0;x<z.length;x++){if((G==null||o(D,G)>0)&&o(D,z[x])<=0){H={start:G,end:z[x]};break}G=z[x]}var E=I.getRange(H.start,H.end,false);for(var x=0;x<E.length;x++){var F=E[x];u(F,function(L){var K=L.toUpperCase();if(K===v&&c(C)){J=C}if(K!==j.ALIAS_KEYWORD){C=L}});if(J){break}}return J}p.registerHelper("hint","sql",function(z,D){h=q(D&&D.tables);var w=D&&D.defaultTable;var A=D&&D.disableKeywords;s=w&&c(w);i=d(z);if(w&&!s){s=m(w,z)}s=s||[];if(s.columns){s=s.columns}var B=z.getCursor();var E=[];var x=z.getTokenAt(B),v,y,C;if(x.end>B.ch){x.end=B.ch;x.string=x.string.slice(0,B.ch-x.start)}if(x.string.match(/^[.`\w@]\w*$/)){C=x.string;v=x.start;y=x.end}else{v=y=B.ch;C=""}if(C.charAt(0)=="."||C.charAt(0)=="`"){v=t(B,x,E,z)}else{f(E,C,h,function(F){return F});f(E,C,s,function(F){return F});if(!A){f(E,C,i,function(F){return F.toUpperCase()})}}return{list:E,from:n(B.line,v),to:n(B.line,y)}})});