0
|
1 using System;
|
|
2 using System.Collections.Generic;
|
|
3 using BLToolkit.Emit;
|
|
4
|
|
5 namespace BLToolkit.Mapping
|
|
6 {
|
|
7 public class ValueMapper : TableDescription, IMapper
|
|
8 {
|
|
9 private readonly Dictionary<string, List<string>> _columnVariations = new Dictionary<string, List<string>>();
|
|
10 private readonly Dictionary<string, int> _columnOccurences = new Dictionary<string, int>();
|
|
11
|
|
12 public string ColumnAlias { get; set; }
|
|
13 public string ColumnName { get; set; }
|
|
14
|
|
15 #region IMapper Members
|
|
16
|
|
17 public int DataReaderIndex { get; set; }
|
|
18 public SetHandler Setter { get; set; }
|
|
19 public Type PropertyType { get; set; }
|
|
20 public string PropertyName { get; set; }
|
|
21
|
|
22 #endregion
|
|
23
|
|
24 public string GetColumnName(string columnName)
|
|
25 {
|
|
26 int occurenceCount;
|
|
27 if (_columnOccurences.ContainsKey(columnName))
|
|
28 {
|
|
29 occurenceCount = _columnOccurences[columnName] + 1;
|
|
30 _columnOccurences[columnName] = occurenceCount;
|
|
31 }
|
|
32 else
|
|
33 {
|
|
34 _columnOccurences[columnName] = 1;
|
|
35 occurenceCount = 1;
|
|
36 }
|
|
37
|
|
38 string res = columnName + (occurenceCount > 1 ? string.Format("_{0}", occurenceCount - 1) : "");
|
|
39
|
|
40 var variations = new List<string>();
|
|
41 if (_columnVariations.ContainsKey(columnName))
|
|
42 {
|
|
43 variations = _columnVariations[columnName];
|
|
44 }
|
|
45
|
|
46 variations.Add(res);
|
|
47 _columnVariations[columnName] = variations;
|
|
48
|
|
49 return res;
|
|
50 }
|
|
51
|
|
52 public bool SetDataReaderIndex(List<string> schemaColumns)
|
|
53 {
|
|
54 string colName = ColumnName;
|
|
55 int index = -1;
|
|
56 if (!schemaColumns.Contains(colName))
|
|
57 {
|
|
58 bool found = false;
|
|
59 int order = 1;
|
|
60 foreach (string key in _columnVariations.Keys)
|
|
61 {
|
|
62 List<string> variations = _columnVariations[key];
|
|
63 if (variations.Contains(colName))
|
|
64 {
|
|
65 if (colName.Contains(key + "_"))
|
|
66 {
|
|
67 string orderString = colName.Replace(key + "_", "");
|
|
68 order = int.Parse(orderString) + 1;
|
|
69 colName = key;
|
|
70 found = true;
|
|
71 break;
|
|
72 }
|
|
73 }
|
|
74 }
|
|
75 if (found)
|
|
76 {
|
|
77 int i = 0, occurenceCnt = 0;
|
|
78 foreach (string column in schemaColumns)
|
|
79 {
|
|
80 if (column == colName)
|
|
81 {
|
|
82 occurenceCnt++;
|
|
83 if (occurenceCnt == order)
|
|
84 {
|
|
85 index = i;
|
|
86 break;
|
|
87 }
|
|
88 }
|
|
89 i++;
|
|
90 }
|
|
91 }
|
|
92 else
|
|
93 {
|
|
94 // TODO Check this condition...
|
|
95 //if (!_ignoreMissingColumns)
|
|
96 //{
|
|
97 // throw new Exception(string.Format("Couldnt find db column {0} in the query result", colName));
|
|
98 //}
|
|
99 return true;
|
|
100 }
|
|
101 }
|
|
102 else
|
|
103 index = schemaColumns.IndexOf(colName);
|
|
104
|
|
105 DataReaderIndex = index;
|
|
106 return false;
|
|
107 }
|
|
108 }
|
|
109 } |