prog

Xgamerz

May 2nd, 2010
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 15.28 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3. using System.Data.Common;
  4. using System.ComponentModel;
  5. using System.Collections.Generic;
  6.  
  7. namespace System.Data.CommonSql
  8. {
  9.     [Flags]
  10.     public enum RowState
  11.     {
  12.         New = 1,
  13.         Loading = 2,
  14.         Attached = 4,
  15.         Modified = 8,
  16.         Deleted = 16
  17.     }
  18.  
  19.     public class Row : IAction, ICloneable, ICustomTypeDescriptor, INotifyPropertyChanging, INotifyPropertyChanged
  20.     {
  21.         #region INotifyPropertyChanging Members
  22.  
  23.         public event PropertyChangingEventHandler PropertyChanging;
  24.  
  25.         #endregion
  26.  
  27.         #region INotifyPropertyChanged Members
  28.  
  29.         public event PropertyChangedEventHandler PropertyChanged;
  30.  
  31.         #endregion
  32.  
  33.         private string tableName;
  34.         public string TableName
  35.         {
  36.             get { return tableName; }
  37.             set { tableName = value; }
  38.         }
  39.  
  40.         private ColumnCollection columns;
  41.         internal ColumnCollection Columns
  42.         {
  43.             get { return columns; }
  44.             set { columns = value; }
  45.         }
  46.  
  47.         private RowState state = RowState.New;
  48.         public RowState State
  49.         {
  50.             get { return state; }
  51.             internal protected set { state = value; }
  52.         }
  53.  
  54.         private ColumnCollection columnsChanged = new ColumnCollection();
  55.         public ColumnCollection ColumnsChanged
  56.         {
  57.             get { return columnsChanged; }
  58.             private set { columnsChanged = value; }
  59.         }
  60.  
  61.         private object[] itemArray;
  62.         internal object[] ItemArray
  63.         {
  64.             get { return itemArray; }
  65.             set
  66.             {
  67.                 //if (value == null)
  68.                 //{
  69.                 //    throw new ArgumentNullException("itemArray");
  70.                 //}
  71.                 //else if (value.Length != Columns.Count)
  72.                 //{
  73.                 //    //temp
  74.                 //    throw new Exception("Array size not confirm the size of columns");
  75.                 //}
  76.                 //for (int i = 0; i < value.Length; i++)
  77.                 //{
  78.                 //    if (value[i] == null && !Columns[i].AllowDBNull)
  79.                 //    {
  80.                 //        throw new NoNullAllowedException("Column " + Columns[i].Name);
  81.                 //    }
  82.                 //}
  83.                 itemArray = value;
  84.                 //originalValues = new object[value.Length];
  85.             }
  86.         }
  87.  
  88.         public object this[int index]
  89.         {
  90.             get
  91.             {
  92.                 Column col = Columns[index];
  93.                 if ((State & RowState.Loading) != RowState.Loading)
  94.                 {
  95.                     if ((col.Permissions & Permission.Select) != Permission.Select)
  96.                     {
  97.                         throw new Exception("Permission not enough to read value from " + col.Name);
  98.                     }
  99.                 }
  100.                 //if (index >= Columns.Count || index < 0)
  101.                 //{
  102.                 //    throw new IndexOutOfRangeException();
  103.                 //}
  104.                 return ItemArray[index];
  105.             }
  106.             set
  107.             {
  108.                 Column col = Columns[index];
  109.                 if (index >= Columns.Count || index < 0)
  110.                 {
  111.                     //31032010
  112.                     throw new IndexOutOfRangeException();
  113.                 }
  114.                 if (!col.AllowDBNull && value == null
  115.                     && (State & RowState.New) != RowState.New
  116.                     && (State & RowState.Loading) != RowState.Loading)
  117.                 {
  118.                     //31032010
  119.                     throw new NoNullAllowedException("Column " + Columns[index].Name);
  120.                 }
  121.                 if ((col.Permissions & Permission.Update) != Permission.Update)
  122.                 {
  123.                     if ((State & RowState.Loading) == RowState.Loading)
  124.                     {
  125.                         //if data numeric ?
  126.                         return;
  127.                     }
  128.                     else
  129.                     {
  130.                         //31032010
  131.                         throw new Exception("Permission not enough to change value of " + col.Name);
  132.                     }
  133.                 }
  134.                 else if (col.PrimaryKey
  135.                     && (State & RowState.New) != RowState.New
  136.                     && (State & RowState.Loading) != RowState.Loading)
  137.                 {
  138.                     //ExceptionPrimaryKey
  139.                     //31032010
  140.                     throw new ReadOnlyException("Column " + Columns[index].Name);
  141.                 }
  142.                 //temp if default value null
  143.                 bool isChanged = false;
  144.                 value = Tools.ConvertTo(col.DbType, (!col.AllowDBNull && value == null) ? col.DefaultValue : value);
  145.  
  146.                 if ((State & RowState.New) != RowState.New
  147.                     && (State & RowState.Loading) != RowState.Loading)
  148.                 {
  149.                     //object oldValue = this[index];
  150.                     //object newValue = (value  == null) ? value : Tools.ConvertTo(col.DbType, value);
  151.                     if ((this[index] == null) ? ((value == null) ? false : true) : !this[index].Equals(value))
  152.                     {
  153.                         isChanged = true;
  154.                         OnPropertyChanging(new PropertyChangingEventArgs(col.Name));
  155.                     }
  156.                 }
  157.                 //if (isChanged)
  158.                 //{
  159.                     ItemArray[index] = value;
  160.                 //}
  161.                 if ((State & RowState.New) != RowState.New
  162.                     && (State & RowState.Loading) != RowState.Loading && isChanged)
  163.                 {
  164.                     if (!ColumnsChanged.Contains(col))
  165.                     {
  166.                         ColumnsChanged.Add(col);
  167.                     }
  168.                     State |= RowState.Modified;
  169.                     OnPropertyChanged(new PropertyChangedEventArgs(col.Name));
  170.                 }
  171.             }
  172.         }
  173.  
  174.         public object this[Column column]
  175.         {
  176.             get
  177.             {
  178.                 if (column == null)
  179.                 {
  180.                     throw new ArgumentNullException("column");
  181.                 }
  182.                 return this[Columns.IndexOf(column)];
  183.             }
  184.             set
  185.             {
  186.                 if (column == null)
  187.                 {
  188.                     throw new ArgumentNullException("column");
  189.                 }
  190.                 this[Columns.IndexOf(column)] = value;
  191.             }
  192.         }
  193.  
  194.         public object this[string name]
  195.         {
  196.             get
  197.             {
  198.                 if (string.IsNullOrEmpty(name))
  199.                 {
  200.                     throw new ArgumentNullException("name");
  201.                 }
  202.                 return this[Columns[name]];
  203.             }
  204.             set
  205.             {
  206.                 if (string.IsNullOrEmpty(name))
  207.                 {
  208.                     throw new ArgumentNullException("name");
  209.                 }
  210.                 this[Columns[name]] = value;
  211.             }
  212.         }        
  213.  
  214.         public Row()
  215.         {
  216.             //this.Where.Source = this;
  217.         }
  218.  
  219.         protected virtual void OnPropertyChanging(PropertyChangingEventArgs e)
  220.         {
  221.             if (PropertyChanging != null)
  222.             {
  223.                 PropertyChanging(this, e);
  224.             }
  225.         }
  226.  
  227.         protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
  228.         {
  229.             if (PropertyChanged != null)
  230.             {
  231.                 PropertyChanged(this, e);
  232.             }
  233.         }
  234.  
  235.         public bool IsNull(int columnIndex)
  236.         {
  237.             return (this.itemArray[columnIndex] == null) ? true : false;
  238.         }
  239.  
  240.         public bool IsNull(Column column)
  241.         {
  242.             return IsNull(this.Columns.IndexOf(column));
  243.         }
  244.        
  245.         public bool IsNull(string columnName)
  246.         {
  247.             return IsNull(this.Columns[columnName]);
  248.         }
  249.  
  250.         internal void Mapping(DataRow dataRow)
  251.         {
  252.             if (dataRow == null)
  253.             {
  254.                 throw new ArgumentNullException("dataRow");
  255.             }
  256.             try
  257.             {
  258.                 state ^= RowState.New;
  259.                 State |= RowState.Loading;
  260.                 for (int i = 0; i < Columns.Count; i++)
  261.                 {
  262.                     if (dataRow.IsNull(i))
  263.                     {
  264.                         //originalValues[i] = this[i] = Columns[i].DefaultValue;
  265.                         this[i] = Columns[i].DefaultValue;
  266.                     }
  267.                     else
  268.                     {
  269.                         //originalValues[i] = this[i] = dataRow[Columns[i].Name];
  270.                         this[i] = dataRow[Columns[i].Name];
  271.                     }
  272.                 }
  273.             }
  274.             finally
  275.             {
  276.                 state ^= RowState.Loading;
  277.                 State |= RowState.Attached;
  278.             }
  279.         }
  280.  
  281.         internal void Mapping(DbDataRecord dataRecord)
  282.         {
  283.             if (dataRecord == null)
  284.             {
  285.                 throw new ArgumentNullException("dataRecord");
  286.             }
  287.             try
  288.             {
  289.                 state ^= RowState.New;
  290.                 State |= RowState.Loading;
  291.                 for (int i = 0; i < Columns.Count; i++)
  292.                 {
  293.                     if (dataRecord.IsDBNull(i))
  294.                     {
  295.                         //originalValues[i] = this[i] = Columns[i].DefaultValue;
  296.                         this[i] = Columns[i].DefaultValue;
  297.                     }
  298.                     else
  299.                     {
  300.                         //originalValues[i] = this[i] = dataRecord[Columns[i].Name];
  301.                         this[i] = dataRecord[Columns[i].Name];
  302.                     }
  303.                 }
  304.             }
  305.             finally
  306.             {
  307.                 state ^= RowState.Loading;
  308.                 State |= RowState.Attached;
  309.             }
  310.         }
  311.  
  312.         #region ICommon Members
  313.  
  314.         public virtual void Clear()
  315.         {
  316.             try
  317.             {
  318.                 State |= RowState.Loading;
  319.                 for (int i = 0, length = Columns.Count; i < length; i++)
  320.                 {
  321.                     //originalValues[i] = ItemArray[i] = Columns[i].DefaultValue;
  322.                     ItemArray[i] = Columns[i].DefaultValue;
  323.                     //ItemArray[i] = null;
  324.                 }
  325.             }
  326.             finally
  327.             {
  328.                 State &= RowState.Loading;
  329.             }
  330.         }
  331.  
  332.         public virtual void Refresh()
  333.         {
  334.             throw new NotImplementedException();
  335.         }
  336.  
  337.         #endregion
  338.  
  339.         #region ICloneable Members
  340.  
  341.         public virtual object Clone()
  342.         {
  343.             Row row = this.MemberwiseClone() as Row;
  344.             row.State = RowState.New | RowState.Attached;
  345.             row.columnsChanged = new ColumnCollection();
  346.             row.itemArray = new object[itemArray.Length];
  347.             itemArray.CopyTo(row.itemArray, 0);
  348.             return row;
  349.         }
  350.  
  351.         #endregion
  352.  
  353.         #region ICustomTypeDescriptor Members
  354.  
  355.         AttributeCollection ICustomTypeDescriptor.GetAttributes()
  356.         {
  357.             return TypeDescriptor.GetAttributes(this, true);
  358.         }
  359.  
  360.         string ICustomTypeDescriptor.GetClassName()
  361.         {
  362.             return TypeDescriptor.GetClassName(this, true);
  363.         }
  364.  
  365.         string ICustomTypeDescriptor.GetComponentName()
  366.         {
  367.             return TypeDescriptor.GetComponentName(this, true);
  368.         }
  369.  
  370.         TypeConverter ICustomTypeDescriptor.GetConverter()
  371.         {
  372.             return TypeDescriptor.GetConverter(this, true);
  373.         }
  374.  
  375.         EventDescriptor ICustomTypeDescriptor.GetDefaultEvent()
  376.         {
  377.             return TypeDescriptor.GetDefaultEvent(this, true);
  378.         }
  379.  
  380.         PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty()
  381.         {
  382.             return TypeDescriptor.GetDefaultProperty(this, true);
  383.         }
  384.  
  385.         object ICustomTypeDescriptor.GetEditor(Type editorBaseType)
  386.         {
  387.             return TypeDescriptor.GetEditor(this, editorBaseType, true);
  388.         }
  389.  
  390.         EventDescriptorCollection ICustomTypeDescriptor.GetEvents(Attribute[] attributes)
  391.         {
  392.             return TypeDescriptor.GetEvents(attributes, true);
  393.         }
  394.  
  395.         EventDescriptorCollection ICustomTypeDescriptor.GetEvents()
  396.         {
  397.             return ((ICustomTypeDescriptor)this).GetEvents(null);
  398.         }
  399.  
  400.         PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] attributes)
  401.         {
  402.             List<PropertyDescriptor> props = new List<PropertyDescriptor>();
  403.             for (int i = 0; i < Columns.Count; i++)
  404.             {
  405.                 Column col = Columns[i];
  406.                 if (col.Visible && (col.Permissions & Permission.Select) == Permission.Select)
  407.                 {
  408.                     props.Add(new ColumnPropertyDescriptor(col/*, this*/, col.PropertyName, attributes));
  409.                 }
  410.             }
  411.  
  412.             //PropertyDescriptorCollection propsColl = TypeDescriptor.GetProperties(this, attributes, true);
  413.             //List<PropertyDescriptor> props = new List<PropertyDescriptor>();
  414.             //foreach (PropertyDescriptor prop in propsColl)
  415.             //{
  416.             //    props.Add(prop);
  417.             //}
  418.  
  419.             return new PropertyDescriptorCollection(props.ToArray());
  420.             //return TypeDescriptor.GetProperties(this, attributes, true);
  421.         }
  422.  
  423.         PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties()
  424.         {
  425.             return ((ICustomTypeDescriptor)this).GetProperties(null);
  426.         }
  427.  
  428.         object ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor pd)
  429.         {
  430.             return this;
  431.         }
  432.  
  433.         #endregion
  434.  
  435.         internal class ColumnPropertyDescriptor : PropertyDescriptor
  436.         {
  437.             private Column column;
  438.  
  439.             public ColumnPropertyDescriptor(Column col, string name, Attribute[] attrs)
  440.                 : base(name, attrs)
  441.             {
  442.                 this.column = col;
  443.             }
  444.  
  445.             public override bool CanResetValue(object component)
  446.             {
  447.                 return false; //!column.PrimaryKey;
  448.             }
  449.  
  450.             public override Type ComponentType
  451.             {
  452.                 get { return typeof(Row); }
  453.             }
  454.  
  455.             public override object GetValue(object component)
  456.             {
  457.                 return ((Row)component)[column];
  458.             }
  459.  
  460.             public override bool IsReadOnly
  461.             {
  462.                 get { return column.ReadOnly; }
  463.             }
  464.  
  465.             public override Type PropertyType
  466.             {
  467.                 get { return Tools.DbTypeToType(column.DbType); }
  468.             }
  469.  
  470.             public override void ResetValue(object component)
  471.             {
  472.                 //((Row)component)[column] = column.DefaultValue;
  473.             }
  474.  
  475.             public override void SetValue(object component, object value)
  476.             {
  477.                 ((Row)component)[column] = value;
  478.             }
  479.  
  480.             public override bool ShouldSerializeValue(object component)
  481.             {
  482.                 return false;
  483.             }
  484.         }
  485.     }
  486. }
Add Comment
Please, Sign In to add comment