[gtkada] APQ connection and callback

Warren W. Gay VE3WWG ve3wwg at cogeco.ca
Wed Aug 25 16:47:00 CEST 2004


Hi Mattias:

I can probably help you on the APQ side, but am
less informed on the GtkAda front.

Matthias Teege wrote:

> Moin,
> 
> I build a small application with glade/gtkada and apq (postgresql
> connection). I need results of database querys in the callback
> functions but I don't know, how to handle the database connection. At
> the moment I create a new connection for each query like this:

If I understand this request properly, you want to
populate Combo1 with the items fetched from the
query, when the widget is realized.

See further comments below..

> procedure On_Combo1_Realize
>      (Object : access Gtk_Combo_Record'Class)
>      is
>          Combo1_Items : String_List.Glist;
>     begin
>          Mdt.Fill_Combo(Combo1_Items,"mdt","ort");
> 	 Combo.Set_Popdown_Strings(Window1.Combo1, Combo1_Items);
>          Free_String_List (Combo1_Items);
>          Call_Statusbar("real");
> end On_Combo1_Realize;
> 
> where Mdt.fill_Combo is:
> 
> procedure Fill_Combo(L : in out String_List.Glist;
>                      Table : in String;
> 		     Field : in String) is
> use APQ.PostgreSQL, APQ.PostgreSQL.Client;
> 
>       C : Connection_Type;
>       Q : Root_Query_Type'Class := New_Query(C);
>       R : String(1 .. 50);
> 		  
>     begin
>        Set_DB_Name(C,"ldb");
>        Set_Notify_Proc(C,Standard_Error_Notify);
>        Connect(C);
>        Prepare(Q, "SELECT "&field);
>        Append_Line(Q, "FROM "&Table&" group by "&Field&" order by "&Field&";");
>        Execute_Checked(Q,C);
>       
>       loop
>           begin
>               Fetch(Q);
>           exception
>               when No_Tuple =>
>           exit;
>           end;
>      
>       begin
>           Value(Q,1,R);
>           String_List.Append(L, R);
>       end;
> end loop;
> 
> How to I handle the connection correct?
> Matthias

The problem with this code structure is that you
make a database connection at the time the widget
is realized. If you have several of these (for
other widgets), it could be very time intensive
(each connect takes a fair amount of time and
overhead).

The other problem of course, is that you must
somehow pass the userid/password/parameters to
make the connection anyway (unless hardcoded).

So I would suggest that you establish your database
connection once at the main program level (complete
with passwords and all connection parameters), and
then pass the "connected" Connection_Type object
as "user data" into the callback (GtkAda experts
on the list can help with that, if necessary).
The Connection_Type object can be shared with many
Query_Type objects.

Once you have access to the Connection_Type object
within the callback as a user data parameter, you
can use it to perform the query as you coded it.
Other callbacks can share the same connection in
the same way.

If you need any further APQ advice, feel free to
contact me directly by email.

Hope that helps, Warren.
-- 
Warren W. Gay VE3WWG
http://home.cogeco.ca/~ve3wwg



More information about the gtkada mailing list