Implement vbs class that supports logical search/word search/phrase search + or/and keywords!

Source: Internet
Author: User

Class function. Replace the input string with the expression following the where keyword of the SQL statement:

Word search [for example: James]

Phrase search
Each word in a phrase will be retrieved.
For example: Xiaoqiang 1 Xiaoqiang 1 xiaoqiqiangqiang

Logical search
The and or operators are supported.
For example, Xiao Ming and Xiao Qiang

Composite condition:
Example: (Xiao Ming or Xiao Ming) and (Xiao Qiang or Xiao Qiang)
Example: (Xiao Ming or Xiao Ming) and Xiao Qiang
Example: root1 and (Cantonese or Beijingers)
-----------------------------------------------------------CopyCodeThe Code is as follows: 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 = "(Title +Article)"
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
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
End if
Else
If (I = "and") or (I = "or") then
Blntru = true
Straaa = straaa & I
Else
Blnbbb = false
Exit
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
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
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
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
End if
Else
Check = orand ()
End if
End Function

End Class

--------------------------- note that the replaced string is not a complete SQL statement. only the expression after the where keyword of the SQL statement is generated. sent to ASP Program . you can add
"select ID, title, name, tablename from searchall where "
similar SQL statement
explain VBScript example Example Explain
dim objroot1
set objroot1 = new createquerystring
objroot1.querystring = strtext' === input the string to be replaced
objroot1.strfield = "field name to be queried" '=== if not set. the default value is "(Title + Article)"
strtext = objroot1 () '========= the replaced SQL statement
If (objquerystring. intstart = 4) Then
call msgbox ("Start search by logic")
end if
set objroot1 = nothing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.