using System; using System.Collections; using System.Collections.Generic; using System.Data; using System.Reflection; using System.Linq; //using BCG.Utilities; using System.Text; namespace BCG { /// /// BlueRow is an analog of DataRow, an immutable one though. It constructs itself either /// from dotNet DataRow or IBlueRow. One row can belong to any number of tables, 0 or 1 or many. /// All what the BlueRow requires is a schema to manage its internal information /// [Serializable] public class BlueRow : IBlueRow { protected Object[] myValues; protected IBlueTableSchema mySchema; #region Private helper methods /// /// Initializes the internal data structures /// /// private void myInit(IBlueTableSchema schema) { mySchema = schema; myValues = new Object[schema.columnNames.Length]; } #endregion #region Constructors protected BlueRow() { } public BlueRow(IBlueRow r) { myInit(r.schema); foreach (ColumnInfo ci in mySchema.columnInfo) myValues[ci.colNo] = r[ci.colNo]; } /// /// The BlueRow initializes itself from the row passed. The row is expected /// to have the same schema (including the col ordering) as the row being created /// /// /// public BlueRow(IBlueTableSchema schema, IBlueRow row) { myInit(schema); foreach (ColumnInfo ci in mySchema.columnInfo) myValues[ci.colNo] = row[ci.colNo]; } /// /// Constructs itself from the schema and the array of values that correspond /// to the rows /// /// /// Schema of the row /// /// /// Values represent the values in the row, the order should be the same as /// represented in the schema /// public BlueRow(IBlueTableSchema schema, params Object[] values) { myValues = values; mySchema = schema; } /// /// Initilizes itself from a dotNet DataRow. The row is expected to /// have the same schema (including the column ordering) as the row being created /// /// /// public BlueRow(IBlueTableSchema schema, DataRow row) { myInit(schema); foreach (DataColumn col in row.Table.Columns) myValues[col.Ordinal] = row[col.Ordinal]; } public BlueRow(IBlueTableSchema schema, IDictionary row) { myInit(schema); // Use GetEnumerator instead of a foreach loop because the latter calls // '((IEnumerator)row).GetEnumerator()', which iterates over values // instead of key value pairs. var en = row.GetEnumerator(); while (en.MoveNext()) { var de = en.Entry; if (mySchema[de.Key as string] is not null) { myValues[mySchema[de.Key as string].colNo] = de.Value; } } } /// /// Constructs a new BlueRow by extracting the values fields enumerated /// in schema from the POD (Plain Old Data) object passed. Obviously /// the object's type must contain at least all the fields defined /// by the schema and the types of these fields should match their counterparts. /// See new BlueTableSchema(string tableName, Type). /// This is a static function rather than a constructor because /// the type signature of the constructor would not be unique (it would /// conflict with several of the constructurs above) /// /// The schema of the row /// The id of the row /// The Plain Old Data object containing the data static public IBlueRow fromPOD(IBlueTableSchema schema, int id, Object pod) { return myBlueRowFromPOD(myGetAccessors(schema, pod), schema, id, pod); } private static Dictionary myGetAccessors(IBlueTableSchema schema, object o) { var accessors = new Dictionary(); foreach (string fn in schema.columnNames) if (fn.ToLower() != "id") { FieldInfo fi = o.GetType().GetField(fn); if (fi != null) accessors[fn] = fi; } return accessors; } private static IBlueRow myBlueRowFromPOD(Dictionary accessors, IBlueTableSchema schema, int id, object pod) { MutableBlueRow r = new MutableBlueRow(schema); foreach (var kvp in accessors) { object v = kvp.Value.GetValue(pod); if (v == null) v = DBNull.Value; r.setValue(kvp.Key, v); } r.setValue("id", id); return r; } public delegate int IdGenerator(object pod); static public IBlueRowCollection fromPODCollection(IBlueTableSchema schema, IdGenerator g, IEnumerable pods) { Dictionary accessors = null; BlueTable result = new BlueTable(schema); foreach (Object o in pods) { if (accessors == null) accessors = myGetAccessors(schema, o); result.addRow(myBlueRowFromPOD(accessors, schema, g(o), o)); } return result; } private static void myPodFromBlueRow(Dictionary accessors, IBlueRow r, object pod) { foreach (var kvp in accessors) if (!(r[kvp.Key] is DBNull)) kvp.Value.SetValue(pod, r[kvp.Key]); } /// /// Copies a blue row to a POD (Plain Old Data) object. /// /// public static T copyToPOD(IBlueRow r) where T : new() { T pod = new T(); myPodFromBlueRow(myGetAccessors(r.schema, pod), r, pod); return pod; } public static List toPODCollection(IBlueRowCollection tb) where T : new() { List result = new List(); Dictionary accessors = null; foreach (IBlueRow r in tb) { T pod = new T(); if (accessors == null) accessors = myGetAccessors(tb.schema, pod); myPodFromBlueRow(accessors, r, pod); result.Add(pod); } return result; } // For viewing BlueRow in the debugger more easily public override string ToString() { return this.ToString(true); } public string ToString(bool names) { StringBuilder sb = new StringBuilder(); bool first = true; for (int x = 0; x < this.myValues.Length; x++) { if (!first) sb.Append(", "); first = false; if (names) { sb.Append(this.mySchema.columnNames[x]); sb.Append("="); } sb.Append((this.myValues[x] ?? "(null)").ToString()); } return sb.ToString(); } /// /// Constructs the schema and then populates the values, both from the IDictionary. /// The table name (the only thing that can not be derived from the dictionary) is /// also provided /// /// /// public BlueRow(string tableName, IDictionary row) { if (row is IBlueRow) myInit(((IBlueRow)row).schema); else { ColumnInfo[] cis = new ColumnInfo[row.Count]; int i = 0; foreach (DictionaryEntry de in row) cis[i] = new ColumnInfo(de.Key as string, i++, de.Value.GetType()); myInit(new BlueTableSchema(cis, tableName)); } foreach (DictionaryEntry de in row) myValues[mySchema[de.Key as string].colNo] = de.Value; } /// /// Constructs the schema and then populates the values, both from the IDictionary. /// /// public BlueRow(IDictionary row) : this("", row) { } #endregion #region IBlueRow members public virtual IBlueTableSchema schema { get { return mySchema; } } public virtual object this[string colName] { get { ColumnInfo ci = mySchema[colName]; if (ci != null) return myValues[ci.colNo]; else throw new Exception(colName + " is not a valid column name for " + schema.tableName); } } public virtual object this[int index] { get { return myValues[index]; } } #endregion #region ICollection members public virtual int Count { get { return myValues.Length; } } public virtual bool IsSynchronized { get { return false; } } public virtual object SyncRoot { get { return this; } } public virtual void CopyTo(Array array, int index) { myValues.CopyTo(array, index); } #endregion #region IDictionary members public virtual bool IsFixedSize { get { return true; } } virtual public bool IsReadOnly { get { return true; } } /// /// The key can either be the string corrsponding to the column name or /// the order of the column /// public virtual object this[object key] { get { if (key is string) return this[(string)key]; else if (key is int) return this[(int)key]; throw new BlueDataException("Wrong type while indexing the row"); } set { throw new NotSupportedException("The IBlueRow is readonly"); } } public virtual ICollection Keys { get { return mySchema.columnNames; } } public virtual ICollection Values { get { return myValues; } } public virtual void Add(object key, object value) { throw new NotSupportedException("The IBlueRow is readonly"); } public virtual void Clear() { throw new NotSupportedException("The IBlueRow is readonly"); } public virtual bool Contains(object key) { return mySchema[(string)key] != null; } /// /// Note, the default enumerator is the IDictionary's enumerator, so if user needs to /// iterate through the values, typecast this into an IEnumerable /// /// public IDictionaryEnumerator GetEnumerator() { return new BlueRowDictionaryEnumerator(this); } IEnumerator IEnumerable.GetEnumerator() { return new BlueRowEnumerator(this); } public virtual void Remove(object key) { throw new NotSupportedException("The IBlueRow is readonly"); } #endregion } }