1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
|
with Ada.Strings.Unbounded;
with Ada.Unchecked_Deallocation;
with Ada.Text_IO;
with Interfaces.C;
with Interfaces.C.Strings;
with Interfaces.C.Pointers;
with System;
package body GNU.DB.SQLite is
pragma Linker_Options("-lsqlite");
package ASU renames Ada.Strings.Unbounded;
package C renames Interfaces.C;
package CS renames Interfaces.C.Strings;
-------------------
-- local support --
-------------------
type Pchars_ptr is access all CS.chars_ptr;
-- equivalent of access string
-- Most of the sqlite functions return an string that contains the error
-- message when an error is occured. Sqlite uses "char**" for errmsg, which
-- can be simulated by "access all CS.chars_ptr" in Ada95.
procedure Free is
new Ada.Unchecked_Deallocation(CS.chars_ptr, Pchars_ptr);
procedure Free_Table
is new Ada.Unchecked_Deallocation(Table_Array, Table_Reference);
subtype chars_ptr is CS.chars_ptr;
-- equivalent of string
type chars_ptr_array is array(C.size_t range <>) of aliased chars_ptr;
-- same as char**
package Pstring is
new Interfaces.C.Pointers(C.size_t,
chars_ptr,
chars_ptr_array,
CS.Null_Ptr);
subtype Pstring_array is Pstring.Pointer;
type exec_sqlite_callback is access function
(pArg : System.Address;
argc : C.int;
argv : Pstring_array;
columnNames : Pstring_array)
return C.int;
-- sqlite.h:91
-- typedef int (*sqlite_callback)(void*,int,char**, char**);
function sqlite_exec
(db : DB_Access;
sql : C.char_array;
cb : exec_sqlite_callback;
arg : System.Address;
errmsg : Pchars_ptr)
return C.int;
pragma Import(C, sqlite_exec, "sqlite_exec");
-- sqlite.h:133
-- int sqlite_exec(
-- sqlite*, /* An open database */
-- const char *sql, /* SQL to be executed */
-- sqlite_callback, /* Callback function */
-- void *, /* 1st argument to callback function */
-- char **errmsg /* Error msg written here */
-- );
procedure check_return_code
(code : in integer)
is
begin
case code is
when 0 => null;
when 1 => raise SQLITE_ERROR;
when 2 => raise SQLITE_INTERNAL;
when 3 => raise SQLITE_PERM;
when 4 => raise SQLITE_ABORT;
when 5 => raise SQLITE_BUSY;
when 6 => raise SQLITE_LOCKED;
when 7 => raise SQLITE_NOMEM;
when 8 => raise SQLITE_READONLY;
when 9 => raise SQLITE_INTERRUPT;
when 10 => raise SQLITE_IOERR;
when 11 => raise SQLITE_CORRUPT;
when 12 => raise SQLITE_NOTFOUND;
when 13 => raise SQLITE_FULL;
when 14 => raise SQLITE_CANTOPEN;
when 15 => raise SQLITE_PROTOCOL;
when 16 => raise SQLITE_EMPTY;
when 17 => raise SQLITE_SCHEMA;
when 18 => raise SQLITE_TOOBIG;
when 19 => raise SQLITE_CONSTRAINT;
when 20 => raise SQLITE_MISMATCH;
when 21 => raise SQLITE_MISUSE;
when 22 => raise SQLITE_NOLFS;
when 23 => raise SQLITE_AUTH;
when 24 => raise SQLITE_FORMAT;
when others => raise UNDEFINED_ERROR;
end case;
end check_return_code;
-- hardcoded in the source of sqlite
procedure exec_real
(db : in out Object;
sql : in string;
proxy : in exec_sqlite_callback;
err : out Ustring)
is
tmperr : Pchars_ptr := new CS.chars_ptr;
ret : integer;
begin
tmperr.all := CS.Null_Ptr;
-- use the proxy here
ret := integer(sqlite_exec(db.db,
C.To_C(sql),
proxy,
System.Null_Address,
tmperr));
if (CS."/="(tmperr.all, CS.Null_Ptr)) then
err := ASU.To_Unbounded_String(CS.Value(tmperr.all));
end if;
Free(tmperr);
check_return_code(ret);
end;
-----------
-- close --
-----------
procedure close
(db : in out Object)
is
procedure sqlite_close
(db : in out DB_Access);
pragma Import(C, sqlite_close, "sqlite_close");
-- sqlite.h:86
-- void sqlite_close(sqlite *);
begin
if not db.open then
raise DB_NOT_OPEN;
end if;
sqlite_close(db.db);
db.db := null;
db.open := FALSE;
end close;
----------
-- exec --
----------
procedure exec
(db : in out Object;
sql : in string;
errmsg : out Ustring)
is
use C;
function nothing
(pArg : System.Address;
argc : C.int;
argv : Pstring_array;
columnNames : Pstring_array)
return C.int
is
begin
return 0;
end nothing;
-- create a proxy callback that converts all C data
-- to Ada data and pass it on to the callback that the user provided
begin
if not db.open then
raise DB_NOT_OPEN;
end if;
exec_real(db, sql, nothing'Unrestricted_Access, errmsg);
end exec;
------------------------
-- exec_with_callback --
------------------------
procedure exec_with_callback
(db : in out Object;
sql : in string;
cb : in Exec_Callback;
errmsg : out Ustring)
is
use C;
function proxy
(pArg : System.Address;
argc : C.int;
argv : Pstring_array;
columnNames : Pstring_array)
return C.int
is
begin
if (argc <= 0) then
return 0;
end if;
declare
i : C.size_t := 0;
argv2 : Row_Array(0 .. integer(argc) - 1 );
columnNames2 : Row_Array(0 .. integer(argc) - 1 );
begin
for i in 0 .. C.size_t(argc) - 1 loop
argv2(integer(i)) := ASU.To_Unbounded_String(CS.Value(Pstring.Value(argv)(i)));
columnNames2(integer(i)) := ASU.To_Unbounded_String(CS.Value(Pstring.Value(columnNames)(i)));
end loop;
return C.int(cb(integer(argc), argv2, columnNames2));
end;
end proxy;
-- create a proxy callback that converts all C data
-- to Ada data and pass it on to the callback that the user provided
begin
if not db.open then
raise DB_NOT_OPEN;
end if;
exec_real(db, sql, proxy'Unrestricted_Access, errmsg);
end exec_with_callback;
----------------------------------
-- exec_with_callback_with_data --
----------------------------------
procedure exec_with_callback_with_data
(db : in out Object;
sql : in string;
cb : in Exec_Callback_with_data;
arg : in out Data_Type;
errmsg : out Ustring)
is
use C;
function proxy
(pArg : System.Address;
argc : C.int;
argv : Pstring_array;
columnNames : Pstring_array)
return C.int
is
begin
if (argc <= 0) then
return 0;
end if;
declare
i : C.size_t := 0;
argv2 : Row_Array(0 .. integer(argc) - 1 );
columnNames2 : Row_Array(0 .. integer(argc) - 1 );
begin
for i in 0 .. C.size_t(argc) - 1 loop
argv2(integer(i)) := ASU.To_Unbounded_String(CS.Value(Pstring.Value(argv)(i)));
columnNames2(integer(i)) := ASU.To_Unbounded_String(CS.Value(Pstring.Value(columnNames)(i)));
end loop;
return C.int(cb(arg, integer(argc), argv2, columnNames2));
end;
end proxy;
-- create a proxy callback that converts all C data
-- to Ada data and pass it on to the callback that the user provided
begin
if not db.open then
raise DB_NOT_OPEN;
end if;
exec_real(db, sql, proxy'Unrestricted_Access, errmsg);
end exec_with_callback_with_data;
--------------
-- Finalize --
--------------
procedure Finalize
(Self : in out Table_Controlled)
is
procedure Free_Row
is new Ada.Unchecked_Deallocation(Row_Array, Row_Reference);
begin
if Self.Table /= null then
for I in Self.Table'Range loop
Free_Row(Self.Table(I));
end loop;
Free_Table(Self.table);
end if;
end Finalize;
---------------
-- get_table --
---------------
procedure get_table
(db : in out Object;
sql : in string;
table : out Table_Controlled;
errmsg : out Ustring)
is
type Node;
type PNode is access all Node;
type Node is record
next : PNode := null;
info : Row_Reference;
end record;
-- type Node declaration
procedure Free
is new Ada.Unchecked_Deallocation(Node, PNode);
Include_Column_Names : boolean := TRUE;
Current : PNode := new Node;
Head : PNode := Current;
Count_Columns : integer := 0;
Count_Rows : integer := 0;
function callback
(argc : integer;
argv : Row_Array;
columnNames : Row_Array)
return integer
is
previous : PNode;
begin
if Include_Column_Names then
Count_Rows := 1;
Count_Columns := columnNames'Last;
-- copy columnNames to Node.info
Current.info := new Row_Array(columnNames'Range);
Current.info.all := columnNames;
Include_Column_Names := FALSE;
end if;
Count_Rows := Count_Rows + 1;
-- insert new Node after the last one
previous := Current;
Current := new Node;
previous.next := Current;
Current.info := new Row_Array(argv'Range);
Current.info.all := argv;
return 0;
end callback;
begin
Exec_with_callback(db,
sql,
callback'Unrestricted_Access,
errmsg);
declare
tmp_table : Table_Reference := new Table_Array(1 .. Count_Rows);
previous : PNode;
begin
Current := Head;
for I in tmp_table'Range loop
tmp_table(I) := Current.info;
-- copy row
-- go to next Node
previous := Current;
Current := Current.next;
if I = tmp_table'Last and Current /= null then
Ada.Text_IO.Put_Line("error with count");
end if;
Free(previous);
-- Free previous Node
end loop;
table.table := tmp_table;
end;
end get_table;
----------------
-- Initialize --
----------------
procedure Initialize
(Self : in out Table_Controlled)
is
begin
Self.Table := null;
end Initialize;
----------
-- open --
----------
procedure open
(db : in out Object;
filename : in string;
mode : in File_Mode_Type := READ_WRITE;
errmsg : out Ustring)
is
function sqlite_open
(filename : C.char_array;
mode : C.int;
errmsg : Pchars_ptr)
return DB_Access;
pragma Import(C, sqlite_open, "sqlite_open");
-- sqlite.h:78
-- sqlite *sqlite_open(const char *filename, int mode, char **errmsg);
begin
if db.open then
raise DB_ALREADY_OPEN;
end if;
declare
tmperr : Pchars_ptr := new CS.chars_ptr;
begin
tmperr.all := CS.Null_Ptr;
-- file mode is not implemented in current implementation
-- of sqlite
db.db := sqlite_open(C.To_C(filename), 0, tmperr);
if (CS."/="(tmperr.all, CS.Null_Ptr)) then
errmsg := ASU.To_Unbounded_String(CS.Value(tmperr.all));
else
db.open := TRUE;
end if;
Free(tmperr);
end;
end open;
end GNU.DB.SQLite;
|