Click here to Skip to main content
15,895,746 members
Articles / Database Development / SQL Server

Light ORM Library for .NET

Rate me:
Please Sign up or sign in to vote.
4.83/5 (39 votes)
8 Oct 2010CPOL17 min read 222.7K   3.1K   184  
This article is about the Light Object-Relational Mapping library.
using System;
using System.Data;
using System.Collections.Generic;

namespace Light
{
	/// <summary>
	/// Represents a query that can limit the records
	/// affected by a delete or select statements.
	/// </summary>
	public class Query : Light.Model.ParamContainer, IEquatable<Query>
	{
		private string whereClause;
		private string orderClause;
		
		/// <summary>
		/// Creates a new query.
		/// </summary>
		public Query() : this(null)
		{}
		
		/// <summary>
		/// Creates a new query with given WHERE clause.
		/// </summary>
		/// <param name="whereClause">WHERE clause</param>
		public Query(string whereClause) : this(whereClause, null, null)
		{}
		
		/// <summary>
		/// Creates a new query with given WHERE and ORDER BY clauses.
		/// </summary>
		/// <param name="whereClause">WHERE clause</param>
		/// <param name="orderClause">ORDER BY clause</param>
		public Query(string whereClause, string orderClause)
			: this(whereClause, orderClause, null)
		{}
		
		/// <summary>
		/// Creates a new query with given WHERE clause
		/// and a list of parameters used in the WHERE clause.
		/// </summary>
		/// <param name="whereClause">WHERE clause</param>
		/// <param name="parameters">list of parameters</param>
		public Query(string whereClause, IEnumerable<Parameter> parameters)
			: this(whereClause, null, parameters)
		{}
		
		/// <summary>
		/// Creates a new query with given WHERE and ORDER BY clauses
		/// and a list of parameters used in the WHERE clause.
		/// </summary>
		/// <param name="whereClause">WHERE clause</param>
		/// <param name="orderClause">ORDER BY clause</param>
		/// <param name="parameters">list of parameters</param>
		public Query(string whereClause, string orderClause, IEnumerable<Parameter> parameters)
		{
			this.whereClause = whereClause;
			this.orderClause = orderClause;
			if(parameters != null)
				this.parameters.AddRange(parameters);
		}
		
		/// <summary>
		/// Gets or sets the WHERE clause.
		/// Do NOT use the "WHERE" keyword in this clause.
		/// </summary>
		public string Where
		{
			get { return whereClause; }
			set { whereClause = value; }
		}
		
		/// <summary>
		/// Gets or sets the ORDER BY clause.
		/// Do NOT use the "ORDER BY" keyword in this clause.
		/// </summary>
		public string Order
		{
			get { return orderClause; }
			set { orderClause = value; }
		}
		
		/// <summary>
		/// Adds given parameter to this query.
		/// </summary>
		/// <param name="parameter">parameter to add</param>
		/// <returns>returns a reference to this query after adding given parameter</returns>
		public Query Add(Parameter parameter)
		{
			if(parameter != null)
				parameters.Add(parameter);
			return this;
		}
		
		/// <summary>
		/// Adds given parameters to this query.
		/// </summary>
		/// <param name="parameters">parameters to add</param>
		/// <returns>returns a reference to this query after adding given parameters</returns>
		public Query Add(params Parameter[] parameters)
		{
			this.parameters.AddRange(parameters);
			return this;
		}
		
		/// <summary>
		/// Sets the where clause of this query.
		/// </summary>
		/// <param name="whereClause">where clause</param>
		/// <returns>reference to this query</returns>
		public Query SetWhere(string whereClause)
		{
			this.whereClause = whereClause;
			return this;
		}
		
		/// <summary>
		/// Sets the order clause of this query.
		/// </summary>
		/// <param name="orderClause">order clause</param>
		/// <returns>reference to this query</returns>
		public Query SetOrder(string orderClause)
		{
			this.orderClause = orderClause;
			return this;
		}
		
		#region - IEquatable -
		/// <summary>
		/// Checks for equality with another object.
		/// </summary>
		/// <param name="obj">another object</param>
		/// <returns>true if equal, false otherwise</returns>
		public override bool Equals(object obj)
		{
			if(obj is Query)
				return Equals((Query)obj);
			else
				return false;
		}
		
		/// <summary>
		/// Checks for equality with another query object.
		/// </summary>
		/// <param name="other">another query</param>
		/// <returns>true if equal, false otherwise</returns>
		public bool Equals(Query other)
		{
			return whereClause == null ? other.whereClause == null : whereClause.Equals(other.whereClause) &&
				orderClause == null ? other.orderClause == null : orderClause.Equals(other.orderClause);
		}
		
		/// <summary>
		/// Gets the hash code for this instance.
		/// </summary>
		/// <returns>hash code</returns>
		public override int GetHashCode()
		{
			return base.GetHashCode();
		}
		#endregion
		
		/// <summary>
		/// Gets a null query.
		/// </summary>
		public static Query Null
		{
			get { return null; }
		}
	}
}

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
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions