0
|
1 using System;
|
|
2 using System.Collections.Generic;
|
|
3 using System.Data;
|
|
4 using System.Globalization;
|
|
5
|
|
6 namespace BLToolkit.ServiceModel
|
|
7 {
|
|
8 using Common;
|
|
9
|
|
10 class ServiceModelDataReader : IDataReader
|
|
11 {
|
|
12 public ServiceModelDataReader(LinqServiceResult result)
|
|
13 {
|
|
14 _result = result;
|
|
15
|
|
16 for (var i = 0; i < result.FieldNames.Length; i++)
|
|
17 _ordinal.Add(result.FieldNames[i], i);
|
|
18 }
|
|
19
|
|
20 readonly LinqServiceResult _result;
|
|
21 readonly Dictionary<string,int> _ordinal = new Dictionary<string,int>();
|
|
22
|
|
23 string[] _data;
|
|
24 int _current = -1;
|
|
25
|
|
26 #region IDataReader Members
|
|
27
|
|
28 public void Close()
|
|
29 {
|
|
30 }
|
|
31
|
|
32 public int Depth
|
|
33 {
|
|
34 get { return 0; }
|
|
35 }
|
|
36
|
|
37 #if !SILVERLIGHT
|
|
38
|
|
39 public DataTable GetSchemaTable()
|
|
40 {
|
|
41 throw new InvalidOperationException();
|
|
42 }
|
|
43
|
|
44 #endif
|
|
45
|
|
46 public bool IsClosed
|
|
47 {
|
|
48 get { throw new InvalidOperationException(); }
|
|
49 }
|
|
50
|
|
51 public bool NextResult()
|
|
52 {
|
|
53 throw new InvalidOperationException();
|
|
54 }
|
|
55
|
|
56 public bool Read()
|
|
57 {
|
|
58 if (++_current < _result.RowCount)
|
|
59 {
|
|
60 _data = _result.Data[_current];
|
|
61
|
|
62 return true;
|
|
63 }
|
|
64
|
|
65 _data = null;
|
|
66
|
|
67 return false;
|
|
68 }
|
|
69
|
|
70 public int RecordsAffected
|
|
71 {
|
|
72 get { throw new InvalidOperationException(); }
|
|
73 }
|
|
74
|
|
75 #endregion
|
|
76
|
|
77 #region IDisposable Members
|
|
78
|
|
79 public void Dispose()
|
|
80 {
|
|
81 }
|
|
82
|
|
83 #endregion
|
|
84
|
|
85 #region IDataRecord Members
|
|
86
|
|
87 public int FieldCount
|
|
88 {
|
|
89 get { return _result.FieldCount; }
|
|
90 }
|
|
91
|
|
92 public bool GetBoolean(int i)
|
|
93 {
|
|
94 return bool.Parse(_data[i]);
|
|
95 }
|
|
96
|
|
97 public byte GetByte(int i)
|
|
98 {
|
|
99 return byte.Parse(_data[i]);
|
|
100 }
|
|
101
|
|
102 public long GetBytes(int i, long fieldOffset, byte[] buffer, int bufferoffset, int length)
|
|
103 {
|
|
104 throw new InvalidOperationException();
|
|
105 }
|
|
106
|
|
107 public char GetChar(int i)
|
|
108 {
|
|
109 return _data[i][0];
|
|
110 }
|
|
111
|
|
112 public long GetChars(int i, long fieldoffset, char[] buffer, int bufferoffset, int length)
|
|
113 {
|
|
114 throw new InvalidOperationException();
|
|
115 }
|
|
116
|
|
117 public IDataReader GetData(int i)
|
|
118 {
|
|
119 throw new InvalidOperationException();
|
|
120 }
|
|
121
|
|
122 public string GetDataTypeName(int i)
|
|
123 {
|
|
124 return _result.FieldTypes[i].FullName;
|
|
125 }
|
|
126
|
|
127 public DateTime GetDateTime(int i)
|
|
128 {
|
|
129 return DateTime.Parse(_data[i], CultureInfo.InvariantCulture);
|
|
130 }
|
|
131
|
|
132 public decimal GetDecimal(int i)
|
|
133 {
|
|
134 return decimal.Parse(_data[i], CultureInfo.InvariantCulture);
|
|
135 }
|
|
136
|
|
137 public double GetDouble(int i)
|
|
138 {
|
|
139 return double.Parse(_data[i], CultureInfo.InvariantCulture);
|
|
140 }
|
|
141
|
|
142 public Type GetFieldType(int i)
|
|
143 {
|
|
144 return _result.FieldTypes[i];
|
|
145 }
|
|
146
|
|
147 public float GetFloat(int i)
|
|
148 {
|
|
149 return float.Parse(_data[i], CultureInfo.InvariantCulture);
|
|
150 }
|
|
151
|
|
152 public Guid GetGuid(int i)
|
|
153 {
|
|
154 return new Guid(_data[i]);
|
|
155 }
|
|
156
|
|
157 public short GetInt16(int i)
|
|
158 {
|
|
159 return short.Parse(_data[i]);
|
|
160 }
|
|
161
|
|
162 public int GetInt32(int i)
|
|
163 {
|
|
164 return int.Parse(_data[i]);
|
|
165 }
|
|
166
|
|
167 public long GetInt64(int i)
|
|
168 {
|
|
169 return long.Parse(_data[i]);
|
|
170 }
|
|
171
|
|
172 public string GetName(int i)
|
|
173 {
|
|
174 return _result.FieldNames[i];
|
|
175 }
|
|
176
|
|
177 public int GetOrdinal(string name)
|
|
178 {
|
|
179 return _ordinal[name];
|
|
180 }
|
|
181
|
|
182 public string GetString(int i)
|
|
183 {
|
|
184 return _data[i];
|
|
185 }
|
|
186
|
|
187 public object GetValue(int i)
|
|
188 {
|
|
189 var type = _result.FieldTypes[i];
|
|
190 var value = _data[i];
|
|
191
|
|
192 if (_result.VaryingTypes.Length > 0 && !string.IsNullOrEmpty(value) && value[0] == '\0')
|
|
193 {
|
|
194 type = _result.VaryingTypes[value[1]];
|
|
195 value = value.Substring(2);
|
|
196 }
|
|
197
|
|
198 if (value == null)
|
|
199 return null;
|
|
200
|
|
201 if (type.IsArray && type == typeof(byte[]))
|
|
202 return System.Convert.FromBase64String(value);
|
|
203
|
|
204 switch (Type.GetTypeCode(type))
|
|
205 {
|
|
206 case TypeCode.String : return value;
|
|
207 case TypeCode.Double : return double. Parse(value, CultureInfo.InvariantCulture);
|
|
208 case TypeCode.Decimal : return decimal. Parse(value, CultureInfo.InvariantCulture);
|
|
209 case TypeCode.Single : return float. Parse(value, CultureInfo.InvariantCulture);
|
|
210 case TypeCode.DateTime : return DateTime.Parse(value, CultureInfo.InvariantCulture);
|
|
211 case TypeCode.Object :
|
|
212 if (type == typeof(double?)) return double. Parse(value, CultureInfo.InvariantCulture);
|
|
213 if (type == typeof(decimal?)) return decimal. Parse(value, CultureInfo.InvariantCulture);
|
|
214 if (type == typeof(float?)) return float. Parse(value, CultureInfo.InvariantCulture);
|
|
215 if (type == typeof(DateTime?)) return DateTime. Parse(value, CultureInfo.InvariantCulture);
|
|
216
|
|
217 if (type == typeof(DateTimeOffset) || type == typeof(DateTimeOffset?))
|
|
218 return DateTimeOffset.Parse(value, CultureInfo.InvariantCulture);
|
|
219 break;
|
|
220 }
|
|
221
|
|
222 return Convert.ChangeTypeFromString(value, type);
|
|
223 }
|
|
224
|
|
225 public int GetValues(object[] values)
|
|
226 {
|
|
227 throw new InvalidOperationException();
|
|
228 }
|
|
229
|
|
230 public bool IsDBNull(int i)
|
|
231 {
|
|
232 return _data[i] == null;
|
|
233 }
|
|
234
|
|
235 public object this[string name]
|
|
236 {
|
|
237 get { return GetValue(GetOrdinal(name)); }
|
|
238 }
|
|
239
|
|
240 public object this[int i]
|
|
241 {
|
|
242 get { return GetValue(i); }
|
|
243 }
|
|
244
|
|
245 #endregion
|
|
246 }
|
|
247 }
|