Skinnysite.net

... un modo di esprimersi.

Font Size

Cpanel

Monitorare le chiamate alle alle stored procedure

Valutazione attuale:  / 0
ScarsoOttimo 

Recentemente ho dovuto verificare l'esecuzione di alcune stored procedure e ho pensato che forse la cosa migliore era quella di fare una piccola classe di supporto che con qualche proprietà, mi permettesse di monitorare le varie chiamate. Così è nata la classe CommandRecordset che presento qui sotto. Preciso che la classe è stata realizzata esplicitamente per comandi che lanciano stored procedure.

class CommandRecordset
    private m_CursorType, m_LockType, m_Opt, m_Log,
    private m_CommandOutput, m_Out, m_URL, m_CursorLocation

    'string
    Public Property Let URL(value)
        'questa proprietà di sola scrittura serve per indicare alla classe
        'in quale url del sito ci troviamo se viene impostata nel file di log
        'verrà tracciata anche la pagina in cui il comando è andato in esecuzione
        m_URL = value
    End Property

    'int
    Public Property Let CursorTypeEnum(value)
        'proprietà di scrittura in cui si imposta il cursortype
        m_CursorType = value
    End Property

    'int
    Public Property Let LockTypeEnum(value)
        'proprietà di scrittura in cui si imposta il LockType
        m_LockType = value
    End Property

    'int
    Public Property Let CursorLocationEnum(value)
        'proprietà di scrittura in cui si legge il CursorLocation
        m_CursorLocation = value
    End Property

    Public Property Let Opt(value)
        'proprietà di scrittura in cui si imposta le opzioni del cursore
        m_Opt = value
    End Property

    'boolena
    Public Property Let Log(value)
        m_Log = value
    End Property

    'string
    Public Property Get Output()
        Output = m_Out
    End Property
    
    'boolena
    Public Property Let CommandOutput(value)
        m_CommandOutput = value
    End Property

    Private Sub Class_Initialize
        'inizializzo variabili e costanti
        Const adOpenForwardOnly = 0
        Const adLockReadOnly = 1
        Const adCmdStoredProc = 4
        Const adUseClient = 3
        
        m_CursorLocation = null
        m_CursorType = null
        m_LockType = null
        m_Opt = null
        m_Out = ""
    end sub

    function Open(cmd)
        dim rs, fs, fname

        'controllo se sono state valorizzate le proprietà del recordset
        'se non sono state valorizzate imposto dei valori di default
        if isnull(m_CursorType) then m_CursorType = adOpenForwardOnly
        if isnull(m_LockType) then m_LockType = adLockReadOnly
        if isnull(m_Opt) then m_Opt = adCmdStoredProc

        'recordset per l'apertura del comando
        set rs = CreateObject("ADODB.Recordset")

        'se è stato richiesto l'output del comando imposto la variabile
        'che fa tornare indietro la proprietà
        if m_CommandOutput then
            m_Out = writeStoredProcedure(cmd)
        else
            m_Out = ""
        end if

        'se è stato richiesto di scrivere un file di log lo scrivo
        if m_Log then
            set fs = Server.CreateObject("Scripting.FileSystemObject")
            'apro un file dove scrivere il log
            set fname = fs.OpenTextFile("c:\log\ado_debug.log", 8, true)
            if not m_CommandOutput then
                'questo è il caso in cui è stato richiesto solo il file di log
                fname.WriteLine now & " : " & m_URL & vbNewLine & writeStoredProcedure(cmd)
            else
                'questo è il caso in cui è sono stati richiesti output e file di log
                fname.WriteLine now & " : " & m_URL & vbNewLine & m_Out
            end if
            fname.Close
            set fname = nothing
            set fs = nothing
        end if

        'aggiungo in coda alla variabile di output anche un <br /> per
        'rendere più leggile la stampa a video
        m_Out = m_Out & "<br />"

        'imposto il CursorLocation di default se non ne ho specificato uno
        if isnull(m_CursorLocation) then m_CursorLocation = adUseClient
        rs.cursorlocation = m_CursorLocation

        'apertura del recordset
        rs.Open cmd, ,m_CursorType, m_LockType, m_Opt

        'restituzione del recordset
        set Open = rs
    end function
    
    'questa funzione analizza la proprietà commandtext del comando e restituisce una stringa
    'che può essere lanciata in sql server
    private function writeStoredProcedure(cmd)
        dim prmValue, commandTextValue, prmValueList, declaration, prmName, s

        'con un po' di replace ripulisco il la proprietà CommandText
        commandTextValue = replace(cmd.CommandText, "{ call ", "")
        commandTextValue = replace(commandTextValue, "?", "")
        commandTextValue = replace(commandTextValue, ",", "")
        commandTextValue = replace(replace(commandTextValue, "(", ""), ")", "")
        commandTextValue = replace(commandTextValue, "}", "")
        commandTextValue = "exec " & Trim(commandTextValue)

        'ciclo su i parametri presenti nel comando della procedura
        for each prm in cmd.parameters
            prmValue = prm.value
            
            'controllo quelli che sono di tipo stringa e gli metto gli apici
            select case prm.type
            case adChar, adVarChar, adWChar, adLongVarChar, adVarWChar, adLongVarWChar
                'se ho dei parametri di output faccio anche dichiarazione
                'per sql server e aggiungo il parametro alla lista
                if prm.Direction = adParamOutput then
                    prmName = replace(prm.name, "@", "")
                    declaration = declaration & "declare @" & prmName & " nvarchar(4000)" & vbNewLine
                    prmValue = " @" & prmName & " output,"
                else
                    prmValue = " '" & prmValue & "',"
                end if
                prmValueList = prmValueList & prmValue
            case else
                if prm.Direction = adParamOutput then
                    prmName = replace(prm.name, "@", "")
                    declaration = declaration & "declare @" & prmName & " bigint" & vbNewLine
                    prmValue = " @" & prmName & " output,"
                else
                    prmValue = " " & prmValue & ","
                end if
                prmValueList = prmValueList & prmValue

            end select
        next

        s = declaration & commandTextValue & prmValueList
        writeStoredProcedure = left(s, len(s)-1)
    end function
end class

Qui riporto un esempio di utilizzo della classe

set rsCommand = new CommandRecordset
with rsCommand
    '.URL = url & "?" & Request.Querystring
    .CursorTypeEnum = adOpenStatic
    .LockTypeEnum = adLockReadOnly
    .CommandOutput = true
    .Log = true
    set rsProducts = .open(cmd)
    response.write .Output
end with
set rsCommand = nothing
Sei qui: Home Code Snippets ASP Classic Monitorare le chiamate alle alle stored procedure