OscarProvider.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 System.Data.OscarClient;
  9. namespace SqlSugar
  10. {
  11. public partial class OscarProvider : AdoProvider
  12. {
  13. public OscarProvider() { }
  14. public override IDbConnection Connection
  15. {
  16. get
  17. {
  18. if (base._DbConnection == null)
  19. {
  20. try
  21. {
  22. var OscarConnectionString = base.Context.CurrentConnectionConfig.ConnectionString;
  23. base._DbConnection = new OscarConnection(OscarConnectionString);
  24. }
  25. catch (Exception ex)
  26. {
  27. Check.Exception(true, ErrorMessage.ConnnectionOpen, ex.Message);
  28. }
  29. }
  30. return base._DbConnection;
  31. }
  32. set
  33. {
  34. base._DbConnection = value;
  35. }
  36. }
  37. public override void BeginTran(string transactionName)
  38. {
  39. base.BeginTran();
  40. }
  41. /// <summary>
  42. /// Only SqlServer
  43. /// </summary>
  44. /// <param name="iso"></param>
  45. /// <param name="transactionName"></param>
  46. public override void BeginTran(IsolationLevel iso, string transactionName)
  47. {
  48. base.BeginTran(iso);
  49. }
  50. public override IDataAdapter GetAdapter()
  51. {
  52. return new OscarDataAdapter();
  53. }
  54. public override DbCommand GetCommand(string sql, SugarParameter[] parameters)
  55. {
  56. OscarCommand sqlCommand = new OscarCommand(sql, (OscarConnection)this.Connection);
  57. sqlCommand.CommandType = this.CommandType;
  58. sqlCommand.CommandTimeout = this.CommandTimeOut;
  59. if (this.Transaction != null)
  60. {
  61. sqlCommand.Transaction = (OscarTransaction)this.Transaction;
  62. }
  63. if (parameters.HasValue())
  64. {
  65. IDataParameter[] ipars = ToIDbDataParameter(parameters);
  66. sqlCommand.Parameters.AddRange((OscarParameter[])ipars);
  67. }
  68. CheckConnection();
  69. return sqlCommand;
  70. }
  71. public override void SetCommandToAdapter(IDataAdapter dataAdapter, DbCommand command)
  72. {
  73. ((OscarDataAdapter)dataAdapter).SelectCommand = (OscarCommand)command;
  74. }
  75. /// <summary>
  76. /// if mysql return MySqlParameter[] pars
  77. /// if sqlerver return SqlParameter[] pars ...
  78. /// </summary>
  79. /// <param name="parameters"></param>
  80. /// <returns></returns>
  81. public override IDataParameter[] ToIDbDataParameter(params SugarParameter[] parameters)
  82. {
  83. if (parameters == null || parameters.Length == 0) return null;
  84. OscarParameter[] result = new OscarParameter[parameters.Length];
  85. int index = 0;
  86. var isVarchar = this.Context.IsVarchar();
  87. foreach (var parameter in parameters)
  88. {
  89. if (parameter.Value == null) parameter.Value = DBNull.Value;
  90. var sqlParameter = new OscarParameter();
  91. sqlParameter.ParameterName = parameter.ParameterName;
  92. sqlParameter.Size = parameter.Size;
  93. sqlParameter.Value = parameter.Value;
  94. sqlParameter.DbType = parameter.DbType;
  95. sqlParameter.Direction = parameter.Direction;
  96. if (parameter.IsJson)
  97. {
  98. sqlParameter.OscarDbType = OscarDbType.Text;
  99. }
  100. if (parameter.IsArray)
  101. {
  102. // sqlParameter.Value = this.Context.Utilities.SerializeObject(sqlParameter.Value);
  103. var type = sqlParameter.Value.GetType();
  104. if (ArrayMapping.ContainsKey(type))
  105. {
  106. sqlParameter.OscarDbType = ArrayMapping[type] | OscarDbType.Array;
  107. }
  108. else
  109. {
  110. Check.Exception(true, sqlParameter.Value.GetType().Name + " No Support");
  111. }
  112. }
  113. if (sqlParameter.Direction == 0)
  114. {
  115. sqlParameter.Direction = ParameterDirection.Input;
  116. }
  117. result[index] = sqlParameter;
  118. if (sqlParameter.Direction.IsIn(ParameterDirection.Output, ParameterDirection.InputOutput, ParameterDirection.ReturnValue))
  119. {
  120. if (this.OutputParameters == null) this.OutputParameters = new List<IDataParameter>();
  121. this.OutputParameters.RemoveAll(it => it.ParameterName == sqlParameter.ParameterName);
  122. this.OutputParameters.Add(sqlParameter);
  123. }
  124. if (isVarchar && sqlParameter.DbType == System.Data.DbType.String)
  125. {
  126. sqlParameter.DbType = System.Data.DbType.AnsiString;
  127. }
  128. ++index;
  129. }
  130. return result;
  131. }
  132. static readonly Dictionary<Type, OscarDbType> ArrayMapping = new Dictionary<Type, OscarDbType>()
  133. {
  134. { typeof(int[]),OscarDbType.Integer},
  135. { typeof(short[]),OscarDbType.SmallInt},
  136. { typeof(long[]),OscarDbType.BigInt},
  137. { typeof(decimal[]),OscarDbType.Numeric},
  138. { typeof(char[]),OscarDbType.Text},
  139. { typeof(byte[]),OscarDbType.Bytea},
  140. { typeof(bool[]),OscarDbType.Boolean},
  141. {typeof(DateTime[]),OscarDbType.Date},
  142. {typeof(float[]),OscarDbType.Real},
  143. { typeof(int?[]),OscarDbType.Integer},
  144. { typeof(short?[]),OscarDbType.SmallInt},
  145. { typeof(long?[]),OscarDbType.BigInt},
  146. { typeof(decimal?[]),OscarDbType.Numeric},
  147. { typeof(char?[]),OscarDbType.Text},
  148. { typeof(byte?[]),OscarDbType.Bytea},
  149. { typeof(bool?[]),OscarDbType.Boolean},
  150. {typeof(DateTime?[]),OscarDbType.Date},
  151. { typeof(string[]), OscarDbType.Text},
  152. {typeof(float?[]),OscarDbType.Real},
  153. };
  154. }
  155. }