PageRenderTime 27ms CodeModel.GetById 11ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/demo/src/view-db_test.adb

http://github.com/ThomasLocke/yolk
Ada | 187 lines | 125 code | 41 blank | 21 comment | 12 complexity | a1fbf03d8f0d1d31232bd8ebf53f5430 MD5 | raw file
  1-------------------------------------------------------------------------------
  2--                                                                           --
  3--                   Copyright (C) 2010-, Thomas Løcke                   --
  4--                                                                           --
  5--  This is free software;  you can redistribute it and/or modify it         --
  6--  under terms of the  GNU General Public License  as published by the      --
  7--  Free Software  Foundation;  either version 3,  or (at your  option) any  --
  8--  later version. This library is distributed in the hope that it will be   --
  9--  useful, but WITHOUT ANY WARRANTY;  without even the implied warranty of  --
 10--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                     --
 11--  You should have received a copy of the GNU General Public License and    --
 12--  a copy of the GCC Runtime Library Exception along with this program;     --
 13--  see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
 14--  <http://www.gnu.org/licenses/>.                                          --
 15--                                                                           --
 16-------------------------------------------------------------------------------
 17
 18with AWS.Templates;
 19with Database;
 20with GNATCOLL.SQL;
 21
 22package body View.DB_Test is
 23
 24   ---------------
 25   --  Generate --
 26   ---------------
 27
 28   function Generate
 29     (Request : in AWS.Status.Data)
 30      return AWS.Response.Data
 31   is
 32      use AWS.Templates;
 33      use Database;
 34      use GNATCOLL.SQL;
 35      use GNATCOLL.SQL.Exec;
 36
 37      PostgreSQL_Conn : Database_Connection;
 38      SQLite_Conn     : Database_Connection;
 39
 40      FC              : Forward_Cursor;
 41
 42      Query_Insert_Data : constant SQL_Query := SQL_Insert
 43        ((Tmp.Id = Integer_Param (1)) &
 44         (Tmp.Name = Text_Param (2)));
 45
 46      Prepared_Query_Insert_Data : constant Prepared_Statement := Prepare
 47        (Query => Query_Insert_Data);
 48
 49      Query_Select_Data : constant SQL_Query := SQL_Select
 50        (Fields => Tmp.Id & Tmp.Name,
 51         From   => Tmp,
 52         Where  => Tmp.Name = Text_Param (1));
 53
 54      Prepared_Query_Select_Data : constant Prepared_Statement := Prepare
 55        (Query => Query_Select_Data);
 56
 57      Billy          : aliased String := "Billy";
 58      Has_Tmp_Table  : Boolean := False;
 59
 60      PostgreSQL_Messages : Vector_Tag;
 61      SQLite_Messages     : Vector_Tag;
 62      T                   : Translate_Set;
 63   begin
 64      --  PostgreSQL test
 65      PostgreSQL_Conn := PostgreSQL_Description.Build_Connection;
 66
 67      if PostgreSQL_Conn.Check_Connection then
 68         Insert (T, Assoc ("POSTGRESQL_SETUP", True));
 69
 70         FC.Fetch (PostgreSQL_Conn,
 71                   "SELECT tablename " &
 72                     "FROM pg_tables " &
 73                     "WHERE schemaname = 'public' " &
 74                     "AND tablename = 'tmp'");
 75
 76         Append (PostgreSQL_Messages, "Checking if table 'tmp' exists.");
 77
 78         while FC.Has_Row loop
 79            Has_Tmp_Table := True;
 80            Append (PostgreSQL_Messages, "Table 'tmp' found.");
 81            exit;
 82         end loop;
 83
 84         if not Has_Tmp_Table then
 85            PostgreSQL_Conn.Execute
 86              ("CREATE TABLE tmp (id INTEGER, name TEXT)");
 87
 88            Append (PostgreSQL_Messages,
 89                    "Table 'tmp' not found. Creating it.");
 90            Append (PostgreSQL_Messages, "Table 'tmp' created.");
 91         end if;
 92
 93         for I in Names'Range loop
 94            PostgreSQL_Conn.Execute (Stmt   => Prepared_Query_Insert_Data,
 95                                     Params => (1 => +I,
 96                                                2 => +Names (I)));
 97
 98            Append (PostgreSQL_Messages,
 99                    "Added " & I'Img & ":" & Names (I).all & " to 'tmp'.");
100         end loop;
101
102         FC.Fetch (Connection => PostgreSQL_Conn,
103                   Stmt       => Prepared_Query_Select_Data,
104                   Params     => (1 => +Billy'Access));
105
106         Append (PostgreSQL_Messages, "Querying 'tmp' for " & Billy & ".");
107
108         while FC.Has_Row loop
109            Append (PostgreSQL_Messages,
110                    "Found "
111                    & FC.Integer_Value (0)'Img
112                    & ":"
113                    & FC.Value (1)
114                    & " pair.");
115            FC.Next;
116         end loop;
117
118         PostgreSQL_Conn.Execute ("DROP TABLE tmp");
119
120         Append (PostgreSQL_Messages, "Table 'tmp' dropped.");
121
122         PostgreSQL_Conn.Commit_Or_Rollback;
123
124         if PostgreSQL_Conn.Success then
125            Insert (T, Assoc ("POSTGRESQL_SUCCESS", True));
126            Append (PostgreSQL_Messages, "Transaction succesfully commited.");
127         else
128            Insert (T, Assoc ("POSTGRESQL_SUCCESS", False));
129            Append (PostgreSQL_Messages, "Commit failed.");
130         end if;
131      else
132         Insert (T, Assoc ("POSTGRESQL_SETUP", False));
133      end if;
134
135      Insert (T, Assoc ("POSTGRESQL_MESSAGES", PostgreSQL_Messages));
136
137      Free (PostgreSQL_Conn);
138
139      --  SQLite test
140      SQLite_Conn := SQLite_Description.Build_Connection;
141
142      for I in Names'Range loop
143         SQLite_Conn.Execute (Stmt   => Prepared_Query_Insert_Data,
144                              Params => (1 => +I,
145                                         2 => +Names (I)));
146
147         Append (SQLite_Messages,
148                 "Added " & I'Img & ":" & Names (I).all & " to 'tmp'.");
149      end loop;
150
151      FC.Fetch (Connection => SQLite_Conn,
152                Stmt       => Prepared_Query_Select_Data,
153                Params     => (1 => +Billy'Access));
154
155      Append (SQLite_Messages, "Querying 'tmp' for " & Billy & ".");
156
157      while FC.Has_Row loop
158         Append (SQLite_Messages,
159                 "Found "
160                 & FC.Integer_Value (0)'Img
161                 & ":"
162                 & FC.Value (1)
163                 & " pair.");
164         FC.Next;
165      end loop;
166
167      SQLite_Conn.Execute ("DELETE FROM tmp");
168
169      SQLite_Conn.Commit_Or_Rollback;
170
171      if SQLite_Conn.Success then
172         Insert (T, Assoc ("SQLITE_SUCCESS", True));
173         Append (SQLite_Messages, "Transaction succesfully commited.");
174      else
175         Insert (T, Assoc ("SQLITE_SUCCESS", False));
176         Append (SQLite_Messages, "Commit failed.");
177      end if;
178
179      Insert (T, Assoc ("SQLITE_MESSAGES", SQLite_Messages));
180
181      return Build_Response
182        (Status_Data   => Request,
183         Template_File => My.Config.Get (My.Template_DB_Test),
184         Translations  => T);
185   end Generate;
186
187end View.DB_Test;