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
;
36 Max_Insert
: constant Positive := 100;
37 Verbose
: constant Boolean := False;
40 entry Start
(Id
: in Positive);
44 entry Start
(Id
: in Positive);
47 type TLS_DBH
is record
48 Handle
: access DB
.Handle
'Class;
52 type TLS_DBH_Access
is access all TLS_DBH
;
54 Null_DBH
: constant TLS_DBH
:=
55 TLS_DBH
'(Handle => null, Connected => False);
57 DB_Path : constant String := "temp.db";
60 new Task_Attributes (Attribute => TLS_DBH, Initial_Value => Null_DBH);
62 procedure Connect (DBH : in TLS_DBH_Access);
63 -- Connects to the database if needed
69 procedure Connect (DBH : in TLS_DBH_Access) is
71 if not DBH.Connected then
72 DBH.Handle := new DB.SQLite.Handle;
73 DBH.Handle.Connect (DB_Path);
74 DBH.Connected := True;
75 DBH_TLS.Set_Value (DBH.all);
84 DBH : constant TLS_DBH_Access := TLS_DBH_Access (DBH_TLS.Reference);
86 Current : Positive := 1;
90 accept Start (Id : in Positive) do
93 -- Insert a first element
95 SQL : constant String :=
96 "insert into test (counter, tid) values ("
97 & Positive'Image (Current) & ", "
98 & Positive'Image (Task_Id) & ")";
100 DBH.Handle.Execute (SQL);
102 Current := Current + 1;
107 SQL : constant String :=
108 "insert into test (counter, tid) values ("
109 & Positive'Image (Current) & ", "
110 & Positive'Image (Task_Id) & ")";
112 DBH.Handle.Execute (SQL);
115 exit when Current = Max_Insert;
116 Current := Current + 1;
123 ("Inserts" & Positive'Image (Task_Id) & "; "
124 & Exception_Information (E));
132 DBH : constant TLS_DBH_Access := TLS_DBH_Access (DBH_TLS.Reference);
136 accept Start (Id : in Positive) do
139 Text_IO.Put_Line ("Reader " & Positive'Image (Task_Id) & " start");
146 Iter : DB.SQLite.Iterator;
147 Line : DB.String_Vectors.Vector;
149 DBH.Handle.Prepare_Select
150 (Iter, "select max(counter), tid from test");
153 Iter.Get_Line (Line);
155 if DB.String_Vectors.Element (Line, 1) /= "" then
157 (DB.String_Vectors.Element (Line, 1)) = Max_Insert
159 Text_IO.Put_Line ("Reader "
160 & Positive'Image (Task_Id)
161 & " Stop successfully");
167 if Verbose and then Natural'Value
168 (DB.String_Vectors.Element (Line, 1)) /= Last
170 Last := Natural'Value
171 (DB.String_Vectors.Element (Line, 1));
173 ("Reader " & Positive'Image (Task_Id) & " see "
174 & DB.String_Vectors.Element (Line, 1) & " write by "
175 & DB.String_Vectors.Element (Line, 2));
188 ("Selects" & Positive'Image (Task_Id) & "; "
189 & Exception_Information (E));
193 Morzhol.Logs.Set (Morzhol.Logs.Information, False);
194 Morzhol.Logs.Set (Morzhol.Logs.Warnings, False);
195 Morzhol.Logs.Set (Morzhol.Logs.Error, True); -- show errors
198 DBH : constant TLS_DBH_Access := TLS_DBH_Access (DBH_TLS.Reference);
200 if Ada.Directories.Exists (DB_Path) then
201 Ada.Directories.Delete_File (DB_Path);
205 ("create table test ('Counter
' boolean, 'tid
' integer)");
209 Writers : array (1 .. 2) of Inserts;
210 Readers : array (1 .. 10) of Selects;
214 for K in Readers'Range loop
215 Readers (K).Start (K);
220 for K in Writers'Range loop
221 Writers (K).Start (K);