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.

175 lines
6.5 KiB

2 years ago
  1. using Oracle.ManagedDataAccess.Client;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Linq;
  6. using System.Text.RegularExpressions;
  7. namespace SqlSugar
  8. {
  9. public class OracleProvider : AdoProvider
  10. {
  11. public OracleProvider()
  12. {
  13. this.FormatSql = sql =>
  14. {
  15. if (sql.HasValue()&&sql.Contains("@")) {
  16. var exceptionalCaseInfo = Regex.Matches(sql,@"\'.*?\@.*?\'");
  17. if (exceptionalCaseInfo != null) {
  18. foreach (var item in exceptionalCaseInfo.Cast<Match>())
  19. {
  20. sql = sql.Replace(item.Value, item.Value.Replace("@", UtilConstants.ReplaceKey));
  21. }
  22. }
  23. sql = sql .Replace("@",":");
  24. sql = sql.Replace(UtilConstants.ReplaceKey, "@");
  25. }
  26. return sql;
  27. };
  28. }
  29. public override string SqlParameterKeyWord
  30. {
  31. get
  32. {
  33. return ":";
  34. }
  35. }
  36. public override IDbConnection Connection
  37. {
  38. get
  39. {
  40. try
  41. {
  42. if (base._DbConnection == null)
  43. {
  44. base._DbConnection = new OracleConnection(base.Context.CurrentConnectionConfig.ConnectionString);
  45. }
  46. }
  47. catch (Exception ex)
  48. {
  49. Check.Exception(true, ErrorMessage.ConnnectionOpen, ex.Message);
  50. }
  51. return base._DbConnection;
  52. }
  53. set
  54. {
  55. base._DbConnection = value;
  56. }
  57. }
  58. /// <summary>
  59. /// Only SqlServer
  60. /// </summary>
  61. /// <param name="transactionName"></param>
  62. public override void BeginTran(string transactionName)
  63. {
  64. ((OracleConnection)this.Connection).BeginTransaction();
  65. }
  66. /// <summary>
  67. /// Only SqlServer
  68. /// </summary>
  69. /// <param name="iso"></param>
  70. /// <param name="transactionName"></param>
  71. public override void BeginTran(IsolationLevel iso, string transactionName)
  72. {
  73. ((OracleConnection)this.Connection).BeginTransaction(iso);
  74. }
  75. public override IDataAdapter GetAdapter()
  76. {
  77. return new OracleDataAdapter();
  78. }
  79. public override IDbCommand GetCommand(string sql, SugarParameter[] parameters)
  80. {
  81. var sqlCommand = new OracleCommand(sql, (OracleConnection)this.Connection)
  82. {
  83. BindByName = true,
  84. CommandType = this.CommandType,
  85. CommandTimeout = this.CommandTimeOut
  86. };
  87. if (this.Transaction != null)
  88. {
  89. sqlCommand.Transaction = (OracleTransaction)this.Transaction;
  90. }
  91. if (parameters.HasValue())
  92. {
  93. var ipars = ToIDbDataParameter(parameters);
  94. sqlCommand.Parameters.AddRange((OracleParameter[])ipars);
  95. }
  96. CheckConnection();
  97. return sqlCommand;
  98. }
  99. public override void SetCommandToAdapter(IDataAdapter dataAdapter, IDbCommand command)
  100. {
  101. ((OracleDataAdapter)dataAdapter).SelectCommand = (OracleCommand)command;
  102. }
  103. /// <summary>
  104. /// if mysql return MySqlParameter[] pars
  105. /// if sqlerver return SqlParameter[] pars ...
  106. /// </summary>
  107. /// <param name="parameters"></param>
  108. /// <returns></returns>
  109. public override IDataParameter[] ToIDbDataParameter(params SugarParameter[] parameters)
  110. {
  111. if (parameters == null || parameters.Length == 0) return null;
  112. var result = new OracleParameter[parameters.Length];
  113. var index = 0;
  114. foreach (var parameter in parameters)
  115. {
  116. if (parameter.Value == null) parameter.Value = DBNull.Value;
  117. var sqlParameter = new OracleParameter
  118. {
  119. Size = parameter.Size == -1 ? 0 : parameter.Size,
  120. ParameterName = parameter.ParameterName
  121. };
  122. if (sqlParameter.ParameterName[0] == '@')
  123. {
  124. sqlParameter.ParameterName = ':' + sqlParameter.ParameterName.Substring(1, sqlParameter.ParameterName.Length - 1);
  125. }
  126. if (this.CommandType == CommandType.StoredProcedure)
  127. {
  128. sqlParameter.ParameterName = sqlParameter.ParameterName.TrimStart(':');
  129. }
  130. if (parameter.IsRefCursor)
  131. {
  132. sqlParameter.OracleDbType = OracleDbType.RefCursor;
  133. }
  134. if (sqlParameter.DbType == System.Data.DbType.Guid)
  135. {
  136. sqlParameter.DbType = System.Data.DbType.String;
  137. sqlParameter.Value = sqlParameter.Value.ObjToString();
  138. }
  139. else if (parameter.DbType == System.Data.DbType.Boolean)
  140. {
  141. sqlParameter.DbType = System.Data.DbType.Int16;
  142. if (parameter.Value == DBNull.Value)
  143. {
  144. parameter.Value = 0;
  145. }
  146. else
  147. {
  148. sqlParameter.Value = (bool)parameter.Value ? 1 : 0;
  149. }
  150. }
  151. else
  152. {
  153. if (parameter.Value != null && parameter.Value.GetType() == UtilConstants.GuidType)
  154. {
  155. parameter.Value = parameter.Value.ToString();
  156. }
  157. sqlParameter.Value = parameter.Value;
  158. }
  159. if (parameter.Direction != 0)
  160. sqlParameter.Direction = parameter.Direction;
  161. result[index] = sqlParameter;
  162. if (sqlParameter.Direction.IsIn(ParameterDirection.Output, ParameterDirection.InputOutput))
  163. {
  164. if (this.OutputParameters == null) this.OutputParameters = new List<IDataParameter>();
  165. this.OutputParameters.RemoveAll(it => it.ParameterName == sqlParameter.ParameterName);
  166. this.OutputParameters.Add(sqlParameter);
  167. }
  168. ++index;
  169. }
  170. return result;
  171. }
  172. }
  173. }