Browse Source

Code optimization

pull/12/MERGE
sunkaixuan 6 years ago
parent
commit
3fdd68a3f6
  1. 316
      Src/Asp.Net/SqlSugar/Abstract/AdoProvider/AdoProvider.cs

316
Src/Asp.Net/SqlSugar/Abstract/AdoProvider/AdoProvider.cs

@ -781,35 +781,8 @@ namespace SqlSugar
} }
public virtual List<T> SqlQuery<T>(string sql, params SugarParameter[] parameters) public virtual List<T> SqlQuery<T>(string sql, params SugarParameter[] parameters)
{ {
this.Context.InitMappingInfo<T>(); var result = SqlQuery<T, object, object, object, object, object, object>(sql, parameters);
var builder = InstanceFactory.GetSqlbuilder(this.Context.CurrentConnectionConfig); return result.Item1;
builder.SqlQueryBuilder.sql.Append(sql);
if (parameters != null && parameters.Any())
builder.SqlQueryBuilder.Parameters.AddRange(parameters);
var dataReader = this.GetDataReader(builder.SqlQueryBuilder.ToSqlString(), builder.SqlQueryBuilder.Parameters.ToArray());
List<T> result = null;
if (typeof(T) == UtilConstants.ObjType)
{
result = this.Context.Utilities.DataReaderToExpandoObjectList(dataReader).Select(it => ((T)(object)it)).ToList();
}
else
{
result = this.DbBind.DataReaderToList<T>(typeof(T), dataReader);
}
builder.SqlQueryBuilder.Clear();
if (this.Context.Ado.DataReaderParameters != null)
{
foreach (IDataParameter item in this.Context.Ado.DataReaderParameters)
{
var parameter = parameters.FirstOrDefault(it => item.ParameterName.Substring(1) == it.ParameterName.Substring(1));
if (parameter != null)
{
parameter.Value = item.Value;
}
}
this.Context.Ado.DataReaderParameters = null;
}
return result;
} }
public virtual List<T> SqlQuery<T>(string sql, List<SugarParameter> parameters) public virtual List<T> SqlQuery<T>(string sql, List<SugarParameter> parameters)
{ {
@ -822,57 +795,6 @@ namespace SqlSugar
return SqlQuery<T>(sql); return SqlQuery<T>(sql);
} }
} }
public virtual Task<List<T>> SqlQueryAsync<T>(string sql, object parameters = null)
{
var sugarParameters = this.GetParameters(parameters);
return SqlQueryAsync<T>(sql, sugarParameters);
}
public virtual async Task<List<T>> SqlQueryAsync<T>(string sql, params SugarParameter[] parameters)
{
this.Context.InitMappingInfo<T>();
var builder = InstanceFactory.GetSqlbuilder(this.Context.CurrentConnectionConfig);
builder.SqlQueryBuilder.sql.Append(sql);
if (parameters != null && parameters.Any())
builder.SqlQueryBuilder.Parameters.AddRange(parameters);
var dataReader = await this.GetDataReaderAsync(builder.SqlQueryBuilder.ToSqlString(), builder.SqlQueryBuilder.Parameters.ToArray());
List<T> result = null;
if (typeof(T) == UtilConstants.ObjType)
{
var list = await this.Context.Utilities.DataReaderToExpandoObjectListAsync(dataReader);
result = list.Select(it => ((T)(object)it)).ToList();
}
else
{
result = await this.DbBind.DataReaderToListAsync<T>(typeof(T), dataReader);
}
builder.SqlQueryBuilder.Clear();
if (this.Context.Ado.DataReaderParameters != null)
{
foreach (IDataParameter item in this.Context.Ado.DataReaderParameters)
{
var parameter = parameters.FirstOrDefault(it => item.ParameterName.Substring(1) == it.ParameterName.Substring(1));
if (parameter != null)
{
parameter.Value = item.Value;
}
}
this.Context.Ado.DataReaderParameters = null;
}
return result;
}
public virtual Task<List<T>> SqlQueryAsync<T>(string sql, List<SugarParameter> parameters)
{
if (parameters != null)
{
return SqlQueryAsync<T>(sql, parameters.ToArray());
}
else
{
return SqlQueryAsync<T>(sql);
}
}
public Tuple<List<T>, List<T2>> SqlQuery<T, T2>(string sql, object parameters = null) public Tuple<List<T>, List<T2>> SqlQuery<T, T2>(string sql, object parameters = null)
{ {
var result = SqlQuery<T, T2, object, object, object, object, object>(sql, parameters); var result = SqlQuery<T, T2, object, object, object, object, object>(sql, parameters);
@ -967,170 +889,51 @@ namespace SqlSugar
} }
} }
public async Task<Tuple<List<T>, List<T2>>> SqlQueryAsync<T, T2>(string sql, object parameters = null) public virtual Task<List<T>> SqlQueryAsync<T>(string sql, object parameters = null)
{ {
var parsmeterArray = this.GetParameters(parameters); var sugarParameters = this.GetParameters(parameters);
this.Context.InitMappingInfo<T>(); return SqlQueryAsync<T>(sql, sugarParameters);
var builder = InstanceFactory.GetSqlbuilder(this.Context.CurrentConnectionConfig); }
builder.SqlQueryBuilder.sql.Append(sql); public virtual async Task<List<T>> SqlQueryAsync<T>(string sql, params SugarParameter[] parameters)
if (parsmeterArray != null && parsmeterArray.Any()) {
builder.SqlQueryBuilder.Parameters.AddRange(parsmeterArray); var result = await SqlQueryAsync<T, object, object, object, object, object, object>(sql, parameters);
using (var dataReader = await this.GetDataReaderNoCloseAsync(builder.SqlQueryBuilder.ToSqlString(), builder.SqlQueryBuilder.Parameters.ToArray())) return result.Item1;
}
public virtual Task<List<T>> SqlQueryAsync<T>(string sql, List<SugarParameter> parameters)
{
if (parameters != null)
{ {
List<T> result = await this.DbBind.DataReaderToListNoUsingAsync<T>(typeof(T), dataReader); return SqlQueryAsync<T>(sql, parameters.ToArray());
NextResult(dataReader);
List<T2> result2 = await this.DbBind.DataReaderToListNoUsingAsync<T2>(typeof(T2), dataReader);
builder.SqlQueryBuilder.Clear();
if (this.Context.Ado.DataReaderParameters != null)
{
foreach (IDataParameter item in this.Context.Ado.DataReaderParameters)
{
var parameter = parsmeterArray.FirstOrDefault(it => item.ParameterName.Substring(1) == it.ParameterName.Substring(1));
if (parameter != null)
{
parameter.Value = item.Value;
}
}
this.Context.Ado.DataReaderParameters = null;
}
return Tuple.Create<List<T>, List<T2>>(result, result2);
} }
else
{
return SqlQueryAsync<T>(sql);
}
}
public async Task<Tuple<List<T>, List<T2>>> SqlQueryAsync<T, T2>(string sql, object parameters = null)
{
var result = await SqlQueryAsync<T, T2, object, object, object, object, object>(sql, parameters);
return new Tuple<List<T>, List<T2>>(result.Item1, result.Item2);
} }
public async Task<Tuple<List<T>, List<T2>, List<T3>>> SqlQueryAsync<T, T2, T3>(string sql, object parameters = null) public async Task<Tuple<List<T>, List<T2>, List<T3>>> SqlQueryAsync<T, T2, T3>(string sql, object parameters = null)
{ {
var parsmeterArray = this.GetParameters(parameters); var result = await SqlQueryAsync<T, T2, T3, object, object, object, object>(sql, parameters);
this.Context.InitMappingInfo<T>(); return new Tuple<List<T>, List<T2>, List<T3>>(result.Item1, result.Item2, result.Item3);
var builder = InstanceFactory.GetSqlbuilder(this.Context.CurrentConnectionConfig);
builder.SqlQueryBuilder.sql.Append(sql);
if (parsmeterArray != null && parsmeterArray.Any())
builder.SqlQueryBuilder.Parameters.AddRange(parsmeterArray);
using (var dataReader = await this.GetDataReaderNoCloseAsync(builder.SqlQueryBuilder.ToSqlString(), builder.SqlQueryBuilder.Parameters.ToArray()))
{
List<T> result = await this.DbBind.DataReaderToListNoUsingAsync<T>(typeof(T), dataReader);
NextResult(dataReader);
List<T2> result2 = await this.DbBind.DataReaderToListNoUsingAsync<T2>(typeof(T2), dataReader);
NextResult(dataReader);
List<T3> result3 = await this.DbBind.DataReaderToListNoUsingAsync<T3>(typeof(T3), dataReader);
builder.SqlQueryBuilder.Clear();
if (this.Context.Ado.DataReaderParameters != null)
{
foreach (IDataParameter item in this.Context.Ado.DataReaderParameters)
{
var parameter = parsmeterArray.FirstOrDefault(it => item.ParameterName.Substring(1) == it.ParameterName.Substring(1));
if (parameter != null)
{
parameter.Value = item.Value;
}
}
this.Context.Ado.DataReaderParameters = null;
}
return Tuple.Create<List<T>, List<T2>, List<T3>>(result, result2, result3);
}
} }
public async Task<Tuple<List<T>, List<T2>, List<T3>, List<T4>>> SqlQueryAsync<T, T2, T3, T4>(string sql, object parameters = null) public async Task<Tuple<List<T>, List<T2>, List<T3>, List<T4>>> SqlQueryAsync<T, T2, T3, T4>(string sql, object parameters = null)
{ {
var parsmeterArray = this.GetParameters(parameters); var result = await SqlQueryAsync<T, T2, T3, T4, object, object, object>(sql, parameters);
this.Context.InitMappingInfo<T>(); return new Tuple<List<T>, List<T2>, List<T3>, List<T4>>(result.Item1, result.Item2, result.Item3, result.Item4);
var builder = InstanceFactory.GetSqlbuilder(this.Context.CurrentConnectionConfig);
builder.SqlQueryBuilder.sql.Append(sql);
if (parsmeterArray != null && parsmeterArray.Any())
builder.SqlQueryBuilder.Parameters.AddRange(parsmeterArray);
using (var dataReader = await this.GetDataReaderNoCloseAsync(builder.SqlQueryBuilder.ToSqlString(), builder.SqlQueryBuilder.Parameters.ToArray()))
{
List<T> result = await this.DbBind.DataReaderToListNoUsingAsync<T>(typeof(T), dataReader);
NextResult(dataReader);
List<T2> result2 = await this.DbBind.DataReaderToListNoUsingAsync<T2>(typeof(T2), dataReader);
NextResult(dataReader);
List<T3> result3 = await this.DbBind.DataReaderToListNoUsingAsync<T3>(typeof(T3), dataReader);
NextResult(dataReader);
List<T4> result4 = await this.DbBind.DataReaderToListNoUsingAsync<T4>(typeof(T4), dataReader);
builder.SqlQueryBuilder.Clear();
if (this.Context.Ado.DataReaderParameters != null)
{
foreach (IDataParameter item in this.Context.Ado.DataReaderParameters)
{
var parameter = parsmeterArray.FirstOrDefault(it => item.ParameterName.Substring(1) == it.ParameterName.Substring(1));
if (parameter != null)
{
parameter.Value = item.Value;
}
}
this.Context.Ado.DataReaderParameters = null;
}
return Tuple.Create<List<T>, List<T2>, List<T3>, List<T4>>(result, result2, result3, result4);
}
} }
public async Task<Tuple<List<T>, List<T2>, List<T3>, List<T4>, List<T5>>> SqlQueryAsync<T, T2, T3, T4, T5>(string sql, object parameters = null) public async Task<Tuple<List<T>, List<T2>, List<T3>, List<T4>, List<T5>>> SqlQueryAsync<T, T2, T3, T4, T5>(string sql, object parameters = null)
{ {
var parsmeterArray = this.GetParameters(parameters); var result = await SqlQueryAsync<T, T2, T3, T4, T5, object, object>(sql, parameters);
this.Context.InitMappingInfo<T>(); return new Tuple<List<T>, List<T2>, List<T3>, List<T4>, List<T5>>(result.Item1, result.Item2, result.Item3, result.Item4, result.Item5);
var builder = InstanceFactory.GetSqlbuilder(this.Context.CurrentConnectionConfig);
builder.SqlQueryBuilder.sql.Append(sql);
if (parsmeterArray != null && parsmeterArray.Any())
builder.SqlQueryBuilder.Parameters.AddRange(parsmeterArray);
using (var dataReader = await this.GetDataReaderNoCloseAsync(builder.SqlQueryBuilder.ToSqlString(), builder.SqlQueryBuilder.Parameters.ToArray()))
{
List<T> result = await this.DbBind.DataReaderToListNoUsingAsync<T>(typeof(T), dataReader);
NextResult(dataReader);
List<T2> result2 = await this.DbBind.DataReaderToListNoUsingAsync<T2>(typeof(T2), dataReader);
NextResult(dataReader);
List<T3> result3 = await this.DbBind.DataReaderToListNoUsingAsync<T3>(typeof(T3), dataReader);
NextResult(dataReader);
List<T4> result4 = await this.DbBind.DataReaderToListNoUsingAsync<T4>(typeof(T4), dataReader);
NextResult(dataReader);
List<T5> result5 = await this.DbBind.DataReaderToListNoUsingAsync<T5>(typeof(T5), dataReader);
builder.SqlQueryBuilder.Clear();
if (this.Context.Ado.DataReaderParameters != null)
{
foreach (IDataParameter item in this.Context.Ado.DataReaderParameters)
{
var parameter = parsmeterArray.FirstOrDefault(it => item.ParameterName.Substring(1) == it.ParameterName.Substring(1));
if (parameter != null)
{
parameter.Value = item.Value;
}
}
this.Context.Ado.DataReaderParameters = null;
}
return Tuple.Create<List<T>, List<T2>, List<T3>, List<T4>, List<T5>>(result, result2, result3, result4, result5);
}
} }
public async Task<Tuple<List<T>, List<T2>, List<T3>, List<T4>, List<T5>, List<T6>>> SqlQueryAsync<T, T2, T3, T4, T5, T6>(string sql, object parameters = null) public async Task<Tuple<List<T>, List<T2>, List<T3>, List<T4>, List<T5>, List<T6>>> SqlQueryAsync<T, T2, T3, T4, T5, T6>(string sql, object parameters = null)
{ {
var parsmeterArray = this.GetParameters(parameters); var result =await SqlQueryAsync<T, T2, T3, T4, T5, T6, object>(sql, parameters);
this.Context.InitMappingInfo<T>(); return new Tuple<List<T>, List<T2>, List<T3>, List<T4>, List<T5>, List<T6>>(result.Item1, result.Item2, result.Item3, result.Item4, result.Item5, result.Item6);
var builder = InstanceFactory.GetSqlbuilder(this.Context.CurrentConnectionConfig);
builder.SqlQueryBuilder.sql.Append(sql);
if (parsmeterArray != null && parsmeterArray.Any())
builder.SqlQueryBuilder.Parameters.AddRange(parsmeterArray);
using (var dataReader = await this.GetDataReaderNoCloseAsync(builder.SqlQueryBuilder.ToSqlString(), builder.SqlQueryBuilder.Parameters.ToArray()))
{
List<T> result = await this.DbBind.DataReaderToListNoUsingAsync<T>(typeof(T), dataReader);
NextResult(dataReader);
List<T2> result2 = await this.DbBind.DataReaderToListNoUsingAsync<T2>(typeof(T2), dataReader);
NextResult(dataReader);
List<T3> result3 = await this.DbBind.DataReaderToListNoUsingAsync<T3>(typeof(T3), dataReader);
NextResult(dataReader);
List<T4> result4 = await this.DbBind.DataReaderToListNoUsingAsync<T4>(typeof(T4), dataReader);
NextResult(dataReader);
List<T5> result5 = await this.DbBind.DataReaderToListNoUsingAsync<T5>(typeof(T5), dataReader);
NextResult(dataReader);
List<T6> result6 = await this.DbBind.DataReaderToListNoUsingAsync<T6>(typeof(T6), dataReader);
builder.SqlQueryBuilder.Clear();
if (this.Context.Ado.DataReaderParameters != null)
{
foreach (IDataParameter item in this.Context.Ado.DataReaderParameters)
{
var parameter = parsmeterArray.FirstOrDefault(it => item.ParameterName.Substring(1) == it.ParameterName.Substring(1));
if (parameter != null)
{
parameter.Value = item.Value;
}
}
this.Context.Ado.DataReaderParameters = null;
}
return Tuple.Create<List<T>, List<T2>, List<T3>, List<T4>, List<T5>, List<T6>>(result, result2, result3, result4, result5, result6);
}
} }
public async Task<Tuple<List<T>, List<T2>, List<T3>, List<T4>, List<T5>, List<T6>, List<T7>>> SqlQueryAsync<T, T2, T3, T4, T5, T6, T7>(string sql, object parameters = null) public async Task<Tuple<List<T>, List<T2>, List<T3>, List<T4>, List<T5>, List<T6>, List<T7>>> SqlQueryAsync<T, T2, T3, T4, T5, T6, T7>(string sql, object parameters = null)
{ {
@ -1142,19 +945,48 @@ namespace SqlSugar
builder.SqlQueryBuilder.Parameters.AddRange(parsmeterArray); builder.SqlQueryBuilder.Parameters.AddRange(parsmeterArray);
using (var dataReader = await this.GetDataReaderNoCloseAsync(builder.SqlQueryBuilder.ToSqlString(), builder.SqlQueryBuilder.Parameters.ToArray())) using (var dataReader = await this.GetDataReaderNoCloseAsync(builder.SqlQueryBuilder.ToSqlString(), builder.SqlQueryBuilder.Parameters.ToArray()))
{ {
List<T> result = await this.DbBind.DataReaderToListNoUsingAsync<T>(typeof(T), dataReader); DbDataReader DbReader = (DbDataReader)dataReader;
NextResult(dataReader); List<T> result = null;
List<T2> result2 = await this.DbBind.DataReaderToListNoUsingAsync<T2>(typeof(T2), dataReader); if (DbReader.HasRows)
NextResult(dataReader); {
List<T3> result3 = await this.DbBind.DataReaderToListNoUsingAsync<T3>(typeof(T3), dataReader); result =await this.DbBind.DataReaderToListNoUsingAsync<T>(typeof(T), dataReader);
NextResult(dataReader); }
List<T4> result4 = await this.DbBind.DataReaderToListNoUsingAsync<T4>(typeof(T4), dataReader); List<T2> result2 = null;
NextResult(dataReader); if (DbReader.HasRows)
List<T5> result5 = await this.DbBind.DataReaderToListNoUsingAsync<T5>(typeof(T5), dataReader); {
NextResult(dataReader); NextResult(dataReader);
List<T6> result6 = await this.DbBind.DataReaderToListNoUsingAsync<T6>(typeof(T6), dataReader); result2 = await this.DbBind.DataReaderToListNoUsingAsync<T2>(typeof(T2), dataReader);
NextResult(dataReader); }
List<T7> result7 = await this.DbBind.DataReaderToListNoUsingAsync<T7>(typeof(T7), dataReader); List<T3> result3 = null;
if (DbReader.HasRows)
{
NextResult(dataReader);
result3 = await this.DbBind.DataReaderToListNoUsingAsync<T3>(typeof(T3), dataReader);
}
List<T4> result4 = null;
if (DbReader.HasRows)
{
NextResult(dataReader);
result4 = await this.DbBind.DataReaderToListNoUsingAsync<T4>(typeof(T4), dataReader);
}
List<T5> result5 = null;
if (DbReader.HasRows)
{
NextResult(dataReader);
result5 = await this.DbBind.DataReaderToListNoUsingAsync<T5>(typeof(T5), dataReader);
}
List<T6> result6 = null;
if (DbReader.HasRows)
{
NextResult(dataReader);
result6 = await this.DbBind.DataReaderToListNoUsingAsync<T6>(typeof(T6), dataReader);
}
List<T7> result7 = null;
if (DbReader.HasRows)
{
NextResult(dataReader);
result7 = await this.DbBind.DataReaderToListNoUsingAsync<T7>(typeof(T7), dataReader);
}
builder.SqlQueryBuilder.Clear(); builder.SqlQueryBuilder.Clear();
if (this.Context.Ado.DataReaderParameters != null) if (this.Context.Ado.DataReaderParameters != null)
{ {

Loading…
Cancel
Save