New test.
[mono.git] / mcs / class / System.Data.OracleClient / System.Data.OracleClient / OciGlue.cs
old mode 100755 (executable)
new mode 100644 (file)
index 7581877..c278123
 // mcs/class/System.Data.OracleClient/System.Data.OracleClient.OCI
 //
 // Assembly: System.Data.OracleClient.dll
-// Namespace: System.Data.OracleClient
+// Namespace: System.Data.OracleClient.Oci
 // 
-// Author: 
+// Authors
 //     Daniel Morgan <danmorg@sc.rr.com>
+//     Tim Coleman <tim@timcoleman.com>
 //         
 // Copyright (C) Daniel Morgan, 2002
+// Copyright (C) Tim Coleman, 2002
 // 
 
 using System;
 using System.Runtime.InteropServices;
+using System.Text;
+
+namespace System.Data.OracleClient.Oci {
+       internal sealed class OciGlue 
+       {
+               #region Fields
+
+               bool connected;
+               OciEnvironmentHandle environment;
+               OciErrorHandle error;
+               OciServerHandle server;
+               OciServiceHandle service;
+               OciSessionHandle session;
+
+               // other codes
+               public const int OCI_DEFAULT = 0;
+               public const int OCI_SUCCESS = 0;
+               public const int OCI_SUCCESS_WITH_INFO = 1;
+               public const int OCI_RESERVED_FOR_INT_USE = 200;
+               public const int OCI_NO_DATA = 100;
+               public const int OCI_ERROR = -1;
+               public const int OCI_INVALID_HANDLE = -2;
+               public const int OCI_NEED_DATA = 99;
+               public const int OCI_STILL_EXECUTING = -3123;
+               public const int OCI_CONTINUE = -24200;
+
+               #endregion // Fields
+
+               #region Properties
+
+               public bool Connected {
+                       get { return connected; }
+               }
 
-namespace System.Data.OracleClient.OCI {
-       internal sealed class OciGlue {
-
-               // TODO: need to clean up, dispose, close, etc...
-               
-               // connection parameters
-               string database = "";
-               string username = "";
-               string password = "";
-
-               public const Int32 OCI_SUCCESS = 0;
-               public const Int32 OCI_SUCCESS_WITH_INFO = 1;
-               public const Int32 OCI_RESERVED_FOR_INT_USE = 200;
-               public const Int32 OCI_NO_DATA = 100;
-               public const Int32 OCI_ERROR = -1;
-               public const Int32 OCI_INVALID_HANDLE = -2;
-               public const Int32 OCI_NEED_DATA = 99;
-               public const Int32 OCI_STILL_EXECUTING = -3123;
-               public const Int32 OCI_CONTINUE = -24200;
-
-               private UInt32 ociGlueConnectionHandle = 0;
-
-               // http://download-west.oracle.com/docs/cd/A87861_01/NT817EE/index.htm
-               // from oracle/ora81/oci/include/oci.h
-
-               [DllImport("ociglue")]
-               public static extern IntPtr OciGlue_Connect (out Int32 status,
-                       out UInt32 ociGlueConnectionHandle, out uint errcode, 
-                       string database, string username, string password);
-
-               [DllImport("ociglue")]
-               public static extern Int32 OciGlue_Disconnect (UInt32 connection_handle);
-
-               [DllImport("ociglue")]
-               public static extern Int32 OciGlue_PrepareAndExecuteNonQuerySimple (
-                       UInt32 ociGlueConnectionHandle,
-                       string sqlstmt, out int found);
-
-               [DllImport("ociglue")]
-               public static extern UInt32 OciGlue_ConnectionCount();
-
-               [DllImport("ociglue")]
-               public static extern IntPtr OciGlue_CheckError (Int32 status, UInt32 connection_handle);
-
-               [DllImport("ociglue")]
-               public static extern void OciGlue_Free (IntPtr obj);
-
-               public string CheckError(Int32 status) {
-                       IntPtr intptrMsg = IntPtr.Zero;
-                       string strMsg = "";
-                       string msg = "";
-                       
-                       intptrMsg = OciGlue_CheckError(status, ociGlueConnectionHandle);
-                       if(intptrMsg != IntPtr.Zero)
-                               strMsg = Marshal.PtrToStringAnsi(intptrMsg);
-                               if(strMsg != null) {
-                                       msg = String.Copy(strMsg);
-                                       OciGlue_Free(intptrMsg);
-                               }
-
-                       return msg;
+               public OciEnvironmentHandle Environment {
+                       get { return environment; }
                }
 
-               public Int32 Connect(OracleConnectionInfo conInfo) {
-
-                       Int32 status = 0;
-                       string error = "";
-                       uint errcode = 0;
-                       string errmsg = "";
-                       string strMsg = "";
-                       IntPtr ipErrMsg = IntPtr.Zero;
-                       
-                       database = conInfo.Database;
-                       username = conInfo.Username;
-                       password = conInfo.Password;
-
-                       Console.WriteLine("OciGlue_Connect");
-                       ipErrMsg = OciGlue_Connect (out status,
-                               out ociGlueConnectionHandle, out errcode, 
-                               database, username, password);
-                       Console.WriteLine("  Handle: " + ociGlueConnectionHandle);
-
-                       if(status != 0) {
-                               if(status == -1) {
-                                       if(ipErrMsg != IntPtr.Zero) {
-                                               strMsg = "";
-                                               strMsg = Marshal.PtrToStringAnsi(ipErrMsg);
-                                               if(strMsg != null) {
-                                                       errmsg = String.Copy(strMsg);
-                                                       OciGlue_Free(ipErrMsg);
-                                               }
-                                       }
-                                       error = "OCI Error - errcode: " + errcode.ToString() + " errmsg: " + errmsg;
-                                       
-                               }
-                               else
-                                       error = CheckStatus(status);
-
-                               throw new Exception("Error: Unable to connect: " + error);
-                       }               
-                                               
-                       return status;
+               public OciErrorHandle ErrorHandle {
+                       get { return error; }
                }
 
-               public Int32 Disconnect() {
-
-                       Int32 status = 0;
-                       string msg = "";
-                       
-                       Console.WriteLine("OciGlue_Disconnect");
-                       Console.WriteLine("  Handle: " + ociGlueConnectionHandle);
-                       status = OciGlue.OciGlue_Disconnect (ociGlueConnectionHandle);
-                       ociGlueConnectionHandle = 0;
-                       
-                       if(status != 0) {
-                               msg = CheckStatus(status);
-                               throw new Exception(msg);
-                       }
-                                               
-                       return status;
+               public OciServiceHandle ServiceContext {
+                       get { return service; }
+               }
+
+               public OciServerHandle ServerHandle {
+                       get { return server; }
+               }
+
+               public OciSessionHandle SessionHandle {
+                       get { return session; }
                }
 
+               #endregion // Properties
+
+               #region Methods
+
+               public void CreateConnection (OracleConnectionInfo conInfo) 
+               {
+                       environment = new OciEnvironmentHandle (OciEnvironmentMode.Threaded | OciEnvironmentMode.NoUserCallback);
+
+                       if (environment.Handle == IntPtr.Zero)
+                               throw new OracleException (0, "Could not allocate the Oracle environment.");
+
+                       service = (OciServiceHandle) environment.Allocate (OciHandleType.Service);
+                       if (service == null) {
+                               OciErrorInfo info = environment.HandleError ();
+                               Disconnect ();
+                               throw new OracleException (info.ErrorCode, info.ErrorMessage);
+                       }
+
+                       error = (OciErrorHandle) environment.Allocate (OciHandleType.Error);
+                       if (error == null) {
+                               OciErrorInfo info = environment.HandleError ();
+                               Disconnect ();
+                               throw new OracleException (info.ErrorCode, info.ErrorMessage);
+                       }
+                       service.ErrorHandle = error;
+
+                       server = (OciServerHandle) environment.Allocate (OciHandleType.Server);
+                       if (server == null) {
+                               OciErrorInfo info = environment.HandleError ();
+                               Disconnect ();
+                               throw new OracleException (info.ErrorCode, info.ErrorMessage);
+                       }
+
+                       session = (OciSessionHandle) environment.Allocate (OciHandleType.Session);
+                       if (session == null) {
+                               OciErrorInfo info = environment.HandleError ();
+                               Disconnect ();
+                               throw new OracleException (info.ErrorCode, info.ErrorMessage);
+                       }
+                       session.Username = conInfo.Username;
+                       session.Password = conInfo.Password;
+                       session.Service = service;
+                               
+                       if (!server.Attach (conInfo.Database, ErrorHandle)) {
+                               OciErrorInfo info = error.HandleError ();
+                               Disconnect ();
+                               throw new OracleException (info.ErrorCode, info.ErrorMessage);
+                       }
+
+                       if (!service.SetServer (server)) {
+                               OciErrorInfo info = error.HandleError ();
+                               Disconnect ();
+                               throw new OracleException (info.ErrorCode, info.ErrorMessage);
+                       }
+
+                       if (!session.BeginSession (conInfo.CredentialType, OciSessionMode.Default, ErrorHandle)) {
+                               OciErrorInfo info = error.HandleError ();
+                               Disconnect ();
+                               throw new OracleException (info.ErrorCode, info.ErrorMessage);
+                       }
+
+                       if (!service.SetSession (session)) {
+                               OciErrorInfo info = error.HandleError ();
+                               Disconnect ();
+                               throw new OracleException (info.ErrorCode, info.ErrorMessage);
+                       }
+
+                       connected = true;
+               }
 
-               // Helper methods
-               public Int32 PrepareAndExecuteNonQuerySimple(string sql) 
+               public OciStatementHandle CreateStatement ()
                {
-                       Int32 status = 0;
-                       int found = 0;
+                       OciStatementHandle statement = (OciStatementHandle) environment.Allocate (OciHandleType.Statement);
+                       if (statement == null) {
+                               OciErrorInfo info = environment.HandleError ();
+                               throw new OracleException (info.ErrorCode, info.ErrorMessage);
+                       }
+                       statement.ErrorHandle = error;
+                       statement.Service = service;
 
-                       Console.WriteLine("PrepareAndExecuteNonQuerySimple");
-                       status = OciGlue_PrepareAndExecuteNonQuerySimple (
-                               ociGlueConnectionHandle, sql, out found);
+                       return statement;       
+               }
 
-                       Console.WriteLine("  Handle: " + ociGlueConnectionHandle +
-                               " Found: " + found.ToString());
+               public OciTransactionHandle CreateTransaction ()
+               {
+                       OciTransactionHandle transaction = (OciTransactionHandle) environment.Allocate (OciHandleType.Transaction);
+                       if (transaction == null) {
+                               OciErrorInfo info = environment.HandleError ();
+                               throw new OracleException (info.ErrorCode, info.ErrorMessage);
+                       }
+                       transaction.ErrorHandle = error;
+                       transaction.Service = service;
 
-                       CheckStatus(status);
-                       return status;
+                       return transaction;
                }
-               
-               public string CheckStatus(Int32 status) {               
-                       string msg = "";
-                                               
-                       switch (status) {
-                       case OCI_SUCCESS:
-                               msg = "Succsss";
-                               break;
-                       case OCI_SUCCESS_WITH_INFO:
-                               msg = "Error - OCI_SUCCESS_WITH_INFO";
-                               break;
-                       case OCI_NEED_DATA:
-                               msg = "Error - OCI_NEED_DATA";
-                               break;
-                       case OCI_NO_DATA:
-                               msg = "Error - OCI_NODATA";
-                               break;
-                       case OCI_ERROR:
-                               if(ociGlueConnectionHandle != 0)
-                                       msg = CheckError(status);
-                               else
-                                       msg = "OCI_ERROR";
-                               break;
-                       case OCI_INVALID_HANDLE:
-                               msg = "Error - OCI_INVALID_HANDLE";
-                               break;
-                       case OCI_STILL_EXECUTING:
-                               msg = "Error - OCI_STILL_EXECUTE";
-                               break;
-                       case OCI_CONTINUE:
-                               msg = "Error - OCI_CONTINUE";
-                               break;
-                       default:
-                               msg = "Default";
-                               break;
+
+               public void Disconnect() 
+               {
+                       if (session != null) {
+                               session.EndSession (error);
+                               session.Dispose ();
+                               session = null;
+                       }
+                       if (server != null) {
+                               server.Detach (error);
+                               server.Dispose ();
+                               server = null;
+                       }
+                       if (error != null) {
+                               error.Dispose ();
+                               error = null;
+                       }
+                       if (service != null) {
+                               service.Dispose ();
+                               service = null;
+                       }
+                       if (environment != null) {
+                               environment.Dispose ();
+                               environment = null;
                        }
-                       return msg;
                }
+
+               #endregion // Methods
        }
 }