mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-09-21 02:58:05 +08:00
-
This commit is contained in:
@@ -48,12 +48,12 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="Config.cs" />
|
<Compile Include="Config.cs" />
|
||||||
<Compile Include="Demo\DbFirst.cs" />
|
<Compile Include="Demos\DbFirst.cs" />
|
||||||
<Compile Include="Demo\Delete.cs" />
|
<Compile Include="Demos\Delete.cs" />
|
||||||
<Compile Include="Demo\DemoBase.cs" />
|
<Compile Include="Demos\DemoBase.cs" />
|
||||||
<Compile Include="Demo\Insert.cs" />
|
<Compile Include="Demos\Insert.cs" />
|
||||||
<Compile Include="Demo\Query.cs" />
|
<Compile Include="Demos\Query.cs" />
|
||||||
<Compile Include="Demo\Update.cs" />
|
<Compile Include="Demos\Update.cs" />
|
||||||
<Compile Include="Models\ViewModelStudent.cs" />
|
<Compile Include="Models\ViewModelStudent.cs" />
|
||||||
<Compile Include="PerformanceTesting\PerformanceBase.cs" />
|
<Compile Include="PerformanceTesting\PerformanceBase.cs" />
|
||||||
<Compile Include="PerformanceTesting\SqlSugarPerformance.cs" />
|
<Compile Include="PerformanceTesting\SqlSugarPerformance.cs" />
|
||||||
|
@@ -23,10 +23,11 @@ namespace SqlSugar
|
|||||||
this.CommandType = CommandType.Text;
|
this.CommandType = CommandType.Text;
|
||||||
this.IsClearParameters = true;
|
this.IsClearParameters = true;
|
||||||
this.CommandTimeOut = 30000;
|
this.CommandTimeOut = 30000;
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Properties
|
#region Properties
|
||||||
|
protected List<IDataParameter> OutputParameters { get; set; }
|
||||||
public virtual string SqlParameterKeyWord { get { return "@"; } }
|
public virtual string SqlParameterKeyWord { get { return "@"; } }
|
||||||
public IDbTransaction Transaction { get; set; }
|
public IDbTransaction Transaction { get; set; }
|
||||||
public virtual SqlSugarClient Context { get; set; }
|
public virtual SqlSugarClient Context { get; set; }
|
||||||
@@ -137,20 +138,6 @@ namespace SqlSugar
|
|||||||
public abstract void BeginTran(IsolationLevel iso, string transactionName);//Only SqlServer
|
public abstract void BeginTran(IsolationLevel iso, string transactionName);//Only SqlServer
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Core
|
|
||||||
public virtual int ExecuteCommand(string sql, params SugarParameter[] pars)
|
|
||||||
{
|
|
||||||
base.SetParamterSize(pars);
|
|
||||||
ExecLogEvent(sql, pars, true);
|
|
||||||
IDbCommand sqlCommand = GetCommand(sql, pars);
|
|
||||||
int count = sqlCommand.ExecuteNonQuery();
|
|
||||||
if (this.IsClearParameters)
|
|
||||||
sqlCommand.Parameters.Clear();
|
|
||||||
ExecLogEvent(sql, pars, false);
|
|
||||||
if (this.Context.CurrentConnectionConfig.IsAutoCloseConnection && this.Transaction == null) this.Close();
|
|
||||||
return count;
|
|
||||||
}
|
|
||||||
|
|
||||||
#region Use
|
#region Use
|
||||||
public SugarMessageResult<bool> UseTran(Action action)
|
public SugarMessageResult<bool> UseTran(Action action)
|
||||||
{
|
{
|
||||||
@@ -220,21 +207,34 @@ namespace SqlSugar
|
|||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
#region Core
|
||||||
|
public virtual int ExecuteCommand(string sql, params SugarParameter[] pars)
|
||||||
|
{
|
||||||
|
base.SetParamterSize(pars);
|
||||||
|
SurroundingEvent(sql, pars, true);
|
||||||
|
IDbCommand sqlCommand = GetCommand(sql, pars);
|
||||||
|
int count = sqlCommand.ExecuteNonQuery();
|
||||||
|
if (this.IsClearParameters)
|
||||||
|
sqlCommand.Parameters.Clear();
|
||||||
|
SurroundingEvent(sql, pars, false);
|
||||||
|
if (this.Context.CurrentConnectionConfig.IsAutoCloseConnection && this.Transaction == null) this.Close();
|
||||||
|
return count;
|
||||||
|
}
|
||||||
public virtual IDataReader GetDataReader(string sql, params SugarParameter[] pars)
|
public virtual IDataReader GetDataReader(string sql, params SugarParameter[] pars)
|
||||||
{
|
{
|
||||||
base.SetParamterSize(pars);
|
base.SetParamterSize(pars);
|
||||||
ExecLogEvent(sql, pars, true);
|
SurroundingEvent(sql, pars, true);
|
||||||
IDbCommand sqlCommand = GetCommand(sql, pars);
|
IDbCommand sqlCommand = GetCommand(sql, pars);
|
||||||
IDataReader sqlDataReader = sqlCommand.ExecuteReader(CommandBehavior.CloseConnection);
|
IDataReader sqlDataReader = sqlCommand.ExecuteReader(CommandBehavior.CloseConnection);
|
||||||
if (this.IsClearParameters)
|
if (this.IsClearParameters)
|
||||||
sqlCommand.Parameters.Clear();
|
sqlCommand.Parameters.Clear();
|
||||||
ExecLogEvent(sql, pars, false);
|
SurroundingEvent(sql, pars, false);
|
||||||
return sqlDataReader;
|
return sqlDataReader;
|
||||||
}
|
}
|
||||||
public virtual DataSet GetDataSetAll(string sql, params SugarParameter[] pars)
|
public virtual DataSet GetDataSetAll(string sql, params SugarParameter[] pars)
|
||||||
{
|
{
|
||||||
base.SetParamterSize(pars);
|
base.SetParamterSize(pars);
|
||||||
ExecLogEvent(sql, pars, true);
|
SurroundingEvent(sql, pars, true);
|
||||||
IDataAdapter dataAdapter = this.GetAdapter();
|
IDataAdapter dataAdapter = this.GetAdapter();
|
||||||
IDbCommand sqlCommand = GetCommand(sql, pars);
|
IDbCommand sqlCommand = GetCommand(sql, pars);
|
||||||
this.SetCommandToAdapter(dataAdapter, sqlCommand);
|
this.SetCommandToAdapter(dataAdapter, sqlCommand);
|
||||||
@@ -242,20 +242,20 @@ namespace SqlSugar
|
|||||||
dataAdapter.Fill(ds);
|
dataAdapter.Fill(ds);
|
||||||
if (this.IsClearParameters)
|
if (this.IsClearParameters)
|
||||||
sqlCommand.Parameters.Clear();
|
sqlCommand.Parameters.Clear();
|
||||||
ExecLogEvent(sql, pars, false);
|
SurroundingEvent(sql, pars, false);
|
||||||
if (this.Context.CurrentConnectionConfig.IsAutoCloseConnection && this.Transaction == null) this.Close();
|
if (this.Context.CurrentConnectionConfig.IsAutoCloseConnection && this.Transaction == null) this.Close();
|
||||||
return ds;
|
return ds;
|
||||||
}
|
}
|
||||||
public virtual object GetScalar(string sql, params SugarParameter[] pars)
|
public virtual object GetScalar(string sql, params SugarParameter[] pars)
|
||||||
{
|
{
|
||||||
base.SetParamterSize(pars);
|
base.SetParamterSize(pars);
|
||||||
ExecLogEvent(sql, pars, true);
|
SurroundingEvent(sql, pars, true);
|
||||||
IDbCommand sqlCommand = GetCommand(sql, pars);
|
IDbCommand sqlCommand = GetCommand(sql, pars);
|
||||||
object scalar = sqlCommand.ExecuteScalar();
|
object scalar = sqlCommand.ExecuteScalar();
|
||||||
scalar = (scalar == null ? 0 : scalar);
|
scalar = (scalar == null ? 0 : scalar);
|
||||||
if (this.IsClearParameters)
|
if (this.IsClearParameters)
|
||||||
sqlCommand.Parameters.Clear();
|
sqlCommand.Parameters.Clear();
|
||||||
ExecLogEvent(sql, pars, false);
|
SurroundingEvent(sql, pars, false);
|
||||||
if (this.Context.CurrentConnectionConfig.IsAutoCloseConnection && this.Transaction == null) this.Close();
|
if (this.Context.CurrentConnectionConfig.IsAutoCloseConnection && this.Transaction == null) this.Close();
|
||||||
return scalar;
|
return scalar;
|
||||||
}
|
}
|
||||||
@@ -503,29 +503,46 @@ namespace SqlSugar
|
|||||||
|
|
||||||
#region Helper
|
#region Helper
|
||||||
|
|
||||||
public virtual void ExecLogEvent(string sql, SugarParameter[] pars, bool isStarting = true)
|
public virtual void SurroundingEvent(string sql, SugarParameter[] parameters, bool isBefore = true)
|
||||||
{
|
{
|
||||||
|
var isAfter = !isBefore;
|
||||||
|
if (isAfter)
|
||||||
|
{
|
||||||
|
var hasParameter = parameters.IsValuable();
|
||||||
|
if (hasParameter)
|
||||||
|
{
|
||||||
|
foreach (var outputParameter in parameters.Where(it => it.Direction == ParameterDirection.Output))
|
||||||
|
{
|
||||||
|
var gobalOutputParamter=this.OutputParameters.Single(it => it.ParameterName == outputParameter.ParameterName);
|
||||||
|
outputParameter.Value = gobalOutputParamter.Value;
|
||||||
|
this.OutputParameters.Remove(gobalOutputParamter);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (isBefore) {
|
||||||
|
|
||||||
|
}
|
||||||
if (this.IsEnableLogEvent)
|
if (this.IsEnableLogEvent)
|
||||||
{
|
{
|
||||||
Action<string, string> action = isStarting ? LogEventStarting : LogEventCompleted;
|
Action<string, string> action = isBefore ? LogEventStarting : LogEventCompleted;
|
||||||
if (action != null)
|
if (action != null)
|
||||||
{
|
{
|
||||||
if (pars == null || pars.Length == 0)
|
if (parameters == null || parameters.Length == 0)
|
||||||
{
|
{
|
||||||
action(sql, null);
|
action(sql, null);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
action(sql, this.Context.RewritableMethods.SerializeObject(pars.Select(it => new { key = it.ParameterName, value = it.Value.ObjToString() })));
|
action(sql, this.Context.RewritableMethods.SerializeObject(parameters.Select(it => new { key = it.ParameterName, value = it.Value.ObjToString() })));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public virtual SugarParameter[] GetParameters(object obj, PropertyInfo[] propertyInfo = null)
|
public virtual SugarParameter[] GetParameters(object parameters, PropertyInfo[] propertyInfo = null)
|
||||||
{
|
{
|
||||||
if (obj == null) return null;
|
if (parameters == null) return null;
|
||||||
return base.GetParameters(obj, propertyInfo, this.SqlParameterKeyWord);
|
return base.GetParameters(parameters, propertyInfo, this.SqlParameterKeyWord);
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -17,7 +17,7 @@ namespace SqlSugar
|
|||||||
IDataParameter[] ToIDbDataParameter(params SugarParameter[] pars);
|
IDataParameter[] ToIDbDataParameter(params SugarParameter[] pars);
|
||||||
SugarParameter[] GetParameters(object obj, PropertyInfo[] propertyInfo = null);
|
SugarParameter[] GetParameters(object obj, PropertyInfo[] propertyInfo = null);
|
||||||
SqlSugarClient Context { get; set; }
|
SqlSugarClient Context { get; set; }
|
||||||
void ExecLogEvent(string sql, SugarParameter[] pars, bool isStarting = true);
|
void SurroundingEvent(string sql, SugarParameter[] pars, bool isBefore = true);
|
||||||
|
|
||||||
ConnectionConfig MasterConnectionConfig { get; set; }
|
ConnectionConfig MasterConnectionConfig { get; set; }
|
||||||
List<ConnectionConfig> SlaveConnectionConfigs { get; set; }
|
List<ConnectionConfig> SlaveConnectionConfigs { get; set; }
|
||||||
|
@@ -89,6 +89,11 @@ namespace SqlSugar
|
|||||||
sqlParameter.DbType = parameter.DbType;
|
sqlParameter.DbType = parameter.DbType;
|
||||||
sqlParameter.Direction = parameter.Direction;
|
sqlParameter.Direction = parameter.Direction;
|
||||||
result[index] = sqlParameter;
|
result[index] = sqlParameter;
|
||||||
|
if (sqlParameter.Direction == ParameterDirection.Output) {
|
||||||
|
if (this.OutputParameters == null) this.OutputParameters = new List<IDataParameter>();
|
||||||
|
this.OutputParameters.RemoveAll(it => it.ParameterName == sqlParameter.ParameterName);
|
||||||
|
this.OutputParameters.Add(sqlParameter);
|
||||||
|
}
|
||||||
++index;
|
++index;
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
|
Reference in New Issue
Block a user