forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReflectionBasedSqlStateExtracter.cs
63 lines (59 loc) · 1.95 KB
/
ReflectionBasedSqlStateExtracter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using System.Reflection;
using System.Collections;
using System.Data.Common;
namespace NHibernate.Exceptions
{
class ReflectionBasedSqlStateExtracter: SqlStateExtracter
{
/* OdbcException, OleDbException, IfxException, Db2Exception, and possible others
* have Errors collection which contains fields: NativeError and SQLState
* These fields can be extracted using reflection
*/
public override int ExtractSingleErrorCode(DbException sqle)
{
System.Type type;
PropertyInfo pi;
int nativeError;
type = sqle.GetType();
pi = type.GetProperty("Errors");
if (pi == null) // there is no Errors property
{
return 0;
}
nativeError = 0;
foreach (object o in (pi.GetValue(sqle, null) as IEnumerable))
{
pi = o.GetType().GetProperty("NativeError");
if (pi == null)
return 0;
nativeError = (int)pi.GetValue(o, null);
if (nativeError != 0)
break;
}
return nativeError;
}
public override string ExtractSingleSqlState(DbException sqle)
{
System.Type type;
PropertyInfo pi;
string sqlState;
type = sqle.GetType();
pi = type.GetProperty("Errors");
if (pi == null) // there is no Errors property
{
return null;
}
sqlState = "";
foreach (object o in (pi.GetValue(sqle, null) as IEnumerable))
{
pi = o.GetType().GetProperty("SQLState");
if (pi == null)
return null;
sqlState = (string)pi.GetValue(o, null);
if (sqlState.Length != 0)
break;
}
return sqlState;
}
}
}