โŒ About FreshRSS

Normal view

There are new articles available, click to refresh the page.
Before yesterdayNewest questions tagged ada - Stack Overflow

How to get access to a record field

Some general context for my problem first.

I need to bind some arguments of a prepared statement using GNATCOLL SQLite bindings. These bindings expect C character pointer as an input (beside other things). This requirements creates two problems on Ada's end:

Problem 1

The variable pointed to by the "bound" pointer must not perish until the prepared statement is finalized (otherwise it will store a pointer to garbage). But, for queries that operate on the same type of record it would be desirable to extract the part of binding of arguments (which are obtained from the record fields) into a separate procedure. If such procedure returns before the statement is finalized the variables (on the stack of such procedure) will be deleted, and pointers now point to garbage.

Problem 2

I only know of three instances in Ada of creating pointers: new-ing, taking a pointer to a function / procedure and taking a pointer to a variable. Since the bindings want a pointer, I don't know how to extract such a pointer from a record unless I "manually unpack" it into a bunch of local variables. Needless to say this is ugly, repetitive and very prone to copy-and-paste error. This also leads to the lifetime issues (since variables "unpacked" in such a way will be deleted before the actual value they are used to capture still exists.)

Example

type Tag is record
  Field : String := "value";
end record;

type Tag_Access is access all Tag;

procedure Bind_Tag (T : Tag_Access; Stmt : Gnade.Statement) is
  --  This variable will vanish before the statement is executed
  Field : aliased constant String := T.Field;
begin
    Gnade.Bind_Text (Stmt, Field'Address, Field'Length);
end Bind_Tag;

procedure Insert_Tag (T : Tag) is
  --  Necessary connection initialization and building of prepared statement
  Tc : Tag := T;  --  Creating a useless variable only to please the compiler
  Ta : Tag_Access := Tc'Access;
begin
  Bind_Tag (Ta, Stmt);
  --  Here, bindings are dead, so we are executing garbage
  Gnade.Step (Db, Stmt);
end Insert_Tag;

If I may enter a plea

I suspect this may be helped by using objects (i.e. new-ing something). I haven't researched this approach because my first experience with Ada (I'm still learning) was very negative when contracting objects. Deallocation combined with absence of convenient object lifetime management (eg. equivalent of C++ RAII) makes using objects a very daunting task. I would like to stay away from this functionality as much as possible.


Edit

I found a way out of this particular conundrum: turns out SQlite can be instructed to make copies when binding strings. This isn't ideal, but at least I can get the strings into the database.

This doesn't mean that the question is solved though. I'd still like to know a more general way of dealing with record fields. Something that in eg. C would be accomplished by taking a pointer to the struct and then adding the size of the fields preceding the field of interest and adding that the the pointer.

What is "extension aggregate" and why do I need it?

I'm trying to use GNAT's SQLite bindings. And I want some way to get errors from these bindings. Trying to read through the code, I found that I can set up the connection by providing a mystery object that is supposed to handle errors...

No matter how I try to shape and provide this object, the library either won't accept it, or I get runtime access check errors (another bizarre aspect of this program as the pointer should be always alive since it's never leaving the scope of the procedure that declared it... but maybe it works differently in Ada).

The error I'm getting looks like this:

type of aggregate has private ancestor "Error_Reporter"
must use extension aggregate

The original Error_Reporter is defined as follows:

type Error_Reporter is abstract tagged private;
...
private

   type Error_Reporter is abstract tagged null record;

Anyways, below is the outline of the problematic code:

The header file:

   -- type Error_Handler is new Gse.Error_Reporter with private;
   type Error_Handler is new Gse.Error_Reporter with record
      Message : Asu.Unbounded_String;
   end record;
   
   type Error_Handler_Access is access Error_Handler;

   procedure On_Error
     (Self       : in out Error_Handler;
      Connection : access Gse.Database_Connection_Record'Class;
      Message    : String);

-- more stuff ...
   
-- private
--    type Error_Handler is new Gse.Error_Reporter with record
--       Message : Asu.Unbounded_String;
--    end record;

the implementation:

...
      declare
         pragma Suppress (Accessibility_Check);
         -- Handler : constant Error_Handler_Access :=
         --   new Error_Handler'(Message => Asu.Null_Unbounded_String);
         Handler : constant access Error_Handler :=
           new Error_Handler'(Message => Asu.Null_Unbounded_String);
         -- Handler : aliased Error_Handler;
         Descr         : Gse.Database_Description    :=
           Gss.Setup (Db_File, Errors => Handler);
--         Gss.Setup (Db_File, Errors => Handler'Access);
...

Commented code shows various things I tried. (Also, pragma has no effect).

Ideally, I don't want to new anything, unless this is the requirement from GNAT's interface.

Even better: if I could entirely avoid using this object (it should be possible to salvage the actual Sqlite connection from this wrapper and get access to return code and the error message), but the code is not very easy to read and is trying to make the user do things that I don't want (ORM, multiple layers of bindings that only complicate things) all while hiding the essential useful functionality. I'd appreciate any guidance in this direction.


UPDATE: In the end, my problem was I couldn't figure out how to use Bind_XXX group of procedures from Gnade package. And this is what prompted me to use the Execute package, which ultimately led to the problem with error reporting. To solve this, I finally figured out how to use Bind_XXX (the one giving me the most problems was Bind_Text due to pointer translation between Ada and C). Once that worked, I rewrote the code to use just Gnade.

โŒ
โŒ