aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/ThirdParty/TribalMedia/TribalMedia.Framework.Data/FieldMapper.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/FieldMapper.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/FieldMapper.cs')
-rw-r--r--ThirdParty/TribalMedia/TribalMedia.Framework.Data/FieldMapper.cs205
1 files changed, 0 insertions, 205 deletions
diff --git a/ThirdParty/TribalMedia/TribalMedia.Framework.Data/FieldMapper.cs b/ThirdParty/TribalMedia/TribalMedia.Framework.Data/FieldMapper.cs
deleted file mode 100644
index 89bc0f0..0000000
--- a/ThirdParty/TribalMedia/TribalMedia.Framework.Data/FieldMapper.cs
+++ /dev/null
@@ -1,205 +0,0 @@
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
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 virtual void ExpandField<TObj>(TObj obj, DbCommand command, List<string> fieldNames)
82 {
83 string fieldName = FieldName;
84 object value = GetParamValue(obj);
85
86 RawAddParam(command, fieldNames, fieldName, m_tableMapper.ConvertToDbType(value));
87 }
88
89 protected virtual object GetValue(DataReader reader)
90 {
91 object value;
92
93 if (ValueType == typeof(Guid))
94 {
95 value = reader.GetGuid(m_fieldName);
96 }
97 else if (ValueType == typeof (bool))
98 {
99 uint boolVal = reader.GetUShort(m_fieldName);
100 value = (boolVal == 1);
101 }
102 else
103 if (ValueType == typeof (byte))
104 {
105 value = reader.GetByte(m_fieldName);
106 }
107 else if (ValueType == typeof (sbyte))
108 {
109 value = reader.GetSByte(m_fieldName);
110 }
111 else if (ValueType == typeof (ushort))
112 {
113 value = reader.GetUShort(m_fieldName);
114 }
115 else if (ValueType == typeof (byte[]))
116 {
117 value = reader.GetBytes(m_fieldName);
118 }
119 else
120 {
121 value = reader.Get(m_fieldName);
122 }
123
124 if (value is DBNull)
125 {
126 value = default(ValueType);
127 }
128
129 return value;
130 }
131 }
132
133 public class RowMapperField<TRowMapper, TField> : FieldMapper
134 where TRowMapper : RowMapper
135 {
136 private readonly RowMapperGetAccessor<TRowMapper, TField> m_fieldGetAccessor;
137 private readonly RowMapperSetAccessor<TRowMapper, TField> m_fieldSetAccessor;
138
139 public override object GetParamValue(object obj)
140 {
141 return m_fieldGetAccessor((TRowMapper) obj);
142 }
143
144 public override void SetPropertyFromReader(object mapper, DataReader reader)
145 {
146 object value;
147
148 value = GetValue(reader);
149
150 if (value == null)
151 {
152 m_fieldSetAccessor((TRowMapper) mapper, default(TField));
153 }
154 else
155 {
156 m_fieldSetAccessor((TRowMapper) mapper, (TField) value);
157 }
158 }
159
160
161 public RowMapperField(TableMapper tableMapper, string fieldName, RowMapperGetAccessor<TRowMapper, TField> rowMapperGetAccessor,
162 RowMapperSetAccessor<TRowMapper, TField> rowMapperSetAccessor)
163 : base(tableMapper, fieldName, typeof(TField))
164 {
165 m_fieldGetAccessor = rowMapperGetAccessor;
166 m_fieldSetAccessor = rowMapperSetAccessor;
167 }
168 }
169
170 public class ObjectField<TObject, TField> : FieldMapper
171 {
172 private readonly ObjectGetAccessor<TObject, TField> m_fieldGetAccessor;
173 private readonly ObjectSetAccessor<TObject, TField> m_fieldSetAccessor;
174
175 public override object GetParamValue(object obj)
176 {
177 return m_fieldGetAccessor((TObject) obj);
178 }
179
180 public override void SetPropertyFromReader(object obj, DataReader reader)
181 {
182 object value;
183
184 value = GetValue(reader);
185
186 if (value == null)
187 {
188 m_fieldSetAccessor((TObject) obj, default(TField));
189 }
190 else
191 {
192 m_fieldSetAccessor((TObject) obj, (TField) value);
193 }
194 }
195
196
197 public ObjectField(TableMapper tableMapper, string fieldName, ObjectGetAccessor<TObject, TField> rowMapperGetAccessor,
198 ObjectSetAccessor<TObject, TField> rowMapperSetAccessor)
199 : base(tableMapper, fieldName, typeof (TField))
200 {
201 m_fieldGetAccessor = rowMapperGetAccessor;
202 m_fieldSetAccessor = rowMapperSetAccessor;
203 }
204 }
205} \ No newline at end of file