ASP(JScript)构建SQL语句“类”
来源:CSDN 时间:2008-04-30 07:12:00
实现功能QuestType 1->查询语句, 2->更新语句, 3->删除语句, 4->插入语句
<%@LANGUAGE="JAVASCRIPT" CODEPAGE="936"%>
<%
var QuestStringBuilder = function(){
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());
%>
标签:类,sql,asp
0
投稿
猜你喜欢
如何解决前端笔记本屏幕显示缩放比例125%,150%对页面布局的影响
2024-04-29 13:11:36
python正则表达式匹配不包含某几个字符的字符串方法
2023-06-11 16:52:45
详解Python if-elif-else知识点
2022-02-23 16:06:23
详解Python如何实现对比两个Excel数据差异
2023-06-14 00:37:10
解决pycharm编辑区显示yaml文件层级结构遇中文乱码问题
2022-03-21 10:30:42
利用Python绘制虎年烟花秀
2022-10-08 06:03:49
MySQL中表复制:create table like 与 create table as select
2024-01-25 06:31:46
Python之ascii转中文的实现
2023-08-24 19:39:12
使用SQL Server 2008中对象相关性
2008-12-12 06:36:00
php的RSA加密解密算法原理与用法分析
2023-07-13 11:27:14
多次执行mysql_fetch_array()的指针归位问题探讨
2024-01-16 00:16:16
vue-element如何实现动态换肤存储
2024-04-27 15:57:35
Vue3 defineExpose要在方法声明定义以后使用的教程
2024-05-21 10:30:03
python字符串str和字节数组相互转化方法
2022-02-14 08:31:48
小米正式开源 SQL 智能优化与改写工具 SOAR
2024-01-20 09:34:56
python 中if else 语句的作用及示例代码
2023-04-17 00:52:35
配置SQL Server文件组对应不同的RAID系统
2009-01-04 14:31:00
深入探讨:PHP使用数据库永久连接方式操作MySQL的是与非
2023-11-20 22:41:16
详解vue-router2.0动态路由获取参数
2024-04-09 10:49:42
Python高并发和多线程有什么关系
2023-12-08 04:24:47