**** Merged from MCS ****
[mono-project.git] / mcs / class / System.Data / System.Data.Odbc / libodbc.cs
blob54bab1feb23bb0ebf6dd00ccae19f0f0ee2f61de
1 //
2 // System.Data.Odbc.libodbc
3 //
4 // Authors:
5 // Brian Ritchie (brianlritchie@hotmail.com)
6 //
7 //
8 // Copyright (C) Brian Ritchie, 2002
9 //
13 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
22 //
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
25 //
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 using System.Data;
36 using System.Data.Common;
37 using System.Runtime.InteropServices;
39 namespace System.Data.Odbc
41 internal enum OdbcHandleType : short {
42 Env = 1,
43 Dbc = 2,
44 Stmt = 3,
45 Desc = 4
48 internal enum OdbcReturn : short {
49 Error = -1,
50 InvalidHandle = -2,
51 StillExecuting = 2,
52 NeedData = 99,
53 Success = 0,
54 SuccessWithInfo = 1,
55 NoData=100
58 internal enum OdbcEnv : ushort {
59 OdbcVersion = 200,
60 ConnectionPooling = 201,
61 CPMatch = 202
64 internal enum OdbcConnectionAttribute : int
66 AutoCommit=102,
67 TransactionIsolation=108
70 internal enum OdbcInputOutputDirection : short
72 Input=1,
73 InputOutput=2,
74 ResultCol=3,
75 Output=4,
76 ReturnValue=5
79 internal enum OdbcLengthIndicator : short
81 NoTotal = -4,
82 NullData = -1
85 [StructLayout(LayoutKind.Sequential)]
86 internal struct OdbcTimestamp
88 internal short year;
89 internal ushort month;
90 internal ushort day;
91 internal ushort hour;
92 internal ushort minute;
93 internal ushort second;
94 internal ulong fraction;
98 // sealed internal class libodbc
99 internal class libodbc
101 #region global constants
102 internal static int SQL_OV_ODBC2 = 2;
103 internal static int SQL_OV_ODBC3 = 3;
105 internal static string SQLSTATE_RIGHT_TRUNC = "01004";
106 internal static char C_NULL = '\0';
107 #endregion
109 internal static OdbcInputOutputDirection ConvertParameterDirection(
110 ParameterDirection dir)
112 switch (dir)
114 case ParameterDirection.Input:
115 return OdbcInputOutputDirection.Input;
116 case ParameterDirection.InputOutput:
117 return OdbcInputOutputDirection.InputOutput;
118 case ParameterDirection.Output:
119 return OdbcInputOutputDirection.Output;
120 case ParameterDirection.ReturnValue:
121 return OdbcInputOutputDirection.ReturnValue;
122 default:
123 return OdbcInputOutputDirection.Input;
127 [DllImport("odbc32")]
128 internal static extern OdbcReturn SQLAllocHandle (OdbcHandleType HandleType, IntPtr InputHandle, ref IntPtr OutputHandlePtr);
130 [DllImport("odbc32")]
131 internal static extern OdbcReturn SQLSetEnvAttr (IntPtr EnvHandle, OdbcEnv Attribute, IntPtr Value, int StringLength);
133 [DllImport("odbc32")]
134 internal static extern OdbcReturn SQLConnect (IntPtr ConnectionHandle, string ServerName, short NameLength1, string UserName, short NameLength2, string Authentication, short NameLength3);
136 [DllImport("odbc32")]
137 internal static extern OdbcReturn SQLDriverConnect(IntPtr ConnectionHandle, IntPtr WindowHandle, string InConnectionString, short StringLength1, string OutConnectionString, short BufferLength, ref short StringLength2Ptr, ushort DriverCompletion);
139 [DllImport("odbc32")]
140 internal static extern OdbcReturn SQLExecDirect (IntPtr StatementHandle, string StatementText, int TextLength);
142 [DllImport("odbc32")]
143 internal static extern OdbcReturn SQLRowCount (IntPtr StatementHandle, ref int RowCount);
145 [DllImport("odbc32")]
146 internal static extern OdbcReturn SQLNumResultCols (IntPtr StatementHandle, ref short ColumnCount);
148 [DllImport("odbc32")]
149 internal static extern OdbcReturn SQLFetch (IntPtr StatementHandle);
151 [DllImport("odbc32")]
152 internal static extern OdbcReturn SQLGetData (IntPtr StatementHandle, ushort ColumnNumber, OdbcType TargetType, ref bool TargetPtr, int BufferLen, ref int Len);
154 [DllImport("odbc32")]
155 internal static extern OdbcReturn SQLGetData (IntPtr StatementHandle, ushort ColumnNumber, OdbcType TargetType, ref double TargetPtr, int BufferLen, ref int Len);
157 [DllImport("odbc32")]
158 internal static extern OdbcReturn SQLGetData (IntPtr StatementHandle, ushort ColumnNumber, OdbcType TargetType, ref long TargetPtr, int BufferLen, ref int Len);
160 [DllImport("odbc32")]
161 internal static extern OdbcReturn SQLGetData (IntPtr StatementHandle, ushort ColumnNumber, OdbcType TargetType, ref short TargetPtr, int BufferLen, ref int Len);
163 [DllImport("odbc32")]
164 internal static extern OdbcReturn SQLGetData (IntPtr StatementHandle, ushort ColumnNumber, OdbcType TargetType, ref float TargetPtr, int BufferLen, ref int Len);
166 [DllImport("odbc32")]
167 internal static extern OdbcReturn SQLGetData (IntPtr StatementHandle, ushort ColumnNumber, OdbcType TargetType, ref OdbcTimestamp TargetPtr, int BufferLen, ref int Len);
169 [DllImport("odbc32")]
170 internal static extern OdbcReturn SQLGetData (IntPtr StatementHandle, ushort ColumnNumber, OdbcType TargetType, ref int TargetPtr, int BufferLen, ref int Len);
172 [DllImport("odbc32")]
173 internal static extern OdbcReturn SQLGetData (IntPtr StatementHandle, ushort ColumnNumber, OdbcType TargetType, byte[] TargetPtr, int BufferLen, ref int Len);
175 [DllImport("odbc32")]
176 internal static extern OdbcReturn SQLDescribeCol(IntPtr StatementHandle, ushort ColumnNumber, byte[] ColumnName, short BufferLength, ref short NameLength, ref short DataType, ref uint ColumnSize, ref short DecimalDigits, ref short Nullable);
178 [DllImport("odbc32")]
179 internal static extern OdbcReturn SQLFreeHandle(ushort HandleType, IntPtr SqlHandle);
181 [DllImport("odbc32")]
182 internal static extern OdbcReturn SQLDisconnect(IntPtr ConnectionHandle);
184 [DllImport("odbc32")]
185 internal static extern OdbcReturn SQLPrepare(IntPtr StatementHandle, string Statement, int TextLength);
187 [DllImport("odbc32")]
188 internal static extern OdbcReturn SQLExecute(IntPtr StatementHandle);
190 [DllImport("odbc32")]
191 internal static extern OdbcReturn SQLSetConnectAttr(IntPtr ConnectionHandle, OdbcConnectionAttribute Attribute, IntPtr Value, int Length);
193 [DllImport("odbc32")]
194 internal static extern OdbcReturn SQLEndTran(int HandleType, IntPtr Handle, short CompletionType);
196 [DllImport("odbc32")]
197 internal static extern OdbcReturn SQLBindParameter(IntPtr StatementHandle, ushort ParamNum,
198 short InputOutputType, short ValueType, short ParamType, uint ColSize,
199 short DecimalDigits, byte[] ParamValue, int BufLen, int StrLen);
201 [DllImport("odbc32")]
202 internal static extern OdbcReturn SQLBindParameter(IntPtr StatementHandle, ushort ParamNum,
203 short InputOutputType, short ValueType, short ParamType, uint ColSize,
204 short DecimalDigits, ref int ParamValue, int BufLen, int StrLen);
206 [DllImport("odbc32")]
207 internal static extern OdbcReturn SQLCancel(IntPtr StatementHandle);
209 [DllImport("odbc32")]
210 internal static extern OdbcReturn SQLCloseCursor(IntPtr StatementHandle);
212 [DllImport("odbc32")]
213 internal static extern OdbcReturn SQLError(IntPtr EnvironmentHandle,
214 IntPtr ConnectionHandle, IntPtr StatementHandle,
215 byte[] Sqlstate, ref int NativeError,
216 byte[] MessageText, short BufferLength,
217 ref short TextLength);
219 [DllImport("odbc32")]
220 internal static extern OdbcReturn SQLGetStmtAttr(IntPtr StatementHandle,
221 int Attribute, ref IntPtr Value, int BufLen, int StrLen);
223 [DllImport("odbc32")]
224 internal static extern OdbcReturn SQLSetDescField(IntPtr DescriptorHandle,
225 short RecNumber, short FieldIdentifier, byte[] Value, int BufLen);
227 [DllImport("odbc32")]
228 internal static extern OdbcReturn SQLGetDiagRec (OdbcHandleType HandleType,
229 IntPtr Handle, ushort RecordNumber,
230 byte [] Sqlstate, ref int NativeError,
231 byte [] MessageText, short BufferLength,
232 ref short TextLength);