OrmUtils.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using Ant.Common;
  2. using Ant.DbExpressions;
  3. using Ant.ORM;
  4. using Ant.Query;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Data;
  8. using System.Linq;
  9. using System.Text;
  10. namespace Ant.Utility
  11. {
  12. static class OrmUtils
  13. {
  14. public static void CheckNull(object obj, string paramName = null)
  15. {
  16. if (obj == null)
  17. throw new ArgumentNullException(paramName);
  18. }
  19. public static bool AreEqual(object obj1, object obj2)
  20. {
  21. if (obj1 == null && obj2 == null)
  22. return true;
  23. if (obj1 != null)
  24. {
  25. return obj1.Equals(obj2);
  26. }
  27. if (obj2 != null)
  28. {
  29. return obj2.Equals(obj1);
  30. }
  31. return object.Equals(obj1, obj2);
  32. }
  33. public static string GenerateUniqueColumnAlias(DbSqlQueryExpression sqlQuery, string defaultAlias = UtilConstants.DefaultColumnAlias)
  34. {
  35. string alias = defaultAlias;
  36. int i = 0;
  37. while (sqlQuery.ColumnSegments.Any(a => string.Equals(a.Alias, alias, StringComparison.OrdinalIgnoreCase)))
  38. {
  39. alias = defaultAlias + i.ToString();
  40. i++;
  41. }
  42. return alias;
  43. }
  44. public static Dictionary<TKey, TValue> Clone<TKey, TValue>(Dictionary<TKey, TValue> source)
  45. {
  46. Dictionary<TKey, TValue> ret = Clone<TKey, TValue>(source, source.Count);
  47. return ret;
  48. }
  49. public static Dictionary<TKey, TValue> Clone<TKey, TValue>(Dictionary<TKey, TValue> source, int capacity)
  50. {
  51. Dictionary<TKey, TValue> ret = new Dictionary<TKey, TValue>(capacity);
  52. foreach (var kv in source)
  53. {
  54. ret.Add(kv.Key, kv.Value);
  55. }
  56. return ret;
  57. }
  58. public static List<T> Clone<T>(List<T> source, int? capacity = null)
  59. {
  60. List<T> ret = new List<T>(capacity ?? source.Count);
  61. ret.AddRange(source);
  62. return ret;
  63. }
  64. public static List<T> CloneAndAppendOne<T>(List<T> source, T t)
  65. {
  66. List<T> ret = new List<T>(source.Count + 1);
  67. ret.AddRange(source);
  68. ret.Add(t);
  69. return ret;
  70. }
  71. public static DbJoinType AsDbJoinType(this JoinType joinType)
  72. {
  73. switch (joinType)
  74. {
  75. case JoinType.InnerJoin:
  76. return DbJoinType.InnerJoin;
  77. case JoinType.LeftJoin:
  78. return DbJoinType.LeftJoin;
  79. case JoinType.RightJoin:
  80. return DbJoinType.RightJoin;
  81. case JoinType.FullJoin:
  82. return DbJoinType.FullJoin;
  83. default:
  84. throw new NotSupportedException();
  85. }
  86. }
  87. public static Type GetFuncDelegateType(Type[] typeArguments)
  88. {
  89. int parameters = typeArguments.Length;
  90. Type funcType = null;
  91. switch (parameters)
  92. {
  93. case 3:
  94. funcType = typeof(Func<,,>);
  95. break;
  96. case 4:
  97. funcType = typeof(Func<,,,>);
  98. break;
  99. case 5:
  100. funcType = typeof(Func<,,,,>);
  101. break;
  102. case 6:
  103. funcType = typeof(Func<,,,,,>);
  104. break;
  105. default:
  106. throw new NotSupportedException();
  107. }
  108. return funcType.MakeGenericType(typeArguments);
  109. }
  110. }
  111. }