* DataColumn.cs :
authorBoris Kirzner <borisk@mono-cvs.ximian.com>
Thu, 17 Jun 2004 11:36:24 +0000 (11:36 -0000)
committerBoris Kirzner <borisk@mono-cvs.ximian.com>
Thu, 17 Jun 2004 11:36:24 +0000 (11:36 -0000)
  - AutoIncrementValue fixes. Now AutoIncrement uses DataContainer.GetInt64 to avoid boxing.
  - DefaultValue fixes. Default value from now on is also stored in special record in DataTable, so we're able to set default value for the column faster (typed).

svn path=/trunk/mcs/; revision=29759

mcs/class/System.Data/System.Data/ChangeLog
mcs/class/System.Data/System.Data/DataColumn.cs

index 4a46dc831b0d1cd733e9ed93518c54bc339af285..18f2de7540ac4d3b0897b2c8089309efdd5bad79 100644 (file)
@@ -1,3 +1,11 @@
+2004-06-17  Boris Kirzner  <borisk@mainsoft.com>
+
+       * DataColumn.cs : 
+         - AutoIncrementValue fixes. Now AutoIncrement uses DataContainer.GetInt64 to avoid boxing.
+         - DefaultValue fixes. Default value from now on is also stored in special record in 
+         DataTable, so we're able to set default value for the column faster (typed).
+       
+       
 2004-06-15  Atsushi Enomoto  <atsushi@ximian.com>
 
        * DataSet.cs : children of non-root rows were not properly written.
index c589c4568e0b0148f2ee2a9969a50a98cfc6af4e..b8e46eb754de61ff9ba13dd288c7fe9a2848f26f 100644 (file)
@@ -135,19 +135,13 @@ namespace System.Data {
                                return DataContainer[index];
                        }
                        set {
-                               DataContainer[index] = value;
+                               if ( !(value == null && AutoIncrement) ) {
+                                       DataContainer[index] = value;
+                               }
 
                                if ( AutoIncrement && !DataContainer.IsNull(index) ) {
                                        long value64 = Convert.ToInt64(value);
-                                       if (_autoIncrementStep > 0 ) {
-                                               if (value64 >= _nextAutoIncrementValue) {
-                                                       _nextAutoIncrementValue = value64;
-                                                       AutoIncrementValue ();
-                                               }
-                                       }
-                                       else if (value64 <= _nextAutoIncrementValue) {
-                                               AutoIncrementValue ();
-                                       }
+                                       UpdateAutoIncrementValue(value64);
                                }
                        }
                }
@@ -222,6 +216,11 @@ namespace System.Data {
                                                throw new ArgumentException("Can not Auto Increment a computed column."); 
                                        }
 
+                                       if ( DefaultValue != DBNull.Value ) {
+                                               throw new ArgumentException("Can not set AutoIncrement while" +
+                                                       " default value exists for this column.");
+                                       }
+
                                        //If the DataType of this Column isn't an Int
                                        //Make it an int
                                        TypeCode typeCode = Type.GetTypeCode(DataType);
@@ -271,6 +270,19 @@ namespace System.Data {
                        }
                }
 
+               internal void UpdateAutoIncrementValue(long value64)
+               {
+                       if (_autoIncrementStep > 0 ) {
+                               if (value64 >= _nextAutoIncrementValue) {
+                                       _nextAutoIncrementValue = value64;
+                                       AutoIncrementValue ();
+                               }
+                       }
+                       else if (value64 <= _nextAutoIncrementValue) {
+                               AutoIncrementValue ();
+                       }
+               }
+
                internal long AutoIncrementValue () 
                {
                        long currentValue = _nextAutoIncrementValue;
@@ -380,39 +392,39 @@ namespace System.Data {
                        get {
                                return _defaultValue;
                        }
+
                        set {
-                               object tmpObj;
-                               if ((this._defaultValue == null) || (!this._defaultValue.Equals(value)))
-                               {
-                                       //If autoIncrement == true throw
-                                       if (AutoIncrement) 
-                                       {
-                                               throw new ArgumentException("Can not set default value while" +
-                                                       " AutoIncrement is true on this column.");
-                                       }
+                               if (AutoIncrement) {
+                                       throw new ArgumentException("Can not set default value while" +
+                                               " AutoIncrement is true on this column.");
+                               }
 
-                                       if (value == null) 
-                                       {
+                               object tmpObj;
+                               if (!this._defaultValue.Equals(value)) {                
+                                       if (value == null) {
                                                tmpObj = DBNull.Value;
                                        }
-                                       else
+                                       else {
                                                tmpObj = value;
+                                       }
 
-                                       if ((this.DataType != typeof (object))&& (tmpObj != DBNull.Value))
-                                       {
-                                               try
-                                               {
+                                       if ((this.DataType != typeof (object))&& (tmpObj != DBNull.Value)) {
+                                               try {
                                                        //Casting to the new type
                                                        tmpObj= Convert.ChangeType(tmpObj,this.DataType);
                                                }
-                                               catch (InvalidCastException)
-                                               {
+                                               catch (InvalidCastException) {
                                                        throw new InvalidCastException("Default Value type is not compatible with" + 
                                                                " column type.");
                                                }
                                        }
                                        _defaultValue = tmpObj;
                                }
+
+                               // store default value in the table if already belongs to
+                               if (Table != null && Table.DefaultValuesRowIndex != -1) {
+                                       DataContainer[Table.DefaultValuesRowIndex] = _defaultValue;
+                               }
                        }
                }
 
@@ -437,7 +449,7 @@ namespace System.Data {
                                expression = value;  
                        }
                }
-               
+
                internal IExpression CompiledExpression {
                        get { return compiledExpression; }
                }
@@ -666,18 +678,30 @@ namespace System.Data {
 
                internal void SetTable(DataTable table) {
                        if(_table!=null) { // serves as double check while adding to a table
-                               throw new ArgumentException("The column already belongs to a different table");
-                        }
-                        _table = table;
-                        // this will get called by DataTable
-                        // and DataColumnCollection
-                        if(unique) {
-                               // if the DataColumn is marked as Unique and then
-                               // added to a DataTable , then a UniqueConstraint
-                               // should be created
-                               UniqueConstraint uc = new UniqueConstraint(this);
-                               _table.Constraints.Add(uc);
-                        }
+                    throw new ArgumentException("The column already belongs to a different table");
+            }
+            _table = table;
+            // this will get called by DataTable
+            // and DataColumnCollection
+            if(unique) {
+                // if the DataColumn is marked as Unique and then
+                   // added to a DataTable , then a UniqueConstraint
+                   // should be created
+                UniqueConstraint uc = new UniqueConstraint(this);
+                _table.Constraints.Add(uc);
+            }
+
+                       // allocate space in the column data container 
+                       DataContainer.Capacity = _table.RecordCache.CurrentCapacity;
+                       
+                       int defaultValuesRowIndex = _table.DefaultValuesRowIndex;
+                       if ( defaultValuesRowIndex != -1) {
+                               // store default value in the table
+                               DataContainer[defaultValuesRowIndex] = _defaultValue;
+                               // Set all the values in data container to default
+                               // it's cheaper that raise event on each row.
+                               DataContainer.FillValues(_table.DefaultValuesRowIndex);
+                       }
                }