Click here to Skip to main content
15,895,833 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.8K   3.1K   184  
This article is about the Light Object-Relational Mapping library.
using System;
using System.Data;
using System.Collections.Generic;

namespace Light.Model
{
	/// <summary>
	/// Represents a table model built from an object type.
	/// </summary>
	public sealed class Table
	{
		private string name = "";
		private string schema = "";
		private string sequence = "";
		private bool readOnly = false;
		
		private AbstractColumn[] columns;
		private AbstractColumn[] primaryColumns;
		private AbstractColumn autoColumn;
		
		private Trigger[] triggers;
		
		internal object CachedObject;
		
		/// <summary>
		/// Creates a new table model.
		/// </summary>
		/// <param name="name">name of the table</param>
		/// <param name="schema">schema to which this table belongs</param>
		/// <param name="sequence">auto number generating sequence</param>
		/// <param name="columns">array of columns for this table</param>
		/// <param name="triggers">array of triggers for this table</param>
		/// <param name="readOnly">whether this table is readonly</param>
		internal Table(string name, string schema, string sequence,
				AbstractColumn[] columns, Trigger[] triggers, bool readOnly)
		{
			this.name = name;
			this.schema = schema;
			this.sequence = sequence;
			this.readOnly = readOnly;
			this.columns = columns;
			this.triggers = triggers;
			
			List<AbstractColumn> pk = new List<AbstractColumn>();
			
			foreach(AbstractColumn c in columns)
			{
				if(c.PrimaryKey)
					pk.Add(c);
				
				if(c.AutoIncrement)
				{
					if(autoColumn == null)
						autoColumn = c;
					else
						throw new DeclarationException("A table cannot have multiple AutoIncrement columns.");
				}
			}
			
			primaryColumns = pk.ToArray();
		}
		
		/// <summary>
		/// Gets the name of this table.
		/// </summary>
		public string Name
		{
			get { return name; }
		}
		
		/// <summary>
		/// Gets the schema of this table.
		/// </summary>
		public string Schema
		{
			get { return schema; }
		}
		
		/// <summary>
		/// Gets the name of the sequence associated with this table.
		/// </summary>
		public string Sequence
		{
			get { return sequence; }
		}
		
		/// <summary>
		/// Gets whether this table is readonly.
		/// </summary>
		public bool ReadOnly
		{
			get { return readOnly; }
		}
		
		/// <summary>
		/// Gets the auto-generated column.
		/// </summary>
		public AbstractColumn AutoIncrementColumn
		{
			get { return autoColumn; }
		}
		
		/// <summary>
		/// Gets the list of all columns.
		/// </summary>
		public AbstractColumn[] Columns
		{
			get { return columns; }
		}
		
		/// <summary>
		/// Gets the list of primary key columns.
		/// </summary>
		public AbstractColumn[] KeyColumns
		{
			get { return primaryColumns; }
		}
		
		/// <summary>
		/// Gets the list of triggers defined on this table.
		/// </summary>
		internal Trigger[] Triggers
		{
			get { return triggers; }
		}
		
		/// <summary>
		/// Gets a column with given name.
		/// </summary>
		/// <param name="name">name of a column</param>
		/// <returns>column with given name</returns>
		public AbstractColumn GetColumn(string name)
		{
			foreach(AbstractColumn column in columns)
			{
				if(column.Name.Equals(name))
					return column;
			}
			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