Make your own free website on Tripod.com

File : oci-thick-statement_pkg.adb


with
   System,
   OCI.Thread;
--with Text_IO,System.Address_Image; use Text_IO,System;
package body OCI.Thick.Statement_pkg is
   use Lib;
   use type Sword;
   use type Ub4;
   use type OCIHandle;

   procedure Destroy(Object : in out Statement) is
   begin
      OCI.Thick.Free(Object.Handle,OCI_HTYPE_STMT);
--      Put_Line("Statement freed");   
   end;

   function Get_Stmt_Attr(Stmt : Statement'Class; Attr : Ub4)
         return Integer is
      Result : aliased Integer := 0;
      Rsize : aliased Ub4 := Integer'Size/System.Storage_Unit;
      Rc : Sword := OCIAttrGet (Trgthndlp => Stmt.Handle,
         Trghndltyp => OCI_HTYPE_STMT,
         Attributep => Result'access,
         Sizep => Rsize'access,
         Attrtype => Attr,
         Errhp => Thread.Error);

   begin
      Check_Error(Rc);
      return Result;
   end Get_Stmt_Attr;

   function Number_Of_Columns(Stmt : Statement) return Natural
         is
   begin
      return Get_Stmt_Attr(Stmt, OCI_ATTR_Param_Count);
   end Number_Of_Columns;

   function Statement_Type(Stmt : Statement'Class) return Natural
         is
   begin
      return Get_Stmt_Attr(Stmt, OCI_ATTR_STMT_TYPE);
   end Statement_Type;

   function Prepare(Connect : Connection; Code : String) return Statement
   is
      H : OCIHandle := Alloc_Handle(Thread.Environment, OCI_HTYPE_STMT);
      Rc : Sword;
   begin
      Rc := OCIStmtPrepare (OCIStmt(H),
         Thread.Error,
         C.To_C(Code),
         Code'Length);
      Check_Error(Rc);
      return (RF.Controlled_Reference with Handle => H, 
               Connect=>Connect, 
               Executed => False);
   end Prepare;
   
   procedure Execute(Stmt : in out Statement) is
      Stype : Integer := Statement_Type(Stmt);
      Rc : Sword := OCIStmtExecute (
         OCISvcCtx(Handle(Stmt.Connect)),
         OCIStmt(Stmt.Handle),
         Thread.Error,
         Boolean'Pos(Stype/=OCI_STMT_SELECT));
   begin
      Check_Error(Rc);
      Stmt.Executed := True;
   end Execute;

   function Execute(Connect : Connection; Code : String) return Statement is
      Result : Statement := Prepare(Connect,Code);
   begin
      Execute(Result);
      return Result;
   end;
   
   procedure Execute(Connect : Connection; Code : String) is
     dummy : Statement := Prepare(Connect,Code);
   begin
      Execute(dummy);
   end;

   function Executed(Stmt : Statement) return boolean is
   begin
     return Stmt.Executed;
   end;

   function Fetch(Stmt : Statement) return Boolean is
      Rc : Sword := OCIStmtFetch(OCIStmt(Stmt.Handle),
         Thread.Error);
   begin
      Check_Error(Rc);
      return Rc=OCI_SUCCESS;
   end Fetch;

   function Get_Connection(Stmt : Statement) return Connection is
   begin
     return Stmt.Connect;
   end;

end OCI.Thick.Statement_pkg;