BaseDataReader.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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.Framework.Data.Base
  31. {
  32. public class BaseDataReader
  33. {
  34. private readonly IDataReader m_source;
  35. public BaseDataReader(IDataReader source)
  36. {
  37. m_source = source;
  38. }
  39. public object Get(string name)
  40. {
  41. return m_source[name];
  42. }
  43. public ushort GetUShort(string name)
  44. {
  45. return (ushort)m_source.GetInt32(m_source.GetOrdinal(name));
  46. }
  47. public byte GetByte(string name)
  48. {
  49. int ordinal = m_source.GetOrdinal(name);
  50. byte value = (byte)m_source.GetInt16(ordinal);
  51. return value;
  52. }
  53. public sbyte GetSByte(string name)
  54. {
  55. return (sbyte)m_source.GetInt16(m_source.GetOrdinal(name));
  56. }
  57. public float GetFloat(string name)
  58. {
  59. return m_source.GetFloat(m_source.GetOrdinal(name));
  60. }
  61. public byte[] GetBytes(string name)
  62. {
  63. int ordinal = m_source.GetOrdinal(name);
  64. if (m_source.GetValue(ordinal) == DBNull.Value)
  65. {
  66. return null;
  67. }
  68. byte[] buffer = new byte[16384];
  69. MemoryStream memStream = new MemoryStream();
  70. long totalRead = 0;
  71. int bytesRead;
  72. do
  73. {
  74. bytesRead = (int)m_source.GetBytes(ordinal, totalRead, buffer, 0, buffer.Length);
  75. totalRead += bytesRead;
  76. memStream.Write(buffer, 0, bytesRead);
  77. } while (bytesRead == buffer.Length);
  78. return memStream.ToArray();
  79. }
  80. public string GetString(string name)
  81. {
  82. int ordinal = m_source.GetOrdinal(name);
  83. object value = m_source.GetValue(ordinal);
  84. if (value is DBNull)
  85. {
  86. return null;
  87. }
  88. return (string)value;
  89. }
  90. public bool Read()
  91. {
  92. return m_source.Read();
  93. }
  94. public Guid GetGuid(string name)
  95. {
  96. string guidString = GetString(name);
  97. if (String.IsNullOrEmpty(guidString))
  98. {
  99. return Guid.Empty;
  100. }
  101. else
  102. {
  103. return new Guid(guidString);
  104. }
  105. }
  106. }
  107. }