------------------------------------------------------------------------------
--                                                                          --
--                An Ada2005 binding to the Qt C++ framework                --
--                                                                          --
------------------------------------------------------------------------------
--                                                                          --
-- Copyright © 2008 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: 3118 $ $Date: 2009-02-09 14:56:57 +0300 (Mon, 09 Feb 2009) $
------------------------------------------------------------------------------
with Qt4.Message_Boxes;
with Qt4.Strings;
with Qt4.Sql_Queries;

package body Connection is

   procedure Create_Connection (DB : in out Qt4.Sql_Databases.Q_Sql_Database)
   is
   begin
      DB := Qt4.Sql_Databases.Add_Database
             (Qt4.Strings.From_Utf_16 ("QSQLITE"));
      DB.Set_Database_Name (Qt4.Strings.From_Utf_16 (":memory:"));

      if not DB.Open then
         declare
            use type Qt4.Message_Boxes.Standard_Button;

            Aux : constant Qt4.Message_Boxes.Standard_Button
              := Qt4.Message_Boxes.Critical
                  (null,
                   Qt4.Strings.From_Utf_16 ("Cannot open database"),
                   Qt4.Strings.From_Utf_16
                    ("Unable to establish a database "
                       & "connection.\nThis example needs SQLite support. "
                       & "Please read "
                       & "the Qt SQL driver documentation for information "
                       & "how to build it.\n\n"
                       & "Click Cancel to exit."),
                   +Qt4.Message_Boxes.Cancel);
            pragma Unreferenced (Aux);

         begin
            raise Program_Error;
         end;
      end if;

      declare
         Query : aliased Qt4.Sql_Queries.Q_Sql_Query
           := Qt4.Sql_Queries.Create (DB);

         procedure Check (Value : Boolean);

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

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

      begin
         Check
          (Query.Exec
            (Qt4.Strings.From_Utf_16
              ("create table person (id int primary key, "
                 & "firstname varchar(20), lastname varchar(20))")));
         Check
          (Query.Exec
            (Qt4.Strings.From_Utf_16
              ("insert into person values(101, 'Danny', 'Young')")));
         Check
          (Query.Exec
            (Qt4.Strings.From_Utf_16
              ("insert into person values(102, 'Christine', 'Holand')")));
         Check
          (Query.Exec
            (Qt4.Strings.From_Utf_16
              ("insert into person values(103, 'Lars', 'Gordon')")));
         Check
          (Query.Exec
            (Qt4.Strings.From_Utf_16
              ("insert into person values(104, 'Roberto', 'Robitaille')")));
         Check
          (Query.Exec
            (Qt4.Strings.From_Utf_16
              ("insert into person values(105, 'Maria', 'Papadopoulos')")));

         Check
          (Query.Exec
            (Qt4.Strings.From_Utf_16
              ("create table trolltechoffice(id int primary key,"
                 & "imagefile int, location varchar(20),"
                 & "country varchar(20), description varchar(100))")));
         Check
          (Query.Exec
            (Qt4.Strings.From_Utf_16
              ("insert into trolltechoffice "
                 & "values(0, 0, 'Oslo', 'Norway',"
                 & "'Oslo is home to more than 500 000 citizens and has "
                 & "a lot to offer.It has been called ""The city with "
                 & "the big heart"" and this is a nickname we are happy "
                 & "to live up to.')")));
         Check
          (Query.Exec
            (Qt4.Strings.From_Utf_16
              ("insert into trolltechoffice values(1, 1, 'Brisbane', "
                 & "'Australia', 'Brisbane is the capital of Queensland,"
                 & " the Sunshine State, where it is beautiful one day, "
                 & "perfect the next. Brisbane is Australia''s 3rd "
                 & "largest city, being home to almost 2 million "
                 & "people.')")));
         Check
          (Query.Exec
            (Qt4.Strings.From_Utf_16
              ("insert into trolltechoffice values(2, 2, 'Redwood City',"
                 & " 'US', 'You find Redwood City in the heart of the "
                 & "Bay Area just north of Silicon Valley. The largest "
                 & "nearby city is San Jose which is the third largest "
                 & "city in California and the 10th largest in the "
                 & "US.')")));
         Check
          (Query.Exec
            (Qt4.Strings.From_Utf_16
              ("insert into trolltechoffice values(3, 3, 'Berlin', "
                 & "'Germany','Berlin, the capital of Germany is "
                 & "dynamic, cosmopolitan and creative, allowing for "
                 & "every kind of lifestyle. East meets West in the "
                 & "metropolis at the heart of a changing Europe.')")));
         Check
          (Query.Exec
            (Qt4.Strings.From_Utf_16
              ("insert into trolltechoffice values(4, 4, 'Munich', "
                 & "'Germany','Several technology companies are "
                 & "represented in Munich, and the city is often called "
                 & "the ""Bavarian Silicon Valley"". The exciting city "
                 & "is also filled with culture, art and music. ')")));
         Check
          (Query.Exec
            (Qt4.Strings.From_Utf_16
              ("insert into trolltechoffice values(5, 5, 'Beijing', "
                 & "'China','Beijing as a capital city has more than "
                 & "3000 years of history. Today the city counts 12 "
                 & "million citizens, and is the political, economic "
                 & "and cultural centre of China.')")));

         Check
          (Query.Exec
            (Qt4.Strings.From_Utf_16
              ("create table images (locationid int, file varchar(20))")));
         Check
          (Query.Exec
            (Qt4.Strings.From_Utf_16
              ("insert into images values(0, 'images/oslo.png')")));
         Check
          (Query.Exec
            (Qt4.Strings.From_Utf_16
              ("insert into images values(1, 'images/brisbane.png')")));
         Check
          (Query.Exec
            (Qt4.Strings.From_Utf_16
              ("insert into images values(2, 'images/redwood.png')")));
         Check
          (Query.Exec
            (Qt4.Strings.From_Utf_16
              ("insert into images values(3, 'images/berlin.png')")));
         Check
          (Query.Exec
            (Qt4.Strings.From_Utf_16
              ("insert into images values(4, 'images/munich.png')")));
         Check
          (Query.Exec
            (Qt4.Strings.From_Utf_16
              ("insert into images values(5, 'images/beijing.png')")));
      end;
   end Create_Connection;

end Connection;
