PageRenderTime 47ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/System.Data/System.Data.Common/DataColumnMappingCollection.cs

https://bitbucket.org/danipen/mono
C# | 338 lines | 258 code | 46 blank | 34 comment | 27 complexity | a4fe4a2e8559d11efec06268c5ac7167 MD5 | raw file
Possible License(s): Unlicense, Apache-2.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0
  1. //
  2. // System.Data.Common.DataColumnMappingCollection
  3. //
  4. // Authors:
  5. // Rodrigo Moya (rodrigo@ximian.com)
  6. // Tim Coleman (tim@timcoleman.com)
  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. {
  39. public sealed class DataColumnMappingCollection : MarshalByRefObject, IColumnMappingCollection , IList, ICollection, IEnumerable
  40. {
  41. #region Fields
  42. readonly ArrayList list;
  43. readonly Hashtable sourceColumns;
  44. readonly Hashtable dataSetColumns;
  45. #endregion // Fields
  46. #region Constructors
  47. public DataColumnMappingCollection ()
  48. {
  49. list = new ArrayList ();
  50. sourceColumns = new Hashtable ();
  51. dataSetColumns = new Hashtable ();
  52. }
  53. #endregion // Constructors
  54. #region Properties
  55. [Browsable (false)]
  56. #if !NET_2_0
  57. [DataSysDescription ("The number of items in the collection")]
  58. #endif
  59. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  60. public int Count {
  61. get { return list.Count; }
  62. }
  63. [Browsable (false)]
  64. #if !NET_2_0
  65. [DataSysDescription ("The specified DataColumnMapping object.")]
  66. #endif
  67. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  68. public DataColumnMapping this [int index] {
  69. get { return (DataColumnMapping)(list[index]); }
  70. set {
  71. DataColumnMapping mapping = (DataColumnMapping)(list[index]);
  72. sourceColumns[mapping] = value;
  73. dataSetColumns[mapping] = value;
  74. list[index] = value;
  75. }
  76. }
  77. [Browsable (false)]
  78. #if !NET_2_0
  79. [DataSysDescription ("The specified DataColumnMapping object.")]
  80. #endif
  81. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  82. public DataColumnMapping this [string sourceColumn] {
  83. get {
  84. if (!Contains(sourceColumn))
  85. throw new IndexOutOfRangeException("DataColumnMappingCollection doesn't contain DataColumnMapping with SourceColumn '" + sourceColumn + "'.");
  86. return (DataColumnMapping) sourceColumns [sourceColumn];
  87. }
  88. set {
  89. this [list.IndexOf (sourceColumns [sourceColumn])] = value;
  90. }
  91. }
  92. object ICollection.SyncRoot {
  93. get { return list.SyncRoot; }
  94. }
  95. bool ICollection.IsSynchronized {
  96. get { return list.IsSynchronized; }
  97. }
  98. object IColumnMappingCollection.this [string index] {
  99. get { return this [index]; }
  100. set {
  101. if (!(value is DataColumnMapping))
  102. throw new ArgumentException ();
  103. this [index] = (DataColumnMapping) value;
  104. }
  105. }
  106. object IList.this [int index] {
  107. get { return this [index]; }
  108. set {
  109. if (!(value is DataColumnMapping))
  110. throw new ArgumentException ();
  111. this [index] = (DataColumnMapping) value;
  112. }
  113. }
  114. bool IList.IsReadOnly {
  115. get { return false; }
  116. }
  117. bool IList.IsFixedSize {
  118. get { return false; }
  119. }
  120. #endregion // Properties
  121. #region Methods
  122. public int Add (object value)
  123. {
  124. if (!(value is DataColumnMapping))
  125. throw new InvalidCastException ();
  126. list.Add (value);
  127. sourceColumns [((DataColumnMapping) value).SourceColumn] = value;
  128. dataSetColumns [((DataColumnMapping )value).DataSetColumn] = value;
  129. return list.IndexOf (value);
  130. }
  131. public DataColumnMapping Add (string sourceColumn, string dataSetColumn)
  132. {
  133. DataColumnMapping mapping = new DataColumnMapping (sourceColumn, dataSetColumn);
  134. Add (mapping);
  135. return mapping;
  136. }
  137. #if NET_2_0
  138. public void AddRange (Array values)
  139. {
  140. for (int i = 0; i < values.Length; ++i)
  141. Add (values.GetValue (i));
  142. }
  143. #endif
  144. public void AddRange (DataColumnMapping[] values)
  145. {
  146. foreach (DataColumnMapping mapping in values)
  147. Add (mapping);
  148. }
  149. public void Clear ()
  150. {
  151. list.Clear ();
  152. }
  153. public bool Contains (object value)
  154. {
  155. if (!(value is DataColumnMapping))
  156. throw new InvalidCastException("Object is not of type DataColumnMapping");
  157. return (list.Contains (value));
  158. }
  159. public bool Contains (string value)
  160. {
  161. return (sourceColumns.Contains (value));
  162. }
  163. public void CopyTo (Array array, int index)
  164. {
  165. list.CopyTo (array,index);
  166. }
  167. #if NET_2_0
  168. public void CopyTo (DataColumnMapping [] array, int index)
  169. {
  170. list.CopyTo (array, index);
  171. }
  172. #endif
  173. public DataColumnMapping GetByDataSetColumn (string value)
  174. {
  175. // this should work case-insenstive.
  176. if (!(dataSetColumns [value] == null))
  177. return (DataColumnMapping) (dataSetColumns [value]);
  178. else {
  179. string lowcasevalue = value.ToLower ();
  180. object [] keyarray = new object [dataSetColumns.Count];
  181. dataSetColumns.Keys.CopyTo (keyarray, 0);
  182. for (int i = 0; i < keyarray.Length; i++) {
  183. string temp = (string) keyarray [i];
  184. if (lowcasevalue.Equals (temp.ToLower ()))
  185. return (DataColumnMapping) (dataSetColumns [keyarray [i]]);
  186. }
  187. return null;
  188. }
  189. }
  190. [EditorBrowsable (EditorBrowsableState.Advanced)]
  191. public static DataColumnMapping GetColumnMappingBySchemaAction (DataColumnMappingCollection columnMappings, string sourceColumn, MissingMappingAction mappingAction)
  192. {
  193. if (columnMappings.Contains (sourceColumn))
  194. return columnMappings[sourceColumn];
  195. if (mappingAction == MissingMappingAction.Ignore)
  196. return null;
  197. if (mappingAction == MissingMappingAction.Error)
  198. throw new InvalidOperationException (String.Format ("Missing SourceColumn mapping for '{0}'", sourceColumn));
  199. return new DataColumnMapping (sourceColumn, sourceColumn);
  200. }
  201. #if NET_2_0
  202. [MonoTODO]
  203. [EditorBrowsable (EditorBrowsableState.Advanced)]
  204. public static DataColumn GetDataColumn (DataColumnMappingCollection columnMappings, string sourceColumn, Type dataType, DataTable dataTable, MissingMappingAction mappingAction, MissingSchemaAction schemaAction)
  205. {
  206. throw new NotImplementedException ();
  207. }
  208. #endif
  209. public IEnumerator GetEnumerator ()
  210. {
  211. return list.GetEnumerator ();
  212. }
  213. IColumnMapping IColumnMappingCollection.Add (string sourceColumnName, string dataSetColumnName)
  214. {
  215. return Add (sourceColumnName, dataSetColumnName);
  216. }
  217. IColumnMapping IColumnMappingCollection.GetByDataSetColumn (string dataSetColumnName)
  218. {
  219. return GetByDataSetColumn (dataSetColumnName);
  220. }
  221. public int IndexOf (object value)
  222. {
  223. return list.IndexOf (value);
  224. }
  225. public int IndexOf (string sourceColumn)
  226. {
  227. return list.IndexOf (sourceColumns [sourceColumn]);
  228. }
  229. public int IndexOfDataSetColumn (string dataSetColumn)
  230. {
  231. // this should work case-insensitive
  232. if (!(dataSetColumns [dataSetColumn] == null))
  233. return list.IndexOf (dataSetColumns [dataSetColumn]);
  234. else {
  235. string lowcasevalue = dataSetColumn.ToLower ();
  236. object [] keyarray = new object[dataSetColumns.Count];
  237. dataSetColumns.Keys.CopyTo (keyarray,0);
  238. for (int i = 0; i < keyarray.Length; i++) {
  239. string temp = (string) keyarray [i];
  240. if (lowcasevalue.Equals (temp.ToLower ()))
  241. return list.IndexOf (dataSetColumns [keyarray [i]]);
  242. }
  243. return -1;
  244. }
  245. }
  246. public void Insert (int index, object value)
  247. {
  248. list.Insert (index, value);
  249. sourceColumns [((DataColumnMapping) value).SourceColumn] = value;
  250. dataSetColumns [((DataColumnMapping) value).DataSetColumn] = value;
  251. }
  252. #if NET_2_0
  253. public void Insert (int index, DataColumnMapping value)
  254. {
  255. list.Insert (index, value);
  256. sourceColumns [value.SourceColumn] = value;
  257. dataSetColumns [value.DataSetColumn] = value;
  258. }
  259. #endif
  260. public void Remove (object value)
  261. {
  262. int index = list.IndexOf (value);
  263. sourceColumns.Remove (((DataColumnMapping) value).SourceColumn);
  264. dataSetColumns.Remove (((DataColumnMapping) value).DataSetColumn);
  265. if (index < 0 || index >=list.Count)
  266. throw new ArgumentException("There is no such element in collection.");
  267. list.Remove (value);
  268. }
  269. #if NET_2_0
  270. public void Remove (DataColumnMapping value)
  271. {
  272. int index = list.IndexOf (value);
  273. sourceColumns.Remove (value.SourceColumn);
  274. dataSetColumns.Remove (value.DataSetColumn);
  275. if ( index < 0 || index >=list.Count)
  276. throw new ArgumentException("There is no such element in collection.");
  277. list.Remove (value);
  278. }
  279. #endif
  280. public void RemoveAt (int index)
  281. {
  282. if (index < 0 || index >=list.Count)
  283. throw new IndexOutOfRangeException("There is no element in collection.");
  284. Remove (list [index]);
  285. }
  286. public void RemoveAt (string sourceColumn)
  287. {
  288. RemoveAt (list.IndexOf (sourceColumns [sourceColumn]));
  289. }
  290. #endregion // Methods
  291. }
  292. }