Преглед на файлове

* 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).

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

Boris Kirzner преди 21 години
родител
ревизия
edddb534ed
променени са 2 файла, в които са добавени 73 реда и са изтрити 41 реда
  1. 8 0
      mcs/class/System.Data/System.Data/ChangeLog
  2. 65 41
      mcs/class/System.Data/System.Data/DataColumn.cs

+ 8 - 0
mcs/class/System.Data/System.Data/ChangeLog

@@ -1,3 +1,11 @@
+2004-06-17  Boris Kirzner  <[email protected]>
+
+	* 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  <[email protected]>
 
 	* DataSet.cs : children of non-root rows were not properly written.

+ 65 - 41
mcs/class/System.Data/System.Data/DataColumn.cs

@@ -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);
+			}
 		}