DataColumnMappingCollection.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. [MonoTODO]
  136. public void AddRange (Array values)
  137. {
  138. for (int i=0; i < values.Length; ++i)
  139. Add (values.GetValue (i));
  140. }
  141. #endif
  142. public void AddRange (DataColumnMapping[] values)
  143. {
  144. foreach (DataColumnMapping mapping in values)
  145. Add (mapping);
  146. }
  147. public void Clear ()
  148. {
  149. list.Clear ();
  150. }
  151. public bool Contains (object value)
  152. {
  153. if (!(value is DataColumnMapping))
  154. throw new InvalidCastException("Object is not of type DataColumnMapping");
  155. return (list.Contains (value));
  156. }
  157. public bool Contains (string value)
  158. {
  159. return (sourceColumns.Contains (value));
  160. }
  161. public void CopyTo (Array array, int index)
  162. {
  163. list.CopyTo(array,index);
  164. }
  165. #if NET_2_0
  166. public void CopyTo (DataColumnMapping[] arr, int index)
  167. {
  168. list.CopyTo (arr, index);
  169. }
  170. #endif
  171. public DataColumnMapping GetByDataSetColumn (string value)
  172. {
  173. // this should work case-insenstive.
  174. if (!(dataSetColumns[value] == null))
  175. return (DataColumnMapping)(dataSetColumns[value]);
  176. else {
  177. string lowcasevalue = value.ToLower();
  178. object [] keyarray = new object[dataSetColumns.Count];
  179. dataSetColumns.Keys.CopyTo(keyarray,0);
  180. for (int i=0; i<keyarray.Length; i++) {
  181. string temp = (string) keyarray[i];
  182. if (lowcasevalue.Equals(temp.ToLower()))
  183. return (DataColumnMapping)(dataSetColumns[keyarray[i]]);
  184. }
  185. return null;
  186. }
  187. }
  188. [EditorBrowsable (EditorBrowsableState.Advanced)]
  189. public static DataColumnMapping GetColumnMappingBySchemaAction (DataColumnMappingCollection columnMappings, string sourceColumn, MissingMappingAction mappingAction)
  190. {
  191. if (columnMappings.Contains (sourceColumn))
  192. return columnMappings[sourceColumn];
  193. if (mappingAction == MissingMappingAction.Ignore)
  194. return null;
  195. if (mappingAction == MissingMappingAction.Error)
  196. throw new InvalidOperationException (String.Format ("Missing SourceColumn mapping for '{0}'", sourceColumn));
  197. return new DataColumnMapping (sourceColumn, sourceColumn);
  198. }
  199. #if NET_2_0
  200. [MonoTODO]
  201. [EditorBrowsable (EditorBrowsableState.Advanced)]
  202. public static DataColumn GetDataColumn (DataColumnMappingCollection columnMappings, string sourceColumn, Type dataType, DataTable dataTable, MissingMappingAction mappingAction, MissingSchemaAction schemaAction)
  203. {
  204. throw new NotImplementedException ();
  205. }
  206. #endif
  207. public IEnumerator GetEnumerator ()
  208. {
  209. return list.GetEnumerator ();
  210. }
  211. IColumnMapping IColumnMappingCollection.Add (string sourceColumnName, string dataSetColumnName)
  212. {
  213. return Add (sourceColumnName, dataSetColumnName);
  214. }
  215. IColumnMapping IColumnMappingCollection.GetByDataSetColumn (string dataSetColumnName)
  216. {
  217. return GetByDataSetColumn (dataSetColumnName);
  218. }
  219. public int IndexOf (object value)
  220. {
  221. return list.IndexOf (value);
  222. }
  223. public int IndexOf (string sourceColumn)
  224. {
  225. return list.IndexOf (sourceColumns[sourceColumn]);
  226. }
  227. public int IndexOfDataSetColumn (string value)
  228. {
  229. // this should work case-insensitive
  230. if (!(dataSetColumns[value] == null))
  231. return list.IndexOf (dataSetColumns[value]);
  232. else {
  233. string lowcasevalue = value.ToLower();
  234. object [] keyarray = new object[dataSetColumns.Count];
  235. dataSetColumns.Keys.CopyTo(keyarray,0);
  236. for (int i=0; i<keyarray.Length; i++) {
  237. string temp = (string) keyarray[i];
  238. if (lowcasevalue.Equals(temp.ToLower()))
  239. return list.IndexOf (dataSetColumns[keyarray[i]]);
  240. }
  241. return -1;
  242. }
  243. }
  244. public void Insert (int index, object value)
  245. {
  246. list.Insert (index, value);
  247. sourceColumns[((DataColumnMapping)value).SourceColumn] = value;
  248. dataSetColumns[((DataColumnMapping)value).DataSetColumn] = value;
  249. }
  250. #if NET_2_0
  251. public void Insert (int index, DataColumnMapping mapping)
  252. {
  253. list.Insert (index, mapping);
  254. sourceColumns[mapping.SourceColumn] = mapping;
  255. dataSetColumns[mapping.DataSetColumn] = mapping;
  256. }
  257. #endif
  258. public void Remove (object value)
  259. {
  260. int index = list.IndexOf (value);
  261. sourceColumns.Remove(((DataColumnMapping)value).SourceColumn);
  262. dataSetColumns.Remove(((DataColumnMapping)value).DataSetColumn);
  263. if (( index < 0 ) || (index >=list.Count))
  264. throw new ArgumentException("There is no such element in collection.");
  265. list.Remove (value);
  266. }
  267. #if NET_2_0
  268. public void Remove (DataColumnMapping value)
  269. {
  270. int index = list.IndexOf (value);
  271. sourceColumns.Remove (value.SourceColumn);
  272. dataSetColumns.Remove (value.DataSetColumn);
  273. if (( index < 0 ) || (index >=list.Count))
  274. throw new ArgumentException("There is no such element in collection.");
  275. list.Remove (value);
  276. }
  277. #endif
  278. public void RemoveAt (int index)
  279. {
  280. if (( index < 0 ) || (index >=list.Count))
  281. throw new IndexOutOfRangeException("There is no element in collection.");
  282. Remove (list[index]);
  283. }
  284. public void RemoveAt (string sourceColumn)
  285. {
  286. RemoveAt (list.IndexOf (sourceColumns[sourceColumn]));
  287. }
  288. #endregion // Methods
  289. }
  290. }