mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-12-29 09:54:44 +08:00
Add SqlSugar.Access and SqlSugar MySqlConnector
This commit is contained in:
140
Src/Asp.Net/SqlSugar.MySqlConnector/MySql/MySqlProvider.cs
Normal file
140
Src/Asp.Net/SqlSugar.MySqlConnector/MySql/MySqlProvider.cs
Normal file
@@ -0,0 +1,140 @@
|
||||
using MySqlConnector;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.Common;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
namespace SqlSugar.MySqlConnector
|
||||
{
|
||||
public class MySqlProvider : AdoProvider
|
||||
{
|
||||
public MySqlProvider() { }
|
||||
public override IDbConnection Connection
|
||||
{
|
||||
get
|
||||
{
|
||||
if (base._DbConnection == null)
|
||||
{
|
||||
try
|
||||
{
|
||||
var mySqlConnectionString = base.Context.CurrentConnectionConfig.ConnectionString;
|
||||
if (!mySqlConnectionString.ToLower().Contains("charset"))
|
||||
{
|
||||
mySqlConnectionString = mySqlConnectionString.Trim().TrimEnd(';') + ";charset=utf8;";
|
||||
}
|
||||
base._DbConnection = new MySqlConnection(mySqlConnectionString);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Check.Exception(true, ex.Message);
|
||||
}
|
||||
}
|
||||
return base._DbConnection;
|
||||
}
|
||||
set
|
||||
{
|
||||
base._DbConnection = value;
|
||||
}
|
||||
}
|
||||
|
||||
public override void BeginTran(string transactionName)
|
||||
{
|
||||
base.BeginTran();
|
||||
}
|
||||
/// <summary>
|
||||
/// Only SqlServer
|
||||
/// </summary>
|
||||
/// <param name="iso"></param>
|
||||
/// <param name="transactionName"></param>
|
||||
public override void BeginTran(IsolationLevel iso, string transactionName)
|
||||
{
|
||||
base.BeginTran(iso);
|
||||
}
|
||||
public override IDataAdapter GetAdapter()
|
||||
{
|
||||
return new MySqlDataAdapter();
|
||||
}
|
||||
public override DbCommand GetCommand(string sql, SugarParameter[] parameters)
|
||||
{
|
||||
MySqlCommand sqlCommand = new MySqlCommand(sql, (MySqlConnection)this.Connection);
|
||||
sqlCommand.CommandType = this.CommandType;
|
||||
sqlCommand.CommandTimeout = this.CommandTimeOut;
|
||||
if (this.Transaction != null)
|
||||
{
|
||||
sqlCommand.Transaction = (MySqlTransaction)this.Transaction;
|
||||
}
|
||||
if (parameters.HasValue())
|
||||
{
|
||||
IDataParameter[] ipars = ToIDbDataParameter(parameters);
|
||||
sqlCommand.Parameters.AddRange((MySqlParameter[])ipars);
|
||||
}
|
||||
CheckConnection();
|
||||
return sqlCommand;
|
||||
}
|
||||
public override void SetCommandToAdapter(IDataAdapter dataAdapter, DbCommand command)
|
||||
{
|
||||
((MySqlDataAdapter)dataAdapter).SelectCommand = (MySqlCommand)command;
|
||||
}
|
||||
/// <summary>
|
||||
/// if mysql return MySqlParameter[] pars
|
||||
/// if sqlerver return SqlParameter[] pars ...
|
||||
/// </summary>
|
||||
/// <param name="parameters"></param>
|
||||
/// <returns></returns>
|
||||
public override IDataParameter[] ToIDbDataParameter(params SugarParameter[] parameters)
|
||||
{
|
||||
if (parameters == null || parameters.Length == 0) return null;
|
||||
MySqlParameter[] result = new MySqlParameter[parameters.Length];
|
||||
int index = 0;
|
||||
var isVarchar =IsVarchar();
|
||||
foreach (var parameter in parameters)
|
||||
{
|
||||
if (parameter.Value == null) parameter.Value = DBNull.Value;
|
||||
var sqlParameter = new MySqlParameter();
|
||||
sqlParameter.ParameterName = parameter.ParameterName;
|
||||
sqlParameter.Size = parameter.Size;
|
||||
sqlParameter.Value = parameter.Value;
|
||||
sqlParameter.DbType = parameter.DbType;
|
||||
if (parameter.Direction == 0)
|
||||
{
|
||||
parameter.Direction = ParameterDirection.Input;
|
||||
}
|
||||
sqlParameter.Direction = parameter.Direction;
|
||||
//if (sqlParameter.Direction == 0)
|
||||
//{
|
||||
// sqlParameter.Direction = ParameterDirection.Input;
|
||||
//}
|
||||
result[index] = sqlParameter;
|
||||
if (sqlParameter.Direction.IsIn(ParameterDirection.Output, ParameterDirection.InputOutput, ParameterDirection.ReturnValue))
|
||||
{
|
||||
if (this.OutputParameters == null) this.OutputParameters = new List<IDataParameter>();
|
||||
this.OutputParameters.RemoveAll(it => it.ParameterName == sqlParameter.ParameterName);
|
||||
this.OutputParameters.Add(sqlParameter);
|
||||
}
|
||||
if (isVarchar && sqlParameter.DbType == System.Data.DbType.String)
|
||||
{
|
||||
sqlParameter.DbType = System.Data.DbType.AnsiString;
|
||||
}
|
||||
else if (parameter.DbType== System.Data.DbType.DateTimeOffset)
|
||||
{
|
||||
if(sqlParameter.Value != DBNull.Value)
|
||||
sqlParameter.Value = UtilMethods.ConvertFromDateTimeOffset((DateTimeOffset)sqlParameter.Value);
|
||||
sqlParameter.DbType = System.Data.DbType.DateTime;
|
||||
}
|
||||
++index;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private bool IsVarchar()
|
||||
{
|
||||
if (this.Context.CurrentConnectionConfig.MoreSettings != null && this.Context.CurrentConnectionConfig.MoreSettings.DisableNvarchar)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user