aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/ThirdParty/TribalMedia/TribalMedia.Framework.Data/BaseDataReader.cs
diff options
context:
space:
mode:
Diffstat (limited to 'ThirdParty/TribalMedia/TribalMedia.Framework.Data/BaseDataReader.cs')
-rw-r--r--ThirdParty/TribalMedia/TribalMedia.Framework.Data/BaseDataReader.cs127
1 files changed, 127 insertions, 0 deletions
diff --git a/ThirdParty/TribalMedia/TribalMedia.Framework.Data/BaseDataReader.cs b/ThirdParty/TribalMedia/TribalMedia.Framework.Data/BaseDataReader.cs
new file mode 100644
index 0000000..34f894c
--- /dev/null
+++ b/ThirdParty/TribalMedia/TribalMedia.Framework.Data/BaseDataReader.cs
@@ -0,0 +1,127 @@
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
27using System;
28using System.Data;
29using System.IO;
30
31namespace TribalMedia.Framework.Data
32{
33 public class DataReader
34 {
35 private readonly IDataReader m_source;
36
37 public DataReader(IDataReader source)
38 {
39 m_source = source;
40 }
41
42 public object Get(string name)
43 {
44 return m_source[name];
45 }
46
47 public ushort GetUShort(string name)
48 {
49 return (ushort)m_source.GetInt32(m_source.GetOrdinal(name));
50 }
51
52 public byte GetByte(string name)
53 {
54 int ordinal = m_source.GetOrdinal(name);
55 byte value = (byte)m_source.GetInt16(ordinal);
56 return value;
57 }
58
59 public sbyte GetSByte(string name)
60 {
61 return (sbyte)m_source.GetInt16(m_source.GetOrdinal(name));
62 }
63
64 public float GetFloat(string name)
65 {
66 return m_source.GetFloat(m_source.GetOrdinal(name));
67 }
68
69 public byte[] GetBytes(string name)
70 {
71 int ordinal = m_source.GetOrdinal(name);
72
73 if (m_source.GetValue(ordinal) == DBNull.Value)
74 {
75 return null;
76 }
77
78 byte[] buffer = new byte[16384];
79
80 MemoryStream memStream = new MemoryStream();
81
82 long totalRead = 0;
83
84 int bytesRead;
85 do
86 {
87 bytesRead = (int)m_source.GetBytes(ordinal, totalRead, buffer, 0, buffer.Length);
88 totalRead += bytesRead;
89
90 memStream.Write(buffer, 0, bytesRead);
91 } while (bytesRead == buffer.Length);
92
93 return memStream.ToArray();
94 }
95
96 public string GetString(string name)
97 {
98 int ordinal = m_source.GetOrdinal(name);
99 object value = m_source.GetValue(ordinal);
100
101 if (value is DBNull)
102 {
103 return null;
104 }
105
106 return (string)value;
107 }
108
109 public bool Read()
110 {
111 return m_source.Read();
112 }
113
114 public Guid GetGuid(string name)
115 {
116 string guidString = GetString(name);
117 if (String.IsNullOrEmpty(guidString))
118 {
119 return Guid.Empty;
120 }
121 else
122 {
123 return new Guid(guidString);
124 }
125 }
126 }
127} \ No newline at end of file