|
1 | | -using System.Collections.Generic; |
2 | | -using System.Linq; |
3 | | -using System.Data; |
4 | | -using Simple.Data.Extensions; |
5 | | - |
6 | | -namespace Simple.Data.Ado |
7 | | -{ |
8 | | - public static class DataReaderExtensions |
9 | | - { |
10 | | - public static IDictionary<string,object> ToDictionary(this IDataReader dataReader) |
11 | | - { |
12 | | - return dataReader.ToDictionary(dataReader.CreateDictionaryIndex()); |
13 | | - } |
14 | | - |
15 | | - public static IEnumerable<IDictionary<string, object>> ToDictionaries(this IDataReader reader) |
16 | | - { |
17 | | - using (reader) |
18 | | - { |
19 | | - return ToDictionariesImpl(reader).ToArray().AsEnumerable(); |
20 | | - } |
21 | | - } |
22 | | - |
23 | | - public static IEnumerable<IEnumerable<IDictionary<string, object>>> ToMultipleDictionaries(this IDataReader reader) |
24 | | - { |
25 | | - using (reader) |
26 | | - { |
27 | | - return ToMultipleDictionariesImpl(reader).ToArray().AsEnumerable(); |
28 | | - } |
29 | | - } |
30 | | - |
31 | | - private static IEnumerable<IEnumerable<IDictionary<string,object>>> ToMultipleDictionariesImpl(IDataReader reader) |
32 | | - { |
33 | | - do |
34 | | - { |
35 | | - yield return ToDictionariesImpl(reader).ToArray().AsEnumerable(); |
36 | | - } while (reader.NextResult()); |
37 | | - |
38 | | - } |
39 | | - |
40 | | - private static IEnumerable<IDictionary<string,object>> ToDictionariesImpl(IDataReader reader) |
41 | | - { |
42 | | - var index = reader.CreateDictionaryIndex(); |
43 | | - var values = new object[reader.FieldCount]; |
44 | | - while (reader.Read()) |
45 | | - { |
46 | | - reader.GetValues(values); |
47 | | - yield return OptimizedDictionary.Create(index, values); |
48 | | - } |
49 | | - } |
50 | | - |
51 | | - public static IEnumerable<object> ToDynamicList(this IDataReader reader) |
52 | | - { |
53 | | - var list = new List<object>(); |
54 | | - using (reader) |
55 | | - { |
56 | | - while (reader.Read()) |
57 | | - { |
58 | | - list.Add(reader.ToDynamicRecord()); |
59 | | - } |
60 | | - } |
61 | | - |
62 | | - return list.AsEnumerable(); |
63 | | - } |
64 | | - |
65 | | - public static IEnumerable<object> ToDynamicList(this IDataReader reader, Database database, string tableName) |
66 | | - { |
67 | | - var list = new List<object>(); |
68 | | - using (reader) |
69 | | - { |
70 | | - while (reader.Read()) |
71 | | - { |
72 | | - list.Add(reader.ToDynamicRecord(tableName, database)); |
73 | | - } |
74 | | - } |
75 | | - |
76 | | - return list.AsEnumerable(); |
77 | | - } |
78 | | - } |
79 | | -} |
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Linq; |
| 3 | +using System.Data; |
| 4 | +using Simple.Data.Extensions; |
| 5 | + |
| 6 | +namespace Simple.Data.Ado |
| 7 | +{ |
| 8 | + public static class DataReaderExtensions |
| 9 | + { |
| 10 | + public static IDictionary<string,object> ToDictionary(this IDataReader dataReader) |
| 11 | + { |
| 12 | + return dataReader.ToDictionary(dataReader.CreateDictionaryIndex()); |
| 13 | + } |
| 14 | + |
| 15 | + public static IEnumerable<IDictionary<string, object>> ToDictionaries(this IDataReader reader) |
| 16 | + { |
| 17 | + using (reader) |
| 18 | + { |
| 19 | + return ToDictionariesImpl(reader).ToArray().AsEnumerable(); |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + public static IEnumerable<IEnumerable<IDictionary<string, object>>> ToMultipleDictionaries(this IDataReader reader) |
| 24 | + { |
| 25 | + using (reader) |
| 26 | + { |
| 27 | + return ToMultipleDictionariesImpl(reader).ToArray().AsEnumerable(); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + private static IEnumerable<IEnumerable<IDictionary<string,object>>> ToMultipleDictionariesImpl(IDataReader reader) |
| 32 | + { |
| 33 | + do |
| 34 | + { |
| 35 | + yield return ToDictionariesImpl(reader).ToArray().AsEnumerable(); |
| 36 | + } while (reader.NextResult()); |
| 37 | + |
| 38 | + } |
| 39 | + |
| 40 | + private static IEnumerable<IDictionary<string,object>> ToDictionariesImpl(IDataReader reader) |
| 41 | + { |
| 42 | + var index = reader.CreateDictionaryIndex(); |
| 43 | + var values = new object[reader.FieldCount]; |
| 44 | + while (reader.Read()) |
| 45 | + { |
| 46 | + reader.GetValues(values); |
| 47 | + |
| 48 | + for (var i = 0; i < reader.FieldCount; i++) |
| 49 | + { |
| 50 | + var value = values[i]; |
| 51 | + if (value is DBNull) |
| 52 | + values[i] = null; |
| 53 | + } |
| 54 | + |
| 55 | + yield return OptimizedDictionary.Create(index, values); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + public static IEnumerable<object> ToDynamicList(this IDataReader reader) |
| 60 | + { |
| 61 | + var list = new List<object>(); |
| 62 | + using (reader) |
| 63 | + { |
| 64 | + while (reader.Read()) |
| 65 | + { |
| 66 | + list.Add(reader.ToDynamicRecord()); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + return list.AsEnumerable(); |
| 71 | + } |
| 72 | + |
| 73 | + public static IEnumerable<object> ToDynamicList(this IDataReader reader, Database database, string tableName) |
| 74 | + { |
| 75 | + var list = new List<object>(); |
| 76 | + using (reader) |
| 77 | + { |
| 78 | + while (reader.Read()) |
| 79 | + { |
| 80 | + list.Add(reader.ToDynamicRecord(tableName, database)); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + return list.AsEnumerable(); |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments