BaseFieldMapper.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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.Framework.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(byte[]))
  96. {
  97. value = reader.GetBytes(m_fieldName);
  98. }
  99. else
  100. {
  101. value = reader.Get(m_fieldName);
  102. }
  103. if (value is DBNull)
  104. {
  105. value = default(ValueType);
  106. }
  107. return value;
  108. }
  109. }
  110. public class ObjectField<TObject, TField> : BaseFieldMapper
  111. {
  112. private readonly ObjectGetAccessor<TObject, TField> m_fieldGetAccessor;
  113. private readonly ObjectSetAccessor<TObject, TField> m_fieldSetAccessor;
  114. public override object GetParamValue(object obj)
  115. {
  116. return m_fieldGetAccessor((TObject)obj);
  117. }
  118. public override void SetPropertyFromReader(object obj, BaseDataReader reader)
  119. {
  120. object value;
  121. value = GetValue(reader);
  122. if (value == null)
  123. {
  124. m_fieldSetAccessor((TObject)obj, default(TField));
  125. }
  126. else
  127. {
  128. m_fieldSetAccessor((TObject)obj, (TField)value);
  129. }
  130. }
  131. public ObjectField(BaseTableMapper tableMapper, string fieldName, ObjectGetAccessor<TObject, TField> rowMapperGetAccessor,
  132. ObjectSetAccessor<TObject, TField> rowMapperSetAccessor)
  133. : base(tableMapper, fieldName, typeof(TField))
  134. {
  135. m_fieldGetAccessor = rowMapperGetAccessor;
  136. m_fieldSetAccessor = rowMapperSetAccessor;
  137. }
  138. }
  139. }