Click here to Skip to main content
15,896,063 members
Articles / Database Development / SQL Server

Database Helper Class Library to Ease Database Operation

Rate me:
Please Sign up or sign in to vote.
3.09/5 (9 votes)
14 Apr 2007CPOL4 min read 88.3K   3K   57  
Database Helper Class Library to Ease Database Operation
///////////////////////////////////////////////////////////////////////////
// Copyright 2003-2005 Falcon Soon
//
// Author: Soon Chun Boon
// Date: 29 August 2004
// Description: 
// Class that groups a number of SqlTableHelper instance.
///////////////////////////////////////////////////////////////////////////
using System;
using System.Data;
using System.Collections;
using System.Globalization;

namespace DBHelper.SqlClient
{
	/// <summary>
	/// Class that groups a number of <see cref="DBHelper.SqlClient.SqlTableHelper"/> instance.
	/// </summary>
	public class SqlTableHelperCollection : InternalDataCollectionBase
	{
        #region Class Member Declarations
        private ArrayList malCollection;
        #endregion

        /// <summary>
        /// Initializes a new instance of <see cref="DBHelper.SqlClient.SqlTableHelperCollection"/>.
        /// </summary>
		public SqlTableHelperCollection()
		{
            this.malCollection = new ArrayList();
		}

        #region Collection Adding and Removal Methods
        
        /// <summary>
        /// Adds a <see cref="DBHelper.SqlClient.SqlTableHelper"/> instance to collection.
        /// </summary>
        /// <param name="objValue">The <see cref="DBHelper.SqlClient.SqlTableHelper"/> instance
        /// to add.</param>
        public void Add(SqlTableHelper objValue)  
        {
            this.List.Add(objValue);
        }

        /// <summary>
        /// Adds an array of <see cref="DBHelper.SqlClient.SqlTableHelper"/> instances to
        /// collection.
        /// </summary>
        /// <param name="aobjValue">An array of <see cref="DBHelper.SqlClient.SqlTableHelper"/>
        /// instances to add.</param>
        public void AddRange(SqlTableHelper[] aobjValue)
        {
            SqlTableHelper objSqlTableHelper;
            
            if (aobjValue == null)
            {
                return;
            }

            SqlTableHelper[] aobjSqlTableHelper = aobjValue;
            for (int iIndex = 0; iIndex < aobjSqlTableHelper.Length; iIndex++)
            {
                objSqlTableHelper = aobjSqlTableHelper[iIndex];
                if (objSqlTableHelper != null)
                {
                    this.Add(objSqlTableHelper);
                }
            }
        }

        /// <summary>
        /// Removes a <see cref="DBHelper.SqlClient.SqlTableHelper"/> instance from collection.
        /// </summary>
        /// <param name="objValue">The <see cref="DBHelper.SqlClient.SqlTableHelper"/> instance
        /// to remove.</param>
        public void Remove(SqlTableHelper objValue)  
        {
            this.List.Remove(objValue);
        }

        /// <summary>
        /// Removes a <see cref="DBHelper.SqlClient.SqlTableHelper"/> instance with the
        /// specified table name.
        /// </summary>
        /// <param name="strTableName">The table name.</param>
        /// <exception cref="DBHelper.DataException.TableHelperNotInTheCollection">
        /// The <see cref="DBHelper.SqlClient.SqlTableHelper"/> instance with the specified
        /// table name is not in collection.
        /// </exception>
        public void Remove(string strTableName)
        {
            SqlTableHelper objSqlTableHelper = this[strTableName];
            if (objSqlTableHelper == null)
            {
                throw new DataException.TableHelperNotInTheCollection(strTableName);
            }
            this.Remove(objSqlTableHelper);
        }

        /// <summary>
        /// Remove a <see cref="DBHelper.SqlClient.SqlTableHelper"/> instance with the specified
        /// index from collection.
        /// </summary>
        /// <param name="iIndex">The index to remove.</param>
        /// <exception cref="DBHelper.DataException.TableHelperOutOfRange">
        /// The specified index to remove is out of range of collection.
        /// </exception>
        public void RemoveAt(int iIndex)
        {
            SqlTableHelper objSqlTableHelper = this[iIndex];
            if (objSqlTableHelper == null)
            {
                throw new DataException.TableHelperOutOfRange(iIndex);
            }
            this.Remove(objSqlTableHelper);
        }

        /// <summary>
        /// Clears the collection.
        /// </summary>
        public void Clear()
        {
            this.List.Clear();
        }
        #endregion

        #region Collection Indexing Methods
        /// <summary>
        /// Get the index of the specified <see cref="DBHelper.SqlClient.SqlTableHelper"/> instance.
        /// </summary>
        /// <param name="objValue">The <see cref="DBHelper.SqlClient.SqlTableHelper"/> instance
        /// to search for.</param>
        /// <returns>The 0-based index of the TableHelper instance, or -1 if the instance
        /// isn't found in the collection.</returns>
        public int IndexOf(SqlTableHelper objValue)  
        {
            int iCount = this.List.Count;
            for (int iIndex = 0; iIndex < iCount; iIndex++)
            {
                if (objValue == (SqlTableHelper)this.List[iIndex])
                {
                    return (iIndex);
                }
            }
            return (-1);
        }

        /// <summary>
        /// Get the index of <see cref="DBHelper.SqlClient.SqlTableHelper"/> with the 
        /// specified table name.
        /// </summary>
        /// <param name="strTableName">The table name to search for.</param>
        /// <returns>The 0-based index of the TableHelper instance, or -1 if the instance
        /// isn't found in the collection.</returns>
        /// <remarks>
        /// Comparison is case-insensitive.
        /// </remarks>
        public int IndexOf(string strTableName)
        {
            int iIndex = this.InternalIndexOf(strTableName);
            if (iIndex >= 0)
            {
                return (iIndex);
            }
            return (-1);
        }

        /// <summary>
        /// Get the index of <see cref="DBHelper.SqlClient.SqlTableHelper"/> instance
        /// with the specified table name.
        /// </summary>
        /// <param name="strTableName">The table name to search for.</param>
        /// <returns>The 0-based index of the TableHelper instance, or -1 if the instance
        /// isn't found in the collection, or -2 if 2 or more instances with the same name
        /// are found. </returns>
        /// <remarks>
        /// Comparison is case-insensitive.
        /// </remarks>
        internal int InternalIndexOf(string strTableName)
        {
            SqlTableHelper objSqlTableHelper;
            int iFoundIndex = -1;

            if ((strTableName == null) || (0 >= strTableName.Length))
            {
                return (iFoundIndex);
            }

            int iCount = this.List.Count;
            int iSeekIndex = 0;
            for (int iIndex = 0; iIndex < iCount; iIndex++)
            {
                objSqlTableHelper = ((SqlTableHelper) this.List[iIndex]);
                iSeekIndex = InternalDataComparationBase.NamesEqual(objSqlTableHelper.TableName, strTableName, false, CultureInfo.InvariantCulture);
                if (iSeekIndex == 1) //relation is found (case sensitive)
                {
                    return (iIndex);
                }
                if (iSeekIndex == -1) //relation is found (case insensitive)
                {
                    iFoundIndex = ((iFoundIndex == -1) ? iIndex : -2);
                }
            }
            return (iFoundIndex);
        }
        #endregion

        #region Collection Consisting Methods
        /// <summary>
        /// Checks whether collection contains the specified table name.
        /// </summary>
        /// <param name="strTableName">The table name to search for.</param>
        /// <returns>True if collection contains the TableHelper instance with the
        /// specified table name, otherwise False.</returns>
        /// <remarks>
        /// Comparison is case-insensitive.
        /// </remarks>
        public bool Contains(string strTableName)
        {
            return (this.InternalIndexOf(strTableName) >= 0);
        }

        /// <summary>
        /// Checks whether the collection contains the specified table name
        /// with a value specifies the checking is case sensitive or insensitive.
        /// </summary>
        /// <param name="strTableName">The table name to search for.</param>
        /// <param name="bCaseSensitive">Value specifies checking is case sensitive or 
        /// insensitive.</param>
        /// <returns>True if collection contains the TableHelper instance with the specified
        /// table name ortherwise False.</returns>
        /// <remarks>
        /// If 2 or more relations with the same name (case insensitive) are found,
        /// False is returned.
        /// </remarks>
        internal bool Contains(string strTableName, bool bCaseSensitive)
        {
            int iCount;
            SqlTableHelper objSqlTableHelper;
            if (!bCaseSensitive)
            {
                return (this.InternalIndexOf(strTableName) >= 0);
            }
            iCount = this.List.Count;
            for (int iIndex = 0; iIndex < iCount; iIndex++)
            {
                objSqlTableHelper = (SqlTableHelper)this.List[iIndex];
                if (InternalDataComparationBase.NamesEqual(objSqlTableHelper.TableName, strTableName, 
                    true, CultureInfo.InvariantCulture) == 1)
                {
                    return (true);
                }
            }
            return (false);
        }
        #endregion

        #region Collection Accessing Methods
        /// <summary>
        /// Gets the <see cref="DBHelper.SqlClient.SqlTableHelper"/> instance with 
        /// the specified index.
        /// </summary>
        /// <exception cref="DBHelper.DataException.TableHelperOutOfRange">
        /// The specified index is out of range of collection.
        /// </exception>
        public SqlTableHelper this[int iIndex]
        {
            get
            {
                if ((iIndex >= 0) && (iIndex < this.List.Count))
                {
                    return ((SqlTableHelper) this.List[iIndex]);
                }
                throw new DataException.TableHelperOutOfRange(iIndex);
            }
        }

        /// <summary>
        /// Gets the <see cref="DBHelper.SqlClient.SqlTableHelper"/> instance with 
        /// the specified table name.
        /// </summary>
        /// <remarks>
        /// Comparison is case insensitive. If no table name is matched, Null is returned.
        /// </remarks>
        /// <exception cref="DBHelper.DataException.CaseInsensitiveNameConflict">
        /// 2 or more TableHelper instances with same name (case insensitive) are found.
        /// </exception>
        public SqlTableHelper this[string strTableName]
        {
            get
            {
                int iIndex = this.InternalIndexOf(strTableName);
                if (iIndex == -2)
                {
                    throw new DataException.CaseInsensitiveNameConflict(strTableName);
                }
                if (iIndex >= 0)
                {
                    return ((SqlTableHelper) this.List[iIndex]);
                }
                return (null);
            }
        }
        #endregion

        #region Class Property Declarations

        /// <summary>
        /// Gets the collection instance that is used to group
        /// <see cref="DBHelper.SqlClient.SqlTableHelper"/> instances.
        /// </summary>
        protected override ArrayList List
        {
            get
            {
                return (this.malCollection);
            }
        }   

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Malaysia Malaysia
Had worked as analyst programmer for 4 years. Now helping in family business but still involved actively in .Net development whenever there is a free time.

Comments and Discussions