2002-12-05 Daniel Morgan <danmorg@sc.rr.com>
[mono.git] / mcs / class / System.Data.OracleClient / System.Data.OracleClient / OciGlue.cs
1 // 
2 // ociglue.cs - provides glue between 
3 //              managed C#/.NET System.Data.OracleClient.dll and 
4 //              unmanaged native c library oci.dll
5 //              to be used in Mono System.Data.OracleClient as
6 //              the Oracle 8i data provider.
7 //  
8 // Part of managed C#/.NET library System.Data.OracleClient.dll
9 //
10 // Part of the Mono class libraries at
11 // mcs/class/System.Data.OracleClient/System.Data.OracleClient.OCI
12 //
13 // Assembly: System.Data.OracleClient.dll
14 // Namespace: System.Data.OracleClient
15 // 
16 // Author: 
17 //     Daniel Morgan <danmorg@sc.rr.com>
18 //         
19 // Copyright (C) Daniel Morgan, 2002
20 // 
21
22 using System;
23 using System.Runtime.InteropServices;
24
25 namespace System.Data.OracleClient.OCI {
26         internal sealed class OciGlue {
27
28                 // TODO: need to clean up, dispose, close, etc...
29                 
30                 // connection parameters
31                 string database = "";
32                 string username = "";
33                 string password = "";
34
35                 private UInt32 ociGlueConnectionHandle = 0;
36
37                 // http://download-west.oracle.com/docs/cd/A87861_01/NT817EE/index.htm
38                 // from oracle/ora81/oci/include/oci.h
39
40                 // Currently, these are not being used, nor are the
41                 // OCI_* constants nor the DllImports for oci.dll
42                 IntPtr myenvhp = IntPtr.Zero; // OCIEnv - the environment handle
43                 IntPtr mysrvhp = IntPtr.Zero; // OCIServer - the server handle
44                 IntPtr myerrhp = IntPtr.Zero; // OCIError - the error handle
45                 IntPtr myusrhp = IntPtr.Zero; // OCISession- user session handle
46                 IntPtr mysvchp = IntPtr.Zero; // OCISvcCtx- the  service handle
47
48                 public const UInt32 OCI_DEFAULT = (UInt32) 0x00;
49
50                 public const UInt32 OCI_THREADED  = (UInt32) 0x01;
51                 public const UInt32 OCI_OBJECT    = (UInt32) 0x02;
52                 public const UInt32 OCI_EVENTS    = (UInt32) 0x04;
53                 public const UInt32 OCI_RESERVED1 = (UInt32) 0x08;
54                 public const UInt32 OCI_SHARED    = (UInt32) 0x10;
55                 public const UInt32 OCI_RESERVED2 = (UInt32) 0x20;
56                 
57                 public const UInt32 OCI_NO_UCB    = (UInt32) 0x40;
58                 public const UInt32  OCI_NO_MUTEX = (UInt32) 0x80;
59                 
60                 public const UInt32  OCI_SHARED_EXT = (UInt32) 0x100;
61                 public const UInt32  OCI_CACHE      = (UInt32) 0x200;
62                 public const UInt32  OCI_NO_CACHE   = (UInt32) 0x400;
63
64                 public const UInt32 OCI_HTYPE_FIRST         = 1;
65                 public const UInt32 OCI_HTYPE_ENV           = 1;
66                 public const UInt32 OCI_HTYPE_ERROR         = 2;
67                 public const UInt32 OCI_HTYPE_SVCCTX        = 3;
68                 public const UInt32 OCI_HTYPE_STMT          = 4;
69                 public const UInt32 OCI_HTYPE_BIND          = 5;
70                 public const UInt32 OCI_HTYPE_DEFINE        = 6;
71                 public const UInt32 OCI_HTYPE_DESCRIBE      = 7;
72                 public const UInt32 OCI_HTYPE_SERVER        = 8;
73                 public const UInt32 OCI_HTYPE_SESSION       = 9;
74                 public const UInt32 OCI_HTYPE_TRANS         =10;
75                 public const UInt32 OCI_HTYPE_COMPLEXOBJECT =11;
76                 public const UInt32 OCI_HTYPE_SECURITY      =12;
77                 public const UInt32 OCI_HTYPE_SUBSCRIPTION  =13;
78                 public const UInt32 OCI_HTYPE_DIRPATH_CTX   =14;
79                 public const UInt32 OCI_HTYPE_DIRPATH_COLUMN_ARRAY =15;
80                 public const UInt32 OCI_HTYPE_DIRPATH_STREAM       =16;
81                 public const UInt32 OCI_HTYPE_PROC          =17;
82                 public const UInt32 OCI_HTYPE_LAST          =17;
83
84                 public const UInt32 OCI_ATTR_FNCODE  =1;
85                 public const UInt32 OCI_ATTR_OBJECT   =2;
86                 public const UInt32 OCI_ATTR_NONBLOCKING_MODE  =3;
87                 public const UInt32 OCI_ATTR_SQLCODE  =4;
88                 public const UInt32 OCI_ATTR_ENV  =5;
89                 public const UInt32 OCI_ATTR_SERVER =6;
90                 public const UInt32 OCI_ATTR_SESSION =7;
91                 public const UInt32 OCI_ATTR_TRANS   =8;
92                 public const UInt32 OCI_ATTR_ROW_COUNT =  9;
93                 public const UInt32 OCI_ATTR_SQLFNCODE =10;
94                 public const UInt32 OCI_ATTR_PREFETCH_ROWS = 11;
95                 public const UInt32 OCI_ATTR_NESTED_PREFETCH_ROWS =12;
96                 public const UInt32 OCI_ATTR_PREFETCH_MEMORY =13;
97                 public const UInt32 OCI_ATTR_NESTED_PREFETCH_MEMORY =14;
98                 public const UInt32 OCI_ATTR_CHAR_COUNT  =15; 
99                 
100                 public const UInt32 OCI_ATTR_PDSCL   =16;
101                 public const UInt32 OCI_ATTR_FSPRECISION =OCI_ATTR_PDSCL;
102                 
103                 public const UInt32 OCI_ATTR_PDPRC   =17;
104                 public const UInt32 OCI_ATTR_LFPRECISION =OCI_ATTR_PDPRC; 
105                 
106                 public const UInt32 OCI_ATTR_PARAM_COUNT =18;
107                 public const UInt32 OCI_ATTR_ROWID   =19;
108                 public const UInt32 OCI_ATTR_CHARSET  =20;
109                 public const UInt32 OCI_ATTR_NCHAR   =21;
110                 public const UInt32 OCI_ATTR_USERNAME =22;
111                 public const UInt32 OCI_ATTR_PASSWORD =23;
112                 public const UInt32 OCI_ATTR_STMT_TYPE  = 24;
113                 public const UInt32 OCI_ATTR_INTERNAL_NAME =  25;
114                 public const UInt32 OCI_ATTR_EXTERNAL_NAME =  26;
115                 public const UInt32 OCI_ATTR_XID  =   27;
116                 public const UInt32 OCI_ATTR_TRANS_LOCK =28;
117                 public const UInt32 OCI_ATTR_TRANS_NAME= 29;
118                 public const UInt32 OCI_ATTR_HEAPALLOC =30;
119                 public const UInt32 OCI_ATTR_CHARSET_ID =31;
120                 public const UInt32 OCI_ATTR_CHARSET_FORM =32;
121                 public const UInt32 OCI_ATTR_MAXDATA_SIZE =33;
122                 public const UInt32 OCI_ATTR_CACHE_OPT_SIZE =34;
123                 public const UInt32 OCI_ATTR_CACHE_MAX_SIZE =35;
124                 public const UInt32 OCI_ATTR_PINOPTION =36;
125                 public const UInt32 OCI_ATTR_ALLOC_DURATION =37;
126                 
127                 public const UInt32 OCI_ATTR_PIN_DURATION =38;
128                 public const UInt32 OCI_ATTR_FDO          =39;
129                 public const UInt32 OCI_ATTR_POSTPROCESSING_CALLBACK =40;
130                 
131                 public const UInt32 OCI_ATTR_POSTPROCESSING_CONTEXT =41;
132                 
133                 public const UInt32 OCI_ATTR_ROWS_RETURNED =42;
134                 
135                 public const UInt32 OCI_ATTR_FOCBK        =43;
136                 public const UInt32 OCI_ATTR_IN_V8_MODE   =44;
137                 public const UInt32 OCI_ATTR_LOBEMPTY     =45;
138                 public const UInt32 OCI_ATTR_SESSLANG     =46;
139
140                 public const UInt32 OCI_ATTR_VISIBILITY =       47;
141                 public const UInt32 OCI_ATTR_RELATIVE_MSGID     =       48;
142                 public const UInt32 OCI_ATTR_SEQUENCE_DEVIATION=        49;
143
144                 public const UInt32 OCI_ATTR_CONSUMER_NAME      =       50;
145                 public const UInt32 OCI_ATTR_DEQ_MODE           =51;
146                 public const UInt32 OCI_ATTR_NAVIGATION =       52;
147                 public const UInt32 OCI_ATTR_WAIT               =       53;
148                 public const UInt32 OCI_ATTR_DEQ_MSGID          =54;
149
150                 public const UInt32 OCI_ATTR_PRIORITY           =55;
151                 public const UInt32 OCI_ATTR_DELAY              =       56;
152                 public const UInt32 OCI_ATTR_EXPIRATION =       57;     
153                 public const UInt32 OCI_ATTR_CORRELATION        =       58;
154                 public const UInt32 OCI_ATTR_ATTEMPTS           =59;    
155                 public const UInt32 OCI_ATTR_RECIPIENT_LIST     =       60;
156                 public const UInt32 OCI_ATTR_EXCEPTION_QUEUE    =61;       
157                 public const UInt32 OCI_ATTR_ENQ_TIME           =62; 
158                 public const UInt32 OCI_ATTR_MSG_STATE          =63;
159                 
160                 public const UInt32 OCI_ATTR_AGENT_NAME =       64;
161                 public const UInt32 OCI_ATTR_AGENT_ADDRESS      =       65;
162                 public const UInt32 OCI_ATTR_AGENT_PROTOCOL     =       66;
163
164                 public const UInt32 OCI_ATTR_SENDER_ID          =68;
165                 public const UInt32 OCI_ATTR_ORIGINAL_MSGID     =     69;
166
167                 public const UInt32 OCI_ATTR_QUEUE_NAME =     70;
168                 public const UInt32 OCI_ATTR_NFY_MSGID         =     71;
169                 public const UInt32 OCI_ATTR_MSG_PROP          =     72;
170
171                 public const UInt32 OCI_ATTR_NUM_DML_ERRORS    =     73;
172                 public const UInt32 OCI_ATTR_DML_ROW_OFFSET    =     74;
173
174                 public const UInt32 OCI_ATTR_DATEFORMAT        =     75;
175                 public const UInt32 OCI_ATTR_BUF_ADDR          =     76;
176                 public const UInt32 OCI_ATTR_BUF_SIZE          =     77;
177                 public const UInt32 OCI_ATTR_DIRPATH_MODE      =     78;
178                 public const UInt32 OCI_ATTR_DIRPATH_NOLOG     =     79;
179                 public const UInt32 OCI_ATTR_DIRPATH_PARALLEL  =     80;
180                 public const UInt32 OCI_ATTR_NUM_ROWS          =     81;
181
182                 public const UInt32 OCI_ATTR_COL_COUNT        =      82;
183                 
184                 public const UInt32 OCI_ATTR_STREAM_OFFSET    =      83;
185                 public const UInt32 OCI_ATTR_SHARED_HEAPALLOC  =     84;
186
187                 public const UInt32 OCI_ATTR_SERVER_GROUP      =     85;
188
189                 public const UInt32 OCI_ATTR_MIGSESSION       =      86;
190
191                 public const UInt32 OCI_ATTR_NOCACHE           =     87;
192
193                 public const UInt32 OCI_ATTR_MEMPOOL_SIZE       =    88;
194                 public const UInt32 OCI_ATTR_MEMPOOL_INSTNAME    =   89;
195                 public const UInt32 OCI_ATTR_MEMPOOL_APPNAME      =  90;
196                 public const UInt32 OCI_ATTR_MEMPOOL_HOMENAME   =    91;
197                 public const UInt32 OCI_ATTR_MEMPOOL_MODEL      =    92;
198                 public const UInt32 OCI_ATTR_MODES              =    93;
199
200                 public const UInt32 OCI_ATTR_SUBSCR_NAME        =    94;
201                 public const UInt32 OCI_ATTR_SUBSCR_CALLBACK    =    95;
202                 public const UInt32 OCI_ATTR_SUBSCR_CTX         =    96;
203                 public const UInt32 OCI_ATTR_SUBSCR_PAYLOAD     =    97;
204                 public const UInt32 OCI_ATTR_SUBSCR_NAMESPACE   =    98;
205
206                 public const UInt32 OCI_ATTR_PROXY_CREDENTIALS   =   99;
207                 public const UInt32 OCI_ATTR_INITIAL_CLIENT_ROLES = 100;
208
209                 public const UInt32 OCI_ATTR_UNK           =   101;
210                 public const UInt32 OCI_ATTR_NUM_COLS      =   102;
211                 public const UInt32 OCI_ATTR_LIST_COLUMNS  =   103;
212                 public const UInt32 OCI_ATTR_RDBA          =   104;
213                 public const UInt32 OCI_ATTR_CLUSTERED     =   105;
214                 public const UInt32  OCI_ATTR_PARTITIONED   =   106;
215                 public const UInt32 OCI_ATTR_INDEX_ONLY    =   107; 
216                 public const UInt32 OCI_ATTR_LIST_ARGUMENTS =  108; 
217                 public const UInt32 OCI_ATTR_LIST_SUBPROGRAMS =109; 
218                 public const UInt32 OCI_ATTR_REF_TDO        =  110; 
219                 public const UInt32 OCI_ATTR_LINK           =  111; 
220                 public const UInt32 OCI_ATTR_MIN            =  112; 
221                 public const UInt32 OCI_ATTR_MAX            =  113; 
222                 public const UInt32 OCI_ATTR_INCR           =  114; 
223                 public const UInt32 OCI_ATTR_CACHE          =  115; 
224                 public const UInt32 OCI_ATTR_ORDER          =  116; 
225                 public const UInt32 OCI_ATTR_HW_MARK        =  117; 
226                 public const UInt32 OCI_ATTR_TYPE_SCHEMA    =  118; 
227                 public const UInt32 OCI_ATTR_TIMESTAMP      =  119; 
228                 public const UInt32 OCI_ATTR_NUM_ATTRS      =  120; 
229                 public const UInt32 OCI_ATTR_NUM_PARAMS     =  121; 
230                 public const UInt32 OCI_ATTR_OBJID          =  122; 
231                 public const UInt32 OCI_ATTR_PTYPE          =  123; 
232                 public const UInt32 OCI_ATTR_PARAM          =  124; 
233                 public const UInt32 OCI_ATTR_OVERLOAD_ID    =  125; 
234                 public const UInt32 OCI_ATTR_TABLESPACE     =  126; 
235                 public const UInt32 OCI_ATTR_TDO            =  127; 
236                 public const UInt32 OCI_ATTR_LTYPE          =  128;   
237                 public const UInt32 OCI_ATTR_PARSE_ERROR_OFFSET =129; 
238                 public const UInt32 OCI_ATTR_IS_TEMPORARY   =  130;   
239                 public const UInt32 OCI_ATTR_IS_TYPED       =  131;   
240                 public const UInt32 OCI_ATTR_DURATION       =  132;   
241                 public const UInt32 OCI_ATTR_IS_INVOKER_RIGHTS= 133;  
242                 public const UInt32 OCI_ATTR_OBJ_NAME      =   134;
243                 public const UInt32 OCI_ATTR_OBJ_SCHEMA    =   135;
244                 public const UInt32 OCI_ATTR_OBJ_ID        =   136;
245
246                 public const UInt32 OCI_ATTR_DIRPATH_SORTED_INDEX =   137; 
247
248                 public const UInt32 OCI_ATTR_DIRPATH_INDEX_MAINT_METHOD= 138;
249
250                 public const UInt32 OCI_ATTR_DIRPATH_FILE            =139;
251                 public const UInt32 OCI_ATTR_DIRPATH_STORAGE_INITIAL =140;
252                 public const UInt32 OCI_ATTR_DIRPATH_STORAGE_NEXT    =141;
253
254                 public const UInt32 OCI_ATTR_TRANS_TIMEOUT     =       142; 
255                 public const UInt32 OCI_ATTR_SERVER_STATUS      =       143;
256                 public const UInt32 OCI_ATTR_STATEMENT         =       144; 
257                 
258                 public const UInt32 OCI_ATTR_NO_CACHE          =      145;
259                 public const UInt32 OCI_ATTR_RESERVED_1        =      146;
260                 public const UInt32 OCI_ATTR_SERVER_BUSY       =      147;
261                 
262                 public const UInt32 OCI_UCS2ID = 1000;
263
264                 public const UInt32 OCI_SERVER_NOT_CONNECTED = 0x0; 
265                 public const UInt32 OCI_SERVER_NORMAL        = 0x1; 
266
267                 public const UInt32 OCI_SUBSCR_NAMESPACE_ANONYMOUS =  0;
268                 public const UInt32 OCI_SUBSCR_NAMESPACE_AQ        =  1;
269                 public const UInt32 OCI_SUBSCR_NAMESPACE_MAX       =  2;
270
271                 public const UInt32 OCI_CRED_RDBMS  =  1;
272                 public const UInt32 OCI_CRED_EXT    =  2;
273                 public const UInt32 OCI_CRED_PROXY  =  3;
274
275                 public const Int32 OCI_SUCCESS = 0;
276                 public const Int32 OCI_SUCCESS_WITH_INFO = 1;
277                 public const Int32 OCI_RESERVED_FOR_INT_USE = 200;
278                 public const Int32 OCI_NO_DATA = 100;
279                 public const Int32 OCI_ERROR = -1;
280                 public const Int32 OCI_INVALID_HANDLE = -2;
281                 public const Int32 OCI_NEED_DATA = 99;
282                 public const Int32 OCI_STILL_EXECUTING = -3123;
283                 public const Int32 OCI_CONTINUE = -24200;
284                 
285                 // from oci/include/ociap.h
286
287                 //------ Platform Invoke to native c library oci.dll ----------
288 /*
289                 [DllImport("oci.dll")]
290                 public static extern Int32 OCIEnvCreate (
291                         out IntPtr envhpp, UInt32 mode, IntPtr ctxp, 
292                         ref IntPtr malocfp, ref IntPtr ralocfp, ref IntPtr mfreefp,
293                         uint xtramemsz, out IntPtr usrmempp);
294                 //sword OCIEnvCreate   ( 
295                 //      OCIEnv **envhpp, ub4 mode, CONST dvoid *ctxp,
296                 //      CONST dvoid *(*malocfp) (dvoid *ctxp, size_t size),
297                 //      CONST dvoid *(*ralocfp) (dvoid *ctxp, dvoid *memptr, size_t newsize),
298                 //      CONST void (*mfreefp) (dvoid *ctxp, dvoid *memptr)),
299                 //      size_t    xtramemsz, dvoid **usrmempp);
300                 // [OUT],[IN],[IN],[IN],[IN],[IN],[IN],[OUT]
301
302                 [DllImport("oci.dll")]
303                 public static extern Int32 OCIHandleAlloc ( 
304                         IntPtr parenth, ref IntPtr hndlpp, UInt32 type, 
305                         int xtramem_sz, ref IntPtr usrmempp);
306                 //sword OCIHandleAlloc(
307                 //      CONST dvoid *parenth, dvoid **hndlpp, ub4 type, 
308                 //      size_t xtramem_sz, dvoid **usrmempp );
309                 // [IN],OUT],[OUT],[IN],[OUT]
310
311                 [DllImport("oci.dll")]
312                 public static extern Int32 OCIServerAttach ( 
313                         ref IntPtr srvhp, ref IntPtr errhp, string dblink,
314                         Int32 dblink_len, UInt32 mode);
315                 //sword OCIServerAttach (
316                 //      OCIServer *srvhp, OCIError *errhp, CONST text *dblink,
317                 //      sb4 dblink_len, ub4 mode);
318                 // [IN/OUT],[IN/OUT],[IN],[IN],[IN]
319
320                 [DllImport("oci.dll")]
321                 public static extern Int32 OCIAttrSet ( 
322                         ref IntPtr trgthndlp, UInt32 trghndltyp, IntPtr attributep,
323                         UInt32 size, UInt32 attrtype,
324                         ref IntPtr errhp);
325                 //sword OCIAttrSet ( 
326                 //      dvoid *trgthndlp, ub4 trghndltyp, dvoid *attributep,
327                 //      ub4 size, ub4 attrtype, OCIError *errhp );
328                 // [IN/OUT], [IN/OUT], [IN], [IN], [IN], [IN/OUT]
329
330                 [DllImport("oci.dll")]
331                 public static extern Int32 OCISessionBegin ( 
332                         IntPtr svchp, ref IntPtr errhp, ref IntPtr usrhp,
333                         UInt32 credt, UInt32 mode);
334                 //sword OCISessionBegin (
335                 //      OCISvcCtx *svchp, OCIError *errhp,
336                 //      OCISession *usrhp, ub4 credt, ub4 mode );
337                 // [IN],[IN],[IN/OUT],[IN],[IN]
338 */
339                 [DllImport("oci.dll")]
340                 public static extern UInt32 OCIErrorGet ( 
341                         ref IntPtr hndlp, UInt32 recordno, ref IntPtr sqlstate,
342                         out Int32 errcodep, out string bufp, UInt32 bufsiz,
343                         UInt32 type);
344                 //sword OCIErrorGet(
345                 //      dvoid *hndlp, ub4 recordno, text *sqlstate,
346                 //      sb4 *errcodep, text *bufp, ub4 bufsiz,
347                 //      ub4 type);
348                 // [IN],[IN],[OUT],[OUT],[OUT],[IN],[IN]
349
350                 //------ Platform Invoke to native c library System.Data.OracleClient.ociglue.dll ----------
351                 // System.Data.OracleClient.ociglue.dll is the glue between C# and oci.dll
352
353                 [DllImport("System.Data.OracleClient.ociglue.dll")]
354                 public static extern IntPtr OciGlue_OCIEnvCreate(ref Int32 status);
355                 // void *OciGlue_OCIEnvCreate(sword *status);
356
357                 [DllImport("System.Data.OracleClient.ociglue.dll")]
358                 public static extern IntPtr OciGlue_OCIHandleAlloc(ref Int32 status,
359                                                 IntPtr parenth, UInt32 type);
360                 // dvoid *OciGlue_OCIHandleAlloc(sword *status, 
361                 //              CONST dvoid *parenth, ub4 type);
362
363                 [DllImport("System.Data.OracleClient.ociglue.dll")]
364                 public static extern void OciGlue_OCIServerAttach(ref Int32 status,
365                                 IntPtr srvhp,
366                                 IntPtr errhp, string dblink, UInt32 mode);
367                 // void OciGlue_OCIServerAttach(sword *status, dvoid *srvhp, 
368                 //              dvoid *errhp, char *dblink, ub4 mode);
369                 
370                 [DllImport("System.Data.OracleClient.ociglue.dll")]
371                 public static extern void OciGlue_OCIAttrSet(ref Int32 status, 
372                         IntPtr trgthndlp, UInt32 trghndltyp, 
373                         IntPtr attributep, UInt32 size, UInt32 attrtype, 
374                         IntPtr errhp);
375                 //void OciGlue_OCIAttrSet(sword *status, 
376                 //      dvoid *trgthndlp, ub4 trghndltyp, 
377                 //      dvoid *attributep, ub4 size, ub4 attrtype, 
378                 //      dvoid *errhp)
379
380                 [DllImport("System.Data.OracleClient.ociglue.dll")]
381                 public static extern IntPtr OciGlue_OCISessionBegin(ref Int32 status,
382                         IntPtr svchp, IntPtr errhp, IntPtr usrhp,
383                         UInt32 credt, UInt32 mode);
384                 // void OciGlue_OCISessionBegin(sword *status, 
385                 //      dvoid *svchp, dvoid *errhp, dvoid *usrhp,
386                 //      ub4 credt, ub4 mode )   
387
388                 [DllImport("System.Data.OracleClient.ociglue.dll")]
389                 public static extern Int32 OciGlue_Connect (
390                         out UInt32 ociGlueConnectionHandle, 
391                         string database, 
392                         string username, string password);
393                 //sword OciGlue_Connect (
394                 //      uint4 oci_glue_handle, 
395                 //      char *database, 
396                 //      char *username, char *password);
397
398                 [DllImport("System.Data.OracleClient.ociglue.dll")]
399                 public static extern Int32 OciGlue_Disconnect (UInt32 connection_handle);
400                 // sword OciGlue_Disconnect (ub4 connection_handle);
401
402                 [DllImport("System.Data.OracleClient.ociglue.dll")]
403                 public static extern Int32 OciGlue_PrepareAndExecuteNonQuerySimple (
404                         UInt32 ociGlueConnectionHandle,
405                         string sqlstmt, out int found);
406                 //sword OciGlue_PrepareAndExecuteNonQuerySimple (
407                 //      guint4 oci_glue_handle, 
408                 //      char *sqlstmt, int *found);
409
410                 [DllImport("System.Data.OracleClient.ociglue.dll")]
411                 public static extern UInt32 OciGlue_ConnectionCount();
412                 //guint OciGlue_ConnectionCount();
413
414                 [DllImport("System.Data.OracleClient.ociglue.dll")]
415                 public static extern IntPtr OciGlue_CheckError (Int32 status, UInt32 connection_handle);
416                 // CONST text *OciGlue_CheckError (sword status, ub4 connection_handle);
417
418                 [DllImport("System.Data.OracleClient.ociglue.dll")]
419                 public static extern void Free (IntPtr obj);
420                 // void Free (void *obj);
421
422                 // --------- Methods --------------------------------
423
424                 public string OciGlueCheckError(Int32 status) {
425                         IntPtr intptrMsg = IntPtr.Zero;
426                         string strMsg = "";
427                         string msg = "";
428                         
429                         intptrMsg = OciGlue_CheckError(status, ociGlueConnectionHandle);
430                         strMsg = Marshal.PtrToStringAnsi(intptrMsg);
431                         if(strMsg != null) {
432                                 msg = String.Copy(strMsg);
433                                 Free(intptrMsg);
434                         }
435
436                         return msg;
437                 }
438
439                 // for debug
440                 public static void IsObjectNull(string usedfor, string name, object obj) {
441                         string exists = "";
442                         if(obj == null) {
443                                 exists = "is null";
444                         }
445                         else {
446                                 exists = "is not null";
447                                 if(obj is IntPtr) {
448                                         IntPtr intptr = (IntPtr) obj;
449                                         if(intptr == IntPtr.Zero)
450                                                 exists = exists + " - IntPtr is IntPtr.Zero";
451                                         else
452                                                 exists = exists + " - IntPtr is not IntPtr.Zero";                                    
453                                 }
454                         }
455                         string outstring = 
456                                 "UsedFor: " + usedfor + 
457                                 " Object name: " + name + 
458                                 " exists: " + exists; 
459                 }
460
461                 public Int32 OciGlueConnect(OracleConnectionInfo conInfo) {
462
463                         Int32 status = 0;
464                         
465                         database = conInfo.Database;
466                         username = conInfo.Username;
467                         password = conInfo.Password;
468
469                         Console.WriteLine("OciGlue_Connect");
470                         status = OciGlue.OciGlue_Connect (out ociGlueConnectionHandle,
471                                 database, username, password);
472                         Console.WriteLine("  Handle: " + ociGlueConnectionHandle);
473
474                         if(status != 0) {
475                                 CheckStatus(status);
476                         }               
477                                                 
478                         return status;
479                 }
480
481                 public Int32 OciGlueDisconnect() {
482
483                         Int32 status = 0;
484                         string msg = "";
485                         
486                         Console.WriteLine("OciGlue_Disconnect");
487                         Console.WriteLine("  Handle: " + ociGlueConnectionHandle);
488                         status = OciGlue.OciGlue_Disconnect (ociGlueConnectionHandle);
489                         ociGlueConnectionHandle = 0;
490                         
491                         if(status != 0) {
492                                 msg = CheckStatus(status);
493                                 throw new Exception(msg);
494                         }
495                                                 
496                         return status;
497                 }
498
499
500                 // Helper methods
501                 public Int32 PrepareAndExecuteNonQuerySimple(string sql) 
502                 {
503                         Int32 status = 0;
504                         int found = 0;
505
506                         Console.WriteLine("PrepareAndExecuteNonQuerySimple");
507                         status = OciGlue_PrepareAndExecuteNonQuerySimple (
508                                 ociGlueConnectionHandle, sql, out found);
509
510                         Console.WriteLine("  Handle: " + ociGlueConnectionHandle +
511                                 " Found: " + found.ToString());
512
513                         CheckStatus(status);
514                         return status;
515                 }
516
517                 // currently not used
518                 public void Connect() 
519                 {       
520                         Int32 status = 0;
521                         IntPtr nothing = IntPtr.Zero;
522                         // initialize the mode to be the threaded and object environment
523                         Console.WriteLine();
524                         // OCI_THREADED|OCI_OBJECT
525                         // OCI_DEFAULT
526                         // OCI_THREADED|OCI_OBJECT|OCI_NO_UCB
527                         //status = OCIEnvCreate(out myenvhp, OCI_DEFAULT|OCI_NO_UCB, 
528                         //      IntPtr.Zero, 
529                         //      ref nothing, ref nothing, ref nothing, 0, 
530                         //      out nothing);
531                         myenvhp = OciGlue_OCIEnvCreate(ref status);
532                         IsObjectNull("Result from OCIEnvCreate for initialize the mode for environment","myenvhp",myenvhp);
533                         CheckStatus(status);
534
535                         // allocate a server handle
536                         Console.WriteLine();
537                         //status = OCIHandleAlloc (myenvhp, ref mysrvhp,
538                         //      OCI_HTYPE_SERVER, 0, ref nothing);
539                         mysrvhp = OciGlue_OCIHandleAlloc(ref status,
540                                                 myenvhp, OCI_HTYPE_SERVER);
541                         IsObjectNull("Result from OCIHandleAlloc for server handle","mysrvhp",mysrvhp);
542                         CheckStatus(status);
543
544                         // allocate an error handle
545                         Console.WriteLine();
546                         //status = OCIHandleAlloc (myenvhp, ref myerrhp,
547                         //      OCI_HTYPE_ERROR, 0, ref nothing);
548                         myerrhp = OciGlue_OCIHandleAlloc(ref status,
549                                                 myenvhp, OCI_HTYPE_ERROR);
550                         IsObjectNull("Result from OCIHandleAlloc for error handle","myerrhp",myerrhp);
551                         CheckError(status);
552
553                         // create a server context
554                         Console.WriteLine();
555                         string dblink = "DANSDB";
556                         //mysrvhp = OciGlueMallocType (OciGlueType_OCIServer);
557                         //status = OCIServerAttach (ref mysrvhp, ref myerrhp, dblink, 
558                         //      dblink.Length, OCI_DEFAULT);
559                         OciGlue_OCIServerAttach(ref status,
560                                 mysrvhp, myerrhp, dblink, OCI_DEFAULT);
561                         IsObjectNull("Result from OCIServerAttach for server context","mysrvhp",mysrvhp);
562                         CheckError(status);
563
564                         // allocate a service handle
565                         Console.WriteLine();
566                         //status = OCIHandleAlloc (myenvhp, ref mysvchp,
567                         //      OCI_HTYPE_SVCCTX, 0, ref nothing);
568                         mysvchp = OciGlue_OCIHandleAlloc(ref status,
569                                 myenvhp, OCI_HTYPE_SVCCTX);
570                         IsObjectNull("Result from OCIHandleAlloc for service handle","myenvhp",mysvchp);
571                         CheckError(status);
572                         Console.Out.Flush();
573
574                         // set the server attribute in the service context handle
575                         Console.WriteLine();
576                         Console.Out.Flush();
577                         //status = OCIAttrSet (
578                         //      ref mysvchp, OCI_HTYPE_SVCCTX, 
579                         //      mysrvhp, 0, OCI_ATTR_SERVER, ref myerrhp);
580                         OciGlue_OCIAttrSet(ref status,
581                                 mysvchp, OCI_HTYPE_SVCCTX, 
582                                 mysrvhp, (UInt32) 0, OCI_ATTR_SERVER, myerrhp);
583
584                         IsObjectNull("Result from OCIAttrSet for server attribute","mysvchp",mysvchp);
585                         Console.WriteLine("status is: " + status.ToString());
586                         CheckError(status);
587
588                         // allocate a user session handle
589                         Console.WriteLine(" blah ");
590                         Console.Out.Flush();
591                         //status = OCIHandleAlloc (myenvhp, ref myusrhp,
592                         //      OCI_HTYPE_SESSION, 0, ref nothing);
593                         myusrhp = OciGlue_OCIHandleAlloc(ref status,
594                                 myenvhp, OCI_HTYPE_SESSION);
595                         IsObjectNull("Result from OCIHandleAlloc for user session","myusrhp",myusrhp);
596                         CheckError(status);
597
598                         // set username attribute in user session handle
599                         Console.WriteLine();
600                         string username = "scott";
601                         IntPtr intptrUsername = Marshal.StringToHGlobalAnsi( username );
602                         //status = OCIAttrSet (
603                         //      ref myusrhp, OCI_HTYPE_SESSION,
604                         //      intptrUsername, (UInt32) username.Length,
605                         //      OCI_ATTR_USERNAME, ref myerrhp);
606                         OciGlue_OCIAttrSet(ref status,
607                                 myusrhp, OCI_HTYPE_SESSION,
608                                 intptrUsername, (UInt32) username.Length,
609                                 OCI_ATTR_USERNAME, myerrhp);
610                         IsObjectNull("Result from OCIAttrSet for username","myusrhp",myusrhp);
611                         CheckError(status);
612
613                         // set password attribute in user session handle
614                         Console.WriteLine();
615                         string password = "tiger";
616                         IntPtr intptrPassword = Marshal.StringToHGlobalAnsi( password );
617                         //status = OCIAttrSet (ref myusrhp, 
618                         //      OCI_HTYPE_SESSION,
619                         //      intptrPassword, (UInt32) password.Length,
620                         //      OCI_ATTR_PASSWORD, ref myerrhp);
621                         OciGlue_OCIAttrSet(ref status,
622                                 myusrhp,
623                                 OCI_HTYPE_SESSION,
624                                 intptrPassword, (UInt32) password.Length,
625                                 OCI_ATTR_PASSWORD, myerrhp);
626                         IsObjectNull("Result from OCIAttrSet for password","myusrhp",myusrhp);
627                         CheckError(status);
628                         
629                         // begin session
630                         Console.WriteLine();
631                         //status = OCISessionBegin (mysvchp, ref myerrhp, ref myusrhp,
632                         //      OCI_CRED_RDBMS, OCI_DEFAULT);
633                         OciGlue_OCISessionBegin(ref status,
634                                 mysvchp, myerrhp, myusrhp,
635                                 OCI_CRED_RDBMS, OCI_DEFAULT);
636                         IsObjectNull("Result from OCISessionBegin","myusrhp",myusrhp);
637                         CheckError(status);
638
639                         // FIXME: check for errors to make sure
640                         //        authentication was successful
641
642                         // set the user session attribute in the service context handle
643                         Console.WriteLine();
644                         //status = OCIAttrSet (ref mysvchp, 
645                         //      OCI_HTYPE_SVCCTX, 
646                         //      myusrhp, 0, OCI_ATTR_SESSION, ref myerrhp);
647                         OciGlue_OCIAttrSet(ref status,
648                                 mysvchp, 
649                                 OCI_HTYPE_SVCCTX, 
650                                 myusrhp, 0, OCI_ATTR_SESSION, myerrhp);
651                         IsObjectNull("Result from OCIAttrSet for user session attribute in service context handle","myusrhp",myusrhp);
652                         CheckError(status);
653
654                         Console.WriteLine();
655                         // FIXME: check for errors after each call
656
657                         // FIXME: need to properly free resources, such as,
658                         //        intptrUsername, intptrPassword, etc...
659                         
660                         // Wait
661                         Console.WriteLine("Waiting... Press Enter to continue.");
662                         string s = Console.ReadLine();
663                         Console.WriteLine();
664                         Console.WriteLine("Exiting...");
665                 }
666
667                 public string CheckStatus(Int32 status) {
668                         return CheckError(status);
669                 }
670
671                 // OCIError errhp, Int32 status
672                 public string CheckError(Int32 status) {                
673                         string msg = "";
674                         Int32 errcode = 0;
675
676                         switch (status) {
677                         case OCI_SUCCESS:
678                                 msg = "Succsss";
679                                 break;
680                         case OCI_SUCCESS_WITH_INFO:
681                                 msg = "Error - OCI_SUCCESS_WITH_INFO";
682                                 break;
683                         case OCI_NEED_DATA:
684                                 msg = "Error - OCI_NEED_DATA";
685                                 break;
686                         case OCI_NO_DATA:
687                                 msg = "Error - OCI_NODATA";
688                                 break;
689                         case OCI_ERROR:
690                                 if(ociGlueConnectionHandle != 0) {
691                                         string errmsg = OciGlueCheckError(status);
692                                         //OCIErrorGet(ref errhp, 1, ref nullString, out errcode,
693                                         //      out errbuf, (UInt32) errbuf.Length, OCI_HTYPE_ERROR);
694                                         msg = "OCI Error - errcode: " + errcode.ToString() + " errbuf: " + errmsg;
695                                 }
696                                 else
697                                         msg = "OCI Error!";
698                                 break;
699                         case OCI_INVALID_HANDLE:
700                                 msg = "Error - OCI_INVALID_HANDLE";
701                                 break;
702                         case OCI_STILL_EXECUTING:
703                                 msg = "Error - OCI_STILL_EXECUTE";
704                                 break;
705                         case OCI_CONTINUE:
706                                 msg = "Error - OCI_CONTINUE";
707                                 break;
708                         default:
709                                 msg = "Default";
710                                 break;
711                         }
712                         return msg;
713                 }
714         }
715 }