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.

116 lines
4.3 KiB

2 years ago
  1. using MySql.Data.MySqlClient;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. namespace SqlSugar
  6. {
  7. public class MySqlProvider : AdoProvider
  8. {
  9. public MySqlProvider() { }
  10. public override IDbConnection Connection
  11. {
  12. get
  13. {
  14. if (base._DbConnection == null)
  15. {
  16. try
  17. {
  18. var mySqlConnectionString = base.Context.CurrentConnectionConfig.ConnectionString;
  19. if (!mySqlConnectionString.ToLower().Contains("charset"))
  20. {
  21. mySqlConnectionString = mySqlConnectionString.Trim().TrimEnd(';') + ";charset=utf8;";
  22. }
  23. base._DbConnection = new MySqlConnection(mySqlConnectionString);
  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 MySqlDataAdapter();
  53. }
  54. public override IDbCommand GetCommand(string sql, SugarParameter[] parameters)
  55. {
  56. var sqlCommand = new MySqlCommand(sql, (MySqlConnection)this.Connection)
  57. {
  58. CommandType = this.CommandType,
  59. CommandTimeout = this.CommandTimeOut
  60. };
  61. if (this.Transaction != null)
  62. {
  63. sqlCommand.Transaction = (MySqlTransaction)this.Transaction;
  64. }
  65. if (parameters.HasValue())
  66. {
  67. var ipars = ToIDbDataParameter(parameters);
  68. sqlCommand.Parameters.AddRange((MySqlParameter[])ipars);
  69. }
  70. CheckConnection();
  71. return sqlCommand;
  72. }
  73. public override void SetCommandToAdapter(IDataAdapter dataAdapter, IDbCommand command)
  74. {
  75. ((MySqlDataAdapter)dataAdapter).SelectCommand = (MySqlCommand)command;
  76. }
  77. /// <summary>
  78. /// if mysql return MySqlParameter[] pars
  79. /// if sqlerver return SqlParameter[] pars ...
  80. /// </summary>
  81. /// <param name="parameters"></param>
  82. /// <returns></returns>
  83. public override IDataParameter[] ToIDbDataParameter(params SugarParameter[] parameters)
  84. {
  85. if (parameters == null || parameters.Length == 0) return null;
  86. var result = new MySqlParameter[parameters.Length];
  87. var index = 0;
  88. foreach (var parameter in parameters)
  89. {
  90. if (parameter.Value == null) parameter.Value = DBNull.Value;
  91. var sqlParameter = new MySqlParameter
  92. {
  93. ParameterName = parameter.ParameterName,
  94. Size = parameter.Size,
  95. Value = parameter.Value,
  96. DbType = parameter.DbType,
  97. Direction = parameter.Direction
  98. };
  99. if (sqlParameter.Direction == 0)
  100. {
  101. sqlParameter.Direction = ParameterDirection.Input;
  102. }
  103. result[index] = sqlParameter;
  104. if (sqlParameter.Direction.IsIn(ParameterDirection.Output, ParameterDirection.InputOutput))
  105. {
  106. if (this.OutputParameters == null) this.OutputParameters = new List<IDataParameter>();
  107. this.OutputParameters.RemoveAll(it => it.ParameterName == sqlParameter.ParameterName);
  108. this.OutputParameters.Add(sqlParameter);
  109. }
  110. ++index;
  111. }
  112. return result;
  113. }
  114. }
  115. }