一個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收集整理,歡迎訪問。