using System;
using System.Collections;
using System.Data.OleDb;
using System.Text;
using Medivators.Common;

namespace Medivators.Data
{
	public class UntypedDataMapper
	{
		//	mapper converts an oledbdatareader into an arraylist 
		//	of dictionaryentry arrays. The dictionaryentry arrays
		//	have one dictionaryentry per field. The arraylist
		//	has one element per datareader.row

		private ArrayList _list = null;
		private bool _consumeErrors = false;

		public UntypedDataMapper(bool ConsumeErrors)
		{
			_consumeErrors = ConsumeErrors;
		}

		public ArrayList Data
		{
			get { return _list; }
		}
		public OleDbDataReader Reader
		{
			set
			{
				//	walk through the reader, casting 
				//	everything by type into the arraylist

				if (_list == null)
					_list = new ArrayList();

				try
				{
					while (value.Read())
					{
						DictionaryEntry[] fields = new DictionaryEntry[value.FieldCount];
						for (int fld = 0; fld < value.FieldCount; fld++)
						{
							DictionaryEntry field = new DictionaryEntry();
							field.Key = value.GetName(fld);
							field.Value = value.GetValue(fld);
							fields[fld] = field;
						}
						_list.Add(fields);
					}
				}
				catch (Exception ex)
				{
					if (_consumeErrors)
						ErrorHandler.GetInstance().LogException("UntypedDataMapper.Reader error: ", ex);
					else
						throw;
				}
			}
		}
		public void Dispose()
		{
			if (_list != null)
				_list.Clear();
		}
	}
}