aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Framework/Data.Base/BaseFieldMapper.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Framework/Data.Base/BaseFieldMapper.cs')
-rw-r--r--OpenSim/Framework/Data.Base/BaseFieldMapper.cs163
1 files changed, 163 insertions, 0 deletions
diff --git a/OpenSim/Framework/Data.Base/BaseFieldMapper.cs b/OpenSim/Framework/Data.Base/BaseFieldMapper.cs
new file mode 100644
index 0000000..20f919a
--- /dev/null
+++ b/OpenSim/Framework/Data.Base/BaseFieldMapper.cs
@@ -0,0 +1,163 @@
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 ObjectGetAccessor<TObj, TField>(TObj obj);
34 public delegate void ObjectSetAccessor<TObj, TField>(TObj obj, TField value);
35
36 public abstract class BaseFieldMapper
37 {
38 private readonly BaseTableMapper m_tableMapper;
39 private readonly string m_fieldName;
40
41 public string FieldName
42 {
43 get { return m_fieldName; }
44 }
45
46 protected Type m_valueType;
47
48 public Type ValueType
49 {
50 get { return m_valueType; }
51 }
52
53 public abstract object GetParamValue(object obj);
54
55 public BaseFieldMapper(BaseTableMapper tableMapper, string fieldName, Type valueType)
56 {
57 m_fieldName = fieldName;
58 m_valueType = valueType;
59 m_tableMapper = tableMapper;
60 }
61
62 public abstract void SetPropertyFromReader(object mapper, BaseDataReader reader);
63
64 public void RawAddParam(DbCommand command, List<string> fieldNames, string fieldName, object value)
65 {
66 string paramName = m_tableMapper.CreateParamName(fieldName);
67 fieldNames.Add(fieldName);
68
69 DbParameter param = command.CreateParameter();
70 param.ParameterName = paramName;
71 param.Value = value;
72
73 command.Parameters.Add(param);
74 }
75
76 public virtual void ExpandField<TObj>(TObj obj, DbCommand command, List<string> fieldNames)
77 {
78 string fieldName = FieldName;
79 object value = GetParamValue(obj);
80
81 RawAddParam(command, fieldNames, fieldName, m_tableMapper.ConvertToDbType(value));
82 }
83
84 protected virtual object GetValue(BaseDataReader reader)
85 {
86 object value;
87
88 if (ValueType == typeof(Guid))
89 {
90 value = reader.GetGuid(m_fieldName);
91 }
92 else if (ValueType == typeof(bool))
93 {
94 uint boolVal = reader.GetUShort(m_fieldName);
95 value = (boolVal == 1);
96 }
97 else
98 if (ValueType == typeof(byte))
99 {
100 value = reader.GetByte(m_fieldName);
101 }
102 else if (ValueType == typeof(sbyte))
103 {
104 value = reader.GetSByte(m_fieldName);
105 }
106 else if (ValueType == typeof(ushort))
107 {
108 value = reader.GetUShort(m_fieldName);
109 }
110 else if (ValueType == typeof(byte[]))
111 {
112 value = reader.GetBytes(m_fieldName);
113 }
114 else
115 {
116 value = reader.Get(m_fieldName);
117 }
118
119 if (value is DBNull)
120 {
121 value = default(ValueType);
122 }
123
124 return value;
125 }
126 }
127
128 public class ObjectField<TObject, TField> : BaseFieldMapper
129 {
130 private readonly ObjectGetAccessor<TObject, TField> m_fieldGetAccessor;
131 private readonly ObjectSetAccessor<TObject, TField> m_fieldSetAccessor;
132
133 public override object GetParamValue(object obj)
134 {
135 return m_fieldGetAccessor((TObject)obj);
136 }
137
138 public override void SetPropertyFromReader(object obj, BaseDataReader reader)
139 {
140 object value;
141
142 value = GetValue(reader);
143
144 if (value == null)
145 {
146 m_fieldSetAccessor((TObject)obj, default(TField));
147 }
148 else
149 {
150 m_fieldSetAccessor((TObject)obj, (TField)value);
151 }
152 }
153
154
155 public ObjectField(BaseTableMapper tableMapper, string fieldName, ObjectGetAccessor<TObject, TField> rowMapperGetAccessor,
156 ObjectSetAccessor<TObject, TField> rowMapperSetAccessor)
157 : base(tableMapper, fieldName, typeof(TField))
158 {
159 m_fieldGetAccessor = rowMapperGetAccessor;
160 m_fieldSetAccessor = rowMapperSetAccessor;
161 }
162 }
163} \ No newline at end of file