Click here to Skip to main content
15,886,258 members
Articles / Hosted Services / Azure

Kerosene ORM: a dynamic, configuration-less and self-adaptive ORM for POCO objects supporting a SQL-like syntax from C#

Rate me:
Please Sign up or sign in to vote.
4.96/5 (71 votes)
1 Mar 2015CPOL35 min read 542.1K   4.6K   212  
The seventh version of the dynamic, configuration-less and self-adaptive Kerosene ORM library, that provides full real support for POCO objects, natural SQL-like syntax from C#, and advanced capabilities while being extremely easy to use.
// ======================================================== UberHelper.TableNames.cs
namespace Kerosene.ORM.Maps.Concrete
{
	using Kerosene.Tools;
	using Kerosene.ORM.Core;
	using System;
	using System.Collections.Generic;

	// ==================================================== 
	/// <summary>
	/// Internal helpers and extensions.
	/// </summary>
	internal static partial class UberHelper
	{
		/// <summary>
		/// Generates a list of possible table names given the name source by using some
		/// pluralization rules.
		/// </summary>
		static List<string> TableNameCandidatesPlural(string source)
		{
			List<string> list = new List<string>();

			list.Add(source + "s"); // bath
			list.Add(source + "S");

			list.Add(source + "es"); // dish
			list.Add(source + "ES");

			if (source.EndsWith("y")) list.Add(source.Substring(0, source.Length - 1) + "ies"); // country
			if (source.EndsWith("Y")) list.Add(source.Substring(0, source.Length - 1) + "IES");

			if (source.EndsWith("d")) list.Add(source.Substring(0, source.Length - 1) + "ren"); // child
			if (source.EndsWith("D")) list.Add(source.Substring(0, source.Length - 1) + "REN");

			if (source.EndsWith("f")) list.Add(source.Substring(0, source.Length - 1) + "ves"); // leaf
			if (source.EndsWith("F")) list.Add(source.Substring(0, source.Length - 1) + "VES");

			if (source.EndsWith("fe")) list.Add(source.Substring(0, source.Length - 2) + "ves"); // knife
			if (source.EndsWith("FE")) list.Add(source.Substring(0, source.Length - 2) + "VES");

			if (source.Contains("oo")) list.Add(source.Replace("oo", "ee")); // foot
			if (source.Contains("OO")) list.Add(source.Replace("OO", "EE"));

			if (source.Contains("man")) list.Add(source.Replace("man", "men")); // woman
			if (source.Contains("MAN")) list.Add(source.Replace("MAN", "MEN"));

			if (source.Contains("person")) list.Add(source.Replace("person", "people")); // people
			if (source.Contains("Person")) list.Add(source.Replace("Person", "People"));
			if (source.Contains("PERSON")) list.Add(source.Replace("PERSON", "PEOPLE"));

			list.Add(source + "n"); // bee (old english)
			list.Add(source + "N");

			list.Add(source + "en"); // ox
			list.Add(source + "EN");

			list.Add(source + "x"); // beau
			list.Add(source + "X");

			if (source.EndsWith("a")) list.Add(source + "e"); // alumna
			if (source.EndsWith("A")) list.Add(source + "E");

			if (source.EndsWith("a")) list.Add(source.Substring(0, source.Length - 1) + "i");
			if (source.EndsWith("A")) list.Add(source.Substring(0, source.Length - 1) + "I");

			if (source.EndsWith("x")) list.Add(source.Substring(0, source.Length - 2) + "ices"); // matrix
			if (source.EndsWith("X")) list.Add(source.Substring(0, source.Length - 2) + "ICES");

			if (source.EndsWith("is")) list.Add(source.Substring(0, source.Length - 2) + "es"); // axis
			if (source.EndsWith("IS")) list.Add(source.Substring(0, source.Length - 2) + "ES");

			if (source.EndsWith("um")) list.Add(source.Substring(0, source.Length - 2) + "a"); // addendum
			if (source.EndsWith("UM")) list.Add(source.Substring(0, source.Length - 2) + "A");

			if (source.EndsWith("na")) list.Add(source.Substring(0, source.Length - 1) + "ous"); // phenomena
			if (source.EndsWith("NA")) list.Add(source.Substring(0, source.Length - 1) + "OUS");

			if (source.EndsWith("us")) list.Add(source.Substring(0, source.Length - 2) + "i"); // alumnus
			if (source.EndsWith("US")) list.Add(source.Substring(0, source.Length - 2) + "I");

			if (source.EndsWith("us")) list.Add(source.Substring(0, source.Length - 2) + "ora"); // corpus
			if (source.EndsWith("US")) list.Add(source.Substring(0, source.Length - 2) + "ORA");

			if (source.EndsWith("us")) list.Add(source.Substring(0, source.Length - 2) + "era"); // genus
			if (source.EndsWith("US")) list.Add(source.Substring(0, source.Length - 2) + "ERA");

			if (source.EndsWith("o")) list.Add(source.Substring(0, source.Length - 1) + "i"); // biscotto
			if (source.EndsWith("O")) list.Add(source.Substring(0, source.Length - 1) + "I");

			return list;
		}

		/// <summary>
		/// Returns a suitable table name on the data link based upon the given source, or null
		/// if no table can be found.
		/// </summary>
		internal static string LocateTableName(IDataLink link, string source)
		{
			if (link == null) throw new ArgumentNullException("link", "Data link cannot be null.");
			source = source.Validated("Type Name Source");

			var names = new List<string>();
			names.Add(source);
			names.AddRange(TableNameCandidatesPlural(source));
			
			foreach (var name in names)
			{
				var found = false;
				var cmd = link.From(x => name).Top(1);
				var rec = (IRecord)null;

				try { rec = (IRecord)cmd.First(); found = true; }
				catch { }
				finally
				{
					if (rec != null) rec.Dispose(disposeSchema: true);
					if (cmd != null) cmd.Dispose();
				}

				if (found) return name;
			}

			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
Spain Spain
mbarbac has worked in start-ups, multinational tech companies, and consulting ones, serving as CIO, CTO, SW Development Director, and Consulting Director, among many other roles.

Solving complex puzzles and getting out of them business value has ever been among his main interests - and that's why he has spent his latest 25 years trying to combine his degree in Theoretical Physics with his MBA... and he is still trying to figure out how all these things can fit together.

Even if flying a lot across many countries, along with the long working days that are customary in IT management and Consultancy, he can say that, after all, he lives in Spain (at least the weekends).

Comments and Discussions