/// <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> > > ExecuteMultipleAsync <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 <DbCommandDefinition> sqlList = query1.Provider.Resolve(new List <object> { query1, query2, query3 }); List <SelectDbCommandDefinition> shapes = sqlList.ToList(x => x as SelectDbCommandDefinition, x => x is SelectDbCommandDefinition); TypeDeserializer deserializer1 = null; TypeDeserializer deserializer2 = null; TypeDeserializer deserializer3 = null; Func <DbCommand, Task <object> > doExecute = async cmd => { reader = await base.ExecuteReaderAsync(cmd); do { if (q1 == null) { deserializer1 = new TypeDeserializer(reader, shapes.Count > 0 ? shapes[0] : null); q1 = deserializer1.Deserialize <T1>(); } else if (q2 == null) { deserializer2 = new TypeDeserializer(reader, shapes.Count > 1 ? shapes[1] : null); q2 = deserializer2.Deserialize <T2>(); } else if (q3 == null) { deserializer3 = new TypeDeserializer(reader, shapes.Count > 2 ? shapes[2] : null); q3 = deserializer3.Deserialize <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> /// 异步执行SQL 语句,并返回 <see cref="IEnumerable"/> 对象 /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <param name="cmd">SQL 命令</param> /// <param name="definition">命令定义对象,用于解析实体的外键</param> /// <returns></returns> async Task <List <T> > ExecuteListAsync <T>(IDbCommand cmd, SelectDbCommandDefinition definition) { IDataReader reader = null; List <T> objList = new List <T>(); try { reader = await this.ExecuteReaderAsync(cmd); TypeDeserializer deserializer = new TypeDeserializer(reader, definition); objList = await deserializer.DeserializeAsync <T>(); } finally { if (cmd != null) { cmd.Dispose(); } if (reader != null) { reader.Dispose(); } this.InternalDispose(); } return(objList); }
// 执行SQL 语句,并返回单个实体对象 async Task <T> ExecuteAsync <T>(IDbCommand cmd, SelectDbCommandDefinition definition) { IDataReader reader = null; IDbConnection conn = null; try { reader = await this.ExecuteReaderAsync(cmd); conn = cmd != null ? cmd.Connection : null; TypeDeserializer deserializer = new TypeDeserializer(reader, definition); List <T> result = await deserializer.DeserializeAsync <T>(); return(result.FirstOrDefault()); } finally { if (cmd != null) { cmd.Dispose(); } if (reader != null) { reader.Dispose(); } this.InternalDispose(); } }
/// <summary> /// 反序列化实体集合 /// <para> /// 适用于自增列与结果集混合输出的场景 /// </para> /// </summary> public List <T> Deserialize <T>(out List <int> identitys) { bool isThisLine = false; object prevLine = null; List <T> collection = null; identitys = null; TypeDeserializer <T> deserializer = null; TypeDeserializer <int> deserializer2 = null; bool isAutoIncrement = false; bool readedName = false; while (_reader.Read()) { if (!isAutoIncrement && !readedName) { isAutoIncrement = _reader.GetName(0) == Constant.AUTOINCREMENTNAME; readedName = true; } if (isAutoIncrement) { // 输出自增列 if (deserializer2 == null) { deserializer2 = new TypeDeserializer <int>(_reader, null); } if (identitys == null) { identitys = new List <int>(1); } int model = deserializer2.Deserialize(prevLine, out isThisLine); identitys.Add(model); } else { // 输出指定类型实体 if (deserializer == null) { deserializer = new TypeDeserializer <T>(_reader, _definition); } T model = deserializer.Deserialize(prevLine, out isThisLine); if (!isThisLine) { if (collection == null) { collection = new List <T>(); } collection.Add(model); prevLine = model; } } } // 返回结果 return(collection); }
// 执行提交 protected virtual List <int> DoSubmit <T>(List <DbCommandDefinition> sqlList, out List <T> result) { IDataReader reader = null; List <int> identitys = null; result = null; List <T> q1 = null; Func <IDbCommand, object> doExecute = cmd => { reader = this.ExecuteReader(cmd); TypeDeserializer deserializer = new TypeDeserializer(reader, null); do { List <int> autoIncrements = null; var collection = deserializer.Deserialize <T>(out autoIncrements); if (autoIncrements != null) { if (identitys == null) { identitys = new List <int>(); } identitys.AddRange(autoIncrements); } else if (collection != null) { if (q1 == null) { q1 = collection; } } }while (reader.NextResult()); // 释放当前的reader if (reader != null) { reader.Dispose(); } return(null); }; try { this.DoExecute <object>(sqlList, doExecute); result = q1 ?? new List <T>(0); return(identitys); } finally { if (reader != null) { reader.Dispose(); } } }
// 执行提交 protected override List <int> DoSubmit(List <DbCommandDefinition> sqlList) { List <int> identitys = new List <int>(); IDataReader reader = null; try { Func <IDbCommand, object> func = cmd => { reader = base.ExecuteReader(cmd); TypeDeserializer deserializer = new TypeDeserializer(reader, null); do { List <int> autoIncrements = null; deserializer.Deserialize <object>(out autoIncrements); if (cmd.Parameters != null) { foreach (IDbDataParameter p in cmd.Parameters) { if (p.Direction != ParameterDirection.Output) { continue; } identitys.Add(Convert.ToInt32(p.Value)); } } }while (reader.NextResult()); // 释放当前的reader if (reader != null) { reader.Dispose(); } return(null); }; base.DoExecute <object>(sqlList, func); return(identitys); } finally { if (reader != null) { reader.Dispose(); } } }
// 异步执行提交 protected override async Task <List <int> > DoSubmitAsync(List <DbCommandDefinition> sqlList) { List <int> identitys = new List <int>(); IDataReader reader = null; try { Func <DbCommand, Task <object> > func = async cmd => { reader = await base.ExecuteReaderAsync(cmd); TypeDeserializer deserializer = new TypeDeserializer(reader, null); do { var result = deserializer.Deserialize <int>(); foreach (IDbDataParameter p in cmd.Parameters) { if (p.Direction != ParameterDirection.Output) { continue; } identitys.Add(Convert.ToInt32(p.Value)); } }while (reader.NextResult()); // 释放当前的reader if (reader != null) { reader.Dispose(); } return(null); }; await base.DoExecuteAsync <object>(sqlList, func); return(identitys); } finally { if (reader != null) { reader.Dispose(); } } }
// 执行 protected virtual async Task <List <int> > DoSubmitAsync(List <DbCommandDefinition> sqlList) { List <int> identitys = new List <int>(); IDataReader reader = null; try { Func <DbCommand, Task <object> > func = async p => { reader = await this.ExecuteReaderAsync(p); TypeDeserializer deserializer = new TypeDeserializer(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.DoExecuteAsync <object>(sqlList, func); return(identitys); } finally { if (reader != null) { reader.Dispose(); } } }
// 执行提交 protected virtual List <int> DoSubmit(List <DbCommandDefinition> sqlList) { IDataReader reader = null; List <int> identitys = null; Func <IDbCommand, object> doExecute = cmd => { reader = this.ExecuteReader(cmd); TypeDeserializer deserializer = new TypeDeserializer(reader, null); do { List <int> autoIncrements = null; deserializer.Deserialize <object>(out autoIncrements); if (autoIncrements != null && autoIncrements.Count > 0) { if (identitys == null) { identitys = new List <int>(); } identitys.AddRange(autoIncrements); } }while (reader.NextResult()); // 释放当前的reader if (reader != null) { reader.Dispose(); } return(null); }; try { this.DoExecute <object>(sqlList, doExecute); return(identitys); } finally { if (reader != null) { reader.Dispose(); } } }
/// <summary> /// 异步反序列化实体集合 /// </summary> public async Task <List <T> > DeserializeAsync <T>() { bool isThisLine = false; object prevLine = null; List <T> collection = new List <T>(); TypeDeserializer <T> deserializer = new TypeDeserializer <T>(_reader, _definition); while (await(_reader as DbDataReader).ReadAsync()) { T model = deserializer.Deserialize(prevLine, out isThisLine); if (!isThisLine) { collection.Add(model); prevLine = model; } } // 返回结果 return(collection); }
/// <summary> /// 反序列化实体集合 /// <para> /// 适用于单一结果集的场景 /// </para> /// </summary> public List <T> Deserialize <T>() { bool isThisLine = false; object prevLine = null; List <T> collection = new List <T>(); TypeDeserializer <T> deserializer = new TypeDeserializer <T>(_reader, _definition); while (_reader.Read()) { T model = deserializer.Deserialize(prevLine, out isThisLine); if (!isThisLine) { collection.Add(model); prevLine = model; } } // 返回结果 return(collection); }
// 执行SQL 语句,并返回单个实体对象 T Execute <T>(IDbCommand cmd, SelectDbCommandDefinition definition) { IDataReader reader = null; try { reader = this.ExecuteReader(cmd); TypeDeserializer deserializer = new TypeDeserializer(reader, definition); List <T> result = deserializer.Deserialize <T>(); return(result.FirstOrDefault()); } finally { if (cmd != null) { cmd.Dispose(); } if (reader != null) { reader.Dispose(); } this.InternalDispose(); } }
// 异步执行 SQL 语句,并返回多个实体集合 async Task <Tuple <List <T1>, List <T2>, List <T3>, List <T4>, List <T5>, List <T6>, List <T7> > > ExecuteMultipleAsync <T1, T2, T3, T4, T5, T6, T7>(IDbCommand cmd, List <SelectDbCommandDefinition> defines = null) { IDataReader reader = null; IDbConnection conn = 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; TypeDeserializer deserializer1 = null; TypeDeserializer deserializer2 = null; TypeDeserializer deserializer3 = null; TypeDeserializer deserializer4 = null; TypeDeserializer deserializer5 = null; TypeDeserializer deserializer6 = null; TypeDeserializer deserializer7 = null; try { int i = 0; reader = await this.ExecuteReaderAsync(cmd); conn = cmd != null ? cmd.Connection : null; do { i += 1; switch (i) { #region 元组赋值 case 1: if (deserializer1 == null) { deserializer1 = new TypeDeserializer(reader, defines != null ? defines[i - 1] : null); } q1 = await deserializer1.DeserializeAsync <T1>(); break; case 2: if (deserializer2 == null) { deserializer2 = new TypeDeserializer(reader, defines != null ? defines[i - 1] : null); } q2 = await deserializer2.DeserializeAsync <T2>(); break; case 3: if (deserializer3 == null) { deserializer3 = new TypeDeserializer(reader, defines != null ? defines[i - 1] : null); } q3 = await deserializer3.DeserializeAsync <T3>(); break; case 4: if (deserializer4 == null) { deserializer4 = new TypeDeserializer(reader, defines != null ? defines[i - 1] : null); } q4 = await deserializer4.DeserializeAsync <T4>(); break; case 5: if (deserializer5 == null) { deserializer5 = new TypeDeserializer(reader, defines != null ? defines[i - 1] : null); } q5 = await deserializer5.DeserializeAsync <T5>(); break; case 6: if (deserializer6 == null) { deserializer6 = new TypeDeserializer(reader, defines != null ? defines[i - 1] : null); } q6 = await deserializer6.DeserializeAsync <T6>(); break; case 7: if (deserializer7 == null) { deserializer7 = new TypeDeserializer(reader, defines != null ? defines[i - 1] : null); } q7 = await deserializer7.DeserializeAsync <T7>(); break; #endregion } }while (reader.NextResult()); } finally { if (cmd != null) { cmd.Dispose(); } if (reader != null) { reader.Dispose(); } this.InternalDispose(); } 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>())); }
// 执行提交 protected override List <int> DoSubmit <T1, T2>(List <DbCommandDefinition> sqlList, out List <T1> result1, out List <T2> result2) { IDataReader reader = null; List <int> identitys = null; result1 = null; List <T1> q1 = null; List <T2> q2 = null; List <SelectDbCommandDefinition> shapes = sqlList.ToList(x => x as SelectDbCommandDefinition, x => x is SelectDbCommandDefinition); Func <IDbCommand, object> doExecute = cmd => { reader = base.ExecuteReader(cmd); TypeDeserializer deserializer1 = null; TypeDeserializer deserializer2 = null; do { if (q1 == null) { // 先查第一个类型集合 List <int> autoIncrements = null; if (deserializer1 == null) { deserializer1 = new TypeDeserializer(reader, shapes.Count > 0 ? shapes[0] : null); } var collection = deserializer1.Deserialize <T1>(out autoIncrements); if (collection != null) { q1 = collection; } if (cmd.Parameters != null) { foreach (IDbDataParameter p in cmd.Parameters) { if (identitys == null) { identitys = new List <int>(); } if (p.Direction != ParameterDirection.Output) { continue; } identitys.Add(Convert.ToInt32(p.Value)); } } } else { // 再查第二个类型集合 List <int> autoIncrements = null; if (deserializer2 == null) { deserializer2 = new TypeDeserializer(reader, shapes.Count > 1 ? shapes[1] : null); } var collection = deserializer2.Deserialize <T2>(out autoIncrements); if (collection != null) { if (q2 == null) { q2 = collection; } } if (cmd.Parameters != null) { foreach (IDbDataParameter p in cmd.Parameters) { if (identitys == null) { identitys = new List <int>(); } if (p.Direction != ParameterDirection.Output) { continue; } identitys.Add(Convert.ToInt32(p.Value)); } } } }while (reader.NextResult()); // 释放当前的reader if (reader != null) { reader.Dispose(); } return(null); }; try { base.DoExecute <object>(sqlList, doExecute); result1 = q1 ?? new List <T1>(0); result2 = q2 ?? new List <T2>(0); return(identitys); } finally { if (reader != null) { reader.Dispose(); } } }
// 执行提交 protected override List <int> DoSubmit <T>(List <DbCommandDefinition> sqlList, out List <T> result) { IDataReader reader = null; List <int> identitys = null; result = null; List <T> q1 = null; Func <IDbCommand, object> doExecute = cmd => { reader = base.ExecuteReader(cmd); TypeDeserializer deserializer = new TypeDeserializer(reader, null); do { List <int> autoIncrements = null; var collection = deserializer.Deserialize <T>(out autoIncrements); if (collection != null) { if (q1 == null) { q1 = collection; } } if (cmd.Parameters != null) { foreach (IDbDataParameter p in cmd.Parameters) { if (identitys == null) { identitys = new List <int>(); } if (p.Direction != ParameterDirection.Output) { continue; } identitys.Add(Convert.ToInt32(p.Value)); } } }while (reader.NextResult()); // 释放当前的reader if (reader != null) { reader.Dispose(); } return(null); }; try { base.DoExecute <object>(sqlList, doExecute); result = q1 ?? new List <T>(0); return(identitys); } finally { if (reader != null) { reader.Dispose(); } } }
// 执行提交 protected virtual List <int> DoSubmit <T1, T2>(List <DbCommandDefinition> sqlList, out List <T1> result1, out List <T2> result2) { IDataReader reader = null; List <int> identitys = null; result1 = null; List <T1> q1 = null; List <T2> q2 = null; List <SelectDbCommandDefinition> defines = sqlList.ToList(x => x as SelectDbCommandDefinition, x => x is SelectDbCommandDefinition); Func <IDbCommand, object> doExecute = cmd => { reader = this.ExecuteReader(cmd); TypeDeserializer deserializer1 = null; TypeDeserializer deserializer2 = null; do { if (q1 == null) { // 先查第一个类型集合 List <int> autoIncrements = null; if (deserializer1 == null) { deserializer1 = new TypeDeserializer(reader, defines.Count > 0 ? defines[0] : null); } var collection = deserializer1.Deserialize <T1>(out autoIncrements); if (autoIncrements != null) { if (identitys == null) { identitys = new List <int>(); } identitys.AddRange(autoIncrements); } else if (collection != null) { q1 = collection; } } else { // 再查第二个类型集合 List <int> autoIncrements = null; if (deserializer2 == null) { deserializer2 = new TypeDeserializer(reader, defines.Count > 1 ? defines[1] : null); } var collection = deserializer2.Deserialize <T2>(out autoIncrements); if (autoIncrements != null) { if (identitys == null) { identitys = new List <int>(); } identitys.AddRange(autoIncrements); } else if (collection != null) { if (q2 == null) { q2 = collection; } } } }while (reader.NextResult()); // 释放当前的reader if (reader != null) { reader.Dispose(); } return(null); }; try { this.DoExecute <object>(sqlList, doExecute); result1 = q1 ?? new List <T1>(0); result2 = q2 ?? new List <T2>(0); return(identitys); } finally { if (reader != null) { reader.Dispose(); } } }