VB6:編寫一個分析sqlserver預存程序執行語句”execute procedurename par1,par2,…….”文法是否正確的函數

來源:互聯網
上載者:User

比較笨,花了兩天時間。

'函數名:parsesrc
'功能:分析sqlserver預存程序執行語句"procedurename par1,par2,......."文法是否正確的函數
'入參:src as string,如"proceduretest '字串''後半部分',343,'2005-2-5'"
'傳回值:字串數組.parsesrc(0)="預存程序的名稱",parsesrc(1)=有效參數的個數(如果為-1,表示有非法參數),parsesrc(>1)=對應的位置的入參
'調用:dim result() as string
'      result=parsesrc("procedurename par1,par2,.......")
'

Function parsesrc(src As String) As String()
   
    Dim mynode() As String
    Dim strleft, strright As String
    Dim iscomma, isend As Boolean
    Dim parmpos, commapos, spacepos, singquotepos, singquotecount, nextsingquotepos As Integer
   
   
    src = Trim(Replace(Replace(src, vbTab, Chr(32)), vbCrLf, Chr(32)))

   
    'index=1的數組元素儲存的是預存程序名
    spacepos = InStr(1, src, Chr(32))
    If spacepos > 0 Then
        strleft = Left(src, spacepos - 1)
        src = Trim(Right(src, Len(src) - spacepos))
        ReDim mynode(2)
        mynode(0) = strleft
    Else
        Exit Function
    End If
   
   
   
   
    '預存程序的參數
    parmpos = 1
   
    While Len(src) > 0
       
        commapos = 0
       
        '參數個數從第index=1開始遞增,即第一個參數的index=2
        parmpos = parmpos + 1
        ReDim Preserve mynode(parmpos)
       
       
        If Left(src, 1) <> "'" Then
            iscomma = True '是","開頭
        Else
            iscomma = False '是"'"開頭
        End If
       
       
        If iscomma Then
            '取第一個“,”的位置
            commapos = InStr(1, src, ",")
           
            '沒有“,”是單個參數
            If commapos = 0 Then
                If InStr(1, src, "'") = 0 Then
                    strleft = src
                    src = ""
                Else
                    GoTo exitit
                End If
                '第一個是“,”省略第一個參數
            ElseIf commapos = 1 And Len(src) > 1 Then
                strleft = ""
                src = Right(src, Len(src) - 1)
                '最後一個是“,”省略最後的參數
            ElseIf commapos = Len(src) And Len(src) > 1 Then
                strleft = Left(src, commapos - 1)
                src = ""
                If InStr(1, strleft, "'") > 0 Then
                    GoTo exitit
                End If
            Else
                strleft = Left(src, commapos - 1)
                src = Right(src, Len(src) - commapos)
                If InStr(1, strleft, "'") > 0 Then
                    GoTo exitit
                End If
            End If
           
        Else
           
            '初始化“'”沒有結束
            isend = False
           
            '初始化第一個“'”的位置
            singquotepos = 1
            singquotecount = 1
           
           
            '如果參賽列表僅有一個“'”語法錯誤
            If Len(src) = 1 Then
                GoTo exitit
            Else
                '從第二個位置開始,不斷尋找下一個“'”
                nextsingquotepos = 1
                While Not isend
                   
                    '尋找下一個“'”
                    nextsingquotepos = InStr(nextsingquotepos + 1, src, "'")
                    '如果找到下一個了,找到的“'”數量+1,繼續處理
                    If singquotecount > 0 Then
                        singquotepos = nextsingquotepos
                        singquotecount = singquotecount + 1
                       
                        '非常關鍵的地方,判斷參賽的寫法是否正確
                        '如果已經找到了偶數個“'”,接下來的字元必須是“,”或“ 若干空格,或“'”或“若干空格”
                        If singquotecount Mod 2 = 0 Then
                            strleft = Left(src, singquotepos)
                            strright = Right(src, Len(src) - singquotepos)
                            '接下來沒有字元了
                            If Trim(strright) = "" Then
                                isend = True
                                src = ""
                                '接下來是“,”
                            ElseIf Left(Trim(strright), 1) = "," Then
                                isend = True
                                strright = Trim(strright)
                                src = Right(strright, Len(strright) - 1)
                                '緊接著的字元是“'”
                            ElseIf Left(strright, 1) = "'" Then
                                isend = False
                            Else
                                isend = True
                                GoTo exitit
                            End If
                           
                            '如果已經找到了奇數個“'”,繼續迴圈
                        Else
                            isend = False
                           
                        End If
                       
                        '處理結束
                    Else
                        '如果已經找到了偶數個“'”
                        If singquotecount Mod 2 = 0 Then
                            strleft = Left(src, singquotepos)
                            strright = Trim(Right(src, Len(src) - singquotepos))
                           
                            '如果接下來沒有任何資料,即是最後的參數
                            If strright = "" Then
                                isend = True
                                src = ""
                                '接下來的是分割符號“,”
                            ElseIf Left(strright, 1) = "," Then
                                isend = True
                                src = Right(strright, Len(strright) - 1)
                                '參數非法
                            Else
                                GoTo exitit
                            End If
                            '奇數個的“'”,參數錯誤
                        Else
                            GoTo exitit
                        End If
                       
                    End If
                   
                Wend
               
            End If
           
        End If
       
        mynode(parmpos) = strleft
       
    Wend
   
    'index=1的數組元素儲存的是參數的個數
    mynode(1) = parmpos - 1
    parsesrc = mynode
   
    Exit Function
   
exitit:
    mynode(parmpos) = "參數錯誤"
    mynode(1) = -1
    parsesrc = mynode
   
   
End Function

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.