1 ------------------------------------------------------------------------------
4 -- Copyright (C) 2006-2007 --
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 ------------------------------------------------------------------------------
22 with Ada
.Unchecked_Deallocation
;
26 package body DB
.SQLite
is
31 Module
: constant Logs
.Module_Name
:= "DB_SQLITE";
33 procedure Unchecked_Free
is new Unchecked_Deallocation
34 (Object
=> SQLite3
.Object
, Name
=> SQLite3
.Handle
);
36 procedure Check_Result
38 Result
: in SQLite3
.Return_Value
);
39 pragma Inline
(Check_Result
);
40 -- Check result, raises and exception if it is an error code
42 procedure Step_Internal
(Iter
: in out Iterator
);
43 -- Advance to the next row and set Iter.More
45 -----------------------
46 -- Begin_Transaction --
47 -----------------------
49 overriding
procedure Begin_Transaction
(DB
: in Handle
) is
51 Logs
.Write
(Module
, "begin");
52 Execute
(DB
, "begin");
53 end Begin_Transaction
;
59 procedure Check_Result
61 Result
: in SQLite3
.Return_Value
)
63 use type SQLite3
.Return_Value
;
65 if Result
/= SQLite3
.SQLITE_OK
then
70 ("Return_Value", SQLite3
.Return_Value
'Image (Result
))
71 & ", " & Logs
.NV
("routine", Routine
));
73 with "SQLite: Error " & SQLite3
.Return_Value
'Image (Result
) &
82 overriding
procedure Close
(DB
: in out Handle
) is
84 Logs
.Write
(Module
, "close");
85 Check_Result
("close", SQLite3
.Close
(DB
.H
));
86 Unchecked_Free
(DB
.H
);
93 overriding
procedure Commit
(DB
: in Handle
) is
95 Logs
.Write
(Module
, "commit");
96 Execute
(DB
, "commit");
103 overriding
procedure Connect
106 User
: in String := "";
107 Password
: in String := "")
109 pragma Unreferenced
(User
, Password
);
110 use type GNU
.DB
.SQLite3
.Handle
;
113 (Module
, "connect " & Logs
.NV
("Name", Name
));
115 DB
.H
:= new GNU
.DB
.SQLite3
.Object
;
117 Check_Result
("connect", SQLite3
.Open
(DB
.H
, Name
));
124 overriding
procedure End_Select
(Iter
: in out Iterator
) is
126 Logs
.Write
(Module
, "end_select");
127 Check_Result
("end_select", SQLite3
.finalize
(Iter
.S
'Unchecked_Access));
134 overriding
procedure Execute
(DB
: in Handle
; SQL
: in String) is
137 (Module
, "execute : " & Logs
.NV
("SQL", SQL
));
138 Check_Result
("execute", SQLite3
.Exec
(DB
.H
, SQL
));
141 raise DB_Error
with "DB_Error on Execute " & SQL
;
148 overriding
procedure Get_Line
149 (Iter
: in out Iterator
;
150 Result
: out String_Vectors
.Vector
)
152 use type SQLite3
.Return_Value
;
154 for K
in 0 .. Iter
.Col
- 1 loop
155 String_Vectors
.Append
156 (Result
, SQLite3
.column_text
(Iter
.S
'Unchecked_Access, K
));
159 Step_Internal
(Iter
);
162 -----------------------
163 -- Last_Insert_Rowid --
164 -----------------------
166 overriding
function Last_Insert_Rowid
(DB
: in Handle
) return String is
168 return SQLite3
.uint64
'Image (SQLite3
.Last_Insert_Rowid
(DB
.H
));
169 end Last_Insert_Rowid
;
175 overriding
function More
(Iter
: in Iterator
) return Boolean is
184 overriding
procedure Prepare_Select
186 Iter
: in out Standard
.DB
.Iterator
'Class;
189 use type SQLite3
.Statement_Reference
;
191 pragma Assert
(Iter
in Iterator
);
193 (Module
, "prepare select : " & Logs
.NV
("SQL", SQL
));
195 Iterator
(Iter
).H
:= DB
;
196 Iterator
(Iter
).More
:= False;
200 SQLite3
.prepare
(DB
.H
, SQL
, Iterator
(Iter
).S
'Unchecked_Access));
202 Iterator
(Iter
).Col
:=
203 SQLite3
.column_count
(Iterator
(Iter
).S
'Unchecked_Access);
205 Step_Internal
(Iterator
(Iter
));
212 overriding
procedure Rollback
(DB
: in Handle
) is
214 Logs
.Write
(Module
, "rollback");
215 Execute
(DB
, "rollback");
222 procedure Step_Internal
(Iter
: in out Iterator
) is
223 use type SQLite3
.Return_Value
;
224 R
: SQLite3
.Return_Value
;
226 R
:= SQLite3
.step
(Iter
.S
'Unchecked_Access);
228 if R
= SQLite3
.SQLITE_DONE
then
230 elsif R
= SQLite3
.SQLITE_ROW
then
233 Check_Result
("step_internal", R
);