/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 ¸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. with AWS.Templates;
  18. with Database;
  19. with GNATCOLL.SQL;
  20. package body View.DB_Test is
  21. ---------------
  22. -- Generate --
  23. ---------------
  24. function Generate
  25. (Request : in AWS.Status.Data)
  26. return AWS.Response.Data
  27. is
  28. use AWS.Templates;
  29. use Database;
  30. use GNATCOLL.SQL;
  31. use GNATCOLL.SQL.Exec;
  32. PostgreSQL_Conn : Database_Connection;
  33. SQLite_Conn : Database_Connection;
  34. FC : Forward_Cursor;
  35. Query_Insert_Data : constant SQL_Query := SQL_Insert
  36. ((Tmp.Id = Integer_Param (1)) &
  37. (Tmp.Name = Text_Param (2)));
  38. Prepared_Query_Insert_Data : constant Prepared_Statement := Prepare
  39. (Query => Query_Insert_Data);
  40. Query_Select_Data : constant SQL_Query := SQL_Select
  41. (Fields => Tmp.Id & Tmp.Name,
  42. From => Tmp,
  43. Where => Tmp.Name = Text_Param (1));
  44. Prepared_Query_Select_Data : constant Prepared_Statement := Prepare
  45. (Query => Query_Select_Data);
  46. Billy : aliased String := "Billy";
  47. Has_Tmp_Table : Boolean := False;
  48. PostgreSQL_Messages : Vector_Tag;
  49. SQLite_Messages : Vector_Tag;
  50. T : Translate_Set;
  51. begin
  52. -- PostgreSQL test
  53. PostgreSQL_Conn := PostgreSQL_Description.Build_Connection;
  54. if PostgreSQL_Conn.Check_Connection then
  55. Insert (T, Assoc ("POSTGRESQL_SETUP", True));
  56. FC.Fetch (PostgreSQL_Conn,
  57. "SELECT tablename " &
  58. "FROM pg_tables " &
  59. "WHERE schemaname = 'public' " &
  60. "AND tablename = 'tmp'");
  61. Append (PostgreSQL_Messages, "Checking if table 'tmp' exists.");
  62. while FC.Has_Row loop
  63. Has_Tmp_Table := True;
  64. Append (PostgreSQL_Messages, "Table 'tmp' found.");
  65. exit;
  66. end loop;
  67. if not Has_Tmp_Table then
  68. PostgreSQL_Conn.Execute
  69. ("CREATE TABLE tmp (id INTEGER, name TEXT)");
  70. Append (PostgreSQL_Messages,
  71. "Table 'tmp' not found. Creating it.");
  72. Append (PostgreSQL_Messages, "Table 'tmp' created.");
  73. end if;
  74. for I in Names'Range loop
  75. PostgreSQL_Conn.Execute (Stmt => Prepared_Query_Insert_Data,
  76. Params => (1 => +I,
  77. 2 => +Names (I)));
  78. Append (PostgreSQL_Messages,
  79. "Added " & I'Img & ":" & Names (I).all & " to 'tmp'.");
  80. end loop;
  81. FC.Fetch (Connection => PostgreSQL_Conn,
  82. Stmt => Prepared_Query_Select_Data,
  83. Params => (1 => +Billy'Access));
  84. Append (PostgreSQL_Messages, "Querying 'tmp' for " & Billy & ".");
  85. while FC.Has_Row loop
  86. Append (PostgreSQL_Messages,
  87. "Found "
  88. & FC.Integer_Value (0)'Img
  89. & ":"
  90. & FC.Value (1)
  91. & " pair.");
  92. FC.Next;
  93. end loop;
  94. PostgreSQL_Conn.Execute ("DROP TABLE tmp");
  95. Append (PostgreSQL_Messages, "Table 'tmp' dropped.");
  96. PostgreSQL_Conn.Commit_Or_Rollback;
  97. if PostgreSQL_Conn.Success then
  98. Insert (T, Assoc ("POSTGRESQL_SUCCESS", True));
  99. Append (PostgreSQL_Messages, "Transaction succesfully commited.");
  100. else
  101. Insert (T, Assoc ("POSTGRESQL_SUCCESS", False));
  102. Append (PostgreSQL_Messages, "Commit failed.");
  103. end if;
  104. else
  105. Insert (T, Assoc ("POSTGRESQL_SETUP", False));
  106. end if;
  107. Insert (T, Assoc ("POSTGRESQL_MESSAGES", PostgreSQL_Messages));
  108. Free (PostgreSQL_Conn);
  109. -- SQLite test
  110. SQLite_Conn := SQLite_Description.Build_Connection;
  111. for I in Names'Range loop
  112. SQLite_Conn.Execute (Stmt => Prepared_Query_Insert_Data,
  113. Params => (1 => +I,
  114. 2 => +Names (I)));
  115. Append (SQLite_Messages,
  116. "Added " & I'Img & ":" & Names (I).all & " to 'tmp'.");
  117. end loop;
  118. FC.Fetch (Connection => SQLite_Conn,
  119. Stmt => Prepared_Query_Select_Data,
  120. Params => (1 => +Billy'Access));
  121. Append (SQLite_Messages, "Querying 'tmp' for " & Billy & ".");
  122. while FC.Has_Row loop
  123. Append (SQLite_Messages,
  124. "Found "
  125. & FC.Integer_Value (0)'Img
  126. & ":"
  127. & FC.Value (1)
  128. & " pair.");
  129. FC.Next;
  130. end loop;
  131. SQLite_Conn.Execute ("DELETE FROM tmp");
  132. SQLite_Conn.Commit_Or_Rollback;
  133. if SQLite_Conn.Success then
  134. Insert (T, Assoc ("SQLITE_SUCCESS", True));
  135. Append (SQLite_Messages, "Transaction succesfully commited.");
  136. else
  137. Insert (T, Assoc ("SQLITE_SUCCESS", False));
  138. Append (SQLite_Messages, "Commit failed.");
  139. end if;
  140. Insert (T, Assoc ("SQLITE_MESSAGES", SQLite_Messages));
  141. return Build_Response
  142. (Status_Data => Request,
  143. Template_File => My.Config.Get (My.Template_DB_Test),
  144. Translations => T);
  145. end Generate;
  146. end View.DB_Test;