Click here to Skip to main content
11,490,801 members (66,096 online)
Click here to Skip to main content
Add your own
alternative version

Database Helper Class Library to Ease Database Operation

, 14 Apr 2007 CPOL 56.9K 2.1K 56
Database Helper Class Library to Ease Database Operation
article_demo.zip
DBHelperQuickRelationshipSamples
DBHelperQuickRelationshipSamples
App.ico
DBHelperQuickRelationshipSamples.csproj.user
DBHelperQuickRelationshipSamples.suo
DBHelperQuickSelectSamples
DBHelperQuickSelectSamples
DBHelperQuickSelectSamples.csproj.user
DBHelperQuickSelectSamples.suo
DBHelperQuickHelperSamples
DBHelperQuickHelperSamples
App.ico
DBHelperQuickHelperSamples.csproj.user
DBHelperQuickHelperSamples.suo
DBHelperQuickInsUpdDelSamples
DBHelperQuickInsUpdDelSamples
DBHelperQuickInsUpdDelSamples.csproj.user
DBHelperQuickInsUpdDelSamples.suo
article_src.zip
Microsoft.ApplicationBlocks.ExceptionManagement.Interfaces
Microsoft.ApplicationBlocks.ExceptionManagement.Interfaces.csproj.user
Microsoft.ApplicationBlocks.ExceptionManagement.suo
DbHelper
DBHelper.chm
DBHelper.csproj.user
DbHelper.ndoc
DBHelper.suo
Microsoft.ApplicationBlocks.ExceptionManagement
ExceptionManagerText.xsx
Microsoft.ApplicationBlocks.ExceptionManagement.csproj.user
Microsoft.ApplicationBlocks.suo
///////////////////////////////////////////////////////////////////////////
// Copyright 2003-2005 Falcon Soon
//
// Author: Soon Chun Boon
// Date: 24 October 2004
// Description: 
// Class that groups a number of DBRelation instance related to 
// SqlTableHelper instance.
///////////////////////////////////////////////////////////////////////////
using System;
using System.Data;
using System.Collections;


namespace DBHelper.SqlClient
{
	/// <summary>
    /// Class that groups a number of <see cref="DBHelper.DBRelation"/> instance related to 
    /// <see cref="DBHelper.SqlClient.SqlTableHelper"/> instance.
	/// </summary>
	internal class SqlTableRelationCollection : DBRelationCollection
	{
        #region Class Member Declarations
        private ArrayList malCollection;
        private SqlTableHelper mobjTable;
        private bool mbParentCollection;
        #endregion

        /// <summary>
        /// Initializes a new instance of <see cref="DBHelper.SqlClient.SqlTableRelationCollection"/>
        /// with the specified <see cref="DBHelper.SqlClient.SqlTableHelper"/> instance and a value
        /// specifying whether this collection is parent or child collection.
        /// </summary>
        /// <param name="objTable">The <see cref="DBHelper.SqlClient.SqlTableHelper"/> instance.</param>
        /// <param name="bParentCollection">True if parent collection otherwise False
        /// if child collection.</param>
        /// <exception cref="DBHelper.DataException.RelationTableNull">
        /// <i>objTable</i> parameter is not provided.
        /// </exception>
        internal SqlTableRelationCollection(SqlTableHelper objTable, bool bParentCollection)
        {
            if (objTable == null)
            {
                throw new DataException.RelationTableNull();
            }
            this.mobjTable = objTable;
            this.mbParentCollection = bParentCollection;
            this.malCollection = new ArrayList();
        }

        /// <summary>
        /// Adds a <see cref="DBHelper.DBRelation"/> instance to collection.
        /// </summary>
        /// <param name="objRelation">The relation to add.</param>
        private void AddCache(DBRelation objRelation)
        {
            this.malCollection.Add(objRelation);
        }

        /// <summary>
        /// Adds a <see cref="DBHelper.DBRelation"/> instance to collection.
        /// </summary>
        /// <param name="objRelation">The relation to add.</param>
        /// <exception cref="DBHelper.DataException.ChildTableMismatch">
        /// Child table in relation is mismatch with this collection's TableHelper
        /// instance (for parent collection).
        /// </exception>
        /// <exception cref="DBHelper.DataException.ParentTableMismatch">
        /// Parent table in relation is mismatch with this collection's TableHelper
        /// instance (for child collection).
        /// </exception>
        /// <exception cref="DBHelper.DataException.DuplicateNameException">
        /// Relation with the same name already exist in collection.
        /// </exception>
        protected override void AddCore(DBRelation objRelation)
        {
            if (this.mbParentCollection)
            {
                if (objRelation.ChildTable != this.mobjTable.Data)
                {
                    throw new DataException.ChildTableMismatch();
                }
            }
            else if (objRelation.ParentTable != this.mobjTable.Data)
            {
                throw new DataException.ParentTableMismatch();
            }

            if (this[objRelation.RelationName] != null)
            {
                throw new DataException.DuplicateNameException(objRelation.RelationName + 
                                              " is duplicate in collection.");
            }

            this.AddCache(objRelation);
        }

        /// <summary>
        /// Remove a <see cref="DBHelper.DBRelation"/> instance from collection.
        /// </summary>
        /// <param name="objRelation">The relation to remove.</param>
        /// <exception cref="DBHelper.DataException.RelationDoesNotExist">
        /// The relation to remove is not exist.
        /// </exception>
        private void RemoveCache(DBRelation objRelation)
        {
            for (int iIndex = 0; iIndex < this.malCollection.Count; iIndex++)
            {
                if (objRelation == this.malCollection[iIndex])
                {
                    this.malCollection.RemoveAt(iIndex);                    
                    return;
                }
            }
            throw new DataException.RelationDoesNotExist();
        }

        /// <summary>
        /// Remove a <see cref="DBHelper.DBRelation"/> instance from collection.
        /// </summary>
        /// <param name="objRelation">The relation to remove.</param>
        /// <exception cref="DBHelper.DataException.RelationDoesNotExist">
        /// The relation to remove is not exist.
        /// </exception>
        /// <exception cref="DBHelper.DataException.ChildTableMismatch">
        /// Child table in relation is mismatch with this collection's TableHelper
        /// instance (for parent collection).
        /// </exception>
        /// <exception cref="DBHelper.DataException.ParentTableMismatch">
        /// Parent table in relation is mismatch with this collection's TableHelper
        /// instance (for child collection).
        /// </exception>
        protected override void RemoveCore(DBRelation objRelation)
        {
            if (this.mbParentCollection)
            {
                if (objRelation.ChildTable != this.mobjTable.Data)
                {
                    throw new DataException.ChildTableMismatch();
                }
            }
            else if (objRelation.ParentTable != this.mobjTable.Data)
            {
                throw new DataException.ParentTableMismatch();
            }
            this.RemoveCache(objRelation);
        }

        /// <summary>
        /// Gets <see cref="DBHelper.DBRelation"/> instance with the specified index.
        /// </summary>
        /// <exception cref="DBHelper.DataException.RelationOutOfRange">
        /// The specified index is out of range of collection.
        /// </exception>
        public override DBRelation this[int iIndex]
        {
            get
            {
                if ((iIndex < 0) || (iIndex >= this.malCollection.Count))
                {
                    throw new DataException.RelationOutOfRange(iIndex);
                }
                return (DBRelation) this.List[iIndex];
            }
        }

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

        #region Class Property Declaration

        /// <summary>
        /// Gets the collection instance that is used to group
        /// <see cref="DBHelper.DBRelation"/> instances.
        /// </summary>
        /// <exception cref="System.ArgumentNullException">
        /// <see cref="DBHelper.SqlClient.SqlTableHelper"/> instance is not provided.
        /// </exception>
        protected override ArrayList List
        {
            get
            {
                if (this.mobjTable == null)
                {
                    throw new ArgumentNullException("objTable");
                }
                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)

Share

About the Author

falconsoon

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150520.1 | Last Updated 14 Apr 2007
Article Copyright 2007 by falconsoon
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid