Click here to Skip to main content
15,896,269 members
Articles / Web Development / ASP.NET

Very Powerful, Generic, Irony Based, Database Searcher

Rate me:
Please Sign up or sign in to vote.
4.00/5 (6 votes)
26 Aug 2010Ms-PL8 min read 30.4K   546   29  
Usage of Irony to produce a Google-like search tool on any column in a database
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;

namespace searchComponent.structure
{
    /// <summary>
    /// This class represents a table that will be considered in the search.
    /// </summary>
    public class TableStructure
    {
        private static readonly ILog log = LogManager.GetLogger("Standard");

        #region Fields
        /// <summary>
        /// The name of the table as it appears in the sql database
        /// </summary>
        private string _name;
        /// <summary>
        /// The list of columns to consider from this table
        /// </summary>
        private List<Column> _columns;
        /// <summary>
        /// The join used for the table in the structure, this is set when the
        /// table is added to the structure.
        /// </summary>
        protected JoinStatement _join;

        #endregion

        #region Attributes
        /// <summary>
        /// The name of the table as it appears in the sql database
        /// </summary>
        public string Name
        {
            get { return this._name; }
        }
        /// <summary>
        /// The list of columns to consider from this table
        /// </summary>
        public List<Column> Columns
        {
            get { return this._columns; }
        }
        /// <summary>
        /// The join used for the table in the structure, this is set when the
        /// table is added to the structure.
        /// </summary>
        public JoinStatement Join
        {
            get { return this._join; }
            set { this._join = value; }
        }
        #endregion

        #region Constructors
        /// <summary>
        /// The constructor of the table structure. All we need is the name of the table. It initializes
        /// the set of columns.
        /// </summary>
        /// <param name="aName"></param>
        public TableStructure(string aName)
        {
            this._columns = new List<Column>();
            this._name = aName;
        }
        #endregion

        #region Methods
        /// <summary>
        /// This method checks if the parameter contains the name of the table and a valid
        /// name of column
        /// </summary>
        /// <param name="baseTblCol"></param>
        /// <returns>true if the column is contained in this table</returns>
        public bool CheckStringColumn(string baseTblCol)
        {
            if (!baseTblCol.StartsWith(this._name + "."))
            {
                return false; 
            }

            string columnName = baseTblCol.Substring(baseTblCol.IndexOf(".") + 1);
            foreach (Column objColumn in this._columns)
            {
                if (columnName.Equals(objColumn.Name))
                    return true;
            }

            return false;
        }

        /// <summary>
        /// This method adds a column to the table. The column should be a well formed object (not null).
        /// Also, the column cannot be already in the table, a chekc is made against the list of columns.
        /// </summary>
        /// <param name="objColumn">new colun to be added</param>
        public void addColumn(Column objColumn)
        {
            if (objColumn == null)
            {
                log.Error("Column to be added cannot be null");
                return;
            }

            foreach (Column obj in _columns)
            {
                if (obj.Name.Equals(objColumn.Name))
                {
                    log.Error("Column " + objColumn.Name + " has already been added");
                    return;
                }
            }

            _columns.Add(objColumn);
        }

        /// <summary>
        /// Returns thee JOIN part of the query corresponding to this structure. First defines
        /// the type and then it forms the join
        /// </summary>
        /// <returns></returns>
        public string GetJoinQuery()
        {
            string result = "";
            switch(Join.Type) {
                case JoinStatement.JoinType.Inner:
                    result += " INNER JOIN ";
                    break;
                case JoinStatement.JoinType.Left:
                    result += " LEFT JOIN ";
                    break;
                case JoinStatement.JoinType.Outer:
                    result += " OUTER JOIN ";
                    break;
                case JoinStatement.JoinType.Right:
                    result += " RIGHT JOIN ";
                    break;
                default:
                    log.Error("Type of Join not known");
                    throw new Exception("Type of Join NOT known");
            }

            result += Name + " ON " + Join.BaseTableColumn + " = " + Join.JoinTableColumn;

            return result;
        }
        #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 Microsoft Public License (Ms-PL)


Written By
Chief Technology Officer Artexacta
Bolivia Bolivia
just a coder. Love Java, but working on asp.net

Comments and Discussions