Explorar o código

* DataColumn.cs (AllowDBNull) : Throw an exception.

	* DataRow.cs (this[]) : First check if row is deleted.
	(ItemArray) : First check if row is deleted.
	(SetColumnValue) : Initiate error message when null violation happens. Check for array type when
	setting the value of the row.
	(EndEdit) : Check that the table's row is not in loading before validating constrains.
	(CheckNullConstraints) : new method for validating null constraint violations.

	* DataRowCollection.cs (ValidateDataRowInternal) : Adding validation of null constraint violation.

	* DataSet.cs (CaseSensitive) : After changing the value - check constrains.
	(EnforceConstraints) : When changing the value to true assert all constraints.

	* UniqueConstraint.cs (AssertConstraint,CalcHashValue, RowsComparer) : Adding support for case insensitive comparison.

svn path=/trunk/mcs/; revision=19764
Eran Domb %!s(int64=22) %!d(string=hai) anos
pai
achega
5ffcf71ce0

+ 2 - 0
mcs/class/System.Data/System.Data/DataColumn.cs

@@ -137,6 +137,8 @@ namespace System.Data {
 							}
 						}
 						
+						if (nullsFound)
+							throw new DataException("Column '" + ColumnName + "' has null values in it.");
 						//TODO: Validate no null values exist
 						//do we also check different versions of the row??
 					}

A diferenza do arquivo foi suprimida porque é demasiado grande
+ 581 - 585
mcs/class/System.Data/System.Data/DataRow.cs


+ 6 - 4
mcs/class/System.Data/System.Data/DataRowCollection.cs

@@ -155,7 +155,7 @@ namespace System.Data
 				if (row.RowState != DataRowState.Deleted)
 				{
 					object primValue = row [primColumnName];
-					if (key == null) 
+					if (key == null) 
 					{
 						if (primValue == null)
 							return row;
@@ -196,12 +196,12 @@ namespace System.Data
 				if (row.RowState != DataRowState.Deleted)
 				{
 					bool eq = true;
-					for (int i = 0; i < keys.Length; i++) 
+					for (int i = 0; i < keys.Length; i++) 
 					{
 					
 						object primValue = row [primColumnNames [i]];
 						object keyValue = keys [i];
-						if (keyValue == null) 
+						if (keyValue == null) 
 						{
 							if (primValue == null)
 								return row;
@@ -211,7 +211,7 @@ namespace System.Data
 								       
 						newKey = Convert.ChangeType (keyValue, Type.GetTypeCode(primValue.GetType ()));
 
-						if (!primValue.Equals (newKey)) 
+						if (!primValue.Equals (newKey)) 
 						{
 							eq = false;
 							break;
@@ -276,6 +276,8 @@ namespace System.Data
 		[MonoTODO]
 		internal void ValidateDataRowInternal(DataRow row)
 		{
+			//first check for null violations.
+			row.CheckNullConstraints();
 			//FIXME: this validates constraints in the order they appear
 			//in the collection. Most probably we need to do it in a 
 			//specific order like unique/primary keys first, then Foreignkeys, etc

+ 22 - 3
mcs/class/System.Data/System.Data/DataSet.cs

@@ -82,6 +82,14 @@ namespace System.Data {
 				}
 
 				caseSensitive = value; 
+				if (!caseSensitive)
+				{
+					foreach (DataTable table in Tables)
+					{
+						foreach (Constraint c in table.Constraints)
+							c.AssertConstraint();
+					}
+				}
 			}
 		}
 
@@ -107,7 +115,20 @@ namespace System.Data {
 		[DefaultValue (true)]
 		public bool EnforceConstraints {
 			get { return enforceConstraints; } 
-			set { enforceConstraints = value; }
+			set { 
+				if (value != enforceConstraints)
+				{
+					enforceConstraints = value; 
+					if (value)
+					{
+						foreach (DataTable table in Tables)
+						{
+							foreach (Constraint c in table.Constraints)
+								c.AssertConstraint();
+						}
+					}
+				}
+			}
 		}
 
 		[Browsable (false)]
@@ -791,12 +812,10 @@ namespace System.Data {
 		#region ISupportInitialize methods
 		public void BeginInit ()
 		{
-			throw new NotImplementedException ();
 		}
 		
 		public void EndInit ()
 		{
-			throw new NotImplementedException ();
 		}
 		#endregion
 

+ 63 - 23
mcs/class/System.Data/System.Data/UniqueConstraint.cs

@@ -359,7 +359,9 @@ namespace System.Data {
 			
 			//Unique?	
 			DataTable tbl = _dataTable;
-
+			bool ignoreCase = false;
+			if (_dataTable.DataSet != null)
+				ignoreCase = !_dataTable.DataSet.CaseSensitive;
 			//TODO: Investigate other ways of speeding up the validation work below.
 
 			//validate no duplicates exists.
@@ -371,20 +373,31 @@ namespace System.Data {
 				DataRow[] rows = new DataRow [tbl.Rows.Count];
 				tbl.Rows.CopyTo (rows, 0);
 				
-				Array.Sort(rows, new RowsComparer(this));
+				Array.Sort(rows, new RowsComparer(this, ignoreCase));
+				
 				for (int i = 0 ; i < rows.Length - 1 ; i++) 
-				{
-					bool match = true;
-					// check if the values in the constraints columns are equal
-					for (int j = 0; j < _dataColumns.Length; j++)
-					{
-						if (!rows[i][_dataColumns[j]].Equals(rows[i + 1][_dataColumns[j]]))
-						{
-							match = false;
-							break;
-						}	
-					}
-					if (match)
+				{
+					bool match = true;
+					// check if the values in the constraints columns are equal
+					for (int j = 0; j < _dataColumns.Length; j++)
+					{
+						if (_dataColumns[j].DataType == typeof(string))
+						{
+							string origVal = (string)rows[i][_dataColumns[j]];
+							string compVal = (string)rows[i + 1][_dataColumns[j]];
+							if (String.Compare(origVal, compVal, ignoreCase) != 0)
+							{
+								match = false;
+								break;
+							}
+						}
+						else if (!rows[i][_dataColumns[j]].Equals(rows[i + 1][_dataColumns[j]]))
+						{
+							match = false;
+							break;
+						}	
+					}
+					if (match)
 						throw new ConstraintException (String.Format ("Column '{0}' contains non-unique values", this._dataColumns[0]));					
 				}
 			}
@@ -420,6 +433,10 @@ namespace System.Data {
 					rowVals[i] = row[_dataColumns[i], DataRowVersion.Current];
 			}
 			
+			bool ignoreCase = false;
+			if (_dataTable.DataSet != null)
+				ignoreCase = !_dataTable.DataSet.CaseSensitive;
+
 			foreach(DataRow compareRow in tbl.Rows)
 			{
 				if (compareRow.RowState != DataRowState.Deleted)
@@ -428,13 +445,21 @@ namespace System.Data {
 					//skip if it is the same row to be validated
 					if(!row.Equals(compareRow))
 					{
-						//FIXME: should we compare to compareRow[DataRowVersion.Current]?
-						//FIXME: We need to compare to all columns the constraint is set to.
 						for (int i = 0; i < _dataColumns.Length; i++)
 						{
 							// if the values in the row are not equal to the values of
 							// the original row from the table we can move to the next row.
-							if(!rowVals[i].Equals( compareRow[_dataColumns[i]]))
+							if (_dataColumns[i].DataType == typeof(string))
+							{
+								string origVal = (string)rowVals[i];
+								string compVal = (string)compareRow[_dataColumns[i]];
+								if (String.Compare(origVal, compVal, ignoreCase) != 0)
+								{
+									isValid = true;
+									break;
+								}
+							}
+							else if (!rowVals[i].Equals( compareRow[_dataColumns[i]]))
 							{
 								isValid = true;
 								break;
@@ -467,15 +492,24 @@ namespace System.Data {
 		}
 		
 		// generates a hash key for a given row based on the constraints columns.
-		internal int CalcHashValue(DataRow row)
+		internal int CalcHashValue(DataRow row, bool ignoreCase)
 		{
 			object o;
 			int retVal = 0;
+			CaseInsensitiveHashCodeProvider ciProvider = null;
+			if (ignoreCase)
+				ciProvider = new CaseInsensitiveHashCodeProvider(_dataTable.Locale);
 			for (int i = 0; i < _dataColumns.Length; i++)
 			{
 				o = row[_dataColumns[i]];
 				if (o != null)
-					retVal += o.GetHashCode();
+				{
+					if (ciProvider != null)
+						retVal += ciProvider.GetHashCode(o);
+					else
+						retVal += o.GetHashCode();
+
+				}
 			}
 			return retVal;
 		}
@@ -485,9 +519,11 @@ namespace System.Data {
 		private class RowsComparer : IComparer
 		{
 			private UniqueConstraint _uc;
+			private bool _ignoreCase;
 			
-			public RowsComparer(UniqueConstraint uc)
+			public RowsComparer(UniqueConstraint uc, bool ignoreCase)
 			{
+				_ignoreCase = ignoreCase;
 				_uc = uc;
 			}
 
@@ -495,10 +531,14 @@ namespace System.Data {
 			{
 				DataRow row1 = (DataRow) o1;
 				DataRow row2 = (DataRow) o2;
-				int val1 = _uc.CalcHashValue(row1);
-				int val2 = _uc.CalcHashValue(row2);
+				int val1 = _uc.CalcHashValue(row1, _ignoreCase);
+				int val2 = _uc.CalcHashValue(row2, _ignoreCase);
 				
-				return val1 - val2;
+				if (val1 > val2)
+					return 1;
+				if (val1 == val2)
+					return 0;
+				return -1;
 			}
 		}
 	}

Algúns arquivos non se mostraron porque demasiados arquivos cambiaron neste cambio