You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
2.8 KiB

2 years ago
  1. using System;
  2. using System.Data;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Text.RegularExpressions;
  6. namespace SqlSugar
  7. {
  8. public class UtilMethods
  9. {
  10. internal static Type GetUnderType(Type oldType)
  11. {
  12. var type = Nullable.GetUnderlyingType(oldType);
  13. return type==null ? oldType : type;
  14. }
  15. internal static Type GetUnderType(PropertyInfo propertyInfo, ref bool isNullable)
  16. {
  17. var unType = Nullable.GetUnderlyingType(propertyInfo.PropertyType);
  18. isNullable = unType != null;
  19. unType = unType ?? propertyInfo.PropertyType;
  20. return unType;
  21. }
  22. internal static Type GetUnderType(PropertyInfo propertyInfo)
  23. {
  24. var unType = Nullable.GetUnderlyingType(propertyInfo.PropertyType);
  25. unType = unType ?? propertyInfo.PropertyType;
  26. return unType;
  27. }
  28. internal static bool IsNullable(PropertyInfo propertyInfo)
  29. {
  30. var unType = Nullable.GetUnderlyingType(propertyInfo.PropertyType);
  31. return unType != null;
  32. }
  33. internal static T IsNullReturnNew<T>(T returnObj) where T : new()
  34. {
  35. if (returnObj.IsNullOrEmpty())
  36. {
  37. returnObj = new T();
  38. }
  39. return returnObj;
  40. }
  41. internal static T ChangeType<T>(T obj, Type type)
  42. {
  43. return (T)Convert.ChangeType(obj, type);
  44. }
  45. internal static T ChangeType<T>(T obj)
  46. {
  47. return (T)Convert.ChangeType(obj, typeof(T));
  48. }
  49. internal static void RepairReplicationParameters(ref string appendSql, SugarParameter[] parameters, int addIndex)
  50. {
  51. if (appendSql.HasValue() && parameters.HasValue())
  52. {
  53. foreach (var parameter in parameters.OrderByDescending(it => it.ParameterName.Length))
  54. {
  55. //Compatible with.NET CORE parameters case
  56. var name = parameter.ParameterName;
  57. var newName = name + addIndex;
  58. appendSql = appendSql.Replace(name, newName);
  59. parameter.ParameterName = newName;
  60. }
  61. }
  62. }
  63. internal static string GetPackTable(string sql, string shortName)
  64. {
  65. return string.Format(" ({0}) {1} ", sql, shortName);
  66. }
  67. internal static string GetParenthesesValue(string dbTypeName)
  68. {
  69. if (Regex.IsMatch(dbTypeName, @"\(.+\)"))
  70. {
  71. dbTypeName = Regex.Replace(dbTypeName, @"\(.+\)", "");
  72. }
  73. dbTypeName = dbTypeName.Trim();
  74. return dbTypeName;
  75. }
  76. internal static T GetOldValue<T>(T value, Action action)
  77. {
  78. action?.Invoke();
  79. return value;
  80. }
  81. }
  82. }