一个ASP(JScript)简单SQL语句构建“类”,同时支持客户端和服务器端执行的

<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 728x15, 创建于 08-4-23MSDN */ google_ad_slot = "3624277373"; google_ad_width = 728; google_ad_height = 15; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 160x600, 创建于 08-4-23MSDN */ google_ad_slot = "4367022601"; google_ad_width = 160; google_ad_height = 600; //--> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<%@language="javascript" codepage="936"%>
<%
var queststringbuilder = function(){
    // 1->查询语句, 2->更新语句, 3->删除语句, 4->插入语句
    var questtype = 0;
    var fields = [];
    var values = [];
    var condition;
    var pk;
    var pkeysort;
    var tablename;
    var state = false;
    var clogic = " and ";
    var coperator = "=";
    this.addfield = function(field,value){
        fields.push(processfield(field));
        values.push(processvalue(value));
    };
    this.addcfield = function(cfield,cvalue){
        var stemp = [];
        if(condition!=undefined&&condition!=""){ stemp.push(condition); }
        if(coperator!="like"){
            stemp.push(processfield(cfield)+coperator+processvalue(cvalue));
        }else{
            stemp.push(processfield(cfield)+" like '%"+cvalue+"%'");            
        }
        condition = stemp.join(clogic);
        if(condition==""){ condition=stemp.tostring();}
    };
    this.table = function(s){tablename = s;};
    this.pkey = function(s){pk = processfield(s);};
    this.pkeyasc = function(){pkeysort="asc";};
    this.pkeydesc = function(){pkeysort="desc";};
    this.getselect = function(){
        questtype = 1;
        return result();
    };
    this.getupdate = function(){
        questtype = 2;
        return result();
    };
    this.getdelete = function(){
        questtype = 3;
        return result();
    };
    this.getinsert = function(){
        questtype = 4;
        return result();
    };
    this.clear = function(){
        fields = [];
        values = [];
        pk = undefined;
        pkeysort = undefined;
        tablename = undefined;
        state = false;
    };
    this.actionstate = function(){ return state;};
    this.changelogic = function(s){ clogic = " "+s.touppercase()+" ";};
    this.changeoperator = function(s){coperator = s.touppercase();};
    var result = function(){
        var strtemp;
        if(fields.length==0) fields.push("*");
        switch(questtype){
            case 1:
                if(tablename!=undefined&&tablename!=""){
                    strtemp = "select " + fields.tostring() + " from " + tablename;
                    if(condition!=undefined){ strtemp += " where " + condition;}
                    if(pkeysort!=undefined&&pk!=undefined){ strtemp += " order by " + pk + " " + pkeysort;}
                    state = true;
                }else{
                    strtemp = geterror(0); // 要求输入表名
                }
                return strtemp;
                break;
            case 2:
                if(tablename!=undefined&&tablename!=""){
                    strtemp = "update " + tablename + " set " + process(fields,values) ;
                    if(condition!=undefined){
                        state = true;
                        strtemp += " where " + condition;
                    }else{
                        strtemp = geterror(1);
                    }
                }else{
                    strtemp = geterror(0); // 要求输入表名
                }                
                return strtemp;
                break;
            case 3:
                if(tablename!=undefined&&tablename!=""){
                    strtemp = "delete from " + tablename ;
                    if(condition!=undefined){
                        state = true;
                        strtemp += " where " + condition;
                    }else{
                        strtemp = geterror(1);
                    }
                }else{
                    strtemp = geterror(0); // 要求输入表名
                }                
                return strtemp;
                break;
            case 4:
                if(tablename!=undefined&&tablename!=""){
                    strtemp = "insert into " + tablename ;
                    if(fields.length!=0){
                        state = true;
                        strtemp += "(" + fields.tostring() + ") values(" + values.tostring() + ")";
                    }else{
                        strtemp = geterror(2);
                    }
                }else{
                    strtemp = geterror(0); // 要求输入表名
                }                
                return strtemp;
                break;
        }
    };
    var processvalue = function(unknow){
        var inputtype = typeof(unknow);
        switch(inputtype){
            case "string":
                return "'"+unknow+"'";
                break;
            case "number":
                return unknow;
                break;
            case "boolean":
                return unknow;
                break;
        }
    };
    var processfield = function(unknow){
        return "["+unknow+"]";
    };
    var process = function(afield,avalue){
        var atemp = [];
        for(i=0;i<afield.length;i++){
            atemp.push(afield[i]+"="+avalue[i]);
        }
        return atemp.tostring();
    }
    var geterror = function(interror){
        if(typeof(interror)=="number"){
            switch(interror){
                case 0:
                    state = false;
                    return "need quest tablename, please using method table input one tablename!";
                    break;
                case 1:
                    state = false;
                    return "need one condition! please using method addcfield add condition!";
                    break;
                case 2:
                    state = false;
                    return "need insert values!";
                    break;
            }
        }
    };
};
var SQL = new queststringbuilder();
SQL.table("mytable");
SQL.addfield("test","1");
SQL.addfield("test1",23);
SQL.addcfield("id",93);
SQL.pkey("id");
SQL.pkeydesc();
//SQL.clear();
response.write(SQL.getdelete()+"<br>"+SQL.actionstate());
%>

刚刚写完的,因为刚做了一个ASP.net的项目(使用access数据库),做了一个简单SQL语句构建类,所以也写了个ASP版的。它看上去对多表查询支持不是很好,但只要把表名换成视图名,就可以达到较好的效果。没有怎么写注释,麻烦大家自己看看了,或有兴趣的,可以改进一下。 < <script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 728x15, 创建于 08-4-23MSDN */ google_ad_slot = "3624277373"; google_ad_width = 728; google_ad_height = 15; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 160x600, 创建于 08-4-23MSDN */ google_ad_slot = "4367022601"; google_ad_width = 160; google_ad_height = 600; //--> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
阅读更多
个人分类: javascript
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭