aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Data/Base/BaseFieldMapper.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Data/Base/BaseFieldMapper.cs')
-rw-r--r--OpenSim/Data/Base/BaseFieldMapper.cs214
1 files changed, 0 insertions, 214 deletions
diff --git a/OpenSim/Data/Base/BaseFieldMapper.cs b/OpenSim/Data/Base/BaseFieldMapper.cs
deleted file mode 100644
index c7d6bc9..0000000
--- a/OpenSim/Data/Base/BaseFieldMapper.cs
+++ /dev/null
@@ -1,214 +0,0 @@
1/*
2 * Copyright (c) Contributors, http://opensimulator.org/
3 * See CONTRIBUTORS.TXT for a full list of copyright holders.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the OpenSim Project nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28using System;
29using System.Collections.Generic;
30using System.Data.Common;
31
32namespace OpenSim.Data.Base
33{
34 public delegate TField ObjectGetAccessor<TObj, TField>(TObj obj);
35 public delegate void ObjectSetAccessor<TObj, TField>(TObj obj, TField value);
36
37 /// <summary>
38 ///
39 /// </summary>
40 public abstract class BaseFieldMapper
41 {
42 private readonly BaseTableMapper m_tableMapper;
43 private readonly string m_fieldName;
44
45 /// <summary>
46 ///
47 /// </summary>
48 public string FieldName
49 {
50 get { return m_fieldName; }
51 }
52
53 protected Type m_valueType;
54
55 /// <summary>
56 ///
57 /// </summary>
58 public Type ValueType
59 {
60 get { return m_valueType; }
61 }
62
63 public abstract object GetParamValue(object obj);
64
65 /// <summary>
66 ///
67 /// </summary>
68 /// <param name="tableMapper"></param>
69 /// <param name="fieldName"></param>
70 /// <param name="valueType"></param>
71 public BaseFieldMapper(BaseTableMapper tableMapper, string fieldName, Type valueType)
72 {
73 m_fieldName = fieldName;
74 m_valueType = valueType;
75 m_tableMapper = tableMapper;
76 }
77
78 public abstract void SetPropertyFromReader(object mapper, BaseDataReader reader);
79
80 /// <summary>
81 ///
82 /// </summary>
83 /// <param name="command"></param>
84 /// <param name="fieldNames"></param>
85 /// <param name="fieldName"></param>
86 /// <param name="value"></param>
87 public void RawAddParam(DbCommand command, List<string> fieldNames, string fieldName, object value)
88 {
89 string paramName = m_tableMapper.CreateParamName(fieldName);
90 fieldNames.Add(fieldName);
91
92 DbParameter param = command.CreateParameter();
93 param.ParameterName = paramName;
94 param.Value = value;
95
96 command.Parameters.Add(param);
97 }
98
99 /// <summary>
100 ///
101 /// </summary>
102 /// <typeparam name="TObj"></typeparam>
103 /// <param name="obj"></param>
104 /// <param name="command"></param>
105 /// <param name="fieldNames"></param>
106 public virtual void ExpandField<TObj>(TObj obj, DbCommand command, List<string> fieldNames)
107 {
108 string fieldName = FieldName;
109 object value = GetParamValue(obj);
110
111 RawAddParam(command, fieldNames, fieldName, m_tableMapper.ConvertToDbType(value));
112 }
113
114 /// <summary>
115 ///
116 /// </summary>
117 /// <param name="reader"></param>
118 /// <returns></returns>
119 protected virtual object GetValue(BaseDataReader reader)
120 {
121 object value;
122
123 if (ValueType == typeof(Guid))
124 {
125 value = reader.GetGuid(m_fieldName);
126 }
127 else if (ValueType == typeof(bool))
128 {
129 uint boolVal = reader.GetUShort(m_fieldName);
130 value = (boolVal == 1);
131 }
132 else
133 if (ValueType == typeof(byte))
134 {
135 value = reader.GetByte(m_fieldName);
136 }
137 else if (ValueType == typeof(sbyte))
138 {
139 value = reader.GetSByte(m_fieldName);
140 }
141 else if (ValueType == typeof(ushort))
142 {
143 value = reader.GetUShort(m_fieldName);
144 }
145 else if (ValueType == typeof(uint))
146 {
147 value = reader.GetUInt32(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
167 /// <summary>
168 ///
169 /// </summary>
170 /// <typeparam name="TObject"></typeparam>
171 /// <typeparam name="TField"></typeparam>
172 public class ObjectField<TObject, TField> : BaseFieldMapper
173 {
174 private readonly ObjectGetAccessor<TObject, TField> m_fieldGetAccessor;
175 private readonly ObjectSetAccessor<TObject, TField> m_fieldSetAccessor;
176
177 public override object GetParamValue(object obj)
178 {
179 return m_fieldGetAccessor((TObject)obj);
180 }
181
182 public override void SetPropertyFromReader(object obj, BaseDataReader reader)
183 {
184 object value;
185
186 value = GetValue(reader);
187
188 if (value == null)
189 {
190 m_fieldSetAccessor((TObject)obj, default(TField));
191 }
192 else
193 {
194 m_fieldSetAccessor((TObject)obj, (TField)value);
195 }
196 }
197
198
199 /// <summary>
200 ///
201 /// </summary>
202 /// <param name="tableMapper"></param>
203 /// <param name="fieldName"></param>
204 /// <param name="rowMapperGetAccessor"></param>
205 /// <param name="rowMapperSetAccessor"></param>
206 public ObjectField(BaseTableMapper tableMapper, string fieldName, ObjectGetAccessor<TObject, TField> rowMapperGetAccessor,
207 ObjectSetAccessor<TObject, TField> rowMapperSetAccessor)
208 : base(tableMapper, fieldName, typeof(TField))
209 {
210 m_fieldGetAccessor = rowMapperGetAccessor;
211 m_fieldSetAccessor = rowMapperSetAccessor;
212 }
213 }
214}