An ASP (VBSCRIPT) Simple SQL statement to build a "class"

Source: Internet
Author: User
Tags insert join trim
vbscript| statement |vbscript <% @LANGUAGE = "VBScript" codepage= "936"%>
<% Option Explicit%>
<% Response.Buffer = True%>
<%
' ////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////
' ///
'///filename: sqlbuilderforvbs
'///function: To build some simple SQL statements, combined with the form when the use, can be more convenient
'///program creator: Zeng Siyuan
'///Description: Simple SQL statement build ' class ', VBS version, as long as this annotation paragraph is retained, regardless of whether it involves business, you can use, reprint or reference
'///Date: 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

'/-----Initialization-----/

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

'/----field name processing----/

Private Function Processfield (ByVal Sfield)
Processfield = "[" & Sfield & "]"
End Function

'/-----field value processing-----/

Private Function processvalue (ByVal svalue)
Dim Tmptype:tmptype = VarType (svalue)
Select Case Tmptype
Case 2,3,4,5,11 ' numeric type, Boolean type
Processvalue = svalue
Case 8 ' character type
Processvalue = "'" & Safe (svalue) & "'"
Case Else ' other type
Processvalue = "'" & Safe (svalue) & "'"
End Select
End Function

'/-----Integrated treatment-----/

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

'/-----A small safe handling-----

Private Function Safe (s)
Safe = Replace (S, "'", "" ")
End Function

'/-----Empty the last input parameter, but keep tablename-----/

Public Sub Clear ()
Objfields.removeall
' strTableName = Null
Strpkey = Null
Strpkeysort = Null
Strcondition = Null
Erase acontition
Stroperator = "="
Strlogic = "and"
Blnstate = False
End Sub

'/----Generate query statement----/

Public Function Getselect ()
Dim strsqltemplate:strsqltemplate = "SELECT {fields} from {table} {conditions} {by} {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



Related Article

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.