Locale.cs removed
[mono.git] / mcs / class / System.Data / System.Data / DataRowCollection.cs
1 //
2 // System.Data.DataRowCollection.cs
3 //
4 // Author:
5 //   Daniel Morgan <danmorg@sc.rr.com>
6 //   Tim Coleman <tim@timcoleman.com>
7 //
8 // (C) Ximian, Inc 2002
9 // (C) Copyright 2002 Tim Coleman
10 // (C) Copyright 2002 Daniel Morgan
11 //
12
13 using System;
14 using System.Collections;
15 using System.ComponentModel;
16
17 namespace System.Data
18 {
19         /// <summary>
20         /// Collection of DataRows in a DataTable
21         /// </summary>
22         [Serializable]
23         public class DataRowCollection : InternalDataCollectionBase 
24         {
25                 private DataTable table;
26
27                 /// <summary>
28                 /// Internal constructor used to build a DataRowCollection.
29                 /// </summary>
30                 internal DataRowCollection (DataTable table) : base ()
31                 {
32                         this.table = table;
33                 }
34
35                 /// <summary>
36                 /// Gets the row at the specified index.
37                 /// </summary>
38                 public DataRow this[int index] 
39                 {
40                         get { 
41                                 if (index >= Count)
42                                         throw new IndexOutOfRangeException ("There is no row at position " + index + ".");
43
44                                 return (DataRow) list[index]; 
45                         }
46                 }
47
48                 /// <summary>
49                 /// This member overrides InternalDataCollectionBase.List
50                 /// </summary>
51                 protected override ArrayList List 
52                 {
53                         get { return list; }
54                 }               
55
56                 /// <summary>
57                 /// Adds the specified DataRow to the DataRowCollection object.
58                 /// </summary>
59                 public void Add (DataRow row) 
60                 {
61                         //TODO: validation
62                         if (row == null)
63                                 throw new ArgumentNullException("row", "'row' argument cannot be null.");
64
65                         if (list.IndexOf(row) != -1)
66                                 throw new ArgumentException ("This row already belongs to this table.");
67                                 
68                         list.Add (row);
69                         row.AttachRow ();
70                         row.Table.ChangedDataRow (row, DataRowAction.Add);
71                 }
72
73                 /// <summary>
74                 /// Creates a row using specified values and adds it to the DataRowCollection.
75                 /// </summary>
76                 public virtual DataRow Add (object[] values) 
77                 {
78                         DataRow row = table.NewRow ();
79                         row.ItemArray = values;
80                         Add (row);
81                         return row;
82                 }
83
84                 /// <summary>
85                 /// Clears the collection of all rows.
86                 /// </summary>
87                 public void Clear () 
88                 {
89                         list.Clear ();
90                 }
91
92                 /// <summary>
93                 /// Gets a value indicating whether the primary key of any row in the collection contains
94                 /// the specified value.
95                 /// </summary>
96                 public bool Contains (object key) 
97                 {
98                         return Find (key) != null;
99                 }
100
101                 /// <summary>
102                 /// Gets a value indicating whether the primary key column(s) of any row in the 
103                 /// collection contains the values specified in the object array.
104                 /// </summary>
105                 public bool Contains (object[] keys) 
106                 {
107                         if (table.PrimaryKey.Length != keys.Length)
108                                 throw new ArgumentException ("Expecting " + table.PrimaryKey.Length + " value(s) for the key " + 
109                                                              "being indexed, but received " + keys.Length + " value(s).");
110
111                         return Find (keys) != null;
112                 }
113
114                 /// <summary>
115                 /// Gets the row specified by the primary key value.
116                 /// </summary>
117                 [MonoTODO]
118                 public DataRow Find (object key) 
119                 {
120                         if (table.PrimaryKey.Length == 0)
121                                 throw new MissingPrimaryKeyException ("Table doesn't have a primary key.");
122                         if (table.PrimaryKey.Length > 1)
123                                 throw new ArgumentException ("Expecting " + table.PrimaryKey.Length + 
124                                                              " value(s) for the key being indexed, but received 1 value(s).");
125
126                         string primColumnName = table.PrimaryKey [0].ColumnName;
127                         Type coltype = null;
128                         object newKey = null;
129                         
130                         foreach (DataRow row in this) {
131                                 
132                                 object primValue = row [primColumnName];
133                                 if (key == null) {
134                                         if (primValue == null)
135                                                 return row;
136                                         else 
137                                                 continue;
138                                 }
139                                        
140                                 newKey = Convert.ChangeType (key, Type.GetTypeCode(primValue.GetType ()));
141
142                                 if (primValue.Equals (newKey))
143                                         return row;
144                         }
145                                                 
146                         // FIXME: is the correct value null?
147                         return null;
148                 }
149
150                 /// <summary>
151                 /// Gets the row containing the specified primary key values.
152                 /// </summary>
153                 [MonoTODO]
154                 public DataRow Find (object[] keys) 
155                 {
156                         if (table.PrimaryKey.Length == 0)
157                                 throw new MissingPrimaryKeyException ("Table doesn't have a primary key.");
158
159                         string  [] primColumnNames = new string [table.PrimaryKey.Length];
160                         
161                         for (int i = 0; i < primColumnNames.Length; i++)
162                                 primColumnNames [i] = table.PrimaryKey [i].ColumnName;
163
164                         Type coltype = null;
165                         object newKey = null;
166                         
167                         foreach (DataRow row in this) {
168                                 
169                                 bool eq = true;
170                                 for (int i = 0; i < keys.Length; i++) {
171                                         
172                                         object primValue = row [primColumnNames [i]];
173                                         object keyValue = keys [i];
174                                         if (keyValue == null) {
175                                                 if (primValue == null)
176                                                         return row;
177                                                 else 
178                                                         continue;
179                                         }
180                                                                        
181                                         newKey = Convert.ChangeType (keyValue, Type.GetTypeCode(primValue.GetType ()));
182
183                                         if (!primValue.Equals (newKey)) {
184                                                 eq = false;
185                                                 break;
186                                         }                                               
187                                 }
188
189                                 if (eq)
190                                         return row;
191                         }
192                                                 
193                         // FIXME: is the correct value null?
194                         return null;
195                 }
196
197                 /// <summary>
198                 /// Inserts a new row into the collection at the specified location.
199                 /// </summary>
200                 public void InsertAt (DataRow row, int pos) 
201                 {
202                         if (pos < 0)
203                                 throw new IndexOutOfRangeException ("The row insert position " + pos + " is invalid.");
204                                 
205                         if (pos >= list.Count)
206                                 list.Add (row);
207                         else
208                                 list.Insert (pos, row);
209                 }
210
211                 /// <summary>
212                 /// Removes the specified DataRow from the collection.
213                 /// </summary>
214                 public void Remove (DataRow row) 
215                 {
216                         // FIXME: This is the way the MS.NET handles this kind of situation. Could be better, but what can you do.
217                         if (row == null || list.IndexOf (row) == -1)
218                                 throw new IndexOutOfRangeException ("The given datarow is not in the current DataRowCollection.");
219
220                         list.Remove (row);
221                         row.DetachRow ();
222                         table.DeletedDataRow (row, DataRowAction.Delete);
223                 }
224
225                 /// <summary>
226                 /// Removes the row at the specified index from the collection.
227                 /// </summary>
228                 public void RemoveAt (int index) 
229                 {                       
230                         if (index < 0 || index >= list.Count)
231                                 throw new IndexOutOfRangeException ("There is no row at position " + index + ".");
232
233                         DataRow row = (DataRow)list [index];
234                         list.RemoveAt (index);                  
235                         table.DeletedDataRow (row, DataRowAction.Delete);
236                 }
237
238                 ///<summary>
239                 ///Internal method used to validate a given DataRow with respect
240                 ///to the DataRowCollection
241                 ///</summary>
242                 [MonoTODO]
243                 internal void ValidateDataRowInternal(DataRow row)
244                 {
245                         //FIXME: this validates constraints in the order they appear
246                         //in the collection. Most probably we need to do it in a 
247                         //specific order like unique/primary keys first, then Foreignkeys, etc
248                         foreach(Constraint constraint in table.Constraints)
249                         {
250                                 constraint.AssertConstraint(row);
251                         }
252
253                 }
254
255         }
256 }