comparison Source/Mapping/DataRowMapper.cs @ 0:f990fcb411a9

Копия текущей версии из github
author cin
date Thu, 27 Mar 2014 21:46:09 +0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f990fcb411a9
1 using System;
2 using System.Data;
3 using System.Collections;
4
5 namespace BLToolkit.Mapping
6 {
7 public class DataRowMapper : MapDataSourceDestinationBase
8 {
9 bool _createColumns;
10 readonly DataRowVersion _version;
11
12 public DataRowMapper(DataRow dataRow)
13 : this(dataRow, DataRowVersion.Default)
14 {
15 }
16
17 public DataRowMapper(DataRowView view)
18 : this(view.Row, view.RowVersion)
19 {
20 }
21
22 public DataRowMapper(DataRow dataRow, DataRowVersion version)
23 {
24 _version = version;
25
26 Init(dataRow);
27 }
28
29 private void Init(DataRow dataRow)
30 {
31 if (_dataRow == null && dataRow != null)
32 _createColumns = dataRow.Table.Columns.Count == 0;
33
34 _dataRow = dataRow;
35 }
36
37 private DataRow _dataRow;
38 public DataRow DataRow
39 {
40 get { return _dataRow; }
41 set { Init(value); }
42 }
43
44 #region IMapDataSource Members
45
46 public override int Count
47 {
48 get { return _dataRow.Table.Columns.Count; }
49 }
50
51 public override Type GetFieldType(int index)
52 {
53 return index < _dataRow.Table.Columns.Count?
54 _dataRow.Table.Columns[index].DataType: null;
55 }
56
57 public override string GetName(int index)
58 {
59 return _dataRow.Table.Columns[index].ColumnName;
60 }
61
62 public override object GetValue(object o, int index)
63 {
64 object value = _version == DataRowVersion.Default ? _dataRow[index] : _dataRow[index, _version];
65 return value is DBNull? null: value;
66 }
67
68 public override object GetValue(object o, string name)
69 {
70 object value = _version == DataRowVersion.Default ? _dataRow[name] : _dataRow[name, _version];
71 return value is DBNull? null: value;
72 }
73
74 public override bool IsNull(object o, int index)
75 {
76 if (_version == DataRowVersion.Default)
77 return _dataRow.IsNull(index);
78
79 DataColumn col = _dataRow.Table.Columns[index];
80
81 return _dataRow.IsNull(col, _version);
82 }
83
84 #endregion
85
86 #region IMapDataDestination Members
87
88 private ArrayList _nameList;
89
90 public override int GetOrdinal(string name)
91 {
92 if (_createColumns)
93 {
94 if (_nameList == null)
95 _nameList = new ArrayList();
96
97 for (int i = 0; i < _nameList.Count; i++)
98 if (name == _nameList[i].ToString())
99 return i;
100
101 return _nameList.Add(name);
102 }
103
104 return _dataRow.Table.Columns.IndexOf(name);
105 }
106
107 private void CreateColumn(int index, object value)
108 {
109 if (_dataRow.Table.Rows.Count > 1)
110 {
111 _createColumns = false;
112 }
113 else
114 {
115 DataColumnCollection cc = _dataRow.Table.Columns;
116 string name = _nameList[index].ToString();
117
118 DataColumn column =
119 value == null || value is DBNull? cc.Add(name): cc.Add(name, value.GetType());
120
121 if (cc.IndexOf(column) != index)
122 throw new MappingException(string.Format("Cant create data column '{0}'.", name));
123 }
124 }
125
126 public override void SetValue(object o, int index, object value)
127 {
128 if (_createColumns)
129 CreateColumn(index, value);
130
131 if (value == null || value is DBNull)
132 {
133 _dataRow[index] = DBNull.Value;
134 }
135 else
136 {
137 DataColumn column = _dataRow.Table.Columns[index];
138
139 if (column.DataType != value.GetType())
140 {
141 if (column.DataType == typeof(Guid))
142 {
143 value = new Guid(value.ToString());
144 }
145 else
146 {
147 if (column.DataType != typeof(string))
148 value = Convert.ChangeType(value, column.DataType);
149 }
150 }
151
152 _dataRow[index] = value;
153 }
154 }
155
156 public override void SetValue(object o, string name, object value)
157 {
158 if (_createColumns)
159 CreateColumn(GetOrdinal(name), value);
160
161 if (value == null || value is DBNull)
162 {
163 _dataRow[name] = DBNull.Value;
164 }
165 else
166 {
167 DataColumn dc = _dataRow.Table.Columns[name];
168
169 if (dc.DataType != value.GetType())
170 {
171 if (dc.DataType == typeof(Guid))
172 {
173 value = new Guid(value.ToString());
174 }
175 else
176 {
177 if (dc.DataType != typeof(string))
178 value = Convert.ChangeType(value, dc.DataType);
179 }
180 }
181
182 _dataRow[name] = value;
183 }
184 }
185
186 #endregion
187 }
188 }