DataColumnMappingCollection.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. [DataSysDescription ("The number of items in the collection")]
  56. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  57. public int Count {
  58. get { return list.Count; }
  59. }
  60. [Browsable (false)]
  61. [DataSysDescription ("The specified DataColumnMapping object.")]
  62. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  63. public DataColumnMapping this [int index] {
  64. get { return (DataColumnMapping)(list[index]); }
  65. set {
  66. DataColumnMapping mapping = (DataColumnMapping)(list[index]);
  67. sourceColumns[mapping] = value;
  68. dataSetColumns[mapping] = value;
  69. list[index] = value;
  70. }
  71. }
  72. [Browsable (false)]
  73. [DataSysDescription ("DataTableMappings_Item")]
  74. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  75. public DataColumnMapping this [string sourceColumn] {
  76. get {
  77. if (!Contains(sourceColumn)) {
  78. throw new IndexOutOfRangeException("DataColumnMappingCollection doesn't contain DataColumnMapping with SourceColumn '" + sourceColumn + "'.");
  79. }
  80. return (DataColumnMapping) sourceColumns[sourceColumn]; }
  81. set { this [list.IndexOf (sourceColumns[sourceColumn])] = value; }
  82. }
  83. object ICollection.SyncRoot {
  84. get { return list.SyncRoot; }
  85. }
  86. bool ICollection.IsSynchronized {
  87. get { return list.IsSynchronized; }
  88. }
  89. object IColumnMappingCollection.this [string sourceColumn] {
  90. get { return this [sourceColumn]; }
  91. set {
  92. if (!(value is DataColumnMapping))
  93. throw new ArgumentException ();
  94. this [sourceColumn] = (DataColumnMapping) value;
  95. }
  96. }
  97. object IList.this [int index] {
  98. get { return this[index]; }
  99. set {
  100. if (!(value is DataColumnMapping))
  101. throw new ArgumentException ();
  102. this [index] = (DataColumnMapping) value;
  103. }
  104. }
  105. bool IList.IsReadOnly {
  106. get { return false; }
  107. }
  108. bool IList.IsFixedSize {
  109. get { return false; }
  110. }
  111. #endregion // Properties
  112. #region Methods
  113. public int Add (object value)
  114. {
  115. if (!(value is DataColumnMapping))
  116. throw new InvalidCastException ();
  117. list.Add (value);
  118. sourceColumns[((DataColumnMapping)value).SourceColumn] = value;
  119. dataSetColumns[((DataColumnMapping)value).DataSetColumn] = value;
  120. return list.IndexOf (value);
  121. }
  122. public DataColumnMapping Add (string sourceColumn, string dataSetColumn)
  123. {
  124. DataColumnMapping mapping = new DataColumnMapping (sourceColumn, dataSetColumn);
  125. Add (mapping);
  126. return mapping;
  127. }
  128. #if NET_2_0
  129. [MonoTODO]
  130. public void AddRange (Array values)
  131. {
  132. throw new NotImplementedException ();
  133. }
  134. #endif
  135. public void AddRange (DataColumnMapping[] values)
  136. {
  137. foreach (DataColumnMapping mapping in values)
  138. Add (mapping);
  139. }
  140. public void Clear ()
  141. {
  142. list.Clear ();
  143. }
  144. public bool Contains (object value)
  145. {
  146. if (!(value is DataColumnMapping))
  147. throw new InvalidCastException("Object is not of type DataColumnMapping");
  148. return (list.Contains (value));
  149. }
  150. public bool Contains (string value)
  151. {
  152. return (sourceColumns.Contains (value));
  153. }
  154. public void CopyTo (Array array, int index)
  155. {
  156. (list.ToArray()).CopyTo(array,index);
  157. }
  158. public DataColumnMapping GetByDataSetColumn (string value)
  159. {
  160. // this should work case-insenstive.
  161. if (!(dataSetColumns[value] == null))
  162. return (DataColumnMapping)(dataSetColumns[value]);
  163. else {
  164. string lowcasevalue = value.ToLower();
  165. object [] keyarray = new object[dataSetColumns.Count];
  166. dataSetColumns.Keys.CopyTo(keyarray,0);
  167. for (int i=0; i<keyarray.Length; i++) {
  168. string temp = (string) keyarray[i];
  169. if (lowcasevalue.Equals(temp.ToLower()))
  170. return (DataColumnMapping)(dataSetColumns[keyarray[i]]);
  171. }
  172. return null;
  173. }
  174. }
  175. [EditorBrowsable (EditorBrowsableState.Advanced)]
  176. public static DataColumnMapping GetColumnMappingBySchemaAction (DataColumnMappingCollection columnMappings, string sourceColumn, MissingMappingAction mappingAction)
  177. {
  178. if (columnMappings.Contains (sourceColumn))
  179. return columnMappings[sourceColumn];
  180. if (mappingAction == MissingMappingAction.Ignore)
  181. return null;
  182. if (mappingAction == MissingMappingAction.Error)
  183. throw new InvalidOperationException (String.Format ("Missing SourceColumn mapping for '{0}'", sourceColumn));
  184. return new DataColumnMapping (sourceColumn, sourceColumn);
  185. }
  186. #if NET_2_0
  187. [MonoTODO]
  188. public static DataColumn GetDataColumn (DataColumnMappingCollection columnMappings, string sourceColumn, Type dataType, DataTable dataTable, MissingMappingAction mappingAction, MissingSchemaAction schemaAction)
  189. {
  190. throw new NotImplementedException ();
  191. }
  192. #endif
  193. public IEnumerator GetEnumerator ()
  194. {
  195. return list.GetEnumerator ();
  196. }
  197. IColumnMapping IColumnMappingCollection.Add (string sourceColumnName, string dataSetColumnName)
  198. {
  199. return Add (sourceColumnName, dataSetColumnName);
  200. }
  201. IColumnMapping IColumnMappingCollection.GetByDataSetColumn (string dataSetColumnName)
  202. {
  203. return GetByDataSetColumn (dataSetColumnName);
  204. }
  205. public int IndexOf (object value)
  206. {
  207. return list.IndexOf (value);
  208. }
  209. public int IndexOf (string sourceColumn)
  210. {
  211. return list.IndexOf (sourceColumns[sourceColumn]);
  212. }
  213. public int IndexOfDataSetColumn (string value)
  214. {
  215. // this should work case-insensitive
  216. if (!(dataSetColumns[value] == null))
  217. return list.IndexOf (dataSetColumns[value]);
  218. else {
  219. string lowcasevalue = value.ToLower();
  220. object [] keyarray = new object[dataSetColumns.Count];
  221. dataSetColumns.Keys.CopyTo(keyarray,0);
  222. for (int i=0; i<keyarray.Length; i++) {
  223. string temp = (string) keyarray[i];
  224. if (lowcasevalue.Equals(temp.ToLower()))
  225. return list.IndexOf (dataSetColumns[keyarray[i]]);
  226. }
  227. return -1;
  228. }
  229. }
  230. public void Insert (int index, object value)
  231. {
  232. list.Insert (index, value);
  233. sourceColumns[((DataColumnMapping)value).SourceColumn] = value;
  234. dataSetColumns[((DataColumnMapping)value).DataSetColumn] = value;
  235. }
  236. public void Remove (object value)
  237. {
  238. int index = list.IndexOf (value);
  239. sourceColumns.Remove(((DataColumnMapping)value).SourceColumn);
  240. dataSetColumns.Remove(((DataColumnMapping)value).DataSetColumn);
  241. if (( index < 0 ) || (index >=list.Count))
  242. throw new ArgumentException("There is no such element in collection.");
  243. list.Remove (value);
  244. }
  245. public void RemoveAt (int index)
  246. {
  247. if (( index < 0 ) || (index >=list.Count))
  248. throw new IndexOutOfRangeException("There is no element in collection.");
  249. Remove (list[index]);
  250. }
  251. public void RemoveAt (string sourceColumn)
  252. {
  253. RemoveAt (list.IndexOf (sourceColumns[sourceColumn]));
  254. }
  255. #endregion // Methods
  256. }
  257. }