diff options
Diffstat (limited to 'ThirdParty/TribalMedia/TribalMedia.Framework.Data/FieldMapper.cs')
-rw-r--r-- | ThirdParty/TribalMedia/TribalMedia.Framework.Data/FieldMapper.cs | 249 |
1 files changed, 249 insertions, 0 deletions
diff --git a/ThirdParty/TribalMedia/TribalMedia.Framework.Data/FieldMapper.cs b/ThirdParty/TribalMedia/TribalMedia.Framework.Data/FieldMapper.cs new file mode 100644 index 0000000..28a603a --- /dev/null +++ b/ThirdParty/TribalMedia/TribalMedia.Framework.Data/FieldMapper.cs | |||
@@ -0,0 +1,249 @@ | |||
1 | /* | ||
2 | * Copyright (c) Tribal Media AB, http://tribalmedia.se/ | ||
3 | * | ||
4 | * Redistribution and use in source and binary forms, with or without | ||
5 | * modification, are permitted provided that the following conditions are met: | ||
6 | * * Redistributions of source code must retain the above copyright | ||
7 | * notice, this list of conditions and the following disclaimer. | ||
8 | * * Redistributions in binary form must reproduce the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer in the | ||
10 | * documentation and/or other materials provided with the distribution. | ||
11 | * * The name of Tribal Media AB may not be used to endorse or promote products | ||
12 | * derived from this software without specific prior written permission. | ||
13 | * | ||
14 | * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY | ||
15 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
16 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
17 | * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY | ||
18 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
19 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
20 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
21 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
23 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
24 | * | ||
25 | */ | ||
26 | |||
27 | using System; | ||
28 | using System.Collections.Generic; | ||
29 | using System.Data.Common; | ||
30 | |||
31 | namespace TribalMedia.Framework.Data | ||
32 | { | ||
33 | public delegate TField RowMapperGetAccessor<TRowMapper, TField>(TRowMapper rowMapper); | ||
34 | |||
35 | public delegate void RowMapperSetAccessor<TRowMapper, TField>(TRowMapper rowMapper, TField value); | ||
36 | |||
37 | public delegate TField ObjectGetAccessor<TObj, TField>(TObj obj); | ||
38 | |||
39 | public delegate void ObjectSetAccessor<TObj, TField>(TObj obj, TField value); | ||
40 | |||
41 | public abstract class FieldMapper | ||
42 | { | ||
43 | private readonly TableMapper m_tableMapper; | ||
44 | private readonly string m_fieldName; | ||
45 | |||
46 | public string FieldName | ||
47 | { | ||
48 | get { return m_fieldName; } | ||
49 | } | ||
50 | |||
51 | protected Type m_valueType; | ||
52 | |||
53 | public Type ValueType | ||
54 | { | ||
55 | get { return m_valueType; } | ||
56 | } | ||
57 | |||
58 | public abstract object GetParamValue(object obj); | ||
59 | |||
60 | public FieldMapper( TableMapper tableMapper, string fieldName, Type valueType) | ||
61 | { | ||
62 | m_fieldName = fieldName; | ||
63 | m_valueType = valueType; | ||
64 | m_tableMapper = tableMapper; | ||
65 | } | ||
66 | |||
67 | public abstract void SetPropertyFromReader(object mapper, DataReader reader); | ||
68 | |||
69 | public void RawAddParam(DbCommand command, List<string> fieldNames, string fieldName, object value) | ||
70 | { | ||
71 | string paramName = m_tableMapper.CreateParamName(fieldName); | ||
72 | fieldNames.Add(fieldName); | ||
73 | |||
74 | DbParameter param = command.CreateParameter(); | ||
75 | param.ParameterName = paramName; | ||
76 | param.Value = value; | ||
77 | |||
78 | command.Parameters.Add(param); | ||
79 | } | ||
80 | |||
81 | public void ExpandField<TObj>(TObj obj, DbCommand command, List<string> fieldNames) | ||
82 | { | ||
83 | string fieldName = FieldName; | ||
84 | object value = GetParamValue(obj); | ||
85 | |||
86 | //if (ValueType == typeof (Vector3)) | ||
87 | //{ | ||
88 | // Vector3 vector = (Vector3) value; | ||
89 | |||
90 | // RawAddParam(command, fieldNames, fieldName + "X", vector.X); | ||
91 | // RawAddParam(command, fieldNames, fieldName + "Y", vector.Y); | ||
92 | // RawAddParam(command, fieldNames, fieldName + "Z", vector.Z); | ||
93 | //} | ||
94 | //else if (ValueType == typeof (Quaternion)) | ||
95 | //{ | ||
96 | // Quaternion quaternion = (Quaternion) value; | ||
97 | |||
98 | // RawAddParam(command, fieldNames, fieldName + "X", quaternion.X); | ||
99 | // RawAddParam(command, fieldNames, fieldName + "Y", quaternion.Y); | ||
100 | // RawAddParam(command, fieldNames, fieldName + "Z", quaternion.Z); | ||
101 | // RawAddParam(command, fieldNames, fieldName + "W", quaternion.W); | ||
102 | //} | ||
103 | //else | ||
104 | //{ | ||
105 | RawAddParam(command, fieldNames, fieldName, ConvertToDbType(value)); | ||
106 | //} | ||
107 | } | ||
108 | |||
109 | protected object GetValue(DataReader reader) | ||
110 | { | ||
111 | object value; | ||
112 | //if (ValueType == typeof (Vector3)) | ||
113 | //{ | ||
114 | // value = reader.GetVector(m_fieldName); | ||
115 | //} | ||
116 | //else if (ValueType == typeof (Quaternion)) | ||
117 | //{ | ||
118 | // value = reader.GetQuaternion(m_fieldName); | ||
119 | //} | ||
120 | //else | ||
121 | //if (ValueType == typeof(UID)) | ||
122 | //{ | ||
123 | // Guid guid = reader.GetGuid(m_fieldName); | ||
124 | // value = new UID(guid); | ||
125 | //} | ||
126 | //else | ||
127 | if (ValueType == typeof(Guid)) | ||
128 | { | ||
129 | value = reader.GetGuid(m_fieldName); | ||
130 | } | ||
131 | else if (ValueType == typeof (bool)) | ||
132 | { | ||
133 | uint boolVal = reader.GetUShort(m_fieldName); | ||
134 | value = (boolVal == 1); | ||
135 | } | ||
136 | else | ||
137 | if (ValueType == typeof (byte)) | ||
138 | { | ||
139 | value = reader.GetByte(m_fieldName); | ||
140 | } | ||
141 | else if (ValueType == typeof (sbyte)) | ||
142 | { | ||
143 | value = reader.GetSByte(m_fieldName); | ||
144 | } | ||
145 | else if (ValueType == typeof (ushort)) | ||
146 | { | ||
147 | value = reader.GetUShort(m_fieldName); | ||
148 | } | ||
149 | else if (ValueType == typeof (byte[])) | ||
150 | { | ||
151 | value = reader.GetBytes(m_fieldName); | ||
152 | } | ||
153 | else | ||
154 | { | ||
155 | value = reader.Get(m_fieldName); | ||
156 | } | ||
157 | |||
158 | if (value is DBNull) | ||
159 | { | ||
160 | value = default(ValueType); | ||
161 | } | ||
162 | |||
163 | return value; | ||
164 | } | ||
165 | |||
166 | public static object ConvertToDbType(object value) | ||
167 | { | ||
168 | //if (value is UID) | ||
169 | //{ | ||
170 | // return (value as UID).UUID.ToString(); | ||
171 | //} | ||
172 | |||
173 | return value; | ||
174 | } | ||
175 | } | ||
176 | |||
177 | public class RowMapperField<TRowMapper, TField> : FieldMapper | ||
178 | where TRowMapper : RowMapper | ||
179 | { | ||
180 | private readonly RowMapperGetAccessor<TRowMapper, TField> m_fieldGetAccessor; | ||
181 | private readonly RowMapperSetAccessor<TRowMapper, TField> m_fieldSetAccessor; | ||
182 | |||
183 | public override object GetParamValue(object obj) | ||
184 | { | ||
185 | return m_fieldGetAccessor((TRowMapper) obj); | ||
186 | } | ||
187 | |||
188 | public override void SetPropertyFromReader(object mapper, DataReader reader) | ||
189 | { | ||
190 | object value; | ||
191 | |||
192 | value = GetValue(reader); | ||
193 | |||
194 | if (value == null) | ||
195 | { | ||
196 | m_fieldSetAccessor((TRowMapper) mapper, default(TField)); | ||
197 | } | ||
198 | else | ||
199 | { | ||
200 | m_fieldSetAccessor((TRowMapper) mapper, (TField) value); | ||
201 | } | ||
202 | } | ||
203 | |||
204 | |||
205 | public RowMapperField(TableMapper tableMapper, string fieldName, RowMapperGetAccessor<TRowMapper, TField> rowMapperGetAccessor, | ||
206 | RowMapperSetAccessor<TRowMapper, TField> rowMapperSetAccessor) | ||
207 | : base(tableMapper, fieldName, typeof(TField)) | ||
208 | { | ||
209 | m_fieldGetAccessor = rowMapperGetAccessor; | ||
210 | m_fieldSetAccessor = rowMapperSetAccessor; | ||
211 | } | ||
212 | } | ||
213 | |||
214 | public class ObjectField<TObject, TField> : FieldMapper | ||
215 | { | ||
216 | private readonly ObjectGetAccessor<TObject, TField> m_fieldGetAccessor; | ||
217 | private readonly ObjectSetAccessor<TObject, TField> m_fieldSetAccessor; | ||
218 | |||
219 | public override object GetParamValue(object obj) | ||
220 | { | ||
221 | return m_fieldGetAccessor((TObject) obj); | ||
222 | } | ||
223 | |||
224 | public override void SetPropertyFromReader(object obj, DataReader reader) | ||
225 | { | ||
226 | object value; | ||
227 | |||
228 | value = GetValue(reader); | ||
229 | |||
230 | if (value == null) | ||
231 | { | ||
232 | m_fieldSetAccessor((TObject) obj, default(TField)); | ||
233 | } | ||
234 | else | ||
235 | { | ||
236 | m_fieldSetAccessor((TObject) obj, (TField) value); | ||
237 | } | ||
238 | } | ||
239 | |||
240 | |||
241 | public ObjectField(TableMapper tableMapper, string fieldName, ObjectGetAccessor<TObject, TField> rowMapperGetAccessor, | ||
242 | ObjectSetAccessor<TObject, TField> rowMapperSetAccessor) | ||
243 | : base(tableMapper, fieldName, typeof (TField)) | ||
244 | { | ||
245 | m_fieldGetAccessor = rowMapperGetAccessor; | ||
246 | m_fieldSetAccessor = rowMapperSetAccessor; | ||
247 | } | ||
248 | } | ||
249 | } \ No newline at end of file | ||