diff options
author | Sean Dague | 2008-04-02 15:24:31 +0000 |
---|---|---|
committer | Sean Dague | 2008-04-02 15:24:31 +0000 |
commit | c52c68f314c67c76c7181a6d0828f476290fbd66 (patch) | |
tree | 66ab347502892902a096fa985f31b25738eb1381 /OpenSim/Data/Base/BaseFieldMapper.cs | |
parent | reorganizing namespaces to put all the Data stuff into it's own namespace (diff) | |
download | opensim-SC_OLD-c52c68f314c67c76c7181a6d0828f476290fbd66.zip opensim-SC_OLD-c52c68f314c67c76c7181a6d0828f476290fbd66.tar.gz opensim-SC_OLD-c52c68f314c67c76c7181a6d0828f476290fbd66.tar.bz2 opensim-SC_OLD-c52c68f314c67c76c7181a6d0828f476290fbd66.tar.xz |
whole lot more moving
Diffstat (limited to 'OpenSim/Data/Base/BaseFieldMapper.cs')
-rw-r--r-- | OpenSim/Data/Base/BaseFieldMapper.cs | 168 |
1 files changed, 168 insertions, 0 deletions
diff --git a/OpenSim/Data/Base/BaseFieldMapper.cs b/OpenSim/Data/Base/BaseFieldMapper.cs new file mode 100644 index 0000000..03c7bfb --- /dev/null +++ b/OpenSim/Data/Base/BaseFieldMapper.cs | |||
@@ -0,0 +1,168 @@ | |||
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 | |||
28 | using System; | ||
29 | using System.Collections.Generic; | ||
30 | using System.Data.Common; | ||
31 | |||
32 | namespace OpenSim.Framework.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 | public abstract class BaseFieldMapper | ||
38 | { | ||
39 | private readonly BaseTableMapper m_tableMapper; | ||
40 | private readonly string m_fieldName; | ||
41 | |||
42 | public string FieldName | ||
43 | { | ||
44 | get { return m_fieldName; } | ||
45 | } | ||
46 | |||
47 | protected Type m_valueType; | ||
48 | |||
49 | public Type ValueType | ||
50 | { | ||
51 | get { return m_valueType; } | ||
52 | } | ||
53 | |||
54 | public abstract object GetParamValue(object obj); | ||
55 | |||
56 | public BaseFieldMapper(BaseTableMapper tableMapper, string fieldName, Type valueType) | ||
57 | { | ||
58 | m_fieldName = fieldName; | ||
59 | m_valueType = valueType; | ||
60 | m_tableMapper = tableMapper; | ||
61 | } | ||
62 | |||
63 | public abstract void SetPropertyFromReader(object mapper, BaseDataReader reader); | ||
64 | |||
65 | public void RawAddParam(DbCommand command, List<string> fieldNames, string fieldName, object value) | ||
66 | { | ||
67 | string paramName = m_tableMapper.CreateParamName(fieldName); | ||
68 | fieldNames.Add(fieldName); | ||
69 | |||
70 | DbParameter param = command.CreateParameter(); | ||
71 | param.ParameterName = paramName; | ||
72 | param.Value = value; | ||
73 | |||
74 | command.Parameters.Add(param); | ||
75 | } | ||
76 | |||
77 | public virtual void ExpandField<TObj>(TObj obj, DbCommand command, List<string> fieldNames) | ||
78 | { | ||
79 | string fieldName = FieldName; | ||
80 | object value = GetParamValue(obj); | ||
81 | |||
82 | RawAddParam(command, fieldNames, fieldName, m_tableMapper.ConvertToDbType(value)); | ||
83 | } | ||
84 | |||
85 | protected virtual object GetValue(BaseDataReader reader) | ||
86 | { | ||
87 | object value; | ||
88 | |||
89 | if (ValueType == typeof(Guid)) | ||
90 | { | ||
91 | value = reader.GetGuid(m_fieldName); | ||
92 | } | ||
93 | else if (ValueType == typeof(bool)) | ||
94 | { | ||
95 | uint boolVal = reader.GetUShort(m_fieldName); | ||
96 | value = (boolVal == 1); | ||
97 | } | ||
98 | else | ||
99 | if (ValueType == typeof(byte)) | ||
100 | { | ||
101 | value = reader.GetByte(m_fieldName); | ||
102 | } | ||
103 | else if (ValueType == typeof(sbyte)) | ||
104 | { | ||
105 | value = reader.GetSByte(m_fieldName); | ||
106 | } | ||
107 | else if (ValueType == typeof(ushort)) | ||
108 | { | ||
109 | value = reader.GetUShort(m_fieldName); | ||
110 | } | ||
111 | else if (ValueType == typeof(uint)) | ||
112 | { | ||
113 | value = reader.GetUInt32(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 ObjectField<TObject, TField> : BaseFieldMapper | ||
134 | { | ||
135 | private readonly ObjectGetAccessor<TObject, TField> m_fieldGetAccessor; | ||
136 | private readonly ObjectSetAccessor<TObject, TField> m_fieldSetAccessor; | ||
137 | |||
138 | public override object GetParamValue(object obj) | ||
139 | { | ||
140 | return m_fieldGetAccessor((TObject)obj); | ||
141 | } | ||
142 | |||
143 | public override void SetPropertyFromReader(object obj, BaseDataReader reader) | ||
144 | { | ||
145 | object value; | ||
146 | |||
147 | value = GetValue(reader); | ||
148 | |||
149 | if (value == null) | ||
150 | { | ||
151 | m_fieldSetAccessor((TObject)obj, default(TField)); | ||
152 | } | ||
153 | else | ||
154 | { | ||
155 | m_fieldSetAccessor((TObject)obj, (TField)value); | ||
156 | } | ||
157 | } | ||
158 | |||
159 | |||
160 | public ObjectField(BaseTableMapper tableMapper, string fieldName, ObjectGetAccessor<TObject, TField> rowMapperGetAccessor, | ||
161 | ObjectSetAccessor<TObject, TField> rowMapperSetAccessor) | ||
162 | : base(tableMapper, fieldName, typeof(TField)) | ||
163 | { | ||
164 | m_fieldGetAccessor = rowMapperGetAccessor; | ||
165 | m_fieldSetAccessor = rowMapperSetAccessor; | ||
166 | } | ||
167 | } | ||
168 | } | ||