DataColumnMappingCollection.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. //------------------------------------------------------------------------------
  2. // <copyright file="DataColumnMappingCollection.cs" company="Microsoft">
  3. // Copyright (c) Microsoft Corporation. All rights reserved.
  4. // </copyright>
  5. // <owner current="true" primary="true">Microsoft</owner>
  6. // <owner current="true" primary="false">Microsoft</owner>
  7. //------------------------------------------------------------------------------
  8. namespace System.Data.Common {
  9. using System;
  10. using System.Collections;
  11. using System.Collections.Generic;
  12. using System.ComponentModel;
  13. using System.Data;
  14. using System.Diagnostics;
  15. public sealed class DataColumnMappingCollection : MarshalByRefObject, IColumnMappingCollection {
  16. private List<DataColumnMapping> items; // delay creation until AddWithoutEvents, Insert, CopyTo, GetEnumerator
  17. public DataColumnMappingCollection() {
  18. }
  19. // explicit ICollection implementation
  20. bool System.Collections.ICollection.IsSynchronized {
  21. get { return false;}
  22. }
  23. object System.Collections.ICollection.SyncRoot {
  24. get { return this;}
  25. }
  26. // explicit IList implementation
  27. bool System.Collections.IList.IsReadOnly {
  28. get { return false;}
  29. }
  30. bool System.Collections.IList.IsFixedSize {
  31. get { return false;}
  32. }
  33. object System.Collections.IList.this[int index] {
  34. get {
  35. return this[index];
  36. }
  37. set {
  38. ValidateType(value);
  39. this[index] = (DataColumnMapping) value;
  40. }
  41. }
  42. // explicit IColumnMappingCollection implementation
  43. object IColumnMappingCollection.this[string index] {
  44. get {
  45. return this[index];
  46. }
  47. set {
  48. ValidateType(value);
  49. this[index] = (DataColumnMapping) value;
  50. }
  51. }
  52. IColumnMapping IColumnMappingCollection.Add(string sourceColumnName, string dataSetColumnName) {
  53. return Add(sourceColumnName, dataSetColumnName);
  54. }
  55. IColumnMapping IColumnMappingCollection.GetByDataSetColumn(string dataSetColumnName) {
  56. return GetByDataSetColumn(dataSetColumnName);
  57. }
  58. [
  59. Browsable(false),
  60. DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
  61. ResDescriptionAttribute(Res.DataColumnMappings_Count),
  62. ]
  63. public int Count {
  64. get {
  65. return ((null != items) ? items.Count : 0);
  66. }
  67. }
  68. private Type ItemType {
  69. get { return typeof(DataColumnMapping); }
  70. }
  71. [
  72. Browsable(false),
  73. DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
  74. ResDescriptionAttribute(Res.DataColumnMappings_Item),
  75. ]
  76. public DataColumnMapping this[int index] {
  77. get {
  78. RangeCheck(index);
  79. return items[index];
  80. }
  81. set {
  82. RangeCheck(index);
  83. Replace(index, value);
  84. }
  85. }
  86. [
  87. Browsable(false),
  88. DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
  89. ResDescriptionAttribute(Res.DataColumnMappings_Item),
  90. ]
  91. public DataColumnMapping this[string sourceColumn] {
  92. get {
  93. int index = RangeCheck(sourceColumn);
  94. return items[index];
  95. }
  96. set {
  97. int index = RangeCheck(sourceColumn);
  98. Replace(index, value);
  99. }
  100. }
  101. public int Add(object value) {
  102. ValidateType(value);
  103. Add((DataColumnMapping) value);
  104. return Count-1;
  105. }
  106. private DataColumnMapping Add(DataColumnMapping value) {
  107. AddWithoutEvents(value);
  108. return value;
  109. }
  110. public DataColumnMapping Add(string sourceColumn, string dataSetColumn) {
  111. return Add(new DataColumnMapping(sourceColumn, dataSetColumn));
  112. }
  113. public void AddRange(DataColumnMapping[] values) { // V1.0.3300
  114. AddEnumerableRange(values, false);
  115. }
  116. public void AddRange(System.Array values) { // V1.2.3300
  117. AddEnumerableRange(values, false);
  118. }
  119. /*/// <include file='doc\DataColumnMappingCollection.uex' path='docs/doc[@for="DataColumnMappingCollection.AddCloneOfRange"]/*' />
  120. public void AddCloneOfRange(IEnumerable values) {
  121. AddEnumerableRange(values, true);
  122. }*/
  123. private void AddEnumerableRange(IEnumerable values, bool doClone) {
  124. if (null == values) {
  125. throw ADP.ArgumentNull("values");
  126. }
  127. foreach(object value in values) {
  128. ValidateType(value);
  129. }
  130. if (doClone) {
  131. foreach(ICloneable value in values) {
  132. AddWithoutEvents(value.Clone() as DataColumnMapping);
  133. }
  134. }
  135. else {
  136. foreach(DataColumnMapping value in values) {
  137. AddWithoutEvents(value);
  138. }
  139. }
  140. }
  141. private void AddWithoutEvents(DataColumnMapping value) {
  142. Validate(-1, value);
  143. value.Parent = this;
  144. ArrayList().Add(value);
  145. }
  146. // implemented as a method, not as a property because the VS7 debugger
  147. // object browser calls properties to display their value, and we want this delayed
  148. private List<DataColumnMapping> ArrayList() {
  149. if (null == this.items) {
  150. this.items = new List<DataColumnMapping>();
  151. }
  152. return this.items;
  153. }
  154. public void Clear() {
  155. if (0 < Count) {
  156. ClearWithoutEvents();
  157. }
  158. }
  159. private void ClearWithoutEvents() {
  160. if (null != items) {
  161. foreach(DataColumnMapping item in items) {
  162. item.Parent = null;
  163. }
  164. items.Clear();
  165. }
  166. }
  167. public bool Contains(string value) {
  168. return(-1 != IndexOf(value));
  169. }
  170. public bool Contains(object value) {
  171. return(-1 != IndexOf(value));
  172. }
  173. public void CopyTo(Array array, int index) {
  174. ((ICollection)ArrayList()).CopyTo(array, index);
  175. }
  176. public void CopyTo(DataColumnMapping[] array, int index) {
  177. ArrayList().CopyTo(array, index);
  178. }
  179. public DataColumnMapping GetByDataSetColumn(string value) {
  180. int index = IndexOfDataSetColumn(value);
  181. if (0 > index) {
  182. throw ADP.ColumnsDataSetColumn(value);
  183. }
  184. return items[index];
  185. }
  186. public IEnumerator GetEnumerator() {
  187. return ArrayList().GetEnumerator();
  188. }
  189. public int IndexOf (object value) {
  190. if (null != value) {
  191. ValidateType(value);
  192. for (int i = 0; i < Count; ++i) {
  193. if (items[i] == value) {
  194. return i;
  195. }
  196. }
  197. }
  198. return -1;
  199. }
  200. public int IndexOf(string sourceColumn) {
  201. if (!ADP.IsEmpty(sourceColumn)) {
  202. int count = Count;
  203. for (int i = 0; i < count; ++i) {
  204. if (0 == ADP.SrcCompare(sourceColumn, items[i].SourceColumn)) {
  205. return i;
  206. }
  207. }
  208. }
  209. return -1;
  210. }
  211. public int IndexOfDataSetColumn(string dataSetColumn) {
  212. if (!ADP.IsEmpty(dataSetColumn)) {
  213. int count = Count;
  214. for (int i = 0; i < count; ++i) {
  215. if ( 0 == ADP.DstCompare(dataSetColumn, items[i].DataSetColumn)) {
  216. return i;
  217. }
  218. }
  219. }
  220. return -1;
  221. }
  222. public void Insert(int index, Object value) {
  223. ValidateType(value);
  224. Insert(index, (DataColumnMapping) value);
  225. }
  226. public void Insert(int index, DataColumnMapping value) {
  227. if (null == value) {
  228. throw ADP.ColumnsAddNullAttempt("value");
  229. }
  230. Validate(-1, value);
  231. value.Parent = this;
  232. ArrayList().Insert(index, value);
  233. }
  234. private void RangeCheck(int index) {
  235. if ((index < 0) || (Count <= index)) {
  236. throw ADP.ColumnsIndexInt32(index, this);
  237. }
  238. }
  239. private int RangeCheck(string sourceColumn) {
  240. int index = IndexOf(sourceColumn);
  241. if (index < 0) {
  242. throw ADP.ColumnsIndexSource(sourceColumn);
  243. }
  244. return index;
  245. }
  246. public void RemoveAt(int index) {
  247. RangeCheck(index);
  248. RemoveIndex(index);
  249. }
  250. public void RemoveAt(string sourceColumn) {
  251. int index = RangeCheck(sourceColumn);
  252. RemoveIndex(index);
  253. }
  254. private void RemoveIndex(int index) {
  255. Debug.Assert((null != items) && (0 <= index) && (index < Count), "RemoveIndex, invalid");
  256. items[index].Parent = null;
  257. items.RemoveAt(index);
  258. }
  259. public void Remove(object value) {
  260. ValidateType(value);
  261. Remove((DataColumnMapping) value);
  262. }
  263. public void Remove (DataColumnMapping value) {
  264. if (null == value) {
  265. throw ADP.ColumnsAddNullAttempt ("value");
  266. }
  267. int index = IndexOf (value);
  268. if (-1 != index) {
  269. RemoveIndex (index);
  270. }
  271. else {
  272. throw ADP.CollectionRemoveInvalidObject(ItemType, this);
  273. }
  274. }
  275. private void Replace(int index, DataColumnMapping newValue) {
  276. Debug.Assert((null != items) && (0 <= index) && (index < Count), "RemoveIndex, invalid");
  277. Validate(index, newValue);
  278. items[index].Parent = null;
  279. newValue.Parent = this;
  280. items[index] = newValue;
  281. }
  282. private void ValidateType(object value) {
  283. if (null == value) {
  284. throw ADP.ColumnsAddNullAttempt("value");
  285. }
  286. else if (!ItemType.IsInstanceOfType(value)) {
  287. throw ADP.NotADataColumnMapping(value);
  288. }
  289. }
  290. private void Validate(int index, DataColumnMapping value) {
  291. if (null == value) {
  292. throw ADP.ColumnsAddNullAttempt("value");
  293. }
  294. if (null != value.Parent) {
  295. if (this != value.Parent) {
  296. throw ADP.ColumnsIsNotParent(this);
  297. }
  298. else if (index != IndexOf(value)) {
  299. throw ADP.ColumnsIsParent(this);
  300. }
  301. }
  302. String name = value.SourceColumn;
  303. if (ADP.IsEmpty(name)) {
  304. index = 1;
  305. do {
  306. name = ADP.SourceColumn + index.ToString(System.Globalization.CultureInfo.InvariantCulture);
  307. index++;
  308. } while (-1 != IndexOf(name));
  309. value.SourceColumn = name;
  310. }
  311. else {
  312. ValidateSourceColumn(index, name);
  313. }
  314. }
  315. internal void ValidateSourceColumn(int index, string value) {
  316. int pindex = IndexOf(value);
  317. if ((-1 != pindex) && (index != pindex)) { // must be non-null and unique
  318. throw ADP.ColumnsUniqueSourceColumn(value);
  319. }
  320. }
  321. [ EditorBrowsableAttribute(EditorBrowsableState.Advanced) ] // MDAC 69508
  322. static public DataColumn GetDataColumn(DataColumnMappingCollection columnMappings, string sourceColumn, Type dataType, DataTable dataTable, MissingMappingAction mappingAction, MissingSchemaAction schemaAction) {
  323. if (null != columnMappings) {
  324. int index = columnMappings.IndexOf(sourceColumn);
  325. if (-1 != index) {
  326. #if DEBUG
  327. if (AdapterSwitches.DataSchema.TraceInfo) {
  328. Debug.WriteLine("mapping match on SourceColumn \"" + sourceColumn + "\"");
  329. }
  330. #endif
  331. return columnMappings.items[index].GetDataColumnBySchemaAction(dataTable, dataType, schemaAction);
  332. }
  333. }
  334. if (ADP.IsEmpty(sourceColumn)) {
  335. throw ADP.InvalidSourceColumn("sourceColumn");
  336. }
  337. switch (mappingAction) {
  338. case MissingMappingAction.Passthrough:
  339. #if DEBUG
  340. if (AdapterSwitches.DataSchema.TraceInfo) {
  341. Debug.WriteLine("mapping passthrough of SourceColumn \"" + sourceColumn + "\"");
  342. }
  343. #endif
  344. return DataColumnMapping.GetDataColumnBySchemaAction(sourceColumn, sourceColumn, dataTable, dataType, schemaAction);
  345. case MissingMappingAction.Ignore:
  346. #if DEBUG
  347. if (AdapterSwitches.DataSchema.TraceWarning) {
  348. Debug.WriteLine("mapping filter of SourceColumn \"" + sourceColumn + "\"");
  349. }
  350. #endif
  351. return null;
  352. case MissingMappingAction.Error:
  353. #if DEBUG
  354. if (AdapterSwitches.DataSchema.TraceError) {
  355. Debug.WriteLine("mapping error on SourceColumn \"" + sourceColumn + "\"");
  356. }
  357. #endif
  358. throw ADP.MissingColumnMapping(sourceColumn);
  359. }
  360. throw ADP.InvalidMissingMappingAction(mappingAction);
  361. }
  362. [ EditorBrowsableAttribute(EditorBrowsableState.Advanced) ] // MDAC 69508
  363. static public DataColumnMapping GetColumnMappingBySchemaAction(DataColumnMappingCollection columnMappings, string sourceColumn, MissingMappingAction mappingAction) {
  364. if (null != columnMappings) {
  365. int index = columnMappings.IndexOf(sourceColumn);
  366. if (-1 != index) {
  367. #if DEBUG
  368. if (AdapterSwitches.DataSchema.TraceInfo) {
  369. Debug.WriteLine("mapping match on SourceColumn \"" + sourceColumn + "\"");
  370. }
  371. #endif
  372. return columnMappings.items[index];
  373. }
  374. }
  375. if (ADP.IsEmpty(sourceColumn)) {
  376. throw ADP.InvalidSourceColumn("sourceColumn");
  377. }
  378. switch (mappingAction) {
  379. case MissingMappingAction.Passthrough:
  380. #if DEBUG
  381. if (AdapterSwitches.DataSchema.TraceInfo) {
  382. Debug.WriteLine("mapping passthrough of SourceColumn \"" + sourceColumn + "\"");
  383. }
  384. #endif
  385. return new DataColumnMapping(sourceColumn, sourceColumn);
  386. case MissingMappingAction.Ignore:
  387. #if DEBUG
  388. if (AdapterSwitches.DataSchema.TraceWarning) {
  389. Debug.WriteLine("mapping filter of SourceColumn \"" + sourceColumn + "\"");
  390. }
  391. #endif
  392. return null;
  393. case MissingMappingAction.Error:
  394. #if DEBUG
  395. if (AdapterSwitches.DataSchema.TraceError) {
  396. Debug.WriteLine("mapping error on SourceColumn \"" + sourceColumn + "\"");
  397. }
  398. #endif
  399. throw ADP.MissingColumnMapping(sourceColumn);
  400. }
  401. throw ADP.InvalidMissingMappingAction(mappingAction);
  402. }
  403. }
  404. }