ParameterUtils.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using EasyDevCore.Common;
  2. using System.Data;
  3. using System.Data.Common;
  4. namespace EasyDevCore.Database
  5. {
  6. /// <summary>
  7. ///
  8. /// </summary>
  9. public static class ParameterUtils
  10. {
  11. /// <summary>
  12. /// Gathers the output parameters.
  13. /// </summary>
  14. /// <param name="rowsAffected">The rows affected.</param>
  15. /// <param name="parameters">The parameters.</param>
  16. /// <returns></returns>
  17. public static Dictionary<string, object> GatherOutputParameters(int rowsAffected, IEnumerable<DbParameter> parameters)
  18. {
  19. Dictionary<string, object> results = new Dictionary<string, object>();
  20. foreach (DbParameter param in parameters.OfType<DbParameter>().Where(p => p.Direction.In(ParameterDirection.Output, ParameterDirection.InputOutput, ParameterDirection.ReturnValue)))
  21. {
  22. switch (param.ParameterName)
  23. {
  24. case "RETURN_ROWS_EFFECTED":
  25. results.Add(param.ParameterName, rowsAffected);
  26. break;
  27. default:
  28. results.Add(param.ParameterName, param.Value);
  29. break;
  30. }
  31. }
  32. return results;
  33. }
  34. /// <summary>
  35. /// Gets the parameters.
  36. /// </summary>
  37. /// <param name="isNumberParam">if set to <c>true</c> [is number parameter].</param>
  38. /// <param name="args">The arguments.</param>
  39. /// <returns></returns>
  40. public static EasyDbParameterCollection GetParameters(bool isNumberParam, params object[] args)
  41. {
  42. EasyDbParameterCollection parameters = null;
  43. bool isProccsed = false;
  44. if (args[args.Length - 1] is object[])
  45. {
  46. var lastArgs = args[args.Length - 1] as object[];
  47. args = args.Where((source, index) => index != args.Length - 1).ToArray();
  48. args = args.Concat(lastArgs).ToArray();
  49. }
  50. if (!isNumberParam)
  51. {
  52. if (args.Length == 1 && args[0] is EasyDbParameterCollection)
  53. {
  54. parameters = (EasyDbParameterCollection)args[0];
  55. isProccsed = true;
  56. }
  57. else if (args.Length == 1 && !args[0].IsBaseSystemType())
  58. {
  59. IDictionary<string, object> argValues = ArgumentHelper.GetArgValues(args);
  60. if (args.Length > 0)
  61. {
  62. parameters = CreateParamters(string.Join(",", argValues.Keys.Select((s) =>
  63. {
  64. if (s.EndsWith("_out", StringComparison.InvariantCultureIgnoreCase))
  65. {
  66. return "=" + s.Left(s.Length - 4);
  67. }
  68. else if (s.EndsWith("_ret", StringComparison.InvariantCultureIgnoreCase))
  69. {
  70. return "==" + s.Left(s.Length - 4);
  71. }
  72. return s;
  73. })), argValues.Values.ToArray());
  74. }
  75. isProccsed = true;
  76. }
  77. else if (args.Length >= 2 && args[0] is string)
  78. {
  79. string paramNames = (string)args[0];
  80. parameters = CreateParamters(paramNames, args.Skip(1).ToArray());
  81. isProccsed = true;
  82. }
  83. }
  84. if (!isProccsed)
  85. {
  86. parameters = new EasyDbParameterCollection();
  87. for (int i = 0; i < args.Length; i++)
  88. {
  89. parameters.Add(CreateParamter("p" + i.ToString(), args[i]));
  90. }
  91. }
  92. return parameters;
  93. }
  94. /// <summary>
  95. /// Creates the paramter.
  96. /// </summary>
  97. /// <param name="paramName">Name of the parameter (prefix = means output, == means return).</param>
  98. /// <param name="value">The value.</param>
  99. /// <returns></returns>
  100. public static EasyDbParameter CreateParamter(string paramName, object value)
  101. {
  102. bool isInOut = paramName.StartsWith("=");
  103. bool isReturn = paramName.StartsWith("==");
  104. paramName = paramName.TrimStart(new[] { '=', '?' });
  105. return CreateParamter(paramName, value, isInOut ? ParameterDirection.InputOutput : isReturn ? ParameterDirection.ReturnValue : ParameterDirection.Input);
  106. }
  107. /// <summary>
  108. /// Creates the paramter.
  109. /// </summary>
  110. /// <param name="paramName">Name of the parameter.</param>
  111. /// <param name="value">The value.</param>
  112. /// <param name="direction">The direction.</param>
  113. /// <returns></returns>
  114. public static EasyDbParameter CreateParamter(string paramName, object value, ParameterDirection direction)
  115. {
  116. EasyDbParameter param = new EasyDbParameter() { ParameterName = paramName, Value = value, Direction = direction };
  117. return param;
  118. }
  119. /// <summary>
  120. /// Creates the paramters.
  121. /// </summary>
  122. /// <param name="paramNames">The parameter names.</param>
  123. /// <param name="values">The values.</param>
  124. /// <returns></returns>
  125. /// <exception cref="ArgumentException">Length of paramNames and values are not the same !</exception>
  126. #pragma warning disable CS8632 // The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
  127. public static EasyDbParameterCollection CreateParamters(string paramNames, params object[] values)
  128. #pragma warning restore CS8632 // The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
  129. {
  130. string[] names = paramNames.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
  131. if (names.Length != values.Length)
  132. {
  133. throw new ArgumentException("Length of paramNames and values are not the same !");
  134. }
  135. EasyDbParameterCollection parameters = new EasyDbParameterCollection();
  136. for (int i = 0; i < values.Length; i++)
  137. {
  138. var param = parameters.Add(CreateParamter(names[i], values[i]));
  139. }
  140. parameters.Add(CreateParamter("RETURN_ROWS_EFFECTED", -1, ParameterDirection.Output));
  141. parameters.Add(CreateParamter("RETURN_VALUE", null, ParameterDirection.Output));
  142. return parameters;
  143. }
  144. }
  145. }