BaseFieldMapper.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. using System;
  28. using System.Collections.Generic;
  29. using System.Data.Common;
  30. namespace OpenSim.Data.Base
  31. {
  32. public delegate TField ObjectGetAccessor<TObj, TField>(TObj obj);
  33. public delegate void ObjectSetAccessor<TObj, TField>(TObj obj, TField value);
  34. /// <summary>
  35. ///
  36. /// </summary>
  37. public abstract class BaseFieldMapper
  38. {
  39. private readonly BaseTableMapper m_tableMapper;
  40. private readonly string m_fieldName;
  41. /// <summary>
  42. ///
  43. /// </summary>
  44. public string FieldName
  45. {
  46. get { return m_fieldName; }
  47. }
  48. protected Type m_valueType;
  49. /// <summary>
  50. ///
  51. /// </summary>
  52. public Type ValueType
  53. {
  54. get { return m_valueType; }
  55. }
  56. public abstract object GetParamValue(object obj);
  57. /// <summary>
  58. ///
  59. /// </summary>
  60. /// <param name="tableMapper"></param>
  61. /// <param name="fieldName"></param>
  62. /// <param name="valueType"></param>
  63. public BaseFieldMapper(BaseTableMapper tableMapper, string fieldName, Type valueType)
  64. {
  65. m_fieldName = fieldName;
  66. m_valueType = valueType;
  67. m_tableMapper = tableMapper;
  68. }
  69. public abstract void SetPropertyFromReader(object mapper, BaseDataReader reader);
  70. /// <summary>
  71. ///
  72. /// </summary>
  73. /// <param name="command"></param>
  74. /// <param name="fieldNames"></param>
  75. /// <param name="fieldName"></param>
  76. /// <param name="value"></param>
  77. public void RawAddParam(DbCommand command, List<string> fieldNames, string fieldName, object value)
  78. {
  79. string paramName = m_tableMapper.CreateParamName(fieldName);
  80. fieldNames.Add(fieldName);
  81. DbParameter param = command.CreateParameter();
  82. param.ParameterName = paramName;
  83. param.Value = value;
  84. command.Parameters.Add(param);
  85. }
  86. /// <summary>
  87. ///
  88. /// </summary>
  89. /// <typeparam name="TObj"></typeparam>
  90. /// <param name="obj"></param>
  91. /// <param name="command"></param>
  92. /// <param name="fieldNames"></param>
  93. public virtual void ExpandField<TObj>(TObj obj, DbCommand command, List<string> fieldNames)
  94. {
  95. string fieldName = FieldName;
  96. object value = GetParamValue(obj);
  97. RawAddParam(command, fieldNames, fieldName, m_tableMapper.ConvertToDbType(value));
  98. }
  99. /// <summary>
  100. ///
  101. /// </summary>
  102. /// <param name="reader"></param>
  103. /// <returns></returns>
  104. protected virtual object GetValue(BaseDataReader reader)
  105. {
  106. object value;
  107. if (ValueType == typeof(Guid))
  108. {
  109. value = reader.GetGuid(m_fieldName);
  110. }
  111. else if (ValueType == typeof(bool))
  112. {
  113. uint boolVal = reader.GetUShort(m_fieldName);
  114. value = (boolVal == 1);
  115. }
  116. else
  117. if (ValueType == typeof(byte))
  118. {
  119. value = reader.GetByte(m_fieldName);
  120. }
  121. else if (ValueType == typeof(sbyte))
  122. {
  123. value = reader.GetSByte(m_fieldName);
  124. }
  125. else if (ValueType == typeof(ushort))
  126. {
  127. value = reader.GetUShort(m_fieldName);
  128. }
  129. else if (ValueType == typeof(uint))
  130. {
  131. value = reader.GetUInt32(m_fieldName);
  132. }
  133. else if (ValueType == typeof(byte[]))
  134. {
  135. value = reader.GetBytes(m_fieldName);
  136. }
  137. else
  138. {
  139. value = reader.Get(m_fieldName);
  140. }
  141. if (value is DBNull)
  142. {
  143. value = default(ValueType);
  144. }
  145. return value;
  146. }
  147. }
  148. /// <summary>
  149. ///
  150. /// </summary>
  151. /// <typeparam name="TObject"></typeparam>
  152. /// <typeparam name="TField"></typeparam>
  153. public class ObjectField<TObject, TField> : BaseFieldMapper
  154. {
  155. private readonly ObjectGetAccessor<TObject, TField> m_fieldGetAccessor;
  156. private readonly ObjectSetAccessor<TObject, TField> m_fieldSetAccessor;
  157. public override object GetParamValue(object obj)
  158. {
  159. return m_fieldGetAccessor((TObject)obj);
  160. }
  161. public override void SetPropertyFromReader(object obj, BaseDataReader reader)
  162. {
  163. object value;
  164. value = GetValue(reader);
  165. if (value == null)
  166. {
  167. m_fieldSetAccessor((TObject)obj, default(TField));
  168. }
  169. else
  170. {
  171. m_fieldSetAccessor((TObject)obj, (TField)value);
  172. }
  173. }
  174. /// <summary>
  175. ///
  176. /// </summary>
  177. /// <param name="tableMapper"></param>
  178. /// <param name="fieldName"></param>
  179. /// <param name="rowMapperGetAccessor"></param>
  180. /// <param name="rowMapperSetAccessor"></param>
  181. public ObjectField(BaseTableMapper tableMapper, string fieldName, ObjectGetAccessor<TObject, TField> rowMapperGetAccessor,
  182. ObjectSetAccessor<TObject, TField> rowMapperSetAccessor)
  183. : base(tableMapper, fieldName, typeof(TField))
  184. {
  185. m_fieldGetAccessor = rowMapperGetAccessor;
  186. m_fieldSetAccessor = rowMapperSetAccessor;
  187. }
  188. }
  189. }