BaseFieldMapper.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. public abstract class BaseFieldMapper
  35. {
  36. private readonly BaseTableMapper m_tableMapper;
  37. private readonly string m_fieldName;
  38. public string FieldName
  39. {
  40. get { return m_fieldName; }
  41. }
  42. protected Type m_valueType;
  43. public Type ValueType
  44. {
  45. get { return m_valueType; }
  46. }
  47. public abstract object GetParamValue(object obj);
  48. public BaseFieldMapper(BaseTableMapper tableMapper, string fieldName, Type valueType)
  49. {
  50. m_fieldName = fieldName;
  51. m_valueType = valueType;
  52. m_tableMapper = tableMapper;
  53. }
  54. public abstract void SetPropertyFromReader(object mapper, BaseDataReader reader);
  55. public void RawAddParam(DbCommand command, List<string> fieldNames, string fieldName, object value)
  56. {
  57. string paramName = m_tableMapper.CreateParamName(fieldName);
  58. fieldNames.Add(fieldName);
  59. DbParameter param = command.CreateParameter();
  60. param.ParameterName = paramName;
  61. param.Value = value;
  62. command.Parameters.Add(param);
  63. }
  64. public virtual void ExpandField<TObj>(TObj obj, DbCommand command, List<string> fieldNames)
  65. {
  66. string fieldName = FieldName;
  67. object value = GetParamValue(obj);
  68. RawAddParam(command, fieldNames, fieldName, m_tableMapper.ConvertToDbType(value));
  69. }
  70. protected virtual object GetValue(BaseDataReader reader)
  71. {
  72. object value;
  73. if (ValueType == typeof(Guid))
  74. {
  75. value = reader.GetGuid(m_fieldName);
  76. }
  77. else if (ValueType == typeof(bool))
  78. {
  79. uint boolVal = reader.GetUShort(m_fieldName);
  80. value = (boolVal == 1);
  81. }
  82. else
  83. if (ValueType == typeof(byte))
  84. {
  85. value = reader.GetByte(m_fieldName);
  86. }
  87. else if (ValueType == typeof(sbyte))
  88. {
  89. value = reader.GetSByte(m_fieldName);
  90. }
  91. else if (ValueType == typeof(ushort))
  92. {
  93. value = reader.GetUShort(m_fieldName);
  94. }
  95. else if (ValueType == typeof(uint))
  96. {
  97. value = reader.GetUInt32(m_fieldName);
  98. }
  99. else if (ValueType == typeof(byte[]))
  100. {
  101. value = reader.GetBytes(m_fieldName);
  102. }
  103. else
  104. {
  105. value = reader.Get(m_fieldName);
  106. }
  107. if (value is DBNull)
  108. {
  109. value = default(ValueType);
  110. }
  111. return value;
  112. }
  113. }
  114. public class ObjectField<TObject, TField> : BaseFieldMapper
  115. {
  116. private readonly ObjectGetAccessor<TObject, TField> m_fieldGetAccessor;
  117. private readonly ObjectSetAccessor<TObject, TField> m_fieldSetAccessor;
  118. public override object GetParamValue(object obj)
  119. {
  120. return m_fieldGetAccessor((TObject)obj);
  121. }
  122. public override void SetPropertyFromReader(object obj, BaseDataReader reader)
  123. {
  124. object value;
  125. value = GetValue(reader);
  126. if (value == null)
  127. {
  128. m_fieldSetAccessor((TObject)obj, default(TField));
  129. }
  130. else
  131. {
  132. m_fieldSetAccessor((TObject)obj, (TField)value);
  133. }
  134. }
  135. public ObjectField(BaseTableMapper tableMapper, string fieldName, ObjectGetAccessor<TObject, TField> rowMapperGetAccessor,
  136. ObjectSetAccessor<TObject, TField> rowMapperSetAccessor)
  137. : base(tableMapper, fieldName, typeof(TField))
  138. {
  139. m_fieldGetAccessor = rowMapperGetAccessor;
  140. m_fieldSetAccessor = rowMapperSetAccessor;
  141. }
  142. }
  143. }