using System; using System.Data; using System.Collections; using System.IO; using System.Reflection; using System.Collections.Generic; using System.Linq; using BCG.Utilities.General; namespace BCG { /// /// The BlueTableSchema is an immutable schema holder. Once created it cannot be modified. It can be created /// either from a DataTable or from an Array of ColumnInfo. The methods through which the user can access it /// are the indexers (both order and name based), and the property columnInfo and columnNames, that return /// the respective data, sorted according to the ColumnOrder. /// [Serializable] public class BlueTableSchema : IBlueTableSchema { public class BlueTableSchemaSerializationHelper { // public static object parse(Type t, string data) { // if (t == BlueDataValidTypes.BOOL) // return Boolean.Parse(data); // else if (t == BlueDataValidTypes.BYTE) // return Byte.Parse(data); // else if (t == BlueDataValidTypes.CHAR) // return char.Parse(data); // else if (t == BlueDataValidTypes.DATETIME) // return DateTime.Parse(data); // else if (t == BlueDataValidTypes.DOUBLE) // return Double.Parse(data); // else if (t == BlueDataValidTypes.DECIMAL) // return Decimal.Parse(data); // else if (t == BlueDataValidTypes.INT) // return int.Parse(data); // else if (t == BlueDataValidTypes.LONG) // return long.Parse(data); // else if (t == BlueDataValidTypes.SBYTE) // return SByte.Parse(data); // else if (t == BlueDataValidTypes.STRING) // return data; // else if (t == BlueDataValidTypes.UINT) // return uint.Parse(data); // else if (t == BlueDataValidTypes.ULONG) // return ulong.Parse(data); // else if (t == BlueDataValidTypes.SINGLE) // return Single.Parse(data); // else // throw new ApplicationException("Can not be parsed"); // } // public static void serialize(BinaryWriter bw, Type t, object o) { // if (t == BlueDataValidTypes.BOOL) // bw.Write((bool)o); // else if (t == BlueDataValidTypes.BYTE) // bw.Write((byte)o); // else if (t == BlueDataValidTypes.CHAR) // bw.Write((char)o); // else if (t == BlueDataValidTypes.DATETIME) // bw.Write(((DateTime)o).Ticks); // else if (t == BlueDataValidTypes.DOUBLE) // bw.Write((double)o); // else if (t == BlueDataValidTypes.DECIMAL) // bw.Write((decimal)o); // else if (t == BlueDataValidTypes.INT) // bw.Write((int)o); // else if (t == BlueDataValidTypes.LONG) // bw.Write((long)o); // else if (t == BlueDataValidTypes.SBYTE) // bw.Write((sbyte)o); // else if (t == BlueDataValidTypes.STRING) // bw.Write(o as string); // else if (t == BlueDataValidTypes.SINGLE) // bw.Write((Single)o); // else if (t == BlueDataValidTypes.BYTEARRAY) { // byte[] bs = o as byte[]; // bw.Write(bs.Length); // bw.Write(bs); // } else if (t == BlueDataValidTypes.UINT) // bw.Write((uint)o); // else if (t == BlueDataValidTypes.ULONG) // bw.Write((ulong)o); // else // throw new ApplicationException("Unsupported type"); // } // public static object deserialize(BinaryReader br, Type t) { // if (t == BlueDataValidTypes.BOOL) // return br.ReadBoolean(); // else if (t == BlueDataValidTypes.BYTE) // return br.ReadByte(); // else if (t == BlueDataValidTypes.CHAR) // return br.ReadChar(); // else if (t == BlueDataValidTypes.DATETIME) // return new DateTime(br.ReadInt64()); // else if (t == BlueDataValidTypes.DOUBLE) // return br.ReadDouble(); // else if (t == BlueDataValidTypes.DECIMAL) // return br.ReadDecimal(); // else if (t == BlueDataValidTypes.INT) // return br.ReadInt32(); // else if (t == BlueDataValidTypes.LONG) // return br.ReadInt64(); // else if (t == BlueDataValidTypes.SBYTE) // return br.ReadSByte(); // else if (t == BlueDataValidTypes.STRING) // return br.ReadString(); // else if (t == BlueDataValidTypes.BYTEARRAY) // return br.ReadBytes(br.ReadInt32()); // else if (t == BlueDataValidTypes.UINT) // return br.ReadUInt32(); // else if (t == BlueDataValidTypes.ULONG) // return br.ReadUInt64(); // else if (t == BlueDataValidTypes.SINGLE) // return br.ReadSingle(); // else // throw new ApplicationException("Unsupported type"); // } } protected Hashtable myIndexByColName = new Hashtable(); protected ColumnInfo[] myIndexByColOrder; private string[] myTableColNames; protected string myTableName; public BlueTableSchema() { } public BlueTableSchema(DataTable table) { myIndexByColOrder = new ColumnInfo[table.Columns.Count]; myTableName = table.TableName; foreach (DataColumn col in table.Columns) { ColumnInfo ci = new ColumnInfo(col.ColumnName, col.Ordinal, col.DataType); myIndexByColName[ci.colName] = ci; myIndexByColOrder[ci.colNo] = ci; } } public BlueTableSchema(ColumnInfo[] cis, string tableName) { myConstruct(cis, tableName); } private void myConstruct(ColumnInfo[] cis, string tableName) { myIndexByColOrder = new ColumnInfo[cis.Length]; myTableName = tableName; foreach (ColumnInfo ci in cis) { myIndexByColOrder[ci.colNo] = ci; myIndexByColName[ci.colName] = ci; } } public BlueTableSchema(string tableName, params ColumnInfo[] cis) : this(cis, tableName) { } /// /// Creates a BlueTableSchema that exactly mirrors the public fields /// present in fromType except that an id field will be added regardless /// of it being present in fromType or not. /// /// the table name /// the plain old data type whose fields will be mirrored in the schema public BlueTableSchema(string tableName, Type fromType) { List cis = new List(); int i = 0; // we sort the field name so that we have a deterministic order given // the same type. bool foundId = false; foreach (FieldInfo fi in fromType.GetFields().OrderBy((f) => { return f.Name; })) { if (fi.Name.ToLower().Equals("id")) foundId = true; cis.Add(new ColumnInfo(fi.Name, i++, fi.FieldType)); } if (!foundId) cis.Add(new ColumnInfo("id", cis.Count, typeof(Int32))); myConstruct(cis.ToArray(), tableName); } public ColumnInfo this[string colName] { get { object retVal = myIndexByColName[colName]; return retVal != null ? retVal as ColumnInfo : null; } } public ColumnInfo this[int colNo] { get { return myIndexByColOrder[colNo]; } } /// /// The returned Array is sorted according to the Column No /// public string[] columnNames { get { if (myTableColNames == null) { myTableColNames = new string[myIndexByColName.Count]; foreach (DictionaryEntry de in myIndexByColName) { ColumnInfo temp = (ColumnInfo)de.Value; myTableColNames[temp.colNo] = temp.colName; } } return myTableColNames; } } /// /// The returned Array is sorted according to the Column No /// public ColumnInfo[] columnInfo { get { return myIndexByColOrder; } } public string tableName { get { return myTableName; } } #region ISerializedType methods public void serialize(BinaryWriter bw) { bw.Write(columnNames.Length); bw.Write(tableName); foreach (ColumnInfo ci in columnInfo) { bw.Write(ci.colName); bw.Write(ci.colType.ToString()); { bw.Write(false); } } } public void deserialze(BinaryReader br) { ColumnInfo[] cis = new ColumnInfo[br.ReadInt32()]; string name = br.ReadString(); for (int i = 0; i < cis.Length; i++) { string colName = br.ReadString(); Type t = TypeInstanceLoader.loadType(br.ReadString(), true); cis[i] = new ColumnInfo(colName, i, t); } myConstruct(cis, name); } public int getSerializedSize() { throw new NotImplementedException("Feature for provided constant values to" + "non-primitive types in schema, not provided as of now"); } #endregion } }