This commit is contained in:
sunkaixuan
2017-05-29 20:00:16 +08:00
parent f1ff818a65
commit 402d037275
10 changed files with 58 additions and 36 deletions

View File

@@ -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" />

View File

@@ -27,6 +27,7 @@ namespace SqlSugar
#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,28 +503,45 @@ 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
} }

View File

@@ -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; }

View File

@@ -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;