2 // System.Data.DataColumn.cs
5 // Franklin Wise (gracenote@earthlink.net)
6 // Christopher Podurgiel (cpodurgiel@msn.com)
7 // Rodrigo Moya (rodrigo@ximian.com)
8 // Daniel Morgan (danmorg@sc.rr.com)
9 // Tim Coleman (tim@timcoleman.com)
11 // (C) Copyright 2002, Franklin Wise
12 // (C) Chris Podurgiel
13 // (C) Ximian, Inc 2002
14 // Copyright (C) Tim Coleman, 2002
15 // Copyright (C) Daniel Morgan, 2002, 2003
19 using System.ComponentModel;
20 using System.Reflection;
21 using System.Collections;
22 using System.Data.Common;
23 using Mono.Data.SqlExpressions;
25 namespace System.Data {
26 internal delegate void DelegateColumnValueChange(DataColumn column, DataRow row, object proposedValue);
29 /// Summary description for DataColumn.
34 [DefaultMember ("Item")]
35 [DefaultProperty ("ColumnName")]
36 [DesignTimeVisible (false)]
37 [TypeConverterAttribute (typeof (ComponentConverter))]
38 public class DataColumn : MarshalByValueComponent
42 //used for constraint validation
43 //if an exception is fired during this event the change should be canceled
44 internal event DelegateColumnValueChange ValidateColumnValueChange;
46 //used for FK Constraint Cascading rules
47 internal event DelegateColumnValueChange ColumnValueChanging;
52 private bool _allowDBNull = true;
53 private bool _autoIncrement;
54 private long _autoIncrementSeed;
55 private long _autoIncrementStep = 1;
56 private long _nextAutoIncrementValue;
57 private bool dataHasBeenSet;
58 private string _caption;
59 private MappingType _columnMapping;
60 private string _columnName;
61 private Type _dataType;
62 private object _defaultValue = DBNull.Value;
63 private string expression;
64 private IExpression compiledExpression;
65 private PropertyCollection _extendedProperties = new PropertyCollection ();
66 private int maxLength = -1; //-1 represents no length limit
67 private string nameSpace = "";
68 private int _ordinal = -1; //-1 represents not part of a collection
69 private string prefix = "";
70 private bool readOnly;
71 private DataTable _table;
78 public DataColumn() : this("", typeof (string), "", MappingType.Element)
82 //TODO: Ctor init vars directly
83 public DataColumn(string columnName): this(columnName, typeof (string), "", MappingType.Element)
87 public DataColumn(string columnName, Type dataType): this(columnName, dataType, "", MappingType.Element)
91 public DataColumn( string columnName, Type dataType,
92 string expr): this(columnName, dataType, expr, MappingType.Element)
96 public DataColumn(string columnName, Type dataType,
97 string expr, MappingType type)
99 ColumnName = (columnName == null ? "" : columnName);
101 if(dataType == null) {
102 throw new ArgumentNullException("dataType can't be null.");
106 Expression = expr == null ? "" : expr;
107 ColumnMapping = type;
113 [DataCategory ("Data")]
114 [DataSysDescription ("Indicates whether null values are allowed in this column.")]
115 [DefaultValue (true)]
116 public bool AllowDBNull
122 //TODO: If we are a part of the table and this value changes
123 //we need to validate that all the existing values conform to the new setting
131 //if Value == false case
134 if (_table.Rows.Count > 0)
136 bool nullsFound = false;
137 for(int r = 0; r < _table.Rows.Count; r++) {
138 DataRow row = _table.Rows[r];
139 if(row.IsNull(this)) {
146 throw new DataException("Column '" + ColumnName + "' has null values in it.");
147 //TODO: Validate no null values exist
148 //do we also check different versions of the row??
152 _allowDBNull = value;
157 /// Gets or sets a value indicating whether the column automatically increments the value of the column for new rows added to the table.
160 /// If the type of this column is not Int16, Int32, or Int64 when this property is set,
161 /// the DataType property is coerced to Int32. An exception is generated if this is a computed column
162 /// (that is, the Expression property is set.) The incremented value is used only if the row's value for this column,
163 /// when added to the columns collection, is equal to the default value.
165 [DataCategory ("Data")]
166 [DataSysDescription ("Indicates whether the column automatically increments itself for new rows added to the table. The type of this column must be Int16, Int32, or Int64.")]
167 [DefaultValue (false)]
168 [RefreshProperties (RefreshProperties.All)]
169 public bool AutoIncrement
172 return _autoIncrement;
177 //Can't be true if this is a computed column
178 if (Expression != string.Empty)
180 throw new ArgumentException("Can not Auto Increment a computed column.");
183 //If the DataType of this Column isn't an Int
185 TypeCode typeCode = Type.GetTypeCode(_dataType);
186 if(typeCode != TypeCode.Int16 &&
187 typeCode != TypeCode.Int32 &&
188 typeCode != TypeCode.Int64)
190 _dataType = typeof(Int32);
193 _autoIncrement = value;
197 [DataCategory ("Data")]
198 [DataSysDescription ("Indicates the starting value for an AutoIncrement column.")]
200 public long AutoIncrementSeed
203 return _autoIncrementSeed;
206 _autoIncrementSeed = value;
207 _nextAutoIncrementValue = _autoIncrementSeed;
211 [DataCategory ("Data")]
212 [DataSysDescription ("Indicates the increment used by an AutoIncrement column.")]
214 public long AutoIncrementStep
217 return _autoIncrementStep;
220 _autoIncrementStep = value;
224 internal void UpdateAutoIncrementValue (long value)
226 if(value > _nextAutoIncrementValue) {
227 _nextAutoIncrementValue = value;
228 AutoIncrementValue ();
232 internal long AutoIncrementValue ()
234 long currentValue = _nextAutoIncrementValue;
235 _nextAutoIncrementValue += AutoIncrementStep;
239 internal long GetAutoIncrementValue ()
241 return _nextAutoIncrementValue;
244 internal bool DataHasBeenSet {
246 return dataHasBeenSet;
249 dataHasBeenSet = value;
253 [DataCategory ("Data")]
254 [DataSysDescription ("Indicates the default user-interface caption for this column.")]
255 public string Caption
267 [DataSysDescription ("Indicates how this column persists in XML: as an attribute, element, simple content node, or nothing.")]
268 [DefaultValue (MappingType.Element)]
269 public virtual MappingType ColumnMapping
272 return _columnMapping;
275 _columnMapping = value;
279 [DataCategory ("Data")]
280 [DataSysDescription ("Indicates the name used to look up this column in the Columns collection of a DataTable.")]
281 [RefreshProperties (RefreshProperties.All)]
283 public string ColumnName
286 return "" + _columnName;
289 //Both are checked after the column is part of the collection
290 //TODO: Check Name duplicate
291 //TODO: check Name != null
296 [DataCategory ("Data")]
297 [DataSysDescription ("Indicates the type of data stored in this column.")]
298 [DefaultValue (typeof (string))]
299 [RefreshProperties (RefreshProperties.All)]
300 [TypeConverterAttribute (typeof (ColumnTypeConverter))]
307 // check if data already exists can we change the datatype
308 if(DataHasBeenSet == true)
309 throw new ArgumentException("The column already has data stored.");
311 // we want to check that the datatype is supported?
312 TypeCode typeCode = Type.GetTypeCode(value);
314 //Check AutoIncrement status, make compatible datatype
315 if(AutoIncrement == true) {
316 if(typeCode != TypeCode.Int16 &&
317 typeCode != TypeCode.Int32 &&
318 typeCode != TypeCode.Int64)
319 AutoIncrement = false;
328 /// <remarks>When AutoIncrement is set to true, there can be no default value.</remarks>
329 /// <exception cref="System.InvalidCastException"></exception>
330 /// <exception cref="System.ArgumentException"></exception>
331 [DataCategory ("Data")]
332 [DataSysDescription ("Indicates the default column value used when adding new rows to the table.")]
333 [TypeConverterAttribute (typeof (System.Data.DefaultValueTypeConverter))]
334 public object DefaultValue
337 return _defaultValue;
341 if ((this._defaultValue == null) || (!this._defaultValue.Equals(value)))
343 //If autoIncrement == true throw
346 throw new ArgumentException("Can not set default value while" +
347 " AutoIncrement is true on this column.");
352 tmpObj = DBNull.Value;
357 if ((this.DataType != typeof (object))&& (tmpObj != DBNull.Value))
361 //Casting to the new type
362 tmpObj= Convert.ChangeType(tmpObj,this.DataType);
364 catch (InvalidCastException)
366 throw new InvalidCastException("Default Value type is not compatible with" +
370 _defaultValue = tmpObj;
375 [DataCategory ("Data")]
376 [DataSysDescription ("Indicates the value that this column computes for each row based on other columns instead of taking user input.")]
378 [RefreshProperties (RefreshProperties.All)]
379 public string Expression
386 value = String.Empty;
388 if (value != String.Empty) {
389 Parser parser = new Parser ();
390 compiledExpression = parser.Compile (value);
399 [DataCategory ("Data")]
400 [DataSysDescription ("The collection that holds custom user information.")]
401 public PropertyCollection ExtendedProperties
404 return _extendedProperties;
408 [DataCategory ("Data")]
409 [DataSysDescription ("Indicates the maximum length of the value this column allows.")]
414 //Default == -1 no max length
418 //only applies to string columns
423 [DataCategory ("Data")]
424 [DataSysDescription ("Indicates the XML uri for elements stored in this column.")]
425 public string Namespace
435 //Need a good way to set the Ordinal when the column is added to a columnCollection.
437 [DataCategory ("Data")]
438 [DataSysDescription ("Indicates the index of this column in the Columns collection.")]
439 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
443 //value is -1 if not part of a collection
448 internal void SetOrdinal(int ordinal)
453 [DataCategory ("Data")]
454 [DataSysDescription ("Indicates the prefix used for this DataColumn in the xml representation.")]
466 [DataCategory ("Data")]
467 [DataSysDescription ("Indicates whether this column allows changes once a row has been added to the table.")]
468 [DefaultValue (false)]
480 [DataCategory ("Data")]
481 [DataSysDescription ("Returns the DataTable to which this column belongs.")]
482 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
483 public DataTable Table
491 [DataCategory ("Data")]
492 [DataSysDescription ("Indicates whether this column should restrict its values in the rows of the table to be unique.")]
493 [DefaultValue (false)]
494 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
501 //if Table == null then the UniqueConstraint is
502 //created on addition to the collection
504 //FIXME?: need to check if value is the same
505 //because when calling "new UniqueConstraint"
506 //the new object tries to set "column.Unique = True"
507 //which creates an infinite loop.
514 if (Expression != null && Expression != "")
515 throw new ArgumentException("Cannot change Unique property for the expression column.");
518 UniqueConstraint uc = new UniqueConstraint(this);
519 _table.Constraints.Add(uc);
526 ConstraintCollection cc = _table.Constraints;
527 //foreach (Constraint c in cc)
528 for (int i = 0; i < cc.Count; i++)
530 Constraint c = cc[i];
531 if (c is UniqueConstraint)
533 DataColumn[] cols = ((UniqueConstraint)c).Columns;
535 if (cols.Length == 1 && cols[0] == this)
537 if (!cc.CanRemove(c))
538 throw new ArgumentException("Cannot remove unique constraint '" + c.ConstraintName + "'. Remove foreign key constraint first.");
552 #endregion // Properties
558 protected internal void CheckNotAllowNull() {
562 protected void CheckUnique() {
567 /// Sets unique true whithout creating Constraint
569 internal void SetUnique()
576 internal void AssertCanAddToCollection()
578 //Check if Default Value is set and AutoInc is set
582 protected internal virtual void
583 OnPropertyChanging (PropertyChangedEventArgs pcevent) {
587 protected internal void RaisePropertyChanging(string name) {
591 /// Gets the Expression of the column, if one exists.
593 /// <returns>The Expression value, if the property is set;
594 /// otherwise, the ColumnName property.</returns>
595 public override string ToString()
597 if (expression != string.Empty)
598 return ColumnName + " + " + expression;
604 internal void SetTable(DataTable table) {
606 // this will get called by DataTable
607 // and DataColumnCollection
611 // Returns true if all the same collumns are in columnSet and compareSet
612 internal static bool AreColumnSetsTheSame(DataColumn[] columnSet, DataColumn[] compareSet)
614 if (null == columnSet && null == compareSet) return true;
615 if (null == columnSet || null == compareSet) return false;
617 if (columnSet.Length != compareSet.Length) return false;
619 foreach (DataColumn col in columnSet)
621 bool matchFound = false;
622 foreach (DataColumn compare in compareSet)
629 if (! matchFound) return false;
635 static internal int CompareValues (object val1, object val2, Type t, bool ignoreCase)
637 IComparer comparer = DBComparerFactory.GetComparer (t, ignoreCase);
638 return comparer.Compare (val1, val2);
641 #endregion // Methods