KdbndpSQLProvider.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.Common;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using Kdbndp;
  9. using KdbndpTypes;
  10. namespace SqlSugar
  11. {
  12. public partial class KdbndpProvider : AdoProvider
  13. {
  14. public KdbndpProvider() { }
  15. public override IDbConnection Connection
  16. {
  17. get
  18. {
  19. if (base._DbConnection == null)
  20. {
  21. try
  22. {
  23. var npgsqlConnectionString = base.Context.CurrentConnectionConfig.ConnectionString;
  24. base._DbConnection = new KdbndpConnection(npgsqlConnectionString);
  25. }
  26. catch (Exception ex)
  27. {
  28. Check.Exception(true, ErrorMessage.ConnnectionOpen, ex.Message);
  29. }
  30. }
  31. return base._DbConnection;
  32. }
  33. set
  34. {
  35. base._DbConnection = value;
  36. }
  37. }
  38. public override void CheckConnection()
  39. {
  40. try
  41. {
  42. base.CheckConnection();
  43. }
  44. catch (Exception ex)
  45. {
  46. if (ex.Message.Contains("Version string portion was too short or too long"))
  47. {
  48. Check.Exception(true, "人大金仓R6请安装 Nuget:SqlSugarCore.Kdbndp到最新版本");
  49. }
  50. throw;
  51. }
  52. }
  53. public override void BeginTran(string transactionName)
  54. {
  55. base.BeginTran();
  56. }
  57. /// <summary>
  58. /// Only SqlServer
  59. /// </summary>
  60. /// <param name="iso"></param>
  61. /// <param name="transactionName"></param>
  62. public override void BeginTran(IsolationLevel iso, string transactionName)
  63. {
  64. base.BeginTran(iso);
  65. }
  66. public override IDataAdapter GetAdapter()
  67. {
  68. return new KdbndpDataAdapter();
  69. }
  70. public override DbCommand GetCommand(string sql, SugarParameter[] parameters)
  71. {
  72. KdbndpCommand sqlCommand = new KdbndpCommand(sql, (KdbndpConnection)this.Connection);
  73. sqlCommand.CommandType = this.CommandType;
  74. sqlCommand.CommandTimeout = this.CommandTimeOut;
  75. if (sqlCommand.CommandType == CommandType.StoredProcedure)
  76. {
  77. sqlCommand.DbModeType = DbMode.Oracle;
  78. }
  79. if (this.Transaction != null)
  80. {
  81. sqlCommand.Transaction = (KdbndpTransaction)this.Transaction;
  82. }
  83. if (parameters.HasValue())
  84. {
  85. IDataParameter[] ipars = ToIDbDataParameter(parameters);
  86. sqlCommand.Parameters.AddRange((KdbndpParameter[])ipars);
  87. }
  88. CheckConnection();
  89. return sqlCommand;
  90. }
  91. public override void SetCommandToAdapter(IDataAdapter dataAdapter, DbCommand command)
  92. {
  93. ((KdbndpDataAdapter)dataAdapter).SelectCommand = (KdbndpCommand)command;
  94. }
  95. /// <summary>
  96. /// if mysql return MySqlParameter[] pars
  97. /// if sqlerver return SqlParameter[] pars ...
  98. /// </summary>
  99. /// <param name="parameters"></param>
  100. /// <returns></returns>
  101. public override IDataParameter[] ToIDbDataParameter(params SugarParameter[] parameters)
  102. {
  103. if (parameters == null || parameters.Length == 0) return null;
  104. KdbndpParameter[] result = new KdbndpParameter[parameters.Length];
  105. int index = 0;
  106. foreach (var parameter in parameters)
  107. {
  108. if (parameter.Value == null) parameter.Value = DBNull.Value;
  109. var sqlParameter = new KdbndpParameter();
  110. sqlParameter.ParameterName = parameter.ParameterName;
  111. sqlParameter.Size = parameter.Size;
  112. sqlParameter.Value = parameter.Value;
  113. sqlParameter.DbType = parameter.DbType;
  114. sqlParameter.Direction = parameter.Direction;
  115. if (parameter.IsJson)
  116. {
  117. sqlParameter.KdbndpDbType = KdbndpDbType.Json;
  118. }
  119. if (parameter.IsArray)
  120. {
  121. // sqlParameter.Value = this.Context.Utilities.SerializeObject(sqlParameter.Value);
  122. var type = sqlParameter.Value.GetType();
  123. if (ArrayMapping.ContainsKey(type))
  124. {
  125. sqlParameter.KdbndpDbType = ArrayMapping[type] | KdbndpDbType.Array;
  126. }
  127. else
  128. {
  129. Check.Exception(true, sqlParameter.Value.GetType().Name + " No Support");
  130. }
  131. }
  132. if (sqlParameter.Direction == 0)
  133. {
  134. sqlParameter.Direction = ParameterDirection.Input;
  135. }
  136. result[index] = sqlParameter;
  137. if (sqlParameter.Direction.IsIn(ParameterDirection.Output, ParameterDirection.InputOutput, ParameterDirection.ReturnValue))
  138. {
  139. if (this.OutputParameters == null) this.OutputParameters = new List<IDataParameter>();
  140. this.OutputParameters.RemoveAll(it => it.ParameterName == sqlParameter.ParameterName);
  141. this.OutputParameters.Add(sqlParameter);
  142. }
  143. ++index;
  144. }
  145. return result;
  146. }
  147. public override Action<SqlSugarException> ErrorEvent => it =>
  148. {
  149. if (base.ErrorEvent != null)
  150. {
  151. base.ErrorEvent(it);
  152. }
  153. if (it.Message != null && it.Message.StartsWith("42883: function uuid_generate_v4() does not exist"))
  154. {
  155. Check.ExceptionEasy(it.Message, $"使用uuid_generate_v4()函数需要创建 CREATE EXTENSION IF NOT EXISTS kbcrypto;CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\" ");
  156. }
  157. };
  158. static readonly Dictionary<Type, KdbndpDbType> ArrayMapping = new Dictionary<Type, KdbndpDbType>()
  159. {
  160. { typeof(int[]),KdbndpDbType.Integer},
  161. { typeof(short[]),KdbndpDbType.Smallint},
  162. { typeof(long[]),KdbndpDbType.Bigint},
  163. { typeof(decimal[]),KdbndpDbType.Numeric},
  164. { typeof(char[]),KdbndpDbType.Text},
  165. { typeof(byte[]),KdbndpDbType.Bytea},
  166. { typeof(bool[]),KdbndpDbType.Boolean},
  167. {typeof(DateTime[]),KdbndpDbType.Date},
  168. { typeof(int?[]),KdbndpDbType.Integer},
  169. { typeof(short?[]),KdbndpDbType.Smallint},
  170. { typeof(long?[]),KdbndpDbType.Bigint},
  171. { typeof(decimal?[]),KdbndpDbType.Numeric},
  172. { typeof(char?[]),KdbndpDbType.Text},
  173. { typeof(byte?[]),KdbndpDbType.Bytea},
  174. { typeof(bool?[]),KdbndpDbType.Boolean},
  175. {typeof(DateTime?[]),KdbndpDbType.Date},
  176. { typeof(string[]), KdbndpDbType.Text},
  177. };
  178. }
  179. }