mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-09-19 10:08:19 +08:00
-
This commit is contained in:
@@ -48,12 +48,12 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Config.cs" />
|
||||
<Compile Include="Demo\DbFirst.cs" />
|
||||
<Compile Include="Demo\Delete.cs" />
|
||||
<Compile Include="Demo\DemoBase.cs" />
|
||||
<Compile Include="Demo\Insert.cs" />
|
||||
<Compile Include="Demo\Query.cs" />
|
||||
<Compile Include="Demo\Update.cs" />
|
||||
<Compile Include="Demos\DbFirst.cs" />
|
||||
<Compile Include="Demos\Delete.cs" />
|
||||
<Compile Include="Demos\DemoBase.cs" />
|
||||
<Compile Include="Demos\Insert.cs" />
|
||||
<Compile Include="Demos\Query.cs" />
|
||||
<Compile Include="Demos\Update.cs" />
|
||||
<Compile Include="Models\ViewModelStudent.cs" />
|
||||
<Compile Include="PerformanceTesting\PerformanceBase.cs" />
|
||||
<Compile Include="PerformanceTesting\SqlSugarPerformance.cs" />
|
||||
|
@@ -27,6 +27,7 @@ namespace SqlSugar
|
||||
#endregion
|
||||
|
||||
#region Properties
|
||||
protected List<IDataParameter> OutputParameters { get; set; }
|
||||
public virtual string SqlParameterKeyWord { get { return "@"; } }
|
||||
public IDbTransaction Transaction { get; set; }
|
||||
public virtual SqlSugarClient Context { get; set; }
|
||||
@@ -137,20 +138,6 @@ namespace SqlSugar
|
||||
public abstract void BeginTran(IsolationLevel iso, string transactionName);//Only SqlServer
|
||||
#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
|
||||
public SugarMessageResult<bool> UseTran(Action action)
|
||||
{
|
||||
@@ -220,21 +207,34 @@ namespace SqlSugar
|
||||
}
|
||||
#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)
|
||||
{
|
||||
base.SetParamterSize(pars);
|
||||
ExecLogEvent(sql, pars, true);
|
||||
SurroundingEvent(sql, pars, true);
|
||||
IDbCommand sqlCommand = GetCommand(sql, pars);
|
||||
IDataReader sqlDataReader = sqlCommand.ExecuteReader(CommandBehavior.CloseConnection);
|
||||
if (this.IsClearParameters)
|
||||
sqlCommand.Parameters.Clear();
|
||||
ExecLogEvent(sql, pars, false);
|
||||
SurroundingEvent(sql, pars, false);
|
||||
return sqlDataReader;
|
||||
}
|
||||
public virtual DataSet GetDataSetAll(string sql, params SugarParameter[] pars)
|
||||
{
|
||||
base.SetParamterSize(pars);
|
||||
ExecLogEvent(sql, pars, true);
|
||||
SurroundingEvent(sql, pars, true);
|
||||
IDataAdapter dataAdapter = this.GetAdapter();
|
||||
IDbCommand sqlCommand = GetCommand(sql, pars);
|
||||
this.SetCommandToAdapter(dataAdapter, sqlCommand);
|
||||
@@ -242,20 +242,20 @@ namespace SqlSugar
|
||||
dataAdapter.Fill(ds);
|
||||
if (this.IsClearParameters)
|
||||
sqlCommand.Parameters.Clear();
|
||||
ExecLogEvent(sql, pars, false);
|
||||
SurroundingEvent(sql, pars, false);
|
||||
if (this.Context.CurrentConnectionConfig.IsAutoCloseConnection && this.Transaction == null) this.Close();
|
||||
return ds;
|
||||
}
|
||||
public virtual object GetScalar(string sql, params SugarParameter[] pars)
|
||||
{
|
||||
base.SetParamterSize(pars);
|
||||
ExecLogEvent(sql, pars, true);
|
||||
SurroundingEvent(sql, pars, true);
|
||||
IDbCommand sqlCommand = GetCommand(sql, pars);
|
||||
object scalar = sqlCommand.ExecuteScalar();
|
||||
scalar = (scalar == null ? 0 : scalar);
|
||||
if (this.IsClearParameters)
|
||||
sqlCommand.Parameters.Clear();
|
||||
ExecLogEvent(sql, pars, false);
|
||||
SurroundingEvent(sql, pars, false);
|
||||
if (this.Context.CurrentConnectionConfig.IsAutoCloseConnection && this.Transaction == null) this.Close();
|
||||
return scalar;
|
||||
}
|
||||
@@ -503,28 +503,45 @@ namespace SqlSugar
|
||||
|
||||
#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)
|
||||
{
|
||||
Action<string, string> action = isStarting ? LogEventStarting : LogEventCompleted;
|
||||
Action<string, string> action = isBefore ? LogEventStarting : LogEventCompleted;
|
||||
if (action != null)
|
||||
{
|
||||
if (pars == null || pars.Length == 0)
|
||||
if (parameters == null || parameters.Length == 0)
|
||||
{
|
||||
action(sql, null);
|
||||
}
|
||||
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;
|
||||
return base.GetParameters(obj, propertyInfo, this.SqlParameterKeyWord);
|
||||
if (parameters == null) return null;
|
||||
return base.GetParameters(parameters, propertyInfo, this.SqlParameterKeyWord);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
|
@@ -17,7 +17,7 @@ namespace SqlSugar
|
||||
IDataParameter[] ToIDbDataParameter(params SugarParameter[] pars);
|
||||
SugarParameter[] GetParameters(object obj, PropertyInfo[] propertyInfo = null);
|
||||
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; }
|
||||
List<ConnectionConfig> SlaveConnectionConfigs { get; set; }
|
||||
|
@@ -89,6 +89,11 @@ namespace SqlSugar
|
||||
sqlParameter.DbType = parameter.DbType;
|
||||
sqlParameter.Direction = parameter.Direction;
|
||||
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;
|
||||
}
|
||||
return result;
|
||||
|
Reference in New Issue
Block a user