using Ant.Common; using Ant.DbExpressions; using Ant.ORM; using Ant.Query; using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; namespace Ant.Utility { static class OrmUtils { public static void CheckNull(object obj, string paramName = null) { if (obj == null) throw new ArgumentNullException(paramName); } public static bool AreEqual(object obj1, object obj2) { if (obj1 == null && obj2 == null) return true; if (obj1 != null) { return obj1.Equals(obj2); } if (obj2 != null) { return obj2.Equals(obj1); } return object.Equals(obj1, obj2); } public static string GenerateUniqueColumnAlias(DbSqlQueryExpression sqlQuery, string defaultAlias = UtilConstants.DefaultColumnAlias) { string alias = defaultAlias; int i = 0; while (sqlQuery.ColumnSegments.Any(a => string.Equals(a.Alias, alias, StringComparison.OrdinalIgnoreCase))) { alias = defaultAlias + i.ToString(); i++; } return alias; } public static Dictionary Clone(Dictionary source) { Dictionary ret = Clone(source, source.Count); return ret; } public static Dictionary Clone(Dictionary source, int capacity) { Dictionary ret = new Dictionary(capacity); foreach (var kv in source) { ret.Add(kv.Key, kv.Value); } return ret; } public static List Clone(List source, int? capacity = null) { List ret = new List(capacity ?? source.Count); ret.AddRange(source); return ret; } public static List CloneAndAppendOne(List source, T t) { List ret = new List(source.Count + 1); ret.AddRange(source); ret.Add(t); return ret; } public static DbJoinType AsDbJoinType(this JoinType joinType) { switch (joinType) { case JoinType.InnerJoin: return DbJoinType.InnerJoin; case JoinType.LeftJoin: return DbJoinType.LeftJoin; case JoinType.RightJoin: return DbJoinType.RightJoin; case JoinType.FullJoin: return DbJoinType.FullJoin; default: throw new NotSupportedException(); } } public static Type GetFuncDelegateType(Type[] typeArguments) { int parameters = typeArguments.Length; Type funcType = null; switch (parameters) { case 3: funcType = typeof(Func<,,>); break; case 4: funcType = typeof(Func<,,,>); break; case 5: funcType = typeof(Func<,,,,>); break; case 6: funcType = typeof(Func<,,,,,>); break; default: throw new NotSupportedException(); } return funcType.MakeGenericType(typeArguments); } } }