Skip to content

Commit bc4cc05

Browse files
David EllingsworthDavid Ellingsworth
David Ellingsworth
authored and
David Ellingsworth
committed
GH-3530: Use type specific methods of DbDataReader where possible and
use Convert with the user provided locale if necessary.
1 parent 4e714b9 commit bc4cc05

39 files changed

+494
-54
lines changed
Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
1+
using System;
2+
using System.Data.Common;
3+
4+
namespace NHibernate.AdoNet
5+
{
6+
internal static class DbRecordSetExtensions
7+
{
8+
public static bool TryGetBoolean(this DbDataReader rs, int ordinal, out bool value)
9+
{
10+
try
11+
{
12+
value = rs.GetBoolean(ordinal);
13+
return true;
14+
}
15+
catch (InvalidCastException)
16+
{
17+
value = default;
18+
return false;
19+
}
20+
}
21+
22+
public static bool TryGetByte(this DbDataReader rs, int ordinal, out byte value)
23+
{
24+
try
25+
{
26+
value = rs.GetByte(ordinal);
27+
return true;
28+
}
29+
catch (InvalidCastException)
30+
{
31+
value = default;
32+
return false;
33+
}
34+
}
35+
36+
public static bool TryGetChar(this DbDataReader rs, int ordinal, out char value)
37+
{
38+
try
39+
{
40+
value = rs.GetChar(ordinal);
41+
return true;
42+
}
43+
catch (InvalidCastException)
44+
{
45+
value = default;
46+
return false;
47+
}
48+
}
49+
50+
public static bool TryGetDecimal(this DbDataReader rs, int ordinal, out decimal value)
51+
{
52+
try
53+
{
54+
value = rs.GetDecimal(ordinal);
55+
return true;
56+
}
57+
catch (InvalidCastException)
58+
{
59+
value = default;
60+
return false;
61+
}
62+
}
63+
64+
public static bool TryGetDouble(this DbDataReader rs, int ordinal, out double value)
65+
{
66+
try
67+
{
68+
value = rs.GetDouble(ordinal);
69+
return true;
70+
}
71+
catch (InvalidCastException)
72+
{
73+
value = default;
74+
return false;
75+
}
76+
}
77+
78+
public static bool TryGetDateTime(this DbDataReader rs, int ordinal, out DateTime value)
79+
{
80+
try
81+
{
82+
value = rs.GetDateTime(ordinal);
83+
return true;
84+
}
85+
catch (InvalidCastException)
86+
{
87+
value = default;
88+
return false;
89+
}
90+
}
91+
public static bool TryGetFloat(this DbDataReader rs, int ordinal, out float value)
92+
{
93+
try
94+
{
95+
value = rs.GetFloat(ordinal);
96+
return true;
97+
}
98+
catch (InvalidCastException)
99+
{
100+
value = default;
101+
return false;
102+
}
103+
}
104+
public static bool TryGetGuid(this DbDataReader rs, int ordinal, out Guid value)
105+
{
106+
try
107+
{
108+
value = rs.GetGuid(ordinal);
109+
return true;
110+
}
111+
catch (InvalidCastException)
112+
{
113+
value = default;
114+
return false;
115+
}
116+
}
117+
118+
public static bool TryGetUInt16(this DbDataReader rs, int ordinal, out ushort value)
119+
{
120+
var dbValue = rs[ordinal];
121+
122+
if (dbValue is ushort)
123+
{
124+
value = (ushort) dbValue;
125+
return true;
126+
}
127+
128+
value = default;
129+
return false;
130+
}
131+
132+
public static bool TryGetInt16(this DbDataReader rs, int ordinal, out short value)
133+
{
134+
try
135+
{
136+
value = rs.GetInt16(ordinal);
137+
return true;
138+
}
139+
catch (InvalidCastException)
140+
{
141+
value = default;
142+
return false;
143+
}
144+
}
145+
public static bool TryGetInt32(this DbDataReader rs, int ordinal, out int value)
146+
{
147+
try
148+
{
149+
value = rs.GetInt32(ordinal);
150+
return true;
151+
}
152+
catch (InvalidCastException)
153+
{
154+
value = default;
155+
return false;
156+
}
157+
}
158+
159+
public static bool TryGetUInt32(this DbDataReader rs, int ordinal, out uint value)
160+
{
161+
var dbValue = rs[ordinal];
162+
163+
if (dbValue is uint)
164+
{
165+
value = (uint) dbValue;
166+
return true;
167+
}
168+
169+
value = default;
170+
return false;
171+
}
172+
173+
public static bool TryGetInt64(this DbDataReader rs, int ordinal, out long value)
174+
{
175+
try
176+
{
177+
value = rs.GetInt64(ordinal);
178+
return true;
179+
}
180+
catch (InvalidCastException)
181+
{
182+
value = default;
183+
return false;
184+
}
185+
}
186+
187+
public static bool TryGetUInt64(this DbDataReader rs, int ordinal, out ulong value)
188+
{
189+
var dbValue = rs[ordinal];
190+
191+
if (dbValue is ulong)
192+
{
193+
value = (ulong) dbValue;
194+
return true;
195+
}
196+
197+
value = default;
198+
return false;
199+
}
200+
201+
public static bool TryGetSByte(this DbDataReader rs, int ordinal, out sbyte value)
202+
{
203+
var dbValue = rs[ordinal];
204+
205+
if (dbValue is sbyte)
206+
{
207+
value = (sbyte) rs[ordinal];
208+
return true;
209+
}
210+
211+
value = default;
212+
return false;
213+
}
214+
215+
public static bool TryGetString(this DbDataReader rs, int ordinal, out string value)
216+
{
217+
try
218+
{
219+
value = rs.GetString(ordinal);
220+
return true;
221+
}
222+
catch (InvalidCastException)
223+
{
224+
value = default;
225+
return false;
226+
}
227+
}
228+
229+
public static bool TryGetTimeSpan(this DbDataReader rs, int ordinal, out TimeSpan value)
230+
{
231+
var dbValue = rs[ordinal];
232+
233+
if (dbValue is TimeSpan)
234+
{
235+
value = (TimeSpan) dbValue;
236+
return true;
237+
}
238+
239+
value = default;
240+
return false;
241+
}
242+
}
243+
}

src/NHibernate/Async/Type/AbstractDateTimeType.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using System.Collections.Generic;
1414
using System.Data.Common;
1515
using System.Globalization;
16+
using NHibernate.AdoNet;
1617
using NHibernate.Engine;
1718
using NHibernate.SqlTypes;
1819

src/NHibernate/Async/Type/ByteType.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using System.Data;
1414
using System.Data.Common;
1515
using System.Numerics;
16+
using NHibernate.AdoNet;
1617
using NHibernate.Engine;
1718
using NHibernate.SqlTypes;
1819

src/NHibernate/Async/Type/Int16Type.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using System.Collections.Generic;
1717
using System.Data;
1818
using System.Numerics;
19+
using NHibernate.AdoNet;
1920

2021
namespace NHibernate.Type
2122
{

src/NHibernate/Async/Type/Int32Type.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using System.Collections.Generic;
1717
using System.Data;
1818
using System.Numerics;
19+
using NHibernate.AdoNet;
1920

2021
namespace NHibernate.Type
2122
{

src/NHibernate/Async/Type/Int64Type.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using System.Data;
1515
using System.Data.Common;
1616
using System.Numerics;
17+
using NHibernate.AdoNet;
1718
using NHibernate.Engine;
1819
using NHibernate.SqlTypes;
1920

src/NHibernate/Async/Type/TicksType.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using System;
1212
using System.Data;
1313
using System.Data.Common;
14+
using NHibernate.AdoNet;
1415
using NHibernate.Engine;
1516
using NHibernate.SqlTypes;
1617

src/NHibernate/Async/Type/TimeAsTimeSpanType.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using NHibernate.SqlTypes;
1616
using System.Collections.Generic;
1717
using System.Data;
18+
using NHibernate.AdoNet;
1819

1920
namespace NHibernate.Type
2021
{

src/NHibernate/Async/Type/TimeSpanType.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using NHibernate.SqlTypes;
1616
using System.Collections.Generic;
1717
using System.Data;
18+
using NHibernate.AdoNet;
1819

1920
namespace NHibernate.Type
2021
{

src/NHibernate/Async/Type/UInt16Type.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using System.Data;
1515
using System.Data.Common;
1616
using System.Numerics;
17+
using NHibernate.AdoNet;
1718
using NHibernate.Engine;
1819
using NHibernate.SqlTypes;
1920

src/NHibernate/Async/Type/UInt32Type.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using System.Data;
1515
using System.Data.Common;
1616
using System.Numerics;
17+
using NHibernate.AdoNet;
1718
using NHibernate.Engine;
1819
using NHibernate.SqlTypes;
1920

src/NHibernate/Async/Type/UInt64Type.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using System.Data;
1515
using System.Data.Common;
1616
using System.Numerics;
17+
using NHibernate.AdoNet;
1718
using NHibernate.Engine;
1819
using NHibernate.SqlTypes;
1920

src/NHibernate/Async/Type/UriType.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
using System;
1212
using System.Data.Common;
13+
using NHibernate.AdoNet;
1314
using NHibernate.Engine;
1415
using NHibernate.SqlTypes;
1516

src/NHibernate/Async/Type/XDocType.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using System;
1212
using System.Data.Common;
1313
using System.Xml.Linq;
14+
using NHibernate.AdoNet;
1415
using NHibernate.Engine;
1516
using NHibernate.SqlTypes;
1617

src/NHibernate/Async/Type/XmlDocType.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using System;
1212
using System.Data.Common;
1313
using System.Xml;
14+
using NHibernate.AdoNet;
1415
using NHibernate.Engine;
1516
using NHibernate.SqlTypes;
1617

src/NHibernate/Type/AbstractCharType.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Data.Common;
3+
using NHibernate.AdoNet;
34
using NHibernate.Engine;
45
using NHibernate.SqlTypes;
56

@@ -20,12 +21,24 @@ public AbstractCharType(SqlType sqlType) : base(sqlType)
2021

2122
public override object Get(DbDataReader rs, int index, ISessionImplementor session)
2223
{
23-
string dbValue = Convert.ToString(rs[index]);
24+
if (rs.TryGetChar(index, out var dbValue))
25+
{
26+
return dbValue;
27+
}
28+
29+
if (!rs.TryGetString(index, out var strValue))
30+
{
31+
var locale = session.Factory.Settings.Locale;
32+
33+
strValue = Convert.ToString(rs[index], locale);
34+
}
35+
2436
// The check of the Length is a workaround see NH-2340
25-
if (dbValue.Length > 0)
37+
if (strValue.Length > 0)
2638
{
27-
return dbValue[0];
39+
return strValue[0];
2840
}
41+
2942
return '\0'; // This line should never be executed
3043
}
3144

0 commit comments

Comments
 (0)