BaseDataReader.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright (c) Tribal Media AB, http://tribalmedia.se/
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. * * Redistributions of source code must retain the above copyright
  7. * notice, this list of conditions and the following disclaimer.
  8. * * Redistributions in binary form must reproduce the above copyright
  9. * notice, this list of conditions and the following disclaimer in the
  10. * documentation and/or other materials provided with the distribution.
  11. * * The name of Tribal Media AB may not be used to endorse or promote products
  12. * derived from this software without specific prior written permission.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  15. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  18. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  19. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  20. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  21. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. *
  25. */
  26. using System;
  27. using System.Data;
  28. using System.IO;
  29. namespace TribalMedia.Framework.Data
  30. {
  31. public class BaseDataReader
  32. {
  33. private readonly IDataReader m_source;
  34. public BaseDataReader(IDataReader source)
  35. {
  36. m_source = source;
  37. }
  38. public object Get(string name)
  39. {
  40. return m_source[name];
  41. }
  42. public ushort GetUShort(string name)
  43. {
  44. return (ushort)m_source.GetInt32(m_source.GetOrdinal(name));
  45. }
  46. public byte GetByte(string name)
  47. {
  48. int ordinal = m_source.GetOrdinal(name);
  49. byte value = (byte)m_source.GetInt16(ordinal);
  50. return value;
  51. }
  52. public sbyte GetSByte(string name)
  53. {
  54. return (sbyte)m_source.GetInt16(m_source.GetOrdinal(name));
  55. }
  56. public float GetFloat(string name)
  57. {
  58. return m_source.GetFloat(m_source.GetOrdinal(name));
  59. }
  60. public byte[] GetBytes(string name)
  61. {
  62. int ordinal = m_source.GetOrdinal(name);
  63. if (m_source.GetValue(ordinal) == DBNull.Value)
  64. {
  65. return null;
  66. }
  67. byte[] buffer = new byte[16384];
  68. MemoryStream memStream = new MemoryStream();
  69. long totalRead = 0;
  70. int bytesRead;
  71. do
  72. {
  73. bytesRead = (int)m_source.GetBytes(ordinal, totalRead, buffer, 0, buffer.Length);
  74. totalRead += bytesRead;
  75. memStream.Write(buffer, 0, bytesRead);
  76. } while (bytesRead == buffer.Length);
  77. return memStream.ToArray();
  78. }
  79. public string GetString(string name)
  80. {
  81. int ordinal = m_source.GetOrdinal(name);
  82. object value = m_source.GetValue(ordinal);
  83. if (value is DBNull)
  84. {
  85. return null;
  86. }
  87. return (string)value;
  88. }
  89. public bool Read()
  90. {
  91. return m_source.Read();
  92. }
  93. public Guid GetGuid(string name)
  94. {
  95. string guidString = GetString(name);
  96. if (String.IsNullOrEmpty(guidString))
  97. {
  98. return Guid.Empty;
  99. }
  100. else
  101. {
  102. return new Guid(guidString);
  103. }
  104. }
  105. }
  106. }