From 9487d685577d1c569481c5860cebba136691c0a4 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Sun, 19 May 2019 10:21:18 +0800 Subject: [PATCH] - --- .../Abstract/AdoProvider/AdoProvider.cs | 209 +++++++++++++++++- .../Abstract/DbBindProvider/DbBindProvider.cs | 19 ++ .../SugarProvider/SqlSugarProvider.cs | 35 ++- .../UpdateProvider/UpdateableProvider.cs | 39 +--- Src/Asp.Net/SqlSugar/Interface/IAdo.cs | 6 + Src/Asp.Net/SqlSugar/Interface/IDbBind.cs | 3 +- 6 files changed, 247 insertions(+), 64 deletions(-) diff --git a/Src/Asp.Net/SqlSugar/Abstract/AdoProvider/AdoProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/AdoProvider/AdoProvider.cs index 624df8565..f10d43858 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/AdoProvider/AdoProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/AdoProvider/AdoProvider.cs @@ -803,7 +803,6 @@ namespace SqlSugar return Tuple.Create, List>(result, result2); } } - public Tuple, List, List> SqlQuery(string sql, object parameters = null) { var parsmeterArray = this.GetParameters(parameters); @@ -835,7 +834,6 @@ namespace SqlSugar return Tuple.Create, List, List>(result, result2, result3); } } - public Tuple, List, List, List> SqlQuery(string sql, object parameters = null) { var parsmeterArray = this.GetParameters(parameters); @@ -904,7 +902,6 @@ namespace SqlSugar return Tuple.Create, List, List, List, List>(result, result2, result3, result4, result5); } } - public Tuple, List, List, List, List, List> SqlQuery(string sql, object parameters = null) { var parsmeterArray = this.GetParameters(parameters); @@ -942,7 +939,6 @@ namespace SqlSugar return Tuple.Create, List, List, List, List, List>(result, result2, result3, result4, result5, result6); } } - public Tuple, List, List, List, List, List, List> SqlQuery(string sql, object parameters = null) { var parsmeterArray = this.GetParameters(parameters); @@ -983,6 +979,211 @@ namespace SqlSugar } } + public async Task, List>> SqlQueryAsync(string sql, object parameters = null) + { + var parsmeterArray = this.GetParameters(parameters); + this.Context.InitMappingInfo(); + 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 result =await this.DbBind.DataReaderToListNoUsingAsync(typeof(T), dataReader); + NextResult(dataReader); + List result2 =await this.DbBind.DataReaderToListNoUsingAsync(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>(result, result2); + } + } + public async Task, List, List>> SqlQueryAsync(string sql, object parameters = null) + { + var parsmeterArray = this.GetParameters(parameters); + this.Context.InitMappingInfo(); + 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 result =await this.DbBind.DataReaderToListNoUsingAsync(typeof(T), dataReader); + NextResult(dataReader); + List result2 =await this.DbBind.DataReaderToListNoUsingAsync(typeof(T2), dataReader); + NextResult(dataReader); + List result3 =await this.DbBind.DataReaderToListNoUsingAsync(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, List>(result, result2, result3); + } + } + public async Task, List, List, List>> SqlQueryAsync(string sql, object parameters = null) + { + var parsmeterArray = this.GetParameters(parameters); + this.Context.InitMappingInfo(); + 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 result =await this.DbBind.DataReaderToListNoUsingAsync(typeof(T), dataReader); + NextResult(dataReader); + List result2 =await this.DbBind.DataReaderToListNoUsingAsync(typeof(T2), dataReader); + NextResult(dataReader); + List result3 =await this.DbBind.DataReaderToListNoUsingAsync(typeof(T3), dataReader); + NextResult(dataReader); + List result4 =await this.DbBind.DataReaderToListNoUsingAsync(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, List, List>(result, result2, result3, result4); + } + } + public async Task, List, List, List, List>> SqlQueryAsync(string sql, object parameters = null) + { + var parsmeterArray = this.GetParameters(parameters); + this.Context.InitMappingInfo(); + 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 result = await this.DbBind.DataReaderToListNoUsingAsync(typeof(T), dataReader); + NextResult(dataReader); + List result2 = await this.DbBind.DataReaderToListNoUsingAsync(typeof(T2), dataReader); + NextResult(dataReader); + List result3 = await this.DbBind.DataReaderToListNoUsingAsync(typeof(T3), dataReader); + NextResult(dataReader); + List result4 = await this.DbBind.DataReaderToListNoUsingAsync(typeof(T4), dataReader); + NextResult(dataReader); + List result5 = await this.DbBind.DataReaderToListNoUsingAsync(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, List, List, List>(result, result2, result3, result4, result5); + } + } + public async Task, List, List, List, List, List>> SqlQueryAsync(string sql, object parameters = null) + { + var parsmeterArray = this.GetParameters(parameters); + this.Context.InitMappingInfo(); + 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 result =await this.DbBind.DataReaderToListNoUsingAsync(typeof(T), dataReader); + NextResult(dataReader); + List result2 =await this.DbBind.DataReaderToListNoUsingAsync(typeof(T2), dataReader); + NextResult(dataReader); + List result3 =await this.DbBind.DataReaderToListNoUsingAsync(typeof(T3), dataReader); + NextResult(dataReader); + List result4 =await this.DbBind.DataReaderToListNoUsingAsync(typeof(T4), dataReader); + NextResult(dataReader); + List result5 =await this.DbBind.DataReaderToListNoUsingAsync(typeof(T5), dataReader); + NextResult(dataReader); + List result6 =await this.DbBind.DataReaderToListNoUsingAsync(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, List, List, List, List>(result, result2, result3, result4, result5, result6); + } + } + public async Task, List, List, List, List, List, List>> SqlQueryAsync(string sql, object parameters = null) + { + var parsmeterArray = this.GetParameters(parameters); + this.Context.InitMappingInfo(); + 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 result = await this.DbBind.DataReaderToListNoUsingAsync(typeof(T), dataReader); + NextResult(dataReader); + List result2 = await this.DbBind.DataReaderToListNoUsingAsync(typeof(T2), dataReader); + NextResult(dataReader); + List result3 = await this.DbBind.DataReaderToListNoUsingAsync(typeof(T3), dataReader); + NextResult(dataReader); + List result4 = await this.DbBind.DataReaderToListNoUsingAsync(typeof(T4), dataReader); + NextResult(dataReader); + List result5 = await this.DbBind.DataReaderToListNoUsingAsync(typeof(T5), dataReader); + NextResult(dataReader); + List result6 = await this.DbBind.DataReaderToListNoUsingAsync(typeof(T6), dataReader); + NextResult(dataReader); + List result7 = await this.DbBind.DataReaderToListNoUsingAsync(typeof(T7), 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, List, List, List, List, List>(result, result2, result3, result4, result5, result6, result7); + } + } + private static void NextResult(IDataReader dataReader) { try diff --git a/Src/Asp.Net/SqlSugar/Abstract/DbBindProvider/DbBindProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/DbBindProvider/DbBindProvider.cs index 40f04f06c..5e3064a7a 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/DbBindProvider/DbBindProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/DbBindProvider/DbBindProvider.cs @@ -244,6 +244,25 @@ namespace SqlSugar return GetEntityList(Context, dataReader); } } + public virtual Task> DataReaderToListNoUsingAsync(Type type, IDataReader dataReader) + { + if (type.Name.Contains("KeyValuePair")) + { + return GetKeyValueListAsync(type, dataReader); + } + else if (type.IsValueType() || type == UtilConstants.StringType || type == UtilConstants.ByteArrayType) + { + return GetValueTypeListAsync(type, dataReader); + } + else if (type.IsArray) + { + return GetArrayListAsync(type, dataReader); + } + else + { + return GetEntityListAsync(Context, dataReader); + } + } #endregion #region Throw rule diff --git a/Src/Asp.Net/SqlSugar/Abstract/SugarProvider/SqlSugarProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/SugarProvider/SqlSugarProvider.cs index 0bcf84a0d..60811492d 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/SugarProvider/SqlSugarProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/SugarProvider/SqlSugarProvider.cs @@ -802,64 +802,57 @@ namespace SqlSugar { return SaveQueuesProvider(isTran, (sql, parameters) => { return this.Ado.SqlQuery(sql, parameters); }); } - public Task> SaveQueuesAsync(bool isTran = true) + public async Task> SaveQueuesAsync(bool isTran = true) { - var result = Task.FromResult(SaveQueues(isTran)); - return result; + return await SaveQueuesProvider(isTran,async (sql, parameters) => { return await this.Ado.SqlQueryAsync(sql, parameters); }); } public Tuple, List> SaveQueues(bool isTran = true) { return SaveQueuesProvider(isTran, (sql, parameters) => { return this.Ado.SqlQuery(sql, parameters); }); } - public Task, List>> SaveQueuesAsync(bool isTran = true) + public async Task, List>> SaveQueuesAsync(bool isTran = true) { - var result = Task.FromResult(SaveQueues(isTran)); - return result; + return await SaveQueuesProvider(isTran,async (sql, parameters) => { return await this.Ado.SqlQueryAsync(sql, parameters); }); } public Tuple, List, List> SaveQueues(bool isTran = true) { return SaveQueuesProvider(isTran, (sql, parameters) => { return this.Ado.SqlQuery(sql, parameters); }); } - public Task, List, List>> SaveQueuesAsync(bool isTran = true) + public async Task, List, List>> SaveQueuesAsync(bool isTran = true) { - var result = Task.FromResult(SaveQueues(isTran)); - return result; + return await SaveQueuesProvider(isTran, async(sql, parameters) => { return await this.Ado.SqlQueryAsync(sql, parameters); }); } public Tuple, List, List, List> SaveQueues(bool isTran = true) { return SaveQueuesProvider(isTran, (sql, parameters) => { return this.Ado.SqlQuery(sql, parameters); }); } - public Task, List, List, List>> SaveQueuesAsync(bool isTran = true) + public async Task, List, List, List>> SaveQueuesAsync(bool isTran = true) { - var result = Task.FromResult(SaveQueues(isTran)); - return result; + return await SaveQueuesProvider(isTran,async (sql, parameters) => { return await this.Ado.SqlQueryAsync(sql, parameters); }); } public Tuple, List, List, List, List> SaveQueues(bool isTran = true) { return SaveQueuesProvider(isTran, (sql, parameters) => { return this.Ado.SqlQuery(sql, parameters); }); } - public Task, List, List, List, List>> SaveQueuesAsync(bool isTran = true) + public async Task, List, List, List, List>> SaveQueuesAsync(bool isTran = true) { - var result = Task.FromResult(SaveQueues(isTran)); - return result; + return await SaveQueuesProvider(isTran,async (sql, parameters) => { return await this.Ado.SqlQueryAsync(sql, parameters); }); } public Tuple, List, List, List, List, List> SaveQueues(bool isTran = true) { return SaveQueuesProvider(isTran, (sql, parameters) => { return this.Ado.SqlQuery(sql, parameters); }); } - public Task, List, List, List, List, List>> SaveQueuesAsync(bool isTran = true) + public async Task, List, List, List, List, List>> SaveQueuesAsync(bool isTran = true) { - var result = Task.FromResult(SaveQueues(isTran)); - return result; + return await SaveQueuesProvider(isTran,async (sql, parameters) => { return await this.Ado.SqlQueryAsync(sql, parameters); }); } public Tuple, List, List, List, List, List, List> SaveQueues(bool isTran = true) { return SaveQueuesProvider(isTran, (sql, parameters) => { return this.Ado.SqlQuery(sql, parameters); }); } - public Task, List, List, List, List, List, List>> SaveQueuesAsync(bool isTran = true) + public async Task, List, List, List, List, List, List>> SaveQueuesAsync(bool isTran = true) { - var result = Task.FromResult(SaveQueues(isTran)); - return result; + return await SaveQueuesProvider(isTran,async (sql, parameters) => { return await this.Ado.SqlQueryAsync(sql, parameters); }); } public void AddQueue(string sql, object parsmeters=null) { diff --git a/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs index d87d30836..13efe5f74 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs @@ -191,7 +191,6 @@ namespace SqlSugar } - public IUpdateable UpdateColumns(Expression> columns) { var updateColumns = UpdateBuilder.GetExpressionValue(columns, ResolveExpressType.ArraySingle).GetResultArray().Select(it => this.SqlBuilder.GetNoTranslationColumnName(it)).ToList(); @@ -598,43 +597,7 @@ namespace SqlSugar this.Context.MappingTables = OldMappingTableList; } } - //private void TaskStart(Task result) - //{ - // if (this.Context.CurrentConnectionConfig.IsShardSameThread) - // { - // Check.Exception(true, "IsShardSameThread=true can't be used async method"); - // } - // result.Start(); - //} - //private IUpdateable CopyUpdateable() - //{ - // var asyncContext = this.Context.Utilities.CopyContext(true); - // asyncContext.CurrentConnectionConfig.IsAutoCloseConnection = true; - // asyncContext.IsAsyncMethod = true; - - // var asyncUpdateable = asyncContext.Updateable(this.UpdateObjs); - // var asyncUpdateableBuilder = asyncUpdateable.UpdateBuilder; - // asyncUpdateableBuilder.DbColumnInfoList = this.UpdateBuilder.DbColumnInfoList; - // asyncUpdateableBuilder.IsNoUpdateNull = this.UpdateBuilder.IsNoUpdateNull; - // asyncUpdateableBuilder.Parameters = this.UpdateBuilder.Parameters; - // asyncUpdateableBuilder.sql = this.UpdateBuilder.sql; - // asyncUpdateableBuilder.WhereValues = this.UpdateBuilder.WhereValues; - // asyncUpdateableBuilder.TableWithString = this.UpdateBuilder.TableWithString; - // asyncUpdateableBuilder.TableName = this.UpdateBuilder.TableName; - // asyncUpdateableBuilder.PrimaryKeys = this.UpdateBuilder.PrimaryKeys; - // asyncUpdateableBuilder.IsOffIdentity = this.UpdateBuilder.IsOffIdentity; - // asyncUpdateableBuilder.SetValues = this.UpdateBuilder.SetValues; - // if (this.IsWhereColumns) - // { - // (asyncUpdateable as UpdateableProvider).WhereColumnList = this.WhereColumnList; - // (asyncUpdateable as UpdateableProvider).IsWhereColumns = this.IsWhereColumns; - // } - // if (this.RemoveCacheFunc != null) - // { - // asyncUpdateable.RemoveDataCache(); - // } - // return asyncUpdateable; - //} + private void ValidateVersion() { diff --git a/Src/Asp.Net/SqlSugar/Interface/IAdo.cs b/Src/Asp.Net/SqlSugar/Interface/IAdo.cs index 1bd4c29a4..5cf523e09 100644 --- a/Src/Asp.Net/SqlSugar/Interface/IAdo.cs +++ b/Src/Asp.Net/SqlSugar/Interface/IAdo.cs @@ -89,6 +89,12 @@ namespace SqlSugar Tuple, List, List, List, List> SqlQuery(string sql, object parameters = null); Tuple, List, List, List, List, List> SqlQuery(string sql, object parameters = null); Tuple, List, List, List, List, List, List> SqlQuery(string sql, object parameters = null); + Task, List>> SqlQueryAsync(string sql, object parameters = null); + Task, List, List>> SqlQueryAsync(string sql, object parameters = null); + Task, List, List, List>> SqlQueryAsync(string sql, object parameters = null); + Task, List, List, List, List>> SqlQueryAsync(string sql, object parameters = null); + Task, List, List, List, List, List>> SqlQueryAsync(string sql, object parameters = null); + Task, List, List, List, List, List, List>> SqlQueryAsync(string sql, object parameters = null); List SqlQuery(string sql, object parameters = null); List SqlQuery(string sql, params SugarParameter[] parameters); diff --git a/Src/Asp.Net/SqlSugar/Interface/IDbBind.cs b/Src/Asp.Net/SqlSugar/Interface/IDbBind.cs index 5103455ae..14e99c092 100644 --- a/Src/Asp.Net/SqlSugar/Interface/IDbBind.cs +++ b/Src/Asp.Net/SqlSugar/Interface/IDbBind.cs @@ -25,6 +25,7 @@ namespace SqlSugar List DataReaderToList(Type type, IDataReader reader); Task> DataReaderToListAsync(Type entityType, IDataReader dataReader); List DataReaderToListNoUsing(Type type, IDataReader reader); - + Task> DataReaderToListNoUsingAsync(Type type, IDataReader reader); + } }