------------------------------------------------------------------------------
--                                                                          --
--                An Ada2005 binding to the Qt C++ framework                --
--                                                                          --
------------------------------------------------------------------------------
--                                                                          --
-- Copyright © 2008-2010 Vadim Godunko <vgodunko@gmail.com>                 --
--                                                                          --
-- QtAda is free software;  you can  redistribute it and/or modify it under --
-- terms of the  GNU General Public License as published  by the Free Soft- --
-- ware  Foundation;  either version 2,  or (at your option) any later ver- --
-- sion. QtAda is distributed in the hope that it will be useful, but WITH- --
-- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
-- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
-- for  more details.  You should have  received  a copy of the GNU General --
-- Public License distributed with QtAda;  see file COPYING.  If not, write --
-- to  the  Free Software Foundation,  51  Franklin  Street,  Fifth  Floor, --
-- Boston, MA 02110-1301, USA.                                              --
--                                                                          --
------------------------------------------------------------------------------
--  $Revision: 3769 $ $Date: 2010-10-24 03:16:28 +0400 (Sun, 24 Oct 2010) $
------------------------------------------------------------------------------
with Qt4.Sql_Relational_Delegates.Constructors;
with Qt4.Sql_Relations;
with Qt4.Sql_Queries;
with Qt4.Table_Views.Constructors;
with Qt4.Variants;

package body Relational_Table_Models is

   ------------------------------
   -- Create_Relational_Tables --
   ------------------------------

   procedure Create_Relational_Tables
    (DB : in out Qt4.Sql_Databases.Q_Sql_Database)
   is
      Query : aliased Qt4.Sql_Queries.Q_Sql_Query
        := Qt4.Sql_Queries.Create (DB);

      procedure Check (Success : Boolean);

      -----------
      -- Check --
      -----------

      procedure Check (Success : Boolean) is
      begin
         if not Success then
            raise Program_Error;
         end if;
      end Check;

   begin
      Check
       (Query.Exec
         (Qt4.Strings.From_Utf_16
           ("create table employee (id int primary key, "
              & "name varchar(20), city int, country int)")));
      Check
       (Query.Exec
         (Qt4.Strings.From_Utf_16
           ("insert into employee values(1, 'Espen', 5000, 47)")));
      Check
       (Query.Exec
         (Qt4.Strings.From_Utf_16
           ("insert into employee values(2, 'Harald', 80000, 49)")));
      Check
       (Query.Exec
         (Qt4.Strings.From_Utf_16
           ("insert into employee values(3, 'Sam', 100, 1)")));

      Check
       (Query.Exec
         (Qt4.Strings.From_Utf_16
           ("create table city(id int, name varchar(20))")));
      Check
       (Query.Exec
         (Qt4.Strings.From_Utf_16
           ("insert into city values(100, 'San Jose')")));
      Check
       (Query.Exec
         (Qt4.Strings.From_Utf_16
           ("insert into city values(5000, 'Oslo')")));
      Check
       (Query.Exec
         (Qt4.Strings.From_Utf_16
           ("insert into city values(80000, 'Munich')")));

      Check
       (Query.Exec
         (Qt4.Strings.From_Utf_16
           ("create table country(id int, name varchar(20))")));
      Check
       (Query.Exec
         (Qt4.Strings.From_Utf_16
           ("insert into country values(1, 'USA')")));
      Check
       (Query.Exec
         (Qt4.Strings.From_Utf_16
           ("insert into country values(47, 'Norway')")));
      Check
       (Query.Exec
         (Qt4.Strings.From_Utf_16
           ("insert into country values(49, 'Germany')")));
   end Create_Relational_Tables;

   ---------------------------------------------------------------------------
   --! <Subprogram>
   --!    <Unit> Create_Views
   --!    <Purpose>
   --!    <Exceptions>
   ---------------------------------------------------------------------------
   function Create_View
    (Title : Qt4.Strings.Q_String;
     Model : access Qt4.Sql_Table_Models.Q_Sql_Table_Model'Class)
       return not null access Qt4.Table_Views.Q_Table_View'Class is
   begin
      return View : constant not null access Qt4.Table_Views.Q_Table_View'Class
        := Qt4.Table_Views.Constructors.Create
      do
         declare
            Delegate : constant not null access
              Qt4.Sql_Relational_Delegates.Q_Sql_Relational_Delegate
                := Qt4.Sql_Relational_Delegates.Constructors.Create (View);

         begin
            View.Set_Model (Model);
            View.Set_Item_Delegate (Delegate);
            View.Set_Window_Title (Title);
         end;
      end return;
   end Create_View;

   ---------------------------------------------------------------------------
   --! <Subprogram>
   --!    <Unit> Initialize_Model
   --!    <Purpose>
   --!    <Exceptions>
   ---------------------------------------------------------------------------
   procedure Initialize_Model
    (Model : access
       Qt4.Sql_Relational_Table_Models.Q_Sql_Relational_Table_Model'Class)
   is
      Relation : constant Qt4.Sql_Relations.Q_Sql_Relation
        := Qt4.Sql_Relations.Create (Qt4.Strings.From_Utf_16 ("country"),
                                     Qt4.Strings.From_Utf_16 ("id"),
                                     Qt4.Strings.From_Utf_16 ("name"));

   begin
      Model.Set_Table (Qt4.Strings.From_Utf_16 ("employee"));

      Model.Set_Edit_Strategy (Qt4.Sql_Table_Models.On_Manual_Submit);
      Model.Set_Relation (2, Qt4.Sql_Relations.Create
                              (Qt4.Strings.From_Utf_16 ("city"),
                               Qt4.Strings.From_Utf_16 ("id"),
                               Qt4.Strings.From_Utf_16 ("name")));
      Model.Set_Relation (3, Relation);

      Model.Set_Header_Data
       (0, Qt4.Horizontal,
        Qt4.Variants.Create (Qt4.Strings.From_Utf_16 ("ID")));
      Model.Set_Header_Data
       (1, Qt4.Horizontal,
        Qt4.Variants.Create (Qt4.Strings.From_Utf_16 ("Name")));
      Model.Set_Header_Data
       (2, Qt4.Horizontal,
        Qt4.Variants.Create (Qt4.Strings.From_Utf_16 ("City")));
      Model.Set_Header_Data
       (3, Qt4.Horizontal,
        Qt4.Variants.Create (Qt4.Strings.From_Utf_16 ("Country")));

      Model.Select_Query;
   end Initialize_Model;

end Relational_Table_Models;
