Merge pull request #1304 from slluis/mac-proxy-autoconfig
[mono.git] / mcs / class / System.Data / System.Data / DataRow.cs
index db4ad0c29c9a7c613b58052fd4454093a948cbb2..2d0df625f37bf7cbec2ba369d04a77211a4b7f20 100644 (file)
@@ -96,6 +96,7 @@ namespace System.Data {
                {
                        _table = table;
                        _rowId = rowId;
+                       rowError = String.Empty;
                }
 
                #endregion // Constructors
@@ -117,11 +118,11 @@ namespace System.Data {
                /// </summary>
                public bool HasErrors {
                        get {
-                               if (RowError != string.Empty)
+                               if (!string.IsNullOrEmpty (RowError))
                                        return true;
 
                                foreach (String columnError in ColumnErrors) {
-                                       if (columnError != null && columnError != string.Empty)
+                                       if (!string.IsNullOrEmpty (columnError))
                                                return true;
                                }
                                return false;
@@ -134,10 +135,7 @@ namespace System.Data {
                public object this [string columnName] {
                        get { return this [columnName, DataRowVersion.Default]; }
                        set {
-                               DataColumn column = _table.Columns [columnName];
-                               if (column == null)
-                                       throw new ArgumentException ("The column '" + columnName +
-                                               "' does not belong to the table : " + _table.TableName);
+                               DataColumn column = GetColumn (columnName);
                                this [column.Ordinal] = value;
                        }
                }
@@ -202,10 +200,7 @@ namespace System.Data {
                /// </summary>
                public object this [string columnName, DataRowVersion version] {
                        get {
-                               DataColumn column = _table.Columns [columnName];
-                               if (column == null)
-                                       throw new ArgumentException ("The column '" + columnName +
-                                               "' does not belong to the table : " + _table.TableName);
+                               DataColumn column = GetColumn (columnName);
                                return this [column.Ordinal, version];
                        }
                }
@@ -514,7 +509,7 @@ namespace System.Data {
                /// </summary>
                public string RowError {
                        get { return rowError; }
-                       set { rowError = value; }
+                       set { rowError = value ?? string.Empty; }
                }
 
                internal int IndexFromVersion (DataRowVersion version)
@@ -548,7 +543,7 @@ namespace System.Data {
                        if (index >= 0)
                                return index;
 
-                       throw new VersionNotFoundException (String.Format ("There is no {0} data to accces.", version));
+                       throw new VersionNotFoundException (String.Format ("There is no {0} data to access.", version));
                }
 
                internal DataRowVersion VersionFromIndex (int index)
@@ -1248,7 +1243,7 @@ namespace System.Data {
                /// </summary>
                public bool IsNull (string columnName)
                {
-                       return IsNull (Table.Columns [columnName]);
+                       return IsNull (GetColumn (columnName));
                }
 
                /// <summary>
@@ -1257,6 +1252,17 @@ namespace System.Data {
                /// </summary>
                public bool IsNull (DataColumn column, DataRowVersion version)
                {
+                       if (column == null)
+                               throw new ArgumentNullException ("column");
+
+                       // use the expresion if there is one
+                       if (column.Expression != String.Empty) {
+                               // FIXME: how does this handle 'version'?
+                               // TODO: Can we avoid the Eval each time by using the cached value?
+                               object o = column.CompiledExpression.Eval (this);
+                               return o == null && o == DBNull.Value;
+                       }
+
                        return column.DataContainer.IsNull (IndexFromVersion (version));
                }
 
@@ -1365,7 +1371,7 @@ namespace System.Data {
                /// </summary>
                public void SetParentRow (DataRow parentRow, DataRelation relation)
                {
-                       if (_table == null || parentRow.Table == null)
+                       if (_table == null || (parentRow != null && parentRow.Table == null))
                                throw new RowNotInTableException ("This row has been removed from a table and does not have any data.  BeginEdit() will allow creation of new data in this row.");
 
                        if (parentRow != null && _table.DataSet != parentRow.Table.DataSet)
@@ -1378,10 +1384,13 @@ namespace System.Data {
                        BeginEdit();
 
                        IEnumerable relations;
-                       if (relation == null)
-                               relations = _table.ParentRelations;
-                       else
+                       if (relation != null) {
+                               if (parentRow != null && relation.ParentColumns [0].Table != parentRow.Table)
+                                       throw new InvalidConstraintException (string.Format ("Parent belongs to table {0} but relation is for table {1}", parentRow.Table, relation.ParentColumns [0].Table));
                                relations = new DataRelation [] { relation };
+                       } else {
+                               relations = _table.ParentRelations;
+                       }
 
                        foreach (DataRelation rel in relations) {
                                DataColumn [] childCols = rel.ChildColumns;
@@ -1390,7 +1399,7 @@ namespace System.Data {
                                for (int i = 0; i < parentCols.Length; i++) {
                                        if (parentRow == null) {
                                                childCols [i].DataContainer [Proposed] = DBNull.Value;
-                                       } else {
+                                       } else if (parentCols [i].Table == parentRow.Table) {
                                                int defaultIdx = parentRow.IndexFromVersion (DataRowVersion.Default);
                                                childCols [i].DataContainer.CopyValue(parentCols [i].DataContainer, defaultIdx, Proposed);
                                        }
@@ -1689,5 +1698,15 @@ namespace System.Data {
                        }
                }
 #endif // NET_2_0
+
+               DataColumn GetColumn (string columnName)
+               {
+                       DataColumn column = _table.Columns [columnName];
+
+                       if (column == null)
+                               throw new ArgumentException ("The column '" + columnName + "' does not belong to the table " + _table.TableName);
+
+                       return column;
+               }
        }
 }