-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDBCWrapper.cs
More file actions
172 lines (154 loc) · 4.44 KB
/
DBCWrapper.cs
File metadata and controls
172 lines (154 loc) · 4.44 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
using System;
using System.Runtime.InteropServices;
namespace Arad.Net.Core.Informix;
internal sealed class DBCWrapper
{
internal InformixConnectionHandle connectionHandle;
internal nint hdbc = nint.Zero;
internal int posInPool = -1;
internal bool _isOpen;
internal bool isConnectionDead;
internal bool _isInTransaction;
internal bool enlisted;
internal Guid TransactionId
{
get
{
byte[] array = new byte[100];
int StringLength = 16;
if (Interop.Odbc.SQLGetConnectAttrW(connectionHandle, Informix32.SQL_ATTR.TRANSACTION_ID, array, 16, out StringLength) != 0)
{
isConnectionDead = true;
return Guid.Empty;
}
return new Guid(array);
}
}
internal DBCWrapper(InformixConnection connection)
{
connectionHandle = connection.ConnectionHandle;
connection._dbcWrapper = this;
Informix32.RetCode retCode = connectionHandle.Connect(connection.ConnectionString);
if (retCode != 0)
{
Exception ex = connection.HandleErrorNoThrow(connectionHandle, retCode);
if (ex != null)
{
throw ex;
}
}
else
{
_isOpen = true;
retCode = Interop.Odbc.SQLSetConnectAttrW(connectionHandle, Informix32.SQL_ATTR.LOCALIZE_DECIMALS, 1, -6);
if (retCode != 0)
{
connection.HandleError(connectionHandle, retCode);
}
retCode = Interop.Odbc.SQLSetConnectAttrW(connectionHandle, Informix32.SQL_ATTR.CALL_FROM_DOTNET, 1, -6);
if (retCode != 0)
{
connection.HandleError(connectionHandle, retCode);
}
if (connection.connSettings.leaveTrailingSpaces)
{
retCode = Interop.Odbc.SQLSetConnectAttrW(connectionHandle, Informix32.SQL_ATTR.LEAVE_TRAILING_SPACES, 1, -6);
if (retCode != 0)
{
connection.HandleError(connectionHandle, retCode);
}
}
}
GC.KeepAlive(this);
}
internal Informix32.RetCode Close()
{
Informix32.RetCode retCode = Informix32.RetCode.SUCCESS;
if (!isConnectionDead)
{
retCode = connectionHandle.Disconnect();
if (retCode != 0)
{
_ = 1;
}
try
{
connectionHandle.FreeConnectHandle();
}
catch (Exception)
{
}
}
hdbc = nint.Zero;
_isOpen = false;
CloseAndRelease();
return retCode;
}
~DBCWrapper()
{
if (_isInTransaction)
{
RollbackDeadTransaction();
}
CloseAndRelease();
}
internal void EnlistAsRequired(InformixConnection connection)
{
if (!connection.connSettingsAtOpen.enlist)
{
return;
}
nint zero = nint.Zero;
try
{
zero = nint.Zero;
Informix32.RetCode retCode = Interop.Odbc.SQLSetConnectAttrW(connectionHandle, Informix32.SQL_ATTR.ENLIST_IN_DTC, zero, 0);
if (retCode != 0)
{
Exception ex = connection.HandleErrorNoThrow(connectionHandle, retCode);
_ = 1;
if (ex != null)
{
throw ex;
}
}
}
finally
{
if (zero != nint.Zero)
{
Marshal.Release(zero);
}
}
enlisted = true;
_isInTransaction = true;
}
public override int GetHashCode()
{
return hdbc.GetHashCode();
}
public override bool Equals(object obj)
{
bool flag = false;
if (obj == null || typeof(DBCWrapper) != obj.GetType())
{
return false;
}
return hdbc.Equals(((DBCWrapper)obj).hdbc);
}
internal void RollbackDeadTransaction()
{
Informix32.RetCode retCode = connectionHandle.CompleteTransaction(1);
_isInTransaction = false;
GC.KeepAlive(this);
}
internal Informix32.RETCODE CloseAndRelease()
{
Informix32.RETCODE result = Informix32.RETCODE.SUCCESS;
if (hdbc != nint.Zero)
{
GC.KeepAlive(this);
}
return result;
}
}