/
ParameterData.cs
226 lines (205 loc) · 6.64 KB
/
ParameterData.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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
//
// This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
// project.
//
// Copyright (C) 1998-2012 OpenLink Software
//
// This project is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation; only version 2 of the License, dated June 1991.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
//
//
//
// $Id$
//
using System;
using System.Data;
using System.Diagnostics;
using System.Runtime.InteropServices;
#if ODBC_CLIENT
namespace OpenLink.Data.VirtuosoOdbcClient
#elif CLIENT
namespace OpenLink.Data.VirtuosoClient
#else
namespace OpenLink.Data.Virtuoso
#endif
{
internal sealed class ParameterData : IDisposable
{
internal readonly static BooleanSwitch Switch =
new BooleanSwitch ("VirtuosoClient.ManagedConnection", "Marshaling");
internal VirtuosoParameterCollection parameters;
internal int[] offset;
internal int[] length;
internal MemoryHandle buffer;
internal ParameterData (VirtuosoParameterCollection parameters)
{
this.parameters = parameters;
Initialize ();
}
~ParameterData ()
{
Dispose (false);
}
public void Dispose ()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose (bool disposing)
{
if (disposing)
{
if (buffer != null)
buffer.Dispose ();
}
parameters = null;
offset = null;
length = null;
buffer = null;
}
private void Initialize ()
{
offset = new int[parameters.Count];
length = new int[parameters.Count];
int totalLength = 4 * parameters.Count;
for (int i = 0; i < parameters.Count; i++)
{
VirtuosoParameter param = (VirtuosoParameter) parameters[i];
object value = param.Value;
if (param.bufferType == null)
{
if (param.paramType != null)
param.bufferType = param.paramType.bufferType;
else if (value == null || Convert.IsDBNull (value))
param.bufferType = VirtuosoParameter.defaultType.bufferType;
else
{
param.bufferType = BufferTypes.InferBufferType (value);
if (param.bufferType == null)
throw new InvalidOperationException ("Cannot infer parameter type");
}
}
value = param.bufferType.ConvertValue (value);
int paramLength = 0;
if (param.Direction == ParameterDirection.Input)
{
if (value == null || Convert.IsDBNull (value))
continue;
int valueLength = param.bufferType.GetBufferSize (value);
if (param.Size == 0)
paramLength = valueLength;
else
{
paramLength = param.bufferType.GetBufferSize (param.Size);
if (paramLength > valueLength)
paramLength = valueLength;
}
}
else
{
if (param.Size == 0)
{
if (!param.bufferType.isFixedSize)
{
if (param.Direction != ParameterDirection.InputOutput
|| value == null || Convert.IsDBNull (value))
throw new InvalidOperationException ("Cannot determine the parameter size.");
}
paramLength = param.bufferType.GetBufferSize (value);
}
else
paramLength = param.bufferType.GetBufferSize (param.Size);
}
if (param.bufferType.alignment > 1)
{
totalLength += param.bufferType.alignment - 1;
totalLength -= totalLength % param.bufferType.alignment;
}
offset[i] = totalLength;
length[i] = paramLength;
totalLength += paramLength;
}
buffer = new MemoryHandle (totalLength);
}
internal void SetParameters (VirtuosoConnection connection, IntPtr hstmt)
{
Debug.WriteLineIf (CLI.FnTrace.Enabled, "ParameterData.SetParameters()");
for (int i = 0; i < parameters.Count; i++)
{
VirtuosoParameter param = (VirtuosoParameter) parameters[i];
Debug.WriteLineIf (Switch.Enabled, " param: " + param.paramName);
CLI.InOutType ioType;
switch (param.Direction)
{
default:
// case ParameterDirection.Input:
ioType = CLI.InOutType.SQL_PARAM_INPUT;
break;
case ParameterDirection.InputOutput:
ioType = CLI.InOutType.SQL_PARAM_INPUT_OUTPUT;
break;
case ParameterDirection.Output:
case ParameterDirection.ReturnValue:
ioType = CLI.InOutType.SQL_PARAM_OUTPUT;
break;
}
Debug.WriteLineIf (Switch.Enabled, " direction: " + param.Direction);
IntPtr paramBuffer = buffer.GetAddress (offset[i]);
int bufferLength = length[i];
int lengthOffset = 4 * i;
CLI.SqlType sqlType = (param.paramType != null ? param.paramType.sqlType : CLI.SqlType.SQL_UNKNOWN_TYPE);
CLI.ReturnCode rc = (CLI.ReturnCode) CLI.SQLBindParameter (
hstmt, (ushort) (i + 1),
(short) ioType, (short) param.bufferType.sqlCType, (short) sqlType,
(IntPtr) param.Size, param.Scale,
paramBuffer, (IntPtr) bufferLength, buffer.GetAddress (lengthOffset));
if (rc != CLI.ReturnCode.SQL_SUCCESS)
Diagnostics.HandleResult (rc, CLI.HandleType.SQL_HANDLE_STMT, hstmt, connection);
if (param.Direction == ParameterDirection.Input || param.Direction == ParameterDirection.InputOutput)
{
object value = param.bufferType.ConvertValue (param.Value);
Debug.WriteLineIf (Switch.Enabled, " value: " + param.Value);
int dataLength;
if (value == null)
dataLength = (int) CLI.LengthCode.SQL_NULL_DATA;
else if (Convert.IsDBNull (value))
dataLength = (int) CLI.LengthCode.SQL_NULL_DATA;
else
dataLength = param.bufferType.ManagedToNative (value, paramBuffer, bufferLength);
Marshal.WriteInt32 (buffer.Handle, lengthOffset, dataLength);
}
}
}
internal void GetParameters ()
{
Debug.WriteLineIf (CLI.FnTrace.Enabled, "ParameterData.GetParameters()");
for (int i = 0; i < parameters.Count; i++)
{
VirtuosoParameter param = (VirtuosoParameter) parameters[i];
if (param.Direction == ParameterDirection.ReturnValue
|| param.Direction == ParameterDirection.Output
|| param.Direction == ParameterDirection.InputOutput)
{
int dataLength = Marshal.ReadInt32 (buffer.Handle, 4 * i);
if (dataLength > length[i])
dataLength = length[i];
if (dataLength == (int) CLI.LengthCode.SQL_NULL_DATA)
param.paramData = DBNull.Value;
else
param.paramData = param.bufferType.NativeToManaged (
buffer.GetAddress (offset[i]), dataLength);
}
}
}
}
}