mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-09-19 01:58:13 +08:00
Update custom database
This commit is contained in:
@@ -136,5 +136,175 @@ namespace SqlSugar.MySqlConnector
|
|||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#region async
|
||||||
|
public async Task CloseAsync()
|
||||||
|
{
|
||||||
|
if (this.Transaction != null)
|
||||||
|
{
|
||||||
|
this.Transaction = null;
|
||||||
|
}
|
||||||
|
if (this.Connection != null && this.Connection.State == ConnectionState.Open)
|
||||||
|
{
|
||||||
|
await (this.Connection as MySqlConnection).CloseAsync();
|
||||||
|
}
|
||||||
|
if (this.IsMasterSlaveSeparation && this.SlaveConnections.HasValue())
|
||||||
|
{
|
||||||
|
foreach (var slaveConnection in this.SlaveConnections)
|
||||||
|
{
|
||||||
|
if (slaveConnection != null && slaveConnection.State == ConnectionState.Open)
|
||||||
|
{
|
||||||
|
await (slaveConnection as MySqlConnection).CloseAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public async Task<DbCommand> GetCommandAsync(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);
|
||||||
|
}
|
||||||
|
if (this.Connection.State != ConnectionState.Open)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await (this.Connection as MySqlConnection).OpenAsync();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Check.Exception(true, ex.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return sqlCommand;
|
||||||
|
}
|
||||||
|
public override async Task<int> ExecuteCommandAsync(string sql, params SugarParameter[] parameters)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Async();
|
||||||
|
InitParameters(ref sql, parameters);
|
||||||
|
if (FormatSql != null)
|
||||||
|
sql = FormatSql(sql);
|
||||||
|
SetConnectionStart(sql);
|
||||||
|
if (this.ProcessingEventStartingSQL != null)
|
||||||
|
ExecuteProcessingSQL(ref sql, parameters);
|
||||||
|
ExecuteBefore(sql, parameters);
|
||||||
|
var sqlCommand =await GetCommandAsync(sql, parameters);
|
||||||
|
int count;
|
||||||
|
if (this.CancellationToken == null)
|
||||||
|
count = await sqlCommand.ExecuteNonQueryAsync();
|
||||||
|
else
|
||||||
|
count = await sqlCommand.ExecuteNonQueryAsync(this.CancellationToken.Value);
|
||||||
|
if (this.IsClearParameters)
|
||||||
|
sqlCommand.Parameters.Clear();
|
||||||
|
ExecuteAfter(sql, parameters);
|
||||||
|
sqlCommand.Dispose();
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
CommandType = CommandType.Text;
|
||||||
|
if (ErrorEvent != null)
|
||||||
|
ExecuteErrorEvent(sql, parameters, ex);
|
||||||
|
throw ex;
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
if (this.IsAutoClose())
|
||||||
|
{
|
||||||
|
await this.CloseAsync();
|
||||||
|
}
|
||||||
|
SetConnectionEnd(sql);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public override async Task<IDataReader> GetDataReaderAsync(string sql, params SugarParameter[] parameters)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Async();
|
||||||
|
InitParameters(ref sql, parameters);
|
||||||
|
if (FormatSql != null)
|
||||||
|
sql = FormatSql(sql);
|
||||||
|
SetConnectionStart(sql);
|
||||||
|
var isSp = this.CommandType == CommandType.StoredProcedure;
|
||||||
|
if (this.ProcessingEventStartingSQL != null)
|
||||||
|
ExecuteProcessingSQL(ref sql, parameters);
|
||||||
|
ExecuteBefore(sql, parameters);
|
||||||
|
var sqlCommand = await GetCommandAsync(sql, parameters);
|
||||||
|
DbDataReader sqlDataReader;
|
||||||
|
if (this.CancellationToken == null)
|
||||||
|
sqlDataReader = await sqlCommand.ExecuteReaderAsync(this.IsAutoClose() ? CommandBehavior.CloseConnection : CommandBehavior.Default);
|
||||||
|
else
|
||||||
|
sqlDataReader = await sqlCommand.ExecuteReaderAsync(this.IsAutoClose() ? CommandBehavior.CloseConnection : CommandBehavior.Default, this.CancellationToken.Value);
|
||||||
|
if (isSp)
|
||||||
|
DataReaderParameters = sqlCommand.Parameters;
|
||||||
|
if (this.IsClearParameters)
|
||||||
|
sqlCommand.Parameters.Clear();
|
||||||
|
ExecuteAfter(sql, parameters);
|
||||||
|
SetConnectionEnd(sql);
|
||||||
|
if (SugarCompatible.IsFramework || this.Context.CurrentConnectionConfig.DbType != DbType.Sqlite)
|
||||||
|
sqlCommand.Dispose();
|
||||||
|
return sqlDataReader;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
CommandType = CommandType.Text;
|
||||||
|
if (ErrorEvent != null)
|
||||||
|
ExecuteErrorEvent(sql, parameters, ex);
|
||||||
|
throw ex;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public override async Task<object> GetScalarAsync(string sql, params SugarParameter[] parameters)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Async();
|
||||||
|
InitParameters(ref sql, parameters);
|
||||||
|
if (FormatSql != null)
|
||||||
|
sql = FormatSql(sql);
|
||||||
|
SetConnectionStart(sql);
|
||||||
|
if (this.ProcessingEventStartingSQL != null)
|
||||||
|
ExecuteProcessingSQL(ref sql, parameters);
|
||||||
|
ExecuteBefore(sql, parameters);
|
||||||
|
var sqlCommand = await GetCommandAsync(sql, parameters) ;
|
||||||
|
object scalar;
|
||||||
|
if (CancellationToken == null)
|
||||||
|
scalar = await sqlCommand.ExecuteScalarAsync();
|
||||||
|
else
|
||||||
|
scalar = await sqlCommand.ExecuteScalarAsync(this.CancellationToken.Value);
|
||||||
|
//scalar = (scalar == null ? 0 : scalar);
|
||||||
|
if (this.IsClearParameters)
|
||||||
|
sqlCommand.Parameters.Clear();
|
||||||
|
ExecuteAfter(sql, parameters);
|
||||||
|
sqlCommand.Dispose();
|
||||||
|
return scalar;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
CommandType = CommandType.Text;
|
||||||
|
if (ErrorEvent != null)
|
||||||
|
ExecuteErrorEvent(sql, parameters, ex);
|
||||||
|
throw ex;
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
if (this.IsAutoClose())
|
||||||
|
{
|
||||||
|
await this.CloseAsync();
|
||||||
|
}
|
||||||
|
SetConnectionEnd(sql);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1253,7 +1253,7 @@ namespace SqlSugar
|
|||||||
{
|
{
|
||||||
this.CancellationToken = null;
|
this.CancellationToken = null;
|
||||||
}
|
}
|
||||||
private void Async()
|
protected void Async()
|
||||||
{
|
{
|
||||||
if (this.Context.Root != null && this.Context.Root.AsyncId == null)
|
if (this.Context.Root != null && this.Context.Root.AsyncId == null)
|
||||||
{
|
{
|
||||||
@@ -1272,7 +1272,7 @@ namespace SqlSugar
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ExecuteProcessingSQL(ref string sql, SugarParameter[] parameters)
|
protected void ExecuteProcessingSQL(ref string sql, SugarParameter[] parameters)
|
||||||
{
|
{
|
||||||
var result = this.ProcessingEventStartingSQL(sql, parameters);
|
var result = this.ProcessingEventStartingSQL(sql, parameters);
|
||||||
sql = result.Key;
|
sql = result.Key;
|
||||||
@@ -1361,18 +1361,18 @@ namespace SqlSugar
|
|||||||
if (parameters == null) return null;
|
if (parameters == null) return null;
|
||||||
return base.GetParameters(parameters, propertyInfo, this.SqlParameterKeyWord);
|
return base.GetParameters(parameters, propertyInfo, this.SqlParameterKeyWord);
|
||||||
}
|
}
|
||||||
private bool IsAutoClose()
|
protected bool IsAutoClose()
|
||||||
{
|
{
|
||||||
return this.Context.CurrentConnectionConfig.IsAutoCloseConnection && this.Transaction == null;
|
return this.Context.CurrentConnectionConfig.IsAutoCloseConnection && this.Transaction == null;
|
||||||
}
|
}
|
||||||
private bool IsMasterSlaveSeparation
|
protected bool IsMasterSlaveSeparation
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
return this.Context.CurrentConnectionConfig.SlaveConnectionConfigs.HasValue() && this.IsDisableMasterSlaveSeparation == false;
|
return this.Context.CurrentConnectionConfig.SlaveConnectionConfigs.HasValue() && this.IsDisableMasterSlaveSeparation == false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private void SetConnectionStart(string sql)
|
protected void SetConnectionStart(string sql)
|
||||||
{
|
{
|
||||||
if (this.Transaction == null && this.IsMasterSlaveSeparation && IsRead(sql))
|
if (this.Transaction == null && this.IsMasterSlaveSeparation && IsRead(sql))
|
||||||
{
|
{
|
||||||
@@ -1408,7 +1408,7 @@ namespace SqlSugar
|
|||||||
return result.Count() == 0;
|
return result.Count() == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void SetConnectionEnd(string sql)
|
protected void SetConnectionEnd(string sql)
|
||||||
{
|
{
|
||||||
if (this.IsMasterSlaveSeparation && IsRead(sql) && this.Transaction == null)
|
if (this.IsMasterSlaveSeparation && IsRead(sql) && this.Transaction == null)
|
||||||
{
|
{
|
||||||
@@ -1424,11 +1424,11 @@ namespace SqlSugar
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ExecuteErrorEvent(string sql, SugarParameter[] parameters, Exception ex)
|
protected void ExecuteErrorEvent(string sql, SugarParameter[] parameters, Exception ex)
|
||||||
{
|
{
|
||||||
ErrorEvent(new SqlSugarException(this.Context, ex, sql, parameters));
|
ErrorEvent(new SqlSugarException(this.Context, ex, sql, parameters));
|
||||||
}
|
}
|
||||||
private void InitParameters(ref string sql, SugarParameter[] parameters)
|
protected void InitParameters(ref string sql, SugarParameter[] parameters)
|
||||||
{
|
{
|
||||||
if (parameters.HasValue())
|
if (parameters.HasValue())
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user