Make your own free website on Tripod.com

File : oci-thick-connection_pkg.adb


with
   System,
   --   Ada.Task_Identification,
   --   Ada.Strings,
   OCI.Thread,
   Ada.Strings.Fixed;
--with Text_IO,System.Address_Image; use Text_IO,System;
package body OCI.Thick.Connection_pkg is
   use Lib;
   use type Sword;
   use type Ub4;
   use type OCIHandle;

   procedure Destroy(Object : in out Connection) is

      Rc : Sword;

   H : OCIHandle := Object.Handle;
   begin
      if H/= Empty_Handle then
        Rc := OCILogoff(Svchp => OCISvcCtx(H),
           Errhp => Thread.Error);
--        Put_Line("Connection freed");   
        -- don't check error code after logoff in 8i   
        -- Check_Error(Rc); -- 8i bug
      end if;
   end;

   function Logon(DBName : String; User : String; Password : String) 
      return Connection is
      Rc : Sword;
      H : aliased OCISvcCtx;
   begin
      Rc := OCILogon(Envhp => Thread.Environment,
         Errhp => Thread.Error,
         Svchp => H'access,
         Username => C.To_C(User),
         Uname_Len => User'Length,
         Password => C.To_C(Password),
         Passwd_Len => Password'Length,
         Dbname => C.To_C(DBName),
         Dbname_Len => DBName'Length);
      Check_Error(Rc);
      return (RF.Controlled_Reference with Handle => OCIHandle(H));
   end Logon;

   function Logon(Connect : String) return Connection
         is
      D1,D2 : Natural;
      use Ada.Strings.Fixed,Ada.Strings;
   begin
      D1 := Index(Connect,"/");
      D2 := Index(Connect,"@",Backward);
      if D2=0 then
         return Logon("",
            Connect(Connect'First..D1-1),
            Connect(D1+1..Connect'Last));
      else
         return Logon(
            Connect(D2+1..Connect'Last),
            Connect(Connect'First..D1-1),
            Connect(D1+1..D2-1));
      end if;
   end Logon;

   procedure Logoff(Connect : in out Connection) is
      Result : Connection;
   begin
      Connect := Result;
   end Logoff;

   function Server_Version(Connect : in Connection) 
      return String is
      Buff : aliased Text := (0..511 => c.nul);
      rc : sword := OCIServerVersion (
               hndlp => Connect.Handle, 
               errhp => Thread.Error,
               bufp  => CStr.To_Chars_Ptr(Buff'Unchecked_Access),
               bufsz  => buff'Length-1,
               hndltype => OCI_HTYPE_SVCCTX);
   begin
     Check_Error(rc);
     return C.To_Ada(Buff);
   end Server_Version;

   procedure Commit(Connect : in Connection) is
     rc : sword := OCITransCommit(
          OCISvcCtx(Connect.Handle),Thread.Error);
   begin
     Check_Error(rc);
   end;
   
   procedure Rollback(Connect : in Connection) is
     rc : sword := OCITransRollback(
        OCISvcCtx(Connect.Handle),Thread.Error);
   begin
     Check_Error(rc);
   end;

end OCI.Thick.Connection_pkg;