1 ------------------------------------------------------------------------------
4 -- Copyright (C) 2008 --
5 -- Pascal Obry - Olivier Ramonat --
7 -- This library is free software; you can redistribute it and/or modify --
8 -- it under the terms of the GNU General Public License as published by --
9 -- the Free Software Foundation; either version 2 of the License, or (at --
10 -- your option) any later version. --
12 -- This library is distributed in the hope that it will be useful, but --
13 -- WITHOUT ANY WARRANTY; without even the implied warranty of --
14 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU --
15 -- General Public License for more details. --
17 -- You should have received a copy of the GNU General Public License --
18 -- along with this library; if not, write to the Free Software Foundation, --
19 -- Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. --
20 ------------------------------------------------------------------------------
24 with Ada
.Task_Attributes
;
37 Max_Insert
: constant Positive := 10000;
38 Verbose
: constant Boolean := False;
41 entry Start
(Id
: in Positive);
45 entry Start
(Id
: in Positive);
48 type TLS_DBH
is record
49 Handle
: access DB
.Handle
'Class;
53 type TLS_DBH_Access
is access all TLS_DBH
;
55 Null_DBH
: constant TLS_DBH
:=
56 TLS_DBH
'(Handle => null, Connected => False);
58 DB_Path : constant String := DB.SQLite.In_Memory_Database;
61 new Task_Attributes (Attribute => TLS_DBH, Initial_Value => Null_DBH);
63 procedure Connect (DBH : in TLS_DBH_Access);
64 -- Connects to the database if needed
70 procedure Connect (DBH : in TLS_DBH_Access) is
72 if not DBH.Connected then
73 DBH.Handle := new DB.SQLite.Handle;
74 DBH.Handle.Connect (DB_Path);
75 DBH.Connected := True;
76 DBH_TLS.Set_Value (DBH.all);
85 DBH : constant TLS_DBH_Access := TLS_DBH_Access (DBH_TLS.Reference);
87 Current : Positive := 1;
91 accept Start (Id : in Positive) do
94 -- Insert a first element
96 SQL : constant String :=
97 "insert into test (counter, tid) values ("
98 & Positive'Image (Current) & ", "
99 & Positive'Image (Task_Id) & ")";
101 DBH.Handle.Execute (SQL);
103 Current := Current + 1;
108 SQL : constant String :=
109 "insert into test (counter, tid) values ("
110 & Positive'Image (Current) & ", "
111 & Positive'Image (Task_Id) & ")";
113 DBH.Handle.Execute (SQL);
116 exit when Current = Max_Insert;
117 Current := Current + 1;
124 ("Inserts" & Positive'Image (Task_Id) & "; "
125 & Exception_Information (E));
133 DBH : constant TLS_DBH_Access := TLS_DBH_Access (DBH_TLS.Reference);
137 accept Start (Id : in Positive) do
140 Text_IO.Put_Line ("Reader " & Positive'Image (Task_Id) & " start");
147 Iter : DB.SQLite.Iterator;
148 Line : DB.String_Vectors.Vector;
150 DBH.Handle.Prepare_Select
151 (Iter, "select max(counter), tid from test");
154 Iter.Get_Line (Line);
156 if DB.String_Vectors.Element (Line, 1) /= "" then
158 (DB.String_Vectors.Element (Line, 1)) = Max_Insert
160 Text_IO.Put_Line ("Reader "
161 & Positive'Image (Task_Id)
162 & " Stop successfully");
168 if Verbose and then Natural'Value
169 (DB.String_Vectors.Element (Line, 1)) /= Last
171 Last := Natural'Value
172 (DB.String_Vectors.Element (Line, 1));
174 ("Reader " & Positive'Image (Task_Id) & " see "
175 & DB.String_Vectors.Element (Line, 1) & " write by "
176 & DB.String_Vectors.Element (Line, 2));
189 ("Selects" & Positive'Image (Task_Id) & "; "
190 & Exception_Information (E));
194 Morzhol.Logs.Set (Morzhol.Logs.Information, False);
195 Morzhol.Logs.Set (Morzhol.Logs.Warnings, False);
196 Morzhol.Logs.Set (Morzhol.Logs.Error, True); -- show errors
199 DBH : constant TLS_DBH_Access := TLS_DBH_Access (DBH_TLS.Reference);
203 ("create table test ('Counter
' boolean, 'tid
' integer)");
207 Writers : array (1 .. 2) of Inserts;
208 Readers : array (1 .. 10) of Selects;
212 for K in Readers'Range loop
213 Readers (K).Start (K);
218 for K in Writers'Range loop
219 Writers (K).Start (K);