public IEnumerable <TResult> ExecuteSqlString <TResult>(string commandText, IRowMapper <TResult> rowMapper, IDictionary <string, object> dbParams) { rowMapper.TablesInfo = this.DatabaseInfo.TablesInfo; DbCommand command = this.CreateCommand(commandText); if (dbParams != null && dbParams.Count != 0) { this.databaseInfo.DbTypeConverter.AddParam(command, dbParams); Logger.Log(Level.Debug, this.sqlLog.GetLogSql(commandText, dbParams)); } else { Logger.Log(Level.Debug, commandText); } List <TResult> result = new List <TResult>(); if (Transaction.Current != null) { command.Connection = Transaction.Current.DbTransactionWrapper.DbTransaction.Connection; command.Transaction = Transaction.Current.DbTransactionWrapper.DbTransaction; using (var reader = command.ExecuteReader() as MySqlDataReader) { if (reader.HasRows) { var record = reader.Cast <IDataRecord>(); foreach (var r in record) { var t = rowMapper.MapRow(r); result.Add(t); } } } } else { using (var conn = OpenConnection()) { command.Connection = conn; using (var reader = command.ExecuteReader() as MySqlDataReader) { //Logger.Log(Level.Debug, reader.RowSize.ToString()); //Logger.Log(Level.Debug, reader.FetchSize.ToString()); if (reader.HasRows) { var record = reader.Cast <IDataRecord>(); foreach (var r in record) { var t = rowMapper.MapRow(r); result.Add(t); } } } } } return(result); }
public async Task <IEnumerable <T> > ReadAsync <T>(IRowMapper <T> mapper, string commandText, CommandType commandType, DbParameter[] parameters) { var results = new List <T>(); using (var connection = SqlConnectionInfo.GetConnection()) { using (var command = SqlConnectionInfo.CreateDbCommand(connection)) { command.CommandText = commandText; command.CommandType = commandType; // Add parameters if (parameters != null) { command.Parameters.AddRange(parameters.ToArray()); } using (IDataReader reader = await command.ExecuteReaderAsync()) { while (reader.Read()) { results.Add(mapper.MapRow(reader)); } } command.Parameters.Clear(); } } return(results); }
public static BoolResult <TEntity> SPToSingle <TEntity>(this DataBase db, string spName, IRowMapper <IDataReader, TEntity> rowMapper, params IDataParameter[] parameters) where TEntity : class, new() { Guard.IsNotNull(rowMapper, "IRowMapper is null"); ValidationResults validationResults = new ValidationResults(); TEntity item = default(TEntity); IDataReader dataReader = null; try { dataReader = db.ExecuteSPReader(db.ConnectionString, spName, parameters); if (dataReader.Read()) { item = rowMapper.MapRow(dataReader, 0); } } catch (Exception ex) { validationResults.Add(ex.Message); } finally { if (dataReader != null) { dataReader.Close(); } } return(new BoolResult <TEntity>(item, validationResults.IsValid, string.Empty, validationResults)); }
/// <summary> /// this implementation should be encapsulated in some CustomSprocAccessor<TR1, TR2> but at this time i have no enough time to do /// this method executes multiple result sets /// </summary> /// <typeparam name="TResult1"></typeparam> /// <typeparam name="TResult2"></typeparam> /// <param name="database"></param> /// <param name="command"></param> /// <param name="rowMapper1"></param> /// <param name="rowMapper2"></param> /// <returns></returns> public static ArrayList ExecuteCommandAccessorMultipleResultSets <TResult1, TResult2>(this Database database, DbCommand command, IRowMapper <TResult1> rowMapper1, IRowMapper <TResult2> rowMapper2) { var finalResult = new ArrayList(2); var result1List = new List <TResult1>(); var result2List = new List <TResult2>(); finalResult.Add(result1List); finalResult.Add(result2List); using (var reader = database.ExecuteReader(command)) { while (reader.Read()) { result1List.Add(rowMapper1.MapRow(reader)); } if (reader.NextResult()) { while (reader.Read()) { result2List.Add(rowMapper2.MapRow(reader)); } } } return(finalResult); }
public IList <T> Read <T>(DbConnection connection, IRowMapper <T> mapper, string commandText, CommandType commandType, DbParameter[] parameters, DbTransaction transaction = null) { var records = new List <T>(); using (var command = DatabaseProvider.GetCommand()) { command.Connection = connection; command.CommandText = commandText; command.CommandType = commandType; if (transaction != null) { command.Transaction = transaction; } // Add parameters if (parameters != null) { command.Parameters.AddRange(parameters.ToArray()); } using (var reader = command.ExecuteReader()) { while (reader.Read()) { records.Add(mapper.MapRow(reader)); } } command.Parameters.Clear(); } return(records); }
public IEnumerable <T> Read <T>(IRowMapper <T> mapper, string commandText, CommandType commandType, DbParameter[] parameters) { using (var connection = SqlConnectionInfo.GetConnection()) { using (var command = SqlConnectionInfo.CreateDbCommand(connection)) { command.CommandText = commandText; command.CommandType = commandType; // Add parameters if (parameters != null) { command.Parameters.AddRange(parameters.ToArray()); } using (IDataReader reader = command.ExecuteReader()) { while (reader.Read()) { yield return(mapper.MapRow(reader)); } } command.Parameters.Clear(); } } }
public IList <T> ExtractData(IDataReader reader) { // Use the more efficient collection if we know how many rows to expect: // ArrayList in case of a known row count, LinkedList if unknown //IList<T> results = (rowsExpected > 0) ? new List<T>(rowsExpected) : new LinkedList<T>(); //how come LinkedList<T> doesn't implement IList<T> ?!?!?! //some web entries claim slow indexer... need to write our own again? return ICollection instead? //http://blogs.msdn.com/kcwalina/archive/2005/09/23/Collections.aspx //We did not implement IList<T> on LinkedList because the indexer would be //very slow. If you really need the interface, you probably can inherit from //LinkedList<T> and implement the interface on the subtype. IList <T> results = new List <T>(); int rowNum = 0; if (rowMapper != null) { while (reader.Read()) { results.Add(rowMapper.MapRow(reader, rowNum++)); } } else { while (reader.Read()) { results.Add(rowMapperDelegate(reader, rowNum++)); } } return(results); }
public virtual PageCollection <TEntity> GetPageCollectionWithCustomQuery(CustomCommand command, IRowMapper <TEntity> rowMapper) { try { SetConnectionString(); var pagedCollection = new PageCollection <TEntity>(); //this.resultSetMapper = new DefaultResultSetMapper(rowMapper); using (var reader = Session.ExecuteReader(new CommandDefinition(command.SqlCommand, command.Parameters, commandType: command.CommandType))) { pagedCollection.ResultList = new List <TEntity>(); while (reader.Read()) { pagedCollection.ResultList.Add(rowMapper.MapRow(reader)); } if (reader.NextResult() && reader.Read()) { pagedCollection.CustomValue = reader.SafeReader(reader.GetName(0)); } if (reader.NextResult() && reader.Read()) { pagedCollection.TotalRecords = reader.SafeReader(reader.GetName(0)).SafeLong(0); } if (reader.NextResult() && reader.Read()) { pagedCollection.TotalSumOne = reader.SafeReader(reader.GetName(0)).SafeDecimal(); } if (reader.NextResult() && reader.Read()) { pagedCollection.TotalSumTwo = reader.SafeReader(reader.GetName(0)).SafeDecimal(); } if (reader.NextResult() && reader.Read()) { pagedCollection.TotalSumThree = reader.SafeReader(reader.GetName(0)).SafeDecimal(); } if (reader.NextResult() && reader.Read()) { pagedCollection.TotalSumFour = reader.SafeReader(reader.GetName(0)).SafeDecimal(); } if (reader.NextResult() && reader.Read()) { pagedCollection.CurrentPageNumber = reader.SafeReader(reader.GetName(0)).SafeLong(0); } if (reader.NextResult() && reader.Read()) { pagedCollection.PageSize = reader.SafeReader(reader.GetName(0)).SafeLong(0); } } return(pagedCollection); } catch (Exception ex) { throw ex; } }
public IEnumerable <TResult> MapSet(IDataReader reader) { using (reader) { while (reader.Read()) { yield return(rowMapper.MapRow(reader)); } } }
public DataList(IRowMapper <T> mapper, Dao dao) { var reader = dao.ExecuteReader(); var index = 0; while (reader.Read()) { _list.Add(mapper.MapRow(reader, index)); index++; } dao.Close(); }
public static TResult ExecuteDbCommand <TResult>(this Database database, DbCommand command, IRowMapper <TResult> mapper) { using (IDataReader reader = database.ExecuteReader(command)) { if (reader.Read()) { return(mapper.MapRow(reader)); } } return(default(TResult)); }
public Domain.Order GetByID(int id) { // string sql = String.Format("select * from Blogs where BlogId={0}", id); string sql = String.Format("select * from Posts"); IDataReader rdr = database.ExecuteReader(CommandType.Text, sql); if (rdr.Read()) { IRowMapper <Domain.Order> mapper = MapBuilder <Domain.Order> .BuildAllProperties(); Domain.Order order = mapper.MapRow(rdr); return(order); } return(null); }
public static List <T> ToList <T>(this DataTable dt, IRowMapper <T> rowMapper) { if (dt == null || dt.Rows.Count == 0) { return(null); } List <T> entites = new List <T>(); foreach (DataRow dr in dt.Rows) { var t = rowMapper.MapRow(dr); entites.Add(t); } return(entites); }
public List <T> Query(string sql, IRowMapper <T> rowMapper, object[] parameters = null) { List <T> result = new List <T>(); MySqlConnection connection = null; MySqlCommand command = null; MySqlDataReader reader = null; try { connection = DatabaseUtils.getMySqlConnection(); connection.Open(); command = connection.CreateCommand(); command.CommandText = sql; if (parameters != null) { SetParameters(command, parameters, sql); } reader = command.ExecuteReader(); if (reader.HasRows) { while (reader.Read()) { result.Add(rowMapper.MapRow(reader)); } } } catch (Exception e) { Console.WriteLine(e.Message); return(result); } finally { if (reader != null) { reader.Close(); } if (connection != null) { connection.Close(); } } return(result); }
// RowMapper voi Interface protected List <T> FetchAll(string sql, IRowMapper <T> mapper, CommandType commandType = CommandType.Text) { using (IDbCommand command = connection.CreateCommand()) { command.CommandText = sql; command.CommandType = commandType; using (IDataReader reader = command.ExecuteReader()) { List <T> list = new List <T>(); while (reader.Read()) { list.Add(mapper.MapRow(reader)); } return(list); } } }
[Test] public void ExtractDataWithRowMapper([Values(0, 2, 10)] int n) { var expected = new List <T>(n); for (int i = 0; i < n; i++) { T value = (T)Convert.ChangeType(i, typeof(T)); Expect.Call(_dataReader.Read()).Return(true); Expect.Call(_rowMapper.MapRow(_dataReader, i)).Return(value); expected.Add(value); } Expect.Call(_dataReader.Read()).Return(false); _mockery.ReplayAll(); var testee = new ExtendedRowMapperResultSetExtractor <T>(_rowMapper); IList <T> result = testee.ExtractData(_dataReader); CollectionAssert.AreEqual(expected, result); _mockery.VerifyAll(); }
/// <summary> /// Deprecate 可以直接使用newtonsoft进行序列化 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="dt"></param> /// <param name="rowMapper"></param> /// <returns></returns> public static string ToJson <T>(this DataTable dt, IRowMapper <T> rowMapper) { if (dt == null || dt.Rows.Count == 0) { return(null); } StringBuilder strBuilder = new StringBuilder("["); foreach (DataRow dr in dt.Rows) { var t = rowMapper.MapRow(dr); strBuilder.Append(t + ","); } strBuilder.Remove(strBuilder.Length - 1, 1); strBuilder.Append("]"); return(strBuilder.ToString()); }
public object ExtractData(System.Data.IDataReader reader) { // Use the more efficient collection if we know how many rows to expect: // ArrayList in case of a known row count, LinkedList if unknown IList results = (rowsExpected > 0) ? (IList) new ArrayList(rowsExpected) : new LinkedList(); int rowNum = 0; if (rowMapper != null) { while (reader.Read()) { results.Add(rowMapper.MapRow(reader, rowNum++)); } } else { while (reader.Read()) { results.Add(rowMapperDelegate(reader, rowNum++)); } } return(results); }
///<summary> /// /// Implementations must implement this method to process all /// result set and rows in the IDataReader. /// ///</summary> /// ///<param name="reader">The IDataReader to extract data from. /// Implementations should not close this: it will be closed /// by the AdoTemplate.</param> ///<returns> ///An arbitrary result object or null if none. The /// extractor will typically be stateful in the latter case. ///</returns> /// public virtual IList <T> ExtractData(IDataReader reader) { reader = ExtendDataReader(reader); IList <T> results = new List <T>(RowsExpected); int rowNum = 0; if (_rowMapper != null) { while (reader.Read()) { results.Add(_rowMapper.MapRow(reader, rowNum++)); } } else { while (reader.Read()) { results.Add(_rowMapperDelegate(reader, rowNum++)); } } return(results); }
/// <summary> /// 通过Mapper转换对象 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="rdr"></param> /// <param name="mapper"></param> /// <returns></returns> public static T RecordTo<T>(this IDataReader rdr, IRowMapper<T> mapper) where T : class, new() { var tt = (IDataRecord)rdr; T obj = mapper.MapRow(tt); return obj; }