实现支持逻辑搜索/单词搜索/词组搜索+支持OR/AND关键字的VBS CLASS
2024-07-21 02:15:37
供稿:网友
class功能.替换传入的字符串成为sql语句where关键字后面的表达式:
词语搜索 [例如: 小明]
词组搜索
词组里面每一个词都将被检索
例如: 小强1 小名1 小强强 小小强
逻辑搜索
支持 and 和 or 运算符.
例如: 小明 and 小强 and 小小强
复合条件:
例如:(小小明 or 小明) and (小强 or 小小强)
例如:(小小明 or 小名) and 小小强
例如: 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 = "(标题+文章)"
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关键字后面的表达式.发送到asp程序的时候.你可以在前面加上
"select id,标题,name,tablename from searchall where "
这样类似的sql语句
-------------------------vbscript例子-----------------------------
dim objroot1
set objroot1 = new createquerystring
objroot1.querystring = strtext '====传入要替换的字符串
objroot1.strfield = "要查询的字段名字" '===如果不设置.默认值是"(标题+文章)"
strtext = objroot1() '=========得到替换好的sql语句
if (objquerystring.intstart = 4) then
call msgbox("启动按逻辑搜索")
end if
set objroot1 = nothing