/// <summary> /// 执行查询,并返回单个对象 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="queryModel"></param> /// <param name="returnDefaultWhenEmpty"></param> /// <returns></returns> public T ExecuteSingle <T>(QueryModel queryModel, bool returnDefaultWhenEmpty) { MySqlQueryModelVisitor queryModelVisitor = new MySqlQueryModelVisitor(); var command = queryModelVisitor.Translate(queryModel); this.provider.ExecutedCommandBuilder = command; using (IDataReader reader = this.provider.ExecuteDataReader(command)) { bool isExists = reader.Read(); // 如果不存在查询记录 if (isExists == false) { // 如果可以默认 if (returnDefaultWhenEmpty) { return(default(T)); } else { throw new InvalidOperationException("未查询出满足条件的任何记录。"); } } var tuple = reader.GetDeserializerState <T>(); return((T)tuple.Func(reader)); } }
/// <summary> /// 执行查询,并返回第一行第一列 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="queryModel"></param> /// <returns></returns> public T ExecuteScalar <T>(QueryModel queryModel) { MySqlQueryModelVisitor queryModelVisitor = new MySqlQueryModelVisitor(); var command = queryModelVisitor.Translate(queryModel); this.provider.ExecutedCommandBuilder = command; return(this.provider.ExecuteScalar <T>(command)); }
/// <summary> /// 执行查询,并返回第一行第一列 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="queryModel"></param> /// <returns></returns> public T ExecuteScalar <T>(QueryModel queryModel) { MySqlQueryModelVisitor queryModelVisitor = new MySqlQueryModelVisitor(); var command = queryModelVisitor.Translate(queryModel); SqlCmd.Current = command; return(this.provider.ExecuteScalar <T>(command.Sql, command.Parameters)); }
/// <summary> /// 执行查询,并返回 IEnumerable<T> 对象 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="queryModel"></param> /// <returns></returns> public IEnumerable <T> ExecuteCollection <T>(QueryModel queryModel) { MySqlQueryModelVisitor queryModelVisitor = new MySqlQueryModelVisitor(); var command = queryModelVisitor.Translate(queryModel); this.provider.ExecutedCommandBuilder = command; using (IDataReader reader = this.provider.ExecuteDataReader(command)) { var tuple = reader.GetDeserializerState <T>(); while (reader.Read()) { yield return((T)tuple.Func(reader)); } } }
/// <summary> /// 执行查询,并返回 IEnumerable<T> 对象 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="queryModel"></param> /// <returns></returns> public IEnumerable <T> ExecuteCollection <T>(QueryModel queryModel) { MySqlQueryModelVisitor queryModelVisitor = new MySqlQueryModelVisitor(); var command = queryModelVisitor.Translate(queryModel); SqlCmd.Current = command; using (IDataReader reader = this.provider.ExecuteDataReader(command.Sql.ToString(), command.Parameters)) { var tuple = reader.GetDeserializerState <T>(); while (reader.Read()) { yield return((T)tuple.Func(reader)); } } }
/// <summary> /// 执行查询,并返回单个对象 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="queryModel"></param> /// <param name="returnDefaultWhenEmpty"></param> /// <returns></returns> public T ExecuteSingle <T>(QueryModel queryModel, bool returnDefaultWhenEmpty) { MySqlQueryModelVisitor queryModelVisitor = new MySqlQueryModelVisitor(); var command = queryModelVisitor.Translate(queryModel); this.provider.ExecutedCommandBuilder = command; using (IDataReader reader = this.provider.ExecuteDataReader(command)) { if (reader.Read() == false && returnDefaultWhenEmpty) { return(default(T)); } var tuple = reader.GetDeserializerState <T>(); return((T)tuple.Func(reader)); } }