一个ASP(VBScript)简单SQL语句构建"类"
2024-05-04 11:06:14
供稿:网友
<%@language="vbscript" codepage="936"%>
<% option explicit %>
<% response.buffer = true %>
<%
' /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
' ///
' /// 文件名: sqlbuilderforvbs
' /// 作用: 构建一些简单的sql语句,结合在提交表单时使用,可以较方便
' /// 程式编写者: 曾思源
' /// 说明: 简单sql语句构建“类”,vbs版,只要保留本注释段,无论是否涉及商业,您可以任意使用,转载或引用
' /// 日期: 2005-1-8
' ///_________________________________________________________________________________________________
' /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
%>
<%
on error resume next
class queststringbuilder
private objfields
private strtablename
private strpkey
private strpkeysort
private strcondition
private acontition()
private stroperator
private strlogic
private blnstate
'/-----初始化-----/
private sub class_initialize()
set objfields = server.createobject("scripting.dictionary")
strtablename = null
strpkey = null
strpkeysort = null
strcondition = null
redim acontition(1)
stroperator = "="
strlogic = " and "
blnstate = false
end sub
private sub class_terminate()
set objfields = nothing
strtablename = null
strpkey = null
strpkeysort = null
strcondition = null
erase acontition
stroperator = null
strlogic = null
blnstate = false
end sub
' /----字段名处理----/
private function processfield(byval sfield)
processfield = "[" & sfield & "]"
end function
' /-----字段值处理-----/
private function processvalue(byval svalue)
dim tmptype : tmptype = vartype(svalue)
select case tmptype
case 2,3,4,5,11 ' 数字类型,布尔类型
processvalue = svalue
case 8 ' 字符类型
processvalue = "'" & safe(svalue) & "'"
case else ' 其它类型
processvalue = "'" & safe(svalue) & "'"
end select
end function
' /-----综合处理-----/
private function process(byref obj, byval strtype)
dim keys : keys = obj.keys
dim items : items = obj.items
dim intcount : intcount = obj.count
dim tmp()
redim tmp(1)
if intcount > 0 then
dim tmparray(), i
redim tmparray(intcount-1)
for i=0 to intcount - 1
tmparray(i) = keys(i) & "=" & items(i)
next
select case ucase(trim(strtype))
case "update"
process = join(tmparray, ", ")
case "select"
process = join(keys, " ,")
case "insert"
tmp(0) = join(keys, " ,")
tmp(1) = join(items, " ,")
process = tmp
erase tmp
end select
erase tmparray
else
select case ucase(trim(strtype))
case "update"
process = false
case "select"
process = "*"
case "insert"
process = tmp
end select
end if
end function
' /-----小小的安全处理-----/
private function safe(s)
safe = replace(s,"'","''")
end function
' /-----清空上一次输入的参数,但保留tablename-----/
public sub clear()
objfields.removeall
'strtablename = null
strpkey = null
strpkeysort = null
strcondition = null
erase acontition
stroperator = "="
strlogic = " and "
blnstate = false
end sub
' /----生成查询语句----/
public function getselect()
dim strsqltemplate : strsqltemplate = "select {fields} from {table} {conditions} {orderby} {sort}"
strsqltemplate = replace(strsqltemplate, "{fields}", process(objfields, "select"))
if vartype(strtablename) = 1 then exit function
strsqltemplate = replace(strsqltemplate, "{table}", strtablename)
if vartype(strcondition) <> 1 and strcondition <> "" then
strsqltemplate = replace(strsqltem本文来源于网页设计爱好者web开发社区http://www.html.org.cn收集整理,欢迎访问。