實(shí)現(xiàn)支持邏輯搜索/單詞搜索/詞組搜索+支持OR/AND關(guān)鍵字的VBS CLASS
2024-07-21 02:15:37
供稿:網(wǎng)友
 
class功能.替換傳入的字符串成為sql語句where關(guān)鍵字后面的表達(dá)式:
詞語搜索 [例如: 小明] 
詞組搜索 
詞組里面每一個詞都將被檢索 
例如: 小強(qiáng)1 小名1 小強(qiáng)強(qiáng) 小小強(qiáng) 
邏輯搜索 
支持 and 和 or 運(yùn)算符. 
例如: 小明 and 小強(qiáng) and 小小強(qiáng)
復(fù)合條件:
例如:(小小明 or 小明) and (小強(qiáng) or 小小強(qiáng)) 
例如:(小小明 or 小名) and 小小強(qiáng)
例如: root1 and (廣東人 or 北京人) 
-----------------------------------------------------------
class createquerystring
public objreg
public intstart
public strfield
private objnode2
private strtext
public property let querystring( strvalue )
strtext = lcase( strvalue )
end property
private sub class_initialize()
set objreg = new regexp
strfield = "(標(biāo)題+文章)"
end sub
private sub class_terminate()
set objreg = nothing
end sub
public default function gettext()
dim blnres
dim strsky
with objreg
.ignorecase = true
.global = true
.pattern = "/s"
blnres = .test( strtext )
end with
if (not blnres) then
intstart = 2
gettext = strfield & " like '%" & strtext & "%'"
else
objreg.pattern = "/sand|/sor"
blnres = objreg.test( strtext )
if blnres then
strsky = check()
if strsky = false then
gettext = wahaha()
else
gettext = strsky
end if
else
gettext = wahaha()
end if
end if
end function
private function wahaha()
dim strter
dim strlikes
dim strors
dim stri
dim objre
strter = ""
strlikes = " or (" & strfield & " like '%"
strors = "%')"
objreg.pattern = "(/s*/s)"
set objre = objreg.execute(strtext)
for each stri in objre 
strter = strter & strlikes & stri & strors
next
wahaha = mid( strter , 4 )
intstart = 3
end function
private function checkyes( strmode , intcount)
dim objnode1
objreg.pattern = strmode
set objnode1 = objreg.execute( strtext )
if objnode1.count < 1 then
checkyes = true
else
set objnode2 = objnode1( 0 )
if objnode2.submatches.count < intcount then
checkyes = true
end if
end if
end function
private function orand()
dim strsss
dim strccc
dim straaa
dim a143
dim i
dim objn
dim blntru
dim blnbbb
strsss = "(" & strfield & " like '%"
strccc = "%')"
straaa = ""
n1 = 0
blntru = true
blnbbb = true
objreg.pattern = "(/s*/s)"
set objn = objreg.execute( strtext )
a143 = objn.count - 1
if (objn.item( a143 ) = "and") or (objn.item( a143 ) = "or") then 
orand = false
exit function
end if
for each i in objn
if blntru then 
if (i <> "and") and (i <> "or") then
blntru = false
straaa = straaa & strsss & i & strccc
else
blnbbb = false
exit for
end if
else
if (i = "and") or (i = "or") then
blntru = true
straaa = straaa & i
else
blnbbb = false
exit for
end if
end if
next
if (not blnbbb) then
orand = false
else
orand = straaa
intstart = 4
end if
end function
private function check()
dim re
dim re1
dim re2
dim re3
dim str
dim str1
dim a1
dim a2
dim a3
dim a4
str = strfield & " like '%"
str1 = "%'"
with objreg
.pattern = "^/(.+/)/s(and|or)/s"
re = .test( strtext )
.pattern = "/s(and|or)/s/(.+/)$"
re3 = .test( strtext )
end with
if re and re3 then
if checkyes( "^/((/s*/s) (/bor/b|/band/b) (/s*/s)/) (and|or) /((/s*/s) (/bor/b|/band/b) (/s*/s)/)$" , 6 ) then
check = false
else
with objnode2
a1 = .submatches(0)
a2 = .submatches(2)
a3 = .submatches(4)
a4 = .submatches(6)
check = "(" & str & a1 & str1 & " " & .submatches(1) & " " & str & a2 & str1 & ") " &_
.submatches(3) & " (" & str & a3 & str1 & " " & .submatches(5) & " " & str & a4 & str1 & ")"
intstart = 5
 |||end with
end if
elseif re then
if checkyes( "^/((/s*/s) (/bor/b|/band/b) (/s*/s)/) (and|or) (.+)" , 4 ) then
check = false
else
with objnode2
a1 = .submatches(0)
a2 = .submatches(2)
a3 = .submatches(4)
check = "(" & str & a1 & str1 & " " & .submatches(1) & " " & str & a2 & str1 & ") " &_
.submatches(3) & " (" & str & a3 & str1 & ")"
intstart = 5
end with
end if
elseif re3 then
if checkyes( "(.+) (and|or) /((/s*/s) (/bor/b|/band/b) (/s*/s)/)$" , 4 ) then
check = false
else
with objnode2
a1 = .submatches(0)
a2 = .submatches(2)
a3 = .submatches(4)
check = "(" & str & a1 & str1 & ") " & .submatches(1) & " (" & str & a2 & str1 & " " &_
.submatches(3) & " " & str & a3 & str1 & ")"
intstart = 5
end with
end if
else
check = orand()
end if
end function
end class
-------------------------注意-----------------------------
替換好的字符串并不是完整的sql語句.只是生成sql語句的where關(guān)鍵字后面的表達(dá)式.發(fā)送到asp程序的時候.你可以在前面加上
"select id,標(biāo)題,name,tablename from searchall where "
這樣類似的sql語句
-------------------------vbscript例子-----------------------------
dim objroot1
set objroot1 = new createquerystring
objroot1.querystring = strtext '====傳入要替換的字符串
objroot1.strfield = "要查詢的字段名字" '===如果不設(shè)置.默認(rèn)值是"(標(biāo)題+文章)"
strtext = objroot1() '=========得到替換好的sql語句
if (objquerystring.intstart = 4) then
call msgbox("啟動按邏輯搜索")
end if
set objroot1 = nothing