comparison Source/Mapping/DataReaderMapper.cs @ 0:f990fcb411a9

Копия текущей версии из github
author cin
date Thu, 27 Mar 2014 21:46:09 +0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f990fcb411a9
1 using System;
2 using System.Data;
3 using System.Data.SqlTypes;
4
5 namespace BLToolkit.Mapping
6 {
7 using Data;
8
9 public class DataReaderMapper : IMapDataSource
10 {
11 public DataReaderMapper(MappingSchema mappingSchema, IDataReader dataReader)
12 {
13 _mappingSchema = mappingSchema;
14 _dataReader = dataReader;
15 _dataReaderEx = dataReader as IDataReaderEx;
16 }
17
18 readonly IDataReaderEx _dataReaderEx;
19
20 readonly IDataReader _dataReader;
21 public IDataReader DataReader
22 {
23 get { return _dataReader; }
24 }
25
26 readonly MappingSchema _mappingSchema;
27 public MappingSchema MappingSchema
28 {
29 get { return _mappingSchema; }
30 }
31
32 #region IMapDataSource Members
33
34 public virtual int Count
35 {
36 get { return _dataReader.FieldCount; }
37 }
38
39 public virtual Type GetFieldType(int index)
40 {
41 return _dataReader.GetFieldType(index);
42 }
43
44 public virtual string GetName(int index)
45 {
46 return _dataReader.GetName(index);
47 }
48
49 public virtual int GetOrdinal(string name)
50 {
51 return _dataReader.GetOrdinal(name);
52 }
53
54 public virtual object GetValue(object o, int index)
55 {
56 var value = _dataReader.GetValue(index);
57 return value is DBNull? null: value;
58 }
59
60 public virtual object GetValue(object o, string name)
61 {
62 var value = _dataReader[name];
63 return value is DBNull? null: value;
64 }
65
66 public virtual bool IsNull (object o, int index) { return _dataReader.IsDBNull(index); }
67 public virtual bool SupportsTypedValues(int index) { return true; }
68
69 // Simple type getters.
70 //
71 [CLSCompliant(false)]
72 public virtual SByte GetSByte (object o, int index) { return _mappingSchema.ConvertToSByte(GetValue(o, index)); }
73 public virtual Int16 GetInt16 (object o, int index) { return _dataReader.GetInt16 (index); }
74 public virtual Int32 GetInt32 (object o, int index) { return _dataReader.GetInt32 (index); }
75 public virtual Int64 GetInt64 (object o, int index) { return _dataReader.GetInt64 (index); }
76
77 public virtual Byte GetByte (object o, int index) { return _dataReader.GetByte (index); }
78 [CLSCompliant(false)]
79 public virtual UInt16 GetUInt16 (object o, int index) { return _mappingSchema.ConvertToUInt16(GetValue(o, index)); }
80 [CLSCompliant(false)]
81 public virtual UInt32 GetUInt32 (object o, int index) { return _mappingSchema.ConvertToUInt32(GetValue(o, index)); }
82 [CLSCompliant(false)]
83 public virtual UInt64 GetUInt64 (object o, int index) { return _mappingSchema.ConvertToUInt64(GetValue(o, index)); }
84
85 public virtual Boolean GetBoolean (object o, int index) { return _dataReader.GetBoolean (index); }
86 public virtual Char GetChar (object o, int index) { return _dataReader.GetChar (index); }
87 public virtual Single GetSingle (object o, int index) { return _dataReader.GetFloat (index); }
88 public virtual Double GetDouble (object o, int index) { return _dataReader.GetDouble (index); }
89 public virtual Decimal GetDecimal (object o, int index) { return _dataReader.GetDecimal (index); }
90 public virtual Guid GetGuid (object o, int index) { return _dataReader.GetGuid (index); }
91 public virtual DateTime GetDateTime(object o, int index) { return _dataReader.GetDateTime(index); }
92
93 public virtual DateTimeOffset GetDateTimeOffset(object o, int index)
94 {
95 return _dataReaderEx != null?
96 _dataReaderEx.GetDateTimeOffset(index):
97 _mappingSchema.ConvertToDateTimeOffset(_dataReader.GetValue(index));
98 }
99
100 // Nullable type getters.
101 //
102 [CLSCompliant(false)]
103 public virtual SByte? GetNullableSByte (object o, int index) { return _dataReader.IsDBNull(index)? null: _mappingSchema.ConvertToNullableSByte(GetValue(o, index)); }
104 public virtual Int16? GetNullableInt16 (object o, int index) { return _dataReader.IsDBNull(index)? null: (Int16?)_dataReader.GetInt16 (index); }
105 public virtual Int32? GetNullableInt32 (object o, int index) { return _dataReader.IsDBNull(index)? null: (Int32?)_dataReader.GetInt32 (index); }
106 public virtual Int64? GetNullableInt64 (object o, int index) { return _dataReader.IsDBNull(index)? null: (Int64?)_dataReader.GetInt64 (index); }
107
108 public virtual Byte? GetNullableByte (object o, int index) { return _dataReader.IsDBNull(index)? null: (Byte?) _dataReader.GetByte (index); }
109 [CLSCompliant(false)]
110 public virtual UInt16? GetNullableUInt16 (object o, int index) { return _dataReader.IsDBNull(index)? null: _mappingSchema.ConvertToNullableUInt16(GetValue(o, index)); }
111 [CLSCompliant(false)]
112 public virtual UInt32? GetNullableUInt32 (object o, int index) { return _dataReader.IsDBNull(index)? null: _mappingSchema.ConvertToNullableUInt32(GetValue(o, index)); }
113 [CLSCompliant(false)]
114 public virtual UInt64? GetNullableUInt64 (object o, int index) { return _dataReader.IsDBNull(index)? null: _mappingSchema.ConvertToNullableUInt64(GetValue(o, index)); }
115
116 public virtual Boolean? GetNullableBoolean (object o, int index) { return _dataReader.IsDBNull(index)? null: (Boolean?) _dataReader.GetBoolean (index); }
117 public virtual Char? GetNullableChar (object o, int index) { return _dataReader.IsDBNull(index)? null: (Char?) _dataReader.GetChar (index); }
118 public virtual Single? GetNullableSingle (object o, int index) { return _dataReader.IsDBNull(index)? null: (Single?) _dataReader.GetFloat (index); }
119 public virtual Double? GetNullableDouble (object o, int index) { return _dataReader.IsDBNull(index)? null: (Double?) _dataReader.GetDouble (index); }
120 public virtual Decimal? GetNullableDecimal (object o, int index) { return _dataReader.IsDBNull(index)? null: (Decimal?) _dataReader.GetDecimal (index); }
121 public virtual Guid? GetNullableGuid (object o, int index) { return _dataReader.IsDBNull(index)? null: (Guid?) _dataReader.GetGuid (index); }
122 public virtual DateTime? GetNullableDateTime(object o, int index) { return _dataReader.IsDBNull(index)? null: (DateTime?)_dataReader.GetDateTime(index); }
123 public virtual DateTimeOffset? GetNullableDateTimeOffset(object o, int index)
124 {
125 return _dataReader.IsDBNull(index)? null:
126 _dataReaderEx != null? _dataReaderEx.GetDateTimeOffset(index):
127 _mappingSchema.ConvertToNullableDateTimeOffset(_dataReader.GetValue(index));
128 }
129
130 #if !SILVERLIGHT
131
132 // SQL type getters.
133 //
134 public virtual SqlByte GetSqlByte (object o, int index) { return _dataReader.IsDBNull(index)? SqlByte. Null: _dataReader.GetByte (index); }
135 public virtual SqlInt16 GetSqlInt16 (object o, int index) { return _dataReader.IsDBNull(index)? SqlInt16. Null: _dataReader.GetInt16 (index); }
136 public virtual SqlInt32 GetSqlInt32 (object o, int index) { return _dataReader.IsDBNull(index)? SqlInt32. Null: _dataReader.GetInt32 (index); }
137 public virtual SqlInt64 GetSqlInt64 (object o, int index) { return _dataReader.IsDBNull(index)? SqlInt64. Null: _dataReader.GetInt64 (index); }
138 public virtual SqlSingle GetSqlSingle (object o, int index) { return _dataReader.IsDBNull(index)? SqlSingle. Null: _dataReader.GetFloat (index); }
139 public virtual SqlBoolean GetSqlBoolean (object o, int index) { return _dataReader.IsDBNull(index)? SqlBoolean. Null: _dataReader.GetBoolean (index); }
140 public virtual SqlDouble GetSqlDouble (object o, int index) { return _dataReader.IsDBNull(index)? SqlDouble. Null: _dataReader.GetDouble (index); }
141 public virtual SqlDateTime GetSqlDateTime(object o, int index) { return _dataReader.IsDBNull(index)? SqlDateTime.Null: _dataReader.GetDateTime(index); }
142 public virtual SqlDecimal GetSqlDecimal (object o, int index) { return _dataReader.IsDBNull(index)? SqlDecimal. Null: _dataReader.GetDecimal (index); }
143 public virtual SqlMoney GetSqlMoney (object o, int index) { return _dataReader.IsDBNull(index)? SqlMoney. Null: _dataReader.GetDecimal (index); }
144 public virtual SqlGuid GetSqlGuid (object o, int index) { return _dataReader.IsDBNull(index)? SqlGuid. Null: _dataReader.GetGuid (index); }
145 public virtual SqlString GetSqlString (object o, int index) { return _dataReader.IsDBNull(index)? SqlString. Null: _dataReader.GetString (index); }
146
147 #endif
148
149 #endregion
150 }
151 }