All pastes #202916 Raw Edit

Shin-Ra

public text v1 · immutable
#202916 ·published 2006-10-14 20:39 UTC
rendered paste body
/******************************************************************
** DarkBulletin v1.0 - Licence Number = {0}
** ----------------------------------------------------------------
** Copyright 2006 Keith Rusler. All Rights Reserved.
** This file may not be redistributed in whole or significant part.
** ------------------- Not Free Software --------------------------
*******************************************************************/
#region Using....
using System;
using System.Diagnostics;
using System.Web;
#endregion
#region Namespace....
//Beginning of DarkBulletin.Data namespace
namespace DarkBulletin.Data
{
    //
    // TODO: Make an option in the forum settings to make turn this class on or off
    //
    #region QueryInfo....
    // Beginning of QueryInfoClass
    /// <summary>
    /// QueryInfo class for finding the calculation of the time it takes for a
    /// SQL Query to execute. This class cannot be inherited.
    /// </summary>
    /// <remarks>
    /// Use this class to get the calculations of the SQL Query time that it takes
    /// from the start of the execution to the end of the execution. To determine
    /// how long it takes for it to execute. This class is merely for debugging
    /// purposes. If you want to use it other than debugging purposes then use it.
    /// But it is recommand not to use it unless it is for debugging.
    /// </remarks>
    public sealed class QueryInfo : IDisposable
    {
        #region Variables....
        /// <summary>
        /// Creates a Stopwatch object
        /// </summary>
        private Stopwatch stopwatch = new Stopwatch( );
        /// <summary>
        /// Creates a variable for sql command usage
        /// </summary>
        private string m_cmd;
        #endregion
        #region Members....
        /// <summary>
        /// QueryInfo's default constructor
        /// </summary>
        /// <param name="sql"></param>
        public QueryInfo(string sql)
        {
            // Assign the sql parameter to m_cmd
            m_cmd = sql;
            
            // If the NumQueries equals null...
            if( HttpContext.Current.Items[ "NumQueries" ] == null )
            {
                // Then start it at one
                HttpContext.Current.Items[ "NumQueries" ] = 1;
            }
            // If not null
            else
            {
                // Then add 1 to the current number
                HttpContext.Current.Items[ "NumQueries" ] = 1 + ( int ) HttpContext.Current.Items[ "NumQueries" ];
            }
            // Start the stopwatch
            stopwatch.Start( );
        }
        // End of the QueryInfo constructor
        /// <summary>
        /// Resets the QueryInfo values
        /// <example>
        /// <remarks>
        /// If you want to make the QueryInfo stats to reset based on a 
        /// certain condition use this to do that.
        /// </remarks>
        /// <code>
        /// QueryInfo qi = new QueryInfo();
        /// gi.Reset();
        /// </code>
        /// </example>
        /// </summary>
        public static void Reset( )
        {
            // Reset the NumQueries to 0
            HttpContext.Current.Items[ "NumQueries" ] = 0;
            // Reset the TimeQueries to 0
            HttpContext.Current.Items[ "TimeQueries" ] = ( double ) 0;
            // Reset the CmdQueries to an empty string
            HttpContext.Current.Items[ "CmdQueries" ] = "";
        }
        // end of the Reset method
        /// <summary>
        /// Returns the NumQueryies number
        /// </summary>
        public static int Count
        {
            get { return ( int ) HttpContext.Current.Items[ "NumQueries" ]; }
        }
        /// <summary>
        /// Returns the Duration of the QueryInfo in millseconds
        /// </summary>
        public static double Duration
        {
            get { return ( double ) HttpContext.Current.Items[ "TimeQueries" ]; }
        }
        /// <summary>
        /// Returns the QueryInfo SQL Command
        /// </summary>
        public static string Commands
        {
            get { return ( string ) HttpContext.Current.Items[ "CmdQueries" ]; }
        }
        
        #region QueryInfo IDisposable Members....
        /// <summary>
        /// Stops the QueryInfo and calculates the results of the QueryInfo SQL Statement
        /// </summary>
        /// <remarks>
        /// This method is used to stop the QueryInfo and then does the calculation of
        /// the of the info and reports it back to the page.
        /// </remarks>
        // Beginning of the Dispose method
        public void Dispose()
        {
            // Stops the stopwatch
            stopwatch.Stop();
            // Divides the stopwatch.ElapsedMilliseconds by 1000.0
            double duration = stopwatch.ElapsedMilliseconds / 1000.0;
            // Assigns the string format to the cmd variable
            m_cmd = string.Format("{0}: {1:N3}", m_cmd, duration);

            // If the TimeQueries equals null then
            if (HttpContext.Current.Items["TimeQueries"] == null)
            {
                // Duration is assigned to the TimeQueries
                HttpContext.Current.Items["TimeQueries"] = duration;
            }
            // If TimeQueries is not null then
            else
            {
                // TimeQueries is assigned with the Duration plus whatever TimeQueries currently is
                HttpContext.Current.Items["TimeQueries"] = duration
                                                             + (double)HttpContext.Current.Items["TimeQueries"];
                // If CmdQueries is null then
                if (HttpContext.Current.Items["CmdQueries"] == null)
                {
                    // CmdQueries is assigned the SQL Query
                    HttpContext.Current.Items["CmdQueries"] = m_cmd;
                }
                // If CmdQueries is not null then
                else
                {
                    // Then CmdQueries is added with HTML Break plus the SQL Query
                    HttpContext.Current.Items["CmdQueries"] += "<br />" + m_cmd;
                }
            }
        }
        // End of the Dispose method
        #endregion
        
        #endregion
    }
    // End of QueryInfo class
    #endregion
}
// End of DarkBulletin.Data namespace
#endregion