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