using System; using System.Data; using System.Collections; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; namespace BCG { [Serializable] public class MutableBlueRow : BlueRow { public MutableBlueRow (IBlueTableSchema schema) : this (schema, MutableBlueRow.myCreateArray (schema.columnNames.Length, DBNull.Value)) {} public MutableBlueRow (IBlueTableSchema schema, params object[] values) : base (schema, MutableBlueRow.myCloneArray (schema,values)) {} public MutableBlueRow (string tableName, IDictionary row) : base (tableName, row) {} public MutableBlueRow (IBlueTableSchema schema, IDictionary row) : base (schema, row) { } public MutableBlueRow (IBlueRow r) : base (r) {} public void setValue (string columnName, object newValue) { setValue (mySchema[columnName].colNo, newValue); } public void setValue (int colNo, object newValue) { myValues[colNo] = newValue; } #nullable enable public IDictionary AsDictionary() => new DictionaryWrapper(this); #nullable disable private static object[] myCloneArray (IBlueTableSchema schema,object[] vals) { object[] retVal = new object[schema.columnNames.Length]; for (int i =0; i < vals.Length; i++) retVal[i] = vals[i]; return retVal; } private static object[] myCreateArray (int sz, object o) { object[] rv = new object[sz]; for (int i = 0; i < sz; i++) rv[i] = DBNull.Value; return rv; } class DictionaryWrapper : IDictionary { readonly MutableBlueRow source; public DictionaryWrapper(MutableBlueRow source) => this.source = source; public ICollection Keys => source.schema.columnNames; public ICollection Values => source.myValues; public int Count => source.Count; public bool IsReadOnly => false; public object this[string key] { get => source[key]; set => source.setValue(key, value); } public void Add(string key, object value) => source.setValue(key, value); public void Add(KeyValuePair item) => source.setValue(item.Key, item.Value); public void Clear() => source.Clear(); public bool Contains(KeyValuePair item) => source.Contains(item.Key) && Equals(source[item.Key], item.Value); public bool ContainsKey(string key) => source.Contains(key); public void CopyTo(KeyValuePair[] array, int arrayIndex) { for (int i = 0; i < source.Count; i++) { array[arrayIndex++] = new(source.schema.columnNames[i], source.myValues[i]); } } public IEnumerator> GetEnumerator() { throw new NotImplementedException(); } public bool Remove(string key) { throw new NotImplementedException(); } public bool Remove(KeyValuePair item) { throw new NotImplementedException(); } public bool TryGetValue(string key, [MaybeNullWhen(false)] out object value) { if (source.Contains(key)) { value = source[key]; return true; } value = default; return false; } IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); } } }