Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Text;
- using System.Data.Common;
- using System.ComponentModel;
- using System.Collections.Generic;
- namespace System.Data.CommonSql
- {
- [Flags]
- public enum RowState
- {
- New = 1,
- Loading = 2,
- Attached = 4,
- Modified = 8,
- Deleted = 16
- }
- public class Row : IAction, ICloneable, ICustomTypeDescriptor, INotifyPropertyChanging, INotifyPropertyChanged
- {
- #region INotifyPropertyChanging Members
- public event PropertyChangingEventHandler PropertyChanging;
- #endregion
- #region INotifyPropertyChanged Members
- public event PropertyChangedEventHandler PropertyChanged;
- #endregion
- private string tableName;
- public string TableName
- {
- get { return tableName; }
- set { tableName = value; }
- }
- private ColumnCollection columns;
- internal ColumnCollection Columns
- {
- get { return columns; }
- set { columns = value; }
- }
- private RowState state = RowState.New;
- public RowState State
- {
- get { return state; }
- internal protected set { state = value; }
- }
- private ColumnCollection columnsChanged = new ColumnCollection();
- public ColumnCollection ColumnsChanged
- {
- get { return columnsChanged; }
- private set { columnsChanged = value; }
- }
- private object[] itemArray;
- internal object[] ItemArray
- {
- get { return itemArray; }
- set
- {
- //if (value == null)
- //{
- // throw new ArgumentNullException("itemArray");
- //}
- //else if (value.Length != Columns.Count)
- //{
- // //temp
- // throw new Exception("Array size not confirm the size of columns");
- //}
- //for (int i = 0; i < value.Length; i++)
- //{
- // if (value[i] == null && !Columns[i].AllowDBNull)
- // {
- // throw new NoNullAllowedException("Column " + Columns[i].Name);
- // }
- //}
- itemArray = value;
- //originalValues = new object[value.Length];
- }
- }
- public object this[int index]
- {
- get
- {
- Column col = Columns[index];
- if ((State & RowState.Loading) != RowState.Loading)
- {
- if ((col.Permissions & Permission.Select) != Permission.Select)
- {
- throw new Exception("Permission not enough to read value from " + col.Name);
- }
- }
- //if (index >= Columns.Count || index < 0)
- //{
- // throw new IndexOutOfRangeException();
- //}
- return ItemArray[index];
- }
- set
- {
- Column col = Columns[index];
- if (index >= Columns.Count || index < 0)
- {
- //31032010
- throw new IndexOutOfRangeException();
- }
- if (!col.AllowDBNull && value == null
- && (State & RowState.New) != RowState.New
- && (State & RowState.Loading) != RowState.Loading)
- {
- //31032010
- throw new NoNullAllowedException("Column " + Columns[index].Name);
- }
- if ((col.Permissions & Permission.Update) != Permission.Update)
- {
- if ((State & RowState.Loading) == RowState.Loading)
- {
- //if data numeric ?
- return;
- }
- else
- {
- //31032010
- throw new Exception("Permission not enough to change value of " + col.Name);
- }
- }
- else if (col.PrimaryKey
- && (State & RowState.New) != RowState.New
- && (State & RowState.Loading) != RowState.Loading)
- {
- //ExceptionPrimaryKey
- //31032010
- throw new ReadOnlyException("Column " + Columns[index].Name);
- }
- //temp if default value null
- bool isChanged = false;
- value = Tools.ConvertTo(col.DbType, (!col.AllowDBNull && value == null) ? col.DefaultValue : value);
- if ((State & RowState.New) != RowState.New
- && (State & RowState.Loading) != RowState.Loading)
- {
- //object oldValue = this[index];
- //object newValue = (value == null) ? value : Tools.ConvertTo(col.DbType, value);
- if ((this[index] == null) ? ((value == null) ? false : true) : !this[index].Equals(value))
- {
- isChanged = true;
- OnPropertyChanging(new PropertyChangingEventArgs(col.Name));
- }
- }
- //if (isChanged)
- //{
- ItemArray[index] = value;
- //}
- if ((State & RowState.New) != RowState.New
- && (State & RowState.Loading) != RowState.Loading && isChanged)
- {
- if (!ColumnsChanged.Contains(col))
- {
- ColumnsChanged.Add(col);
- }
- State |= RowState.Modified;
- OnPropertyChanged(new PropertyChangedEventArgs(col.Name));
- }
- }
- }
- public object this[Column column]
- {
- get
- {
- if (column == null)
- {
- throw new ArgumentNullException("column");
- }
- return this[Columns.IndexOf(column)];
- }
- set
- {
- if (column == null)
- {
- throw new ArgumentNullException("column");
- }
- this[Columns.IndexOf(column)] = value;
- }
- }
- public object this[string name]
- {
- get
- {
- if (string.IsNullOrEmpty(name))
- {
- throw new ArgumentNullException("name");
- }
- return this[Columns[name]];
- }
- set
- {
- if (string.IsNullOrEmpty(name))
- {
- throw new ArgumentNullException("name");
- }
- this[Columns[name]] = value;
- }
- }
- public Row()
- {
- //this.Where.Source = this;
- }
- protected virtual void OnPropertyChanging(PropertyChangingEventArgs e)
- {
- if (PropertyChanging != null)
- {
- PropertyChanging(this, e);
- }
- }
- protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
- {
- if (PropertyChanged != null)
- {
- PropertyChanged(this, e);
- }
- }
- public bool IsNull(int columnIndex)
- {
- return (this.itemArray[columnIndex] == null) ? true : false;
- }
- public bool IsNull(Column column)
- {
- return IsNull(this.Columns.IndexOf(column));
- }
- public bool IsNull(string columnName)
- {
- return IsNull(this.Columns[columnName]);
- }
- internal void Mapping(DataRow dataRow)
- {
- if (dataRow == null)
- {
- throw new ArgumentNullException("dataRow");
- }
- try
- {
- state ^= RowState.New;
- State |= RowState.Loading;
- for (int i = 0; i < Columns.Count; i++)
- {
- if (dataRow.IsNull(i))
- {
- //originalValues[i] = this[i] = Columns[i].DefaultValue;
- this[i] = Columns[i].DefaultValue;
- }
- else
- {
- //originalValues[i] = this[i] = dataRow[Columns[i].Name];
- this[i] = dataRow[Columns[i].Name];
- }
- }
- }
- finally
- {
- state ^= RowState.Loading;
- State |= RowState.Attached;
- }
- }
- internal void Mapping(DbDataRecord dataRecord)
- {
- if (dataRecord == null)
- {
- throw new ArgumentNullException("dataRecord");
- }
- try
- {
- state ^= RowState.New;
- State |= RowState.Loading;
- for (int i = 0; i < Columns.Count; i++)
- {
- if (dataRecord.IsDBNull(i))
- {
- //originalValues[i] = this[i] = Columns[i].DefaultValue;
- this[i] = Columns[i].DefaultValue;
- }
- else
- {
- //originalValues[i] = this[i] = dataRecord[Columns[i].Name];
- this[i] = dataRecord[Columns[i].Name];
- }
- }
- }
- finally
- {
- state ^= RowState.Loading;
- State |= RowState.Attached;
- }
- }
- #region ICommon Members
- public virtual void Clear()
- {
- try
- {
- State |= RowState.Loading;
- for (int i = 0, length = Columns.Count; i < length; i++)
- {
- //originalValues[i] = ItemArray[i] = Columns[i].DefaultValue;
- ItemArray[i] = Columns[i].DefaultValue;
- //ItemArray[i] = null;
- }
- }
- finally
- {
- State &= RowState.Loading;
- }
- }
- public virtual void Refresh()
- {
- throw new NotImplementedException();
- }
- #endregion
- #region ICloneable Members
- public virtual object Clone()
- {
- Row row = this.MemberwiseClone() as Row;
- row.State = RowState.New | RowState.Attached;
- row.columnsChanged = new ColumnCollection();
- row.itemArray = new object[itemArray.Length];
- itemArray.CopyTo(row.itemArray, 0);
- return row;
- }
- #endregion
- #region ICustomTypeDescriptor Members
- AttributeCollection ICustomTypeDescriptor.GetAttributes()
- {
- return TypeDescriptor.GetAttributes(this, true);
- }
- string ICustomTypeDescriptor.GetClassName()
- {
- return TypeDescriptor.GetClassName(this, true);
- }
- string ICustomTypeDescriptor.GetComponentName()
- {
- return TypeDescriptor.GetComponentName(this, true);
- }
- TypeConverter ICustomTypeDescriptor.GetConverter()
- {
- return TypeDescriptor.GetConverter(this, true);
- }
- EventDescriptor ICustomTypeDescriptor.GetDefaultEvent()
- {
- return TypeDescriptor.GetDefaultEvent(this, true);
- }
- PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty()
- {
- return TypeDescriptor.GetDefaultProperty(this, true);
- }
- object ICustomTypeDescriptor.GetEditor(Type editorBaseType)
- {
- return TypeDescriptor.GetEditor(this, editorBaseType, true);
- }
- EventDescriptorCollection ICustomTypeDescriptor.GetEvents(Attribute[] attributes)
- {
- return TypeDescriptor.GetEvents(attributes, true);
- }
- EventDescriptorCollection ICustomTypeDescriptor.GetEvents()
- {
- return ((ICustomTypeDescriptor)this).GetEvents(null);
- }
- PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] attributes)
- {
- List<PropertyDescriptor> props = new List<PropertyDescriptor>();
- for (int i = 0; i < Columns.Count; i++)
- {
- Column col = Columns[i];
- if (col.Visible && (col.Permissions & Permission.Select) == Permission.Select)
- {
- props.Add(new ColumnPropertyDescriptor(col/*, this*/, col.PropertyName, attributes));
- }
- }
- //PropertyDescriptorCollection propsColl = TypeDescriptor.GetProperties(this, attributes, true);
- //List<PropertyDescriptor> props = new List<PropertyDescriptor>();
- //foreach (PropertyDescriptor prop in propsColl)
- //{
- // props.Add(prop);
- //}
- return new PropertyDescriptorCollection(props.ToArray());
- //return TypeDescriptor.GetProperties(this, attributes, true);
- }
- PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties()
- {
- return ((ICustomTypeDescriptor)this).GetProperties(null);
- }
- object ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor pd)
- {
- return this;
- }
- #endregion
- internal class ColumnPropertyDescriptor : PropertyDescriptor
- {
- private Column column;
- public ColumnPropertyDescriptor(Column col, string name, Attribute[] attrs)
- : base(name, attrs)
- {
- this.column = col;
- }
- public override bool CanResetValue(object component)
- {
- return false; //!column.PrimaryKey;
- }
- public override Type ComponentType
- {
- get { return typeof(Row); }
- }
- public override object GetValue(object component)
- {
- return ((Row)component)[column];
- }
- public override bool IsReadOnly
- {
- get { return column.ReadOnly; }
- }
- public override Type PropertyType
- {
- get { return Tools.DbTypeToType(column.DbType); }
- }
- public override void ResetValue(object component)
- {
- //((Row)component)[column] = column.DefaultValue;
- }
- public override void SetValue(object component, object value)
- {
- ((Row)component)[column] = value;
- }
- public override bool ShouldSerializeValue(object component)
- {
- return false;
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment