Click here to Skip to main content
Click here to Skip to main content
Articles » Database » Database » ADO.NET » Downloads
 
Add your own
alternative version

Light ORM Library for .NET

, 8 Oct 2010
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)

About the Author

Sergey Busel
Software Developer (Senior)
United States United States
No Biography provided

| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 8 Oct 2010
Article Copyright 2007 by Sergey Busel
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid