aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/ThirdParty/TribalMedia/TribalMedia.Framework.Data/BaseFieldMapper.cs
diff options
context:
space:
mode:
authorlbsa712008-01-15 20:07:02 +0000
committerlbsa712008-01-15 20:07:02 +0000
commitf5103b98be60cc8b1a69288328cdc20f78857835 (patch)
treeb50381385478f57a05ed9fef7c5176397d76d2f2 /ThirdParty/TribalMedia/TribalMedia.Framework.Data/BaseFieldMapper.cs
parent* Added some comments to the linkset positioning code (diff)
downloadopensim-SC_OLD-f5103b98be60cc8b1a69288328cdc20f78857835.zip
opensim-SC_OLD-f5103b98be60cc8b1a69288328cdc20f78857835.tar.gz
opensim-SC_OLD-f5103b98be60cc8b1a69288328cdc20f78857835.tar.bz2
opensim-SC_OLD-f5103b98be60cc8b1a69288328cdc20f78857835.tar.xz
* Renamed a bunch of Data baseclasses for clarity and readability
(Slowly getting there)
Diffstat (limited to 'ThirdParty/TribalMedia/TribalMedia.Framework.Data/BaseFieldMapper.cs')
-rw-r--r--ThirdParty/TribalMedia/TribalMedia.Framework.Data/BaseFieldMapper.cs203
1 files changed, 203 insertions, 0 deletions
diff --git a/ThirdParty/TribalMedia/TribalMedia.Framework.Data/BaseFieldMapper.cs b/ThirdParty/TribalMedia/TribalMedia.Framework.Data/BaseFieldMapper.cs
new file mode 100644
index 0000000..20d8cf1
--- /dev/null
+++ b/ThirdParty/TribalMedia/TribalMedia.Framework.Data/BaseFieldMapper.cs
@@ -0,0 +1,203 @@
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
27using System;
28using System.Collections.Generic;
29using System.Data.Common;
30
31namespace TribalMedia.Framework.Data
32{
33 //public delegate TField RowMapperGetAccessor<TRowMapper, TField>(TRowMapper rowMapper);
34 //public delegate void RowMapperSetAccessor<TRowMapper, TField>(TRowMapper rowMapper, TField value);
35
36 public delegate TField ObjectGetAccessor<TObj, TField>(TObj obj);
37 public delegate void ObjectSetAccessor<TObj, TField>(TObj obj, TField value);
38
39 public abstract class BaseFieldMapper
40 {
41 private readonly BaseTableMapper m_tableMapper;
42 private readonly string m_fieldName;
43
44 public string FieldName
45 {
46 get { return m_fieldName; }
47 }
48
49 protected Type m_valueType;
50
51 public Type ValueType
52 {
53 get { return m_valueType; }
54 }
55
56 public abstract object GetParamValue(object obj);
57
58 public BaseFieldMapper(BaseTableMapper tableMapper, string fieldName, Type valueType)
59 {
60 m_fieldName = fieldName;
61 m_valueType = valueType;
62 m_tableMapper = tableMapper;
63 }
64
65 public abstract void SetPropertyFromReader(object mapper, DataReader reader);
66
67 public void RawAddParam(DbCommand command, List<string> fieldNames, string fieldName, object value)
68 {
69 string paramName = m_tableMapper.CreateParamName(fieldName);
70 fieldNames.Add(fieldName);
71
72 DbParameter param = command.CreateParameter();
73 param.ParameterName = paramName;
74 param.Value = value;
75
76 command.Parameters.Add(param);
77 }
78
79 public virtual void ExpandField<TObj>(TObj obj, DbCommand command, List<string> fieldNames)
80 {
81 string fieldName = FieldName;
82 object value = GetParamValue(obj);
83
84 RawAddParam(command, fieldNames, fieldName, m_tableMapper.ConvertToDbType(value));
85 }
86
87 protected virtual object GetValue(DataReader reader)
88 {
89 object value;
90
91 if (ValueType == typeof(Guid))
92 {
93 value = reader.GetGuid(m_fieldName);
94 }
95 else if (ValueType == typeof(bool))
96 {
97 uint boolVal = reader.GetUShort(m_fieldName);
98 value = (boolVal == 1);
99 }
100 else
101 if (ValueType == typeof(byte))
102 {
103 value = reader.GetByte(m_fieldName);
104 }
105 else if (ValueType == typeof(sbyte))
106 {
107 value = reader.GetSByte(m_fieldName);
108 }
109 else if (ValueType == typeof(ushort))
110 {
111 value = reader.GetUShort(m_fieldName);
112 }
113 else if (ValueType == typeof(byte[]))
114 {
115 value = reader.GetBytes(m_fieldName);
116 }
117 else
118 {
119 value = reader.Get(m_fieldName);
120 }
121
122 if (value is DBNull)
123 {
124 value = default(ValueType);
125 }
126
127 return value;
128 }
129 }
130
131 //public class RowMapperField<TRowMapper, TField> : FieldMapper
132 // where TRowMapper : RowMapper
133 //{
134 // private readonly RowMapperGetAccessor<TRowMapper, TField> m_fieldGetAccessor;
135 // private readonly RowMapperSetAccessor<TRowMapper, TField> m_fieldSetAccessor;
136
137 // public override object GetParamValue(object obj)
138 // {
139 // return m_fieldGetAccessor((TRowMapper) obj);
140 // }
141
142 // public override void SetPropertyFromReader(object mapper, DataReader reader)
143 // {
144 // object value;
145
146 // value = GetValue(reader);
147
148 // if (value == null)
149 // {
150 // m_fieldSetAccessor((TRowMapper) mapper, default(TField));
151 // }
152 // else
153 // {
154 // m_fieldSetAccessor((TRowMapper) mapper, (TField) value);
155 // }
156 // }
157
158
159 // public RowMapperField(TableMapper tableMapper, string fieldName, RowMapperGetAccessor<TRowMapper, TField> rowMapperGetAccessor,
160 // RowMapperSetAccessor<TRowMapper, TField> rowMapperSetAccessor)
161 // : base(tableMapper, fieldName, typeof(TField))
162 // {
163 // m_fieldGetAccessor = rowMapperGetAccessor;
164 // m_fieldSetAccessor = rowMapperSetAccessor;
165 // }
166 //}
167
168 public class ObjectField<TObject, TField> : BaseFieldMapper
169 {
170 private readonly ObjectGetAccessor<TObject, TField> m_fieldGetAccessor;
171 private readonly ObjectSetAccessor<TObject, TField> m_fieldSetAccessor;
172
173 public override object GetParamValue(object obj)
174 {
175 return m_fieldGetAccessor((TObject)obj);
176 }
177
178 public override void SetPropertyFromReader(object obj, DataReader reader)
179 {
180 object value;
181
182 value = GetValue(reader);
183
184 if (value == null)
185 {
186 m_fieldSetAccessor((TObject)obj, default(TField));
187 }
188 else
189 {
190 m_fieldSetAccessor((TObject)obj, (TField)value);
191 }
192 }
193
194
195 public ObjectField(BaseTableMapper tableMapper, string fieldName, ObjectGetAccessor<TObject, TField> rowMapperGetAccessor,
196 ObjectSetAccessor<TObject, TField> rowMapperSetAccessor)
197 : base(tableMapper, fieldName, typeof(TField))
198 {
199 m_fieldGetAccessor = rowMapperGetAccessor;
200 m_fieldSetAccessor = rowMapperSetAccessor;
201 }
202 }
203} \ No newline at end of file