/// <summary> /// 异步执行 SQL 语句,并返回两个实体集合 /// </summary> /// <param name="query1">SQL 命令</param> /// <param name="query2">SQL 命令</param> /// <param name="query3">SQL 命令</param> public override async Task <Tuple <List <T1>, List <T2>, List <T3> > > ExecuteAsync <T1, T2, T3>(IDbQueryable <T1> query1, IDbQueryable <T2> query2, IDbQueryable <T3> query3) { List <T1> q1 = null; List <T2> q2 = null; List <T3> q3 = null; IDataReader reader = null; List <DbRawCommand> sqlList = query1.Provider.Translate(new List <object> { query1, query2, query3 }); List <IMapDescriptor> maps = sqlList.ToList(a => a is IMapDescriptor, a => a as IMapDescriptor); TypeDeserializer deserializer1 = null; TypeDeserializer deserializer2 = null; TypeDeserializer deserializer3 = null; Func <System.Data.Common.DbCommand, Task <object> > doExecute = async cmd => { reader = await base.ExecuteReaderAsync(cmd); do { if (q1 == null) { deserializer1 = new TypeDeserializer(_context, reader, maps.Count > 0 ? maps[0] : null); q1 = deserializer1.Deserialize <List <T1> >(); } else if (q2 == null) { deserializer2 = new TypeDeserializer(_context, reader, maps.Count > 1 ? maps[1] : null); q2 = deserializer2.Deserialize <List <T2> >(); } else if (q3 == null) { deserializer3 = new TypeDeserializer(_context, reader, maps.Count > 2 ? maps[2] : null); q3 = deserializer3.Deserialize <List <T3> >(); } }while (reader.NextResult()); // 释放当前的reader if (reader != null) { reader.Dispose(); } return(null); }; try { await base.DoExecuteAsync <object>(sqlList, doExecute); return(new Tuple <List <T1>, List <T2>, List <T3> >(q1, q2, q3)); } finally { if (reader != null) { reader.Dispose(); } } }
/// <summary> /// 计算要插入、更新或删除的已修改对象的集,并执行相应命令以实现对数据库的更改 /// </summary> /// <returns></returns> public virtual async Task <int> SubmitChangesAsync() { int rowCount = _dbQueryables.Count; if (rowCount == 0) { return(0); } IDataReader reader = null; List <int> identitys = new List <int>(); List <DbRawCommand> sqlList = this.Translate(); try { Func <IDbCommand, Task <object> > func = async cmd => { reader = await this.Database.ExecuteReaderAsync(cmd); TypeDeserializer deserializer = new TypeDeserializer(this, reader, null); do { List <int> result = null; deserializer.Deserialize <int>(out result); if (result != null && result.Count > 0) { identitys.AddRange(result); } }while (reader.NextResult()); // 释放当前的reader if (reader != null) { reader.Dispose(); } return(null); }; await this.Database.ExecuteAsync <object>(sqlList, func); this.SetIdentityValue(_dbQueryables, identitys); return(rowCount); } finally { if (reader != null) { reader.Dispose(); } this.InternalDispose(); } }
// datareader 转实体 T GetResult <T>(IDataReader reader, IMapDescriptor map) { T result = default(T); if (typeof(T) == typeof(DataTable)) { DataTable table = new DataTable(); table.Load(reader); result = (T)(object)table; } else if (typeof(T) == typeof(DataSet)) { InternalDataSet data = new InternalDataSet(); data.Load(reader, LoadOption.OverwriteChanges, null, new DataTable[] { }); result = (T)(object)data; } else { TypeDeserializer deserializer = new TypeDeserializer(_context, reader, map); result = deserializer.Deserialize <T>(); } return(result); }
/// <summary> /// 执行 SQL 语句,并返回多个实体集合 /// </summary> /// <typeparam name="T1">第一个列表的元素类型</typeparam> /// <typeparam name="T2">第二个列表的元素类型</typeparam> /// <typeparam name="T3">第三个列表的元素类型</typeparam> /// <typeparam name="T4">第四个列表的元素类型</typeparam> /// <typeparam name="T5">第五个列表的元素类型</typeparam> /// <typeparam name="T6">第六个列表的元素类型</typeparam> /// <typeparam name="T7">第七个列表的元素类型</typeparam> /// <param name="command">SQL 命令</param> /// <param name="maps">实体映射描述列表</param> /// <returns></returns> protected override Tuple <List <T1>, List <T2>, List <T3>, List <T4>, List <T5>, List <T6>, List <T7> > Execute <T1, T2, T3, T4, T5, T6, T7>(IDbCommand command, List <IMapDescriptor> maps = null) { List <T1> q1 = null; List <T2> q2 = null; List <T3> q3 = null; List <T4> q4 = null; List <T5> q5 = null; List <T6> q6 = null; List <T7> q7 = null; IDataReader reader = null; List <DbRawCommand> myList = this.ParseCommand(command.CommandText, command.Parameters, command.CommandType); if (myList == null) { q1 = this.Execute <List <T1> >(command, maps != null && maps.Count > 0 ? maps[0] : null); } else { TypeDeserializer deserializer1 = null; TypeDeserializer deserializer2 = null; TypeDeserializer deserializer3 = null; TypeDeserializer deserializer4 = null; TypeDeserializer deserializer5 = null; TypeDeserializer deserializer6 = null; TypeDeserializer deserializer7 = null; Func <IDbCommand, object> doExecute = cmd => { reader = base.ExecuteReader(cmd); do { if (q1 == null) { deserializer1 = new TypeDeserializer(_context, reader, maps != null && maps.Count > 0 ? maps[0] : null); q1 = deserializer1.Deserialize <List <T1> >(); } else if (q2 == null) { deserializer2 = new TypeDeserializer(_context, reader, maps != null && maps.Count > 1 ? maps[1] : null); q2 = deserializer2.Deserialize <List <T2> >(); } else if (q3 == null) { deserializer3 = new TypeDeserializer(_context, reader, maps != null && maps.Count > 2 ? maps[2] : null); q3 = deserializer3.Deserialize <List <T3> >(); } else if (q4 == null) { deserializer4 = new TypeDeserializer(_context, reader, maps != null && maps.Count > 3 ? maps[3] : null); q4 = deserializer4.Deserialize <List <T4> >(); } else if (q5 == null) { deserializer5 = new TypeDeserializer(_context, reader, maps != null && maps.Count > 4 ? maps[4] : null); q5 = deserializer5.Deserialize <List <T5> >(); } else if (q6 == null) { deserializer6 = new TypeDeserializer(_context, reader, maps != null && maps.Count > 5 ? maps[5] : null); q6 = deserializer6.Deserialize <List <T6> >(); } else if (q7 == null) { deserializer7 = new TypeDeserializer(_context, reader, maps != null && maps.Count > 6 ? maps[6] : null); q7 = deserializer7.Deserialize <List <T7> >(); } }while (reader.NextResult()); // 释放当前的reader if (reader != null) { reader.Dispose(); } return(null); }; try { base.DoExecute <object>(myList, doExecute); } finally { if (reader != null) { reader.Dispose(); } } } return(new Tuple <List <T1>, List <T2>, List <T3>, List <T4>, List <T5>, List <T6>, List <T7> >( q1 ?? new List <T1>(), q2 ?? new List <T2>(), q3 ?? new List <T3>(), q4 ?? new List <T4>(), q5 ?? new List <T5>(), q6 ?? new List <T6>(), q7 ?? new List <T7>())); }