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.

86 lines
3.7 KiB

2 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Reflection;
  6. namespace SqlSugar
  7. {
  8. public partial class AdoAccessory
  9. {
  10. protected IDbBind _DbBind;
  11. protected IDbFirst _DbFirst;
  12. protected ICodeFirst _CodeFirst;
  13. protected IDbMaintenance _DbMaintenance;
  14. protected IDbConnection _DbConnection;
  15. protected virtual SugarParameter[] GetParameters(object parameters, PropertyInfo[] propertyInfo, string sqlParameterKeyWord)
  16. {
  17. var result = new List<SugarParameter>();
  18. if (parameters != null)
  19. {
  20. var entityType = parameters.GetType();
  21. var isDictionary = entityType.IsIn(UtilConstants.DicArraySO, UtilConstants.DicArraySS);
  22. if (isDictionary)
  23. DictionaryToParameters(parameters, sqlParameterKeyWord, result, entityType);
  24. else if (parameters is List<SugarParameter>)
  25. {
  26. result = (parameters as List<SugarParameter>);
  27. }
  28. else if (parameters is SugarParameter[])
  29. {
  30. result = (parameters as SugarParameter[]).ToList();
  31. }
  32. else
  33. {
  34. Check.Exception(!entityType.IsAnonymousType(), "The parameter format is wrong. \nUse new{{xx=xx, xx2=xx2}} or \nDictionary<string, object> or \nSugarParameter [] ");
  35. ProperyToParameter(parameters, propertyInfo, sqlParameterKeyWord, result, entityType);
  36. }
  37. }
  38. return result.ToArray();
  39. }
  40. protected void ProperyToParameter(object parameters, PropertyInfo[] propertyInfo, string sqlParameterKeyWord, List<SugarParameter> listParams, Type entityType)
  41. {
  42. PropertyInfo[] properties = null;
  43. properties = propertyInfo != null ? propertyInfo : entityType.GetProperties();
  44. foreach (PropertyInfo properyty in properties)
  45. {
  46. var value = properyty.GetValue(parameters, null);
  47. if (properyty.PropertyType.IsEnum())
  48. value = Convert.ToInt64(value);
  49. if (value == null || value.Equals(DateTime.MinValue)) value = DBNull.Value;
  50. if (properyty.Name.ToLower().Contains("hierarchyid"))
  51. {
  52. var parameter = new SugarParameter(sqlParameterKeyWord + properyty.Name, SqlDbType.Udt)
  53. {
  54. UdtTypeName = "HIERARCHYID",
  55. Value = value
  56. };
  57. listParams.Add(parameter);
  58. }
  59. else
  60. {
  61. var parameter = new SugarParameter(sqlParameterKeyWord + properyty.Name, value);
  62. listParams.Add(parameter);
  63. }
  64. }
  65. }
  66. protected void DictionaryToParameters(object parameters, string sqlParameterKeyWord, List<SugarParameter> listParams, Type entityType)
  67. {
  68. if (entityType == UtilConstants.DicArraySO)
  69. {
  70. var dictionaryParameters = (Dictionary<string, object>)parameters;
  71. var sugarParameters = dictionaryParameters.Select(it => new SugarParameter(sqlParameterKeyWord + it.Key, it.Value));
  72. listParams.AddRange(sugarParameters);
  73. }
  74. else
  75. {
  76. var dictionaryParameters = (Dictionary<string, string>)parameters;
  77. var sugarParameters = dictionaryParameters.Select(it => new SugarParameter(sqlParameterKeyWord + it.Key, it.Value));
  78. listParams.AddRange(sugarParameters);
  79. }
  80. }
  81. }
  82. }