1
0

BaseDataReader.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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.Data;
  29. using System.IO;
  30. namespace OpenSim.Data.Base
  31. {
  32. /// <summary>
  33. ///
  34. /// </summary>
  35. public abstract class BaseDataReader
  36. {
  37. private readonly IDataReader m_source;
  38. /// <summary>
  39. ///
  40. /// </summary>
  41. /// <param name="source"></param>
  42. public BaseDataReader(IDataReader source)
  43. {
  44. m_source = source;
  45. }
  46. /// <summary>
  47. ///
  48. /// </summary>
  49. /// <param name="name"></param>
  50. /// <returns></returns>
  51. public object Get(string name)
  52. {
  53. return m_source[name];
  54. }
  55. /// <summary>
  56. ///
  57. /// </summary>
  58. /// <param name="name"></param>
  59. /// <returns></returns>
  60. public ushort GetUShort(string name)
  61. {
  62. return (ushort)m_source.GetInt32(m_source.GetOrdinal(name));
  63. }
  64. /// <summary>
  65. ///
  66. /// </summary>
  67. /// <param name="name"></param>
  68. /// <returns></returns>
  69. public byte GetByte(string name)
  70. {
  71. int ordinal = m_source.GetOrdinal(name);
  72. byte value = (byte)m_source.GetInt16(ordinal);
  73. return value;
  74. }
  75. /// <summary>
  76. ///
  77. /// </summary>
  78. /// <param name="name"></param>
  79. /// <returns></returns>
  80. public sbyte GetSByte(string name)
  81. {
  82. return (sbyte)m_source.GetInt16(m_source.GetOrdinal(name));
  83. }
  84. /// <summary>
  85. ///
  86. /// </summary>
  87. /// <param name="name"></param>
  88. /// <returns></returns>
  89. public float GetFloat(string name)
  90. {
  91. return m_source.GetFloat(m_source.GetOrdinal(name));
  92. }
  93. /// <summary>
  94. ///
  95. /// </summary>
  96. /// <param name="name"></param>
  97. /// <returns></returns>
  98. public byte[] GetBytes(string name)
  99. {
  100. int ordinal = m_source.GetOrdinal(name);
  101. if (m_source.GetValue(ordinal) == DBNull.Value)
  102. {
  103. return null;
  104. }
  105. byte[] buffer = new byte[16384];
  106. MemoryStream memStream = new MemoryStream();
  107. long totalRead = 0;
  108. int bytesRead;
  109. do
  110. {
  111. bytesRead = (int)m_source.GetBytes(ordinal, totalRead, buffer, 0, buffer.Length);
  112. totalRead += bytesRead;
  113. memStream.Write(buffer, 0, bytesRead);
  114. } while (bytesRead == buffer.Length);
  115. return memStream.ToArray();
  116. }
  117. /// <summary>
  118. ///
  119. /// </summary>
  120. /// <param name="name"></param>
  121. /// <returns></returns>
  122. public string GetString(string name)
  123. {
  124. int ordinal = m_source.GetOrdinal(name);
  125. object value = m_source.GetValue(ordinal);
  126. if (value is DBNull)
  127. {
  128. return null;
  129. }
  130. return (string)value;
  131. }
  132. /// <summary>
  133. ///
  134. /// </summary>
  135. /// <returns></returns>
  136. public bool Read()
  137. {
  138. return m_source.Read();
  139. }
  140. /// <summary>
  141. ///
  142. /// </summary>
  143. /// <param name="name"></param>
  144. /// <returns></returns>
  145. public virtual Guid GetGuid(string name)
  146. {
  147. return m_source.GetGuid(m_source.GetOrdinal(name));
  148. }
  149. /// <summary>
  150. ///
  151. /// </summary>
  152. /// <param name="name"></param>
  153. /// <returns></returns>
  154. public UInt32 GetUInt32(string name)
  155. {
  156. return (UInt32)GetInt32(name);
  157. }
  158. /// <summary>
  159. ///
  160. /// </summary>
  161. /// <param name="name"></param>
  162. /// <returns></returns>
  163. private Int32 GetInt32(string name)
  164. {
  165. int ordinal = m_source.GetOrdinal(name);
  166. int int32 = m_source.GetInt32(ordinal);
  167. return int32;
  168. }
  169. /// <summary>
  170. ///
  171. /// </summary>
  172. /// <param name="name"></param>
  173. /// <returns></returns>
  174. public Int64 GetInt64(string name)
  175. {
  176. int ordinal = m_source.GetOrdinal(name);
  177. long int64 = m_source.GetInt64(ordinal);
  178. return int64;
  179. }
  180. }
  181. }