DataColumnMappingCollection.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. //
  2. // System.Data.Common.DataColumnMappingCollection
  3. //
  4. // Authors:
  5. // Rodrigo Moya ([email protected])
  6. // Tim Coleman ([email protected])
  7. //
  8. // (C) Ximian, Inc
  9. // Copyright (C) Tim Coleman, 2002-2003
  10. //
  11. //
  12. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  13. //
  14. // Permission is hereby granted, free of charge, to any person obtaining
  15. // a copy of this software and associated documentation files (the
  16. // "Software"), to deal in the Software without restriction, including
  17. // without limitation the rights to use, copy, modify, merge, publish,
  18. // distribute, sublicense, and/or sell copies of the Software, and to
  19. // permit persons to whom the Software is furnished to do so, subject to
  20. // the following conditions:
  21. //
  22. // The above copyright notice and this permission notice shall be
  23. // included in all copies or substantial portions of the Software.
  24. //
  25. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  29. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  30. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  31. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  32. //
  33. using System;
  34. using System.Collections;
  35. using System.ComponentModel;
  36. using System.Data;
  37. namespace System.Data.Common {
  38. public sealed class DataColumnMappingCollection : MarshalByRefObject, IColumnMappingCollection , IList, ICollection, IEnumerable
  39. {
  40. #region Fields
  41. ArrayList list;
  42. Hashtable sourceColumns;
  43. Hashtable dataSetColumns;
  44. #endregion // Fields
  45. #region Constructors
  46. public DataColumnMappingCollection ()
  47. {
  48. list = new ArrayList ();
  49. sourceColumns = new Hashtable ();
  50. dataSetColumns = new Hashtable ();
  51. }
  52. #endregion // Constructors
  53. #region Properties
  54. [Browsable (false)]
  55. #if !NET_2_0
  56. [DataSysDescription ("The number of items in the collection")]
  57. #endif
  58. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  59. public int Count {
  60. get { return list.Count; }
  61. }
  62. [Browsable (false)]
  63. #if !NET_2_0
  64. [DataSysDescription ("The specified DataColumnMapping object.")]
  65. #endif
  66. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  67. public DataColumnMapping this [int index] {
  68. get { return (DataColumnMapping)(list[index]); }
  69. set {
  70. DataColumnMapping mapping = (DataColumnMapping)(list[index]);
  71. sourceColumns[mapping] = value;
  72. dataSetColumns[mapping] = value;
  73. list[index] = value;
  74. }
  75. }
  76. [Browsable (false)]
  77. #if !NET_2_0
  78. [DataSysDescription ("The specified DataColumnMapping object.")]
  79. #endif
  80. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  81. public DataColumnMapping this [string sourceColumn] {
  82. get {
  83. if (!Contains(sourceColumn)) {
  84. throw new IndexOutOfRangeException("DataColumnMappingCollection doesn't contain DataColumnMapping with SourceColumn '" + sourceColumn + "'.");
  85. }
  86. return (DataColumnMapping) sourceColumns[sourceColumn]; }
  87. set { this [list.IndexOf (sourceColumns[sourceColumn])] = value; }
  88. }
  89. object ICollection.SyncRoot {
  90. get { return list.SyncRoot; }
  91. }
  92. bool ICollection.IsSynchronized {
  93. get { return list.IsSynchronized; }
  94. }
  95. object IColumnMappingCollection.this [string sourceColumn] {
  96. get { return this [sourceColumn]; }
  97. set {
  98. if (!(value is DataColumnMapping))
  99. throw new ArgumentException ();
  100. this [sourceColumn] = (DataColumnMapping) value;
  101. }
  102. }
  103. object IList.this [int index] {
  104. get { return this[index]; }
  105. set {
  106. if (!(value is DataColumnMapping))
  107. throw new ArgumentException ();
  108. this [index] = (DataColumnMapping) value;
  109. }
  110. }
  111. bool IList.IsReadOnly {
  112. get { return false; }
  113. }
  114. bool IList.IsFixedSize {
  115. get { return false; }
  116. }
  117. #endregion // Properties
  118. #region Methods
  119. public int Add (object value)
  120. {
  121. if (!(value is DataColumnMapping))
  122. throw new InvalidCastException ();
  123. list.Add (value);
  124. sourceColumns[((DataColumnMapping)value).SourceColumn] = value;
  125. dataSetColumns[((DataColumnMapping)value).DataSetColumn] = value;
  126. return list.IndexOf (value);
  127. }
  128. public DataColumnMapping Add (string sourceColumn, string dataSetColumn)
  129. {
  130. DataColumnMapping mapping = new DataColumnMapping (sourceColumn, dataSetColumn);
  131. Add (mapping);
  132. return mapping;
  133. }
  134. #if NET_2_0
  135. public void AddRange (Array values)
  136. {
  137. for (int i = 0; i < values.Length; ++i)
  138. Add (values.GetValue (i));
  139. }
  140. #endif
  141. public void AddRange (DataColumnMapping[] values)
  142. {
  143. foreach (DataColumnMapping mapping in values)
  144. Add (mapping);
  145. }
  146. public void Clear ()
  147. {
  148. list.Clear ();
  149. }
  150. public bool Contains (object value)
  151. {
  152. if (!(value is DataColumnMapping))
  153. throw new InvalidCastException("Object is not of type DataColumnMapping");
  154. return (list.Contains (value));
  155. }
  156. public bool Contains (string value)
  157. {
  158. return (sourceColumns.Contains (value));
  159. }
  160. public void CopyTo (Array array, int index)
  161. {
  162. list.CopyTo(array,index);
  163. }
  164. #if NET_2_0
  165. public void CopyTo (DataColumnMapping[] arr, int index)
  166. {
  167. list.CopyTo (arr, index);
  168. }
  169. #endif
  170. public DataColumnMapping GetByDataSetColumn (string value)
  171. {
  172. // this should work case-insenstive.
  173. if (!(dataSetColumns[value] == null))
  174. return (DataColumnMapping)(dataSetColumns[value]);
  175. else {
  176. string lowcasevalue = value.ToLower();
  177. object [] keyarray = new object[dataSetColumns.Count];
  178. dataSetColumns.Keys.CopyTo(keyarray,0);
  179. for (int i=0; i<keyarray.Length; i++) {
  180. string temp = (string) keyarray[i];
  181. if (lowcasevalue.Equals(temp.ToLower()))
  182. return (DataColumnMapping)(dataSetColumns[keyarray[i]]);
  183. }
  184. return null;
  185. }
  186. }
  187. [EditorBrowsable (EditorBrowsableState.Advanced)]
  188. public static DataColumnMapping GetColumnMappingBySchemaAction (DataColumnMappingCollection columnMappings, string sourceColumn, MissingMappingAction mappingAction)
  189. {
  190. if (columnMappings.Contains (sourceColumn))
  191. return columnMappings[sourceColumn];
  192. if (mappingAction == MissingMappingAction.Ignore)
  193. return null;
  194. if (mappingAction == MissingMappingAction.Error)
  195. throw new InvalidOperationException (String.Format ("Missing SourceColumn mapping for '{0}'", sourceColumn));
  196. return new DataColumnMapping (sourceColumn, sourceColumn);
  197. }
  198. #if NET_2_0
  199. [MonoTODO]
  200. [EditorBrowsable (EditorBrowsableState.Advanced)]
  201. public static DataColumn GetDataColumn (DataColumnMappingCollection columnMappings, string sourceColumn, Type dataType, DataTable dataTable, MissingMappingAction mappingAction, MissingSchemaAction schemaAction)
  202. {
  203. throw new NotImplementedException ();
  204. }
  205. #endif
  206. public IEnumerator GetEnumerator ()
  207. {
  208. return list.GetEnumerator ();
  209. }
  210. IColumnMapping IColumnMappingCollection.Add (string sourceColumnName, string dataSetColumnName)
  211. {
  212. return Add (sourceColumnName, dataSetColumnName);
  213. }
  214. IColumnMapping IColumnMappingCollection.GetByDataSetColumn (string dataSetColumnName)
  215. {
  216. return GetByDataSetColumn (dataSetColumnName);
  217. }
  218. public int IndexOf (object value)
  219. {
  220. return list.IndexOf (value);
  221. }
  222. public int IndexOf (string sourceColumn)
  223. {
  224. return list.IndexOf (sourceColumns[sourceColumn]);
  225. }
  226. public int IndexOfDataSetColumn (string value)
  227. {
  228. // this should work case-insensitive
  229. if (!(dataSetColumns[value] == null))
  230. return list.IndexOf (dataSetColumns[value]);
  231. else {
  232. string lowcasevalue = value.ToLower();
  233. object [] keyarray = new object[dataSetColumns.Count];
  234. dataSetColumns.Keys.CopyTo(keyarray,0);
  235. for (int i=0; i<keyarray.Length; i++) {
  236. string temp = (string) keyarray[i];
  237. if (lowcasevalue.Equals(temp.ToLower()))
  238. return list.IndexOf (dataSetColumns[keyarray[i]]);
  239. }
  240. return -1;
  241. }
  242. }
  243. public void Insert (int index, object value)
  244. {
  245. list.Insert (index, value);
  246. sourceColumns[((DataColumnMapping)value).SourceColumn] = value;
  247. dataSetColumns[((DataColumnMapping)value).DataSetColumn] = value;
  248. }
  249. #if NET_2_0
  250. public void Insert (int index, DataColumnMapping mapping)
  251. {
  252. list.Insert (index, mapping);
  253. sourceColumns[mapping.SourceColumn] = mapping;
  254. dataSetColumns[mapping.DataSetColumn] = mapping;
  255. }
  256. #endif
  257. public void Remove (object value)
  258. {
  259. int index = list.IndexOf (value);
  260. sourceColumns.Remove(((DataColumnMapping)value).SourceColumn);
  261. dataSetColumns.Remove(((DataColumnMapping)value).DataSetColumn);
  262. if (( index < 0 ) || (index >=list.Count))
  263. throw new ArgumentException("There is no such element in collection.");
  264. list.Remove (value);
  265. }
  266. #if NET_2_0
  267. public void Remove (DataColumnMapping value)
  268. {
  269. int index = list.IndexOf (value);
  270. sourceColumns.Remove (value.SourceColumn);
  271. dataSetColumns.Remove (value.DataSetColumn);
  272. if (( index < 0 ) || (index >=list.Count))
  273. throw new ArgumentException("There is no such element in collection.");
  274. list.Remove (value);
  275. }
  276. #endif
  277. public void RemoveAt (int index)
  278. {
  279. if (( index < 0 ) || (index >=list.Count))
  280. throw new IndexOutOfRangeException("There is no element in collection.");
  281. Remove (list[index]);
  282. }
  283. public void RemoveAt (string sourceColumn)
  284. {
  285. RemoveAt (list.IndexOf (sourceColumns[sourceColumn]));
  286. }
  287. #endregion // Methods
  288. }
  289. }