js类定义(翻出的js代码)

/*类机制定义*/ window.Class = function(name, src) { src.constructor.prototype = src; window[name] = src.constructor; }; /*自定义类*/ Class("ArrayUtils", { constructor: function() { }, toArray : function(iterable) { var length = iterable.length, results = new Array(length); while (length--) { results[length] = iterable[length] }; return results; }, camelize : function(s) { return s.replace(/\-(.)/g, function(m, l){return l.toUpperCase()}); }, inArray : function(arr,item) { return (this.search(arr,item) != null); }, search : function(arr,itm) { for(var i=0; i 100000){ this.items.shift(); } }, remove : function(item){ var s = []; //移除item,然后遍历,把剩下发的赋值给items for(var i = 0, len = this.items.length; i =,,between and * SQL:= TB2.REF_CITY_CODE = '1' AND TB3.REF_REGION_CODE IN (select * from aaa) * 【建议把in的用括号包含】 TB2.REF_CITY_CODE = '1' AND (TB3.REF_REGION_CODE IN (select * from aaa) ) * * HAVING和WHERE的处理方式是一样的HAVING SUM(TB1.PAY_MONEY)0 * where中有聚合函数TB2.REF_CITY_CODE = '1' and sum(TB2.REF_CITY_CODE) * * 【建议把between and用括号包含,要不然就拆成2个大于小于】 * TB2.REF_CITY_CODE between 21 and 44可以改成2个TB2.REF_CITY_CODE> 21 and TB2.REF_CITY_CODE200-----建议使用别名,不要把聚合函数放到where里面 * * select a,b from (select a,b, sum(aa) as c from table1 where c > 100 ) * * * * 目前还不支持一个长括号里面有评级的,能取出来,但是不能替换 * * * * minCell--minCellCopy[相同的()的已被替换成'@',],然后对minCellCopy[i]进行分词即可 * * */ Class("WhereParser", { constructor: function() { // this.key = key;//可能处理类型,key:[where|having] this.items = []; this.myStack = new Stack();//上面有定义 this.inputString = '';//需要解析的字符串 this.logicOperatorKey = ['and','or','in'];//逻辑运算符,报表中where的关键字分隔符:and ,or 和 in this.argoOperatorKey = ['=',''];//算数运算符=,>=, this.exceptOperatorKey = ['between'];//特殊处理between and this.dataObject =new Array();//对象数组,准备放对象的 {field , op , value} this.logicOpArray = []; this.opArray = []; this.typeArray = []; }, getDataObject:function(){ return this.dataObject; }, getLogicOpArray:function(){ return this.logicOpArray; }, getOpArray:function(){ return this.opArray; }, getTypeArray:function(){ return this.typeArray; }, trim: function( text ) { var rtrim = /^(\s|\u00A0)+|(\s|\u00A0)+$/g; return (text || "").replace( rtrim, "" ); }, /*判断是否存在此关键字key,type取值{l是逻辑运算符,a是算数运算符}*/ hasKey : function(type,key){ var arrayUtils = new ArrayUtils(); if(type == 'l'){ return arrayUtils.inArray(this.logicOperatorKey,key); } if(type == 'a'){ return arrayUtils.inArray(this.argoOperatorKey,key); } }, /*设置要解析的字符串*/ setParseString : function(inputString){ this.inputString = inputString; //document.write( "输入字符串:"+this.inputString+"

"); return this;//链式操作 }, /* */ process : function(){ var myString = this.inputString; //document.write("匹对过程
"); //最小计算单元内的左括号个数,当栈的总和为0,此数归0 var leftCount=0; //傀儡,只为存放一个char,其实可以不用 var dummy = new Array(); //this.msg(this.inputString.length); var stackString = "";//只存放栈中完整的字符 var stackStartFlag = false; var wordString = ""; var wordChar = ""; for(var i =0;i<mystring.length i count dummy if>0){ //遇到空格,分词 -------下一个字符不是算数运算符 if(dummy[i-1]==' '&amp;&amp;this.hasKey('a',dummy[i])==false){ //遇空则清 wordString = ""; } wordString = wordString + dummy[i]; if(dummy[i]==' '&amp;&amp;wordString.length&gt;1&amp;&amp;this.myStack.getCount()==0){ var wordString = this.trim(wordString); if(wordString=='AND' || wordString=='OR'){ this.logicOpArray.push(wordString); }else{ //显示不是and和or的 //alert(wordString); this.dataObject.push(wordString); } } } if(stackStartFlag){ stackString = stackString+dummy[i]; } //当'('的时候入栈,记下位置 if(dummy[i]=='('){ this.myStack.add(i); if(leftCount==0){ //alert("开始啦"); stackString = "("; stackStartFlag = true; } leftCount++; this.count++; } //当')'的时候出栈 if(dummy[i]==')'){ var before = this.myStack.next(); var current = i+1; var str = myString.substring(before,current); leftCount--; if(leftCount==0){ //alert("结束啦"); //this.dataObject.push(stackString); stackStartFlag = false; } if(this.myStack.getCount()==0){ wordString = stackString; //recureProcessing(wordString); } document.write("leftCount:="+leftCount+" result="+str+"<hr>"); var bStr = myString.substring(0,before); var aStr = myString.substring(current,myString.length); var finalStr = bStr + "@" + aStr; //alert(aStr.length); if(leftCount&gt;0){ //myStr=myStr.replace(me+'',' '); if(aStr.length"); // document.write( "<font color="red">this.dataObject:"+this.dataObject.length+"个</font><br>"); // document.write("<br>"+this.inputString+"<br>"+"<br>"); var op =""; for(var i =0;i ")&gt;-1){ op="&gt;"; } if(s.indexOf("-1){ op="-1){ op="="; } if(s.indexOf("&gt;=")&gt;-1){ op="&gt;="; } if(s.indexOf("-1){ op="")&gt;-1){ op=""; } var type = "number" ; if(s.indexOf("'")&gt;-1){ type="string"; } // if(s.indexOf(" OR ")&gt;-1){ // logicOp="and"; // } // this.typeArray.push(type); this.opArray.push(op); //document.write( "第"+i+"个 "+s+" op="+op+" type="+type+" logicOp="+this.logicOpArray[i]+" <br>"); } // document.write("<br>"); // document.write("<br>"); // document.write("<br>"); return this;//链式操作 }, msg:function(msg){ alert(msg); } }); //(f=3) and (a=1 and (b=2 or (c=4 and e=5) or (c1=4 and e=5) and ( d=3 ) //var a = new WhereParser(); // a.setParseString(" a&gt;='2' or (a in(20)) and b=3 and ( (c in(1,2,3,4)) and (a in(2,3,4) )) or (a=23) and e=3 "); //a.setParseString(" A&gt;='2' OR C&gt;33 AND B3 AND E</mystring.length>

你可能感兴趣的:(js)