agpl_1.0.0_b5da3320/src/agpl-db/gnu-db-sqlite3.ads

  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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
-------------------------------------------------------------------------------
--									                                                  --
--  Filename        : $Source: /cvsroot/gnade/gnade/dbi/sqlite3/gnu-db-sqlite3.ads,v $
--  Description     : SQLite Base Package                                    --
--  Author          : Ching Bon Lam                                          --
--  Created         : 26.7.2003                                              --
--  Last Modified By: $Author: merdmann $				                             --
--  Last Modified On: $Date: 2004/12/28 21:31:38 $		                       --
--  Status          : $State: Exp $					                             --
--									                                                  --
--  Copyright (C) 2003 Ching Bon Lam                                         --
--                                                                           --
--  GNADE is free software;  you can redistribute it  and/or modify it under --
--  terms of the  GNU General Public License as published  by the Free Soft- --
--  ware  Foundation;  either version 2,  or (at your option) any later ver- --
--  sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
--  OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
--  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
--  for  more details.  You should have  received  a copy of the GNU General --
--  Public License  distributed with GNAT;  see file COPYING.  If not, write --
--  to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
--  MA 02111-1307, USA.                                                      --
--                                                                           --
--  As a special exception,  if other files  instantiate  generics from this --
--  unit, or you link  this unit with other files  to produce an executable, --
--  this  unit  does not  by itself cause  the resulting  executable  to  be --
--  covered  by the  GNU  General  Public  License.  This exception does not --
--  however invalidate  any other reasons why  the executable file  might be --
--  covered by the  GNU Public License.                                      --
--                                                                           --
--  Functional Description                                                   --
--  ======================                                                   --
--  Thick binding to sqlite (http://www.sqlite.org).                         --
--                                                                           --
--  Restrictions                                                             --
--  ============                                                             --
--  None                                                                     --
--                                                                           --
--  References                                                               --
--  ==========                                                               --
--  Sqlite homepage : http://www.sqlite.org                                  --
--                                                                           --
--  Contact                                                                  --
--  =======                                                                  --
--  Error reports shall be handled via http://gnade.sourceforge.net          --
--  Features and ideas via: gnade-develop@lists.sourceforge.net              --
--                                                                           --
--  Author contact:                                                          --
--    Ching Bon Lam  <cblam@gmx.net>                                         --
--									                                                  --
-------------------------------------------------------------------------------

with Ada.Finalization;
with Ada.Containers.Indefinite_Vectors;
with Ada.Containers.Vectors;
with Interfaces.C;

package GNU.DB.SQLite3 is

   type Object is private;
   type Handle is access all Object;
   -- sqlite3.h:47
   -- typedef struct sqlite3 sqlite3;

   ----------------------------------------------------------------------------
   -- return values -----------------------------------------------------------
   ----------------------------------------------------------------------------

   type Return_Value is (
   -- sqlite3.h:133-161
      SQLITE_OK         , -- Successful result
      SQLITE_ERROR      , -- SQL error or missing database
      SQLITE_INTERNAL   , -- An internal logic error in SQLite
      SQLITE_PERM       , -- Access permission denied
      SQLITE_ABORT      , -- Callback routine requested an abort
      SQLITE_BUSY       , -- The database file is locked
      SQLITE_LOCKED     , -- A table in the database is locked
      SQLITE_NOMEM      , -- A malloc() failed
      SQLITE_READONLY   , -- Attempt to write a readonly database
      SQLITE_INTERRUPT  , -- Operation terminated by sqlite3_interrupt()
      SQLITE_IOERR      , -- Some kind of disk I/O error occurred
      SQLITE_CORRUPT    , -- The database disk image is malformed
      SQLITE_NOTFOUND   , -- (Internal Only) Table or record not found
      SQLITE_FULL       , -- Insertion failed because database is full
      SQLITE_CANTOPEN   , -- Unable to open the database file
      SQLITE_PROTOCOL   , -- Database lock protocol error
      SQLITE_EMPTY      , -- Database is empty
      SQLITE_SCHEMA     , -- The database schema changed
      SQLITE_TOOBIG     , -- Too much data for one row of a table
      SQLITE_CONSTRAINT , -- Abort due to contraint violation
      SQLITE_MISMATCH   , -- Data type mismatch
      SQLITE_MISUSE     , -- Library used incorrectly
      SQLITE_NOLFS      , -- Uses OS features not supported on host
      SQLITE_AUTH       , -- Authorization denied
      SQLITE_FORMAT     , -- Auxiliary database format error
      SQLITE_RANGE      , -- 2nd parameter to sqlite3_bind out of range
      SQLITE_NOTADB     , -- File opened that is not a database file
      SQLITE_ROW        , -- sqlite3_step() has another row ready
      SQLITE_DONE       , -- sqlite3_step() has finished executing
      UNDEFINED_ERROR
   );

   ----------------------------------------------------------------------------
   -- Other important types ---------------------------------------------------
   ----------------------------------------------------------------------------

   type Statement is private;
   type Statement_Reference is access all Statement;
   -- sqlite3.h:554
   -- typedef struct sqlite3_stmt sqlite3_stmt;

   type uint64 is mod 2 ** 64;
   type int64 is
      range -(2 ** (64 - 1)) ..
            +(2 ** (64 - 1) - 1);
   -- sqlite3.h:55

   package String_Vectors
      is new Ada.Containers.Indefinite_Vectors(natural,
                                               string);

   package Wide_String_Vectors
      is new Ada.Containers.Indefinite_Vectors(natural,
                                               wide_string);

   type Callback is access function
     (argc        : integer;
      argv        : String_Vectors.Vector;
      columnNames : String_Vectors.Vector)
      return Return_Value;

   type Callback16 is access function
     (argc        : integer;
      argv        : Wide_String_Vectors.Vector;
      columnNames : Wide_String_Vectors.Vector)
      return Return_Value;

   package String_Tables
      is new Ada.Containers.Vectors(natural,
                                    String_Vectors.Vector,
                                    String_Vectors."=");

   package Wide_String_Tables
      is new Ada.Containers.Vectors(natural,
                                    Wide_String_Vectors.Vector,
                                    Wide_String_Vectors."=");

   type Table_Reference is access all String_Tables.Vector;
   type Table16_Reference is access all Wide_String_Tables.Vector;

   ----------------------------------------------------------------------------
   -- Basic operations --------------------------------------------------------
   ----------------------------------------------------------------------------

   function Close
     (Self : Handle)
      return Return_Value;
   -- sqlite3.h:75
   -- int sqlite3_close(sqlite3 *);

   function Last_Insert_Rowid
     (Self : Handle)
      return uint64;
   -- sqlite3.h:172
   -- sqlite_int64 sqlite3_last_insert_rowid(sqlite3*);

   function Changes
     (Self : Handle)
      return integer;
   -- sqlite3.h:194
   -- int sqlite3_changes(sqlite3*);

   function Total_Changes
     (Self : Handle)
      return integer;
   -- sqlite3.h:212
   -- int sqlite3_total_changes(sqlite3*);

   procedure Interrupt
     (Self : Handle);
   -- sqlite3.h:220
   -- void sqlite3_interrupt(sqlite3*);

   function Complete
     (sql : string)
      return boolean;
   -- sqlite3.h:233
   -- int sqlite3_complete(const char *sql);

   function Complete16
     (sql : wide_string)
      return boolean;
   -- sqlite3.h:234
   -- int sqlite3_complete16(const void *sql);

   function Open
     (Self     : Handle;
      Filename : string)
      return Return_Value;
   -- sqlite3.h:504
   -- int sqlite3_open(
   --   const char *filename,   /* Database filename (UTF-8) */
   --   sqlite3 **ppDb          /* OUT: SQLite db handle */
   -- );

   function open16
     (Self     : Handle;
      Filename : wide_string)
      return Return_Value;
   -- sqlite3.h:508
   -- int sqlite3_open16(
   --   const void *filename,   /* Database filename (UTF-16) */
   --   sqlite3 **ppDb          /* OUT: SQLite db handle */
   -- );

   ----------------------------------------------------------------------------
   -- Exec --------------------------------------------------------------------
   ----------------------------------------------------------------------------

   generic
      type Data_Type is private;
      type Data_Handle is access all Data_Type;
   package Generic_Exec is

      type Callback is access function
        (pArg        : Data_Handle;
         argc        : integer;
         argv        : String_Vectors.Vector;
         columnNames : String_Vectors.Vector)
         return Return_Value;
      -- sqlite3.h:80
      -- typedef int (*sqlite3_callback)(void*,int,char**, char**);

      function Exec
        (Self    : Handle;
         sql     : string;
         cb      : Callback;
         arg     : Data_Handle)
         return Return_Value;
      -- sqlite3.h:122
      -- int sqlite3_exec(
      --   sqlite3*,         /* An open database */
      --   const char *sql,  /* SQL to be executed */
      --   sqlite3_callback, /* Callback function */
      --   void *,           /* 1st argument to callback function */
      --   char **errmsg     /* Error msg written here */
      -- );
   end Generic_Exec;

   generic
      type Data_Type is private;
      type Data_Handle is access all Data_Type;
   package Generic_Exec16 is

      type Callback16 is access function
        (pArg        : Data_Handle;
         argc        : integer;
         argv        : Wide_String_Vectors.Vector;
         columnNames : Wide_String_Vectors.Vector)
         return Return_Value;

      function Exec16
        (Self    : Handle;
         sql     : wide_string;
         cb      : Callback16;
         arg     : Data_Handle)
         return Return_Value;
   end Generic_Exec16;

   function Exec
     (Self : Handle;
      sql  : string;
      cb   : Callback)
      return Return_Value;

   function Exec16
     (Self : Handle;
      sql  : wide_string;
      cb   : Callback16)
      return Return_Value;

   function Exec
     (Self : Handle;
      sql  : string)
      return Return_Value;

   function Exec16
     (Self : Handle;
      sql  : wide_string)
      return Return_Value;

   ----------------------------------------------------------------------------
   -- Tables ------------------------------------------------------------------
   ----------------------------------------------------------------------------

   function Get_Table
     (Self  : Handle;
      Sql   : string;
      table : Table_Reference)
      return Return_Value;

   function Get_Table16
     (Self  : Handle;
      Sql   : wide_string;
      table : Table16_Reference)
      return Return_Value;

   function Row_Count
     (Self : Table_Reference)
      return natural;

   function Column_Count
     (Self : Table_Reference;
      Row  : natural)
      return natural;

   function Element
     (Self : Table_Reference;
      row  : natural;
      col  : natural)
      return string;

   function Element16
     (Self : Table16_Reference;
      row  : natural;
      col  : natural)
      return wide_string;


   ----------------------------------------------------------------------------
   -- Error stuff -------------------------------------------------------------
   ----------------------------------------------------------------------------

   function Errcode
     (Self : Handle)
      return integer;
   -- sqlite3.h:528
   -- int sqlite3_errcode(sqlite3 *db);

   function Errmsg
     (Self : Handle)
      return string;
   -- sqlite3.h:538
   -- const char *sqlite3_errmsg(sqlite3*);

   function Errmsg16
     (Self : Handle)
      return wide_string;
   -- sqlite3.h:548
   -- const void *sqlite3_errmsg16(sqlite3*);

   ----------------------------------------------------------------------------
   -- Stepping ----------------------------------------------------------------
   ----------------------------------------------------------------------------

   SQLITE_INTEGER : constant integer := 1;
   SQLITE_FLOAT   : constant integer := 2;
   SQLITE_TEXT    : constant integer := 3;
   SQLITE_BLOB    : constant integer := 4;
   SQLITE_NULL    : constant integer := 5;

   function prepare
     (self : Handle;
      sql  : string;
      stmt : Statement_Reference)
      return Return_Value;
   -- sqlite3.h:581
   -- int sqlite3_prepare(
   --   sqlite3 *db,            /* Database handle */
   --   const char *zSql,       /* SQL statement, UTF-8 encoded */
   --   int nBytes,             /* Length of zSql in bytes. */
   --   sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
   --   const char **pzTail     /* OUT: Pointer to unused portion of zSql */
   -- );

   function prepare16
     (self : Handle;
      sql  : wide_string;
      stmt : Statement_Reference)
      return Return_Value;
   -- sqlite3.h:588
   -- int sqlite3_prepare16(
   --   sqlite3 *db,            /* Database handle */
   --   const void *zSql,       /* SQL statement, UTF-16 encoded */
   --   int nBytes,             /* Length of zSql in bytes. */
   --   sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
   --   const void **pzTail     /* OUT: Pointer to unused portion of zSql */
   -- );


   -- function bind_blob
   --   (stmt : Statement_Reference;
   --    N    : integer;
   --    B    : something)
   --    return Return_Value;
   -- sqlite3.h:626
   -- int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*));

   function bind_double
     (stmt : Statement_Reference;
      N    : integer;
      D    : long_float)
      return Return_Value;
   -- sqlite3.h:627
   -- int sqlite3_bind_double(sqlite3_stmt*, int, double);

   function bind_int
     (stmt : Statement_Reference;
      N    : integer;
      I    : integer)
      return Return_Value;
   -- sqlite3.h:628
   -- int sqlite3_bind_int(sqlite3_stmt*, int, int);

   function bind_int64
     (stmt : Statement_Reference;
      N    : integer;
      I    : int64)
      return Return_Value;
   -- sqlite3.h:629
   -- int sqlite3_bind_int64(sqlite3_stmt*, int, sqlite_int64);

   function bind_null
     (stmt : Statement_Reference;
      N    : integer)
      return Return_Value;
   -- sqlite3.h:630
   -- int sqlite3_bind_null(sqlite3_stmt*, int);

   function bind_text
     (stmt : Statement_Reference;
      N    : integer;
      T    : string)
      return Return_Value;
   -- sqlite3.h:631
   -- int sqlite3_bind_text(sqlite3_stmt*, int, const char*, int n, void(*)(void*));

   function bind_text16
     (stmt : Statement_Reference;
      N    : integer;
      T    : wide_string)
      return Return_Value;
   -- sqlite3.h:632
   -- int sqlite3_bind_text16(sqlite3_stmt*, int, const void*, int, void(*)(void*));

   -- function bind_value
   --   (stmt : Statement_Reference;
   --    N    : integer;
   --    V    : Value)
   --    return Return_Value;
   -- sqlite3.h:633
   -- int sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*);

   function column_count
     (stmt : Statement_Reference)
      return integer;
   -- sqlite3.h:661
   -- int sqlite3_column_count(sqlite3_stmt *pStmt);

   function column_name
     (stmt : Statement_Reference;
      iCol : integer)
      return string;
   -- sqlite3.h:669
   -- const char *sqlite3_column_name(sqlite3_stmt*,int);

   function column_name16
     (stmt : Statement_Reference;
      iCol : integer)
      return wide_string;
   -- sqlite3.h:670
   -- const void *sqlite3_column_name16(sqlite3_stmt*,int);

   function column_decltype
     (stmt : Statement_Reference;
      iCol : integer)
      return string;
   -- sqlite3.h:690
   -- const char *sqlite3_column_decltype(sqlite3_stmt *, int i);

   function column_decltype16
     (stmt : Statement_Reference;
      iCol : integer)
      return wide_string;
   -- sqlite3.h:710
   -- const void *sqlite3_column_decltype16(sqlite3_stmt*,int);

   function step
     (stmt : Statement_Reference)
      return Return_Value;
   -- sqlite3.h:744
   -- int sqlite3_step(sqlite3_stmt*);

   function data_count
     (stmt : Statement_Reference)
      return integer;
   -- sqlite3.h:755
   -- int sqlite3_data_count(sqlite3_stmt *pStmt);

   function column_int
     (stmt : Statement_Reference;
      iCol : integer)
      return integer;
   -- sqlite3.h:828
   -- int sqlite3_column_int(sqlite3_stmt*, int iCol);

   function column_text
     (stmt : Statement_Reference;
      iCol : integer)
      return string;
   -- sqlite3.h:830
   --const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol);

   function column_text16
     (stmt : Statement_Reference;
      iCol : integer)
      return wide_string;
   -- sqlite3.h:831
   --const void *sqlite3_column_text16(sqlite3_stmt*, int iCol);

   function column_type
     (stmt : Statement_Reference;
      iCol : integer)
      return integer;
   -- sqlite3.h:832
   -- int sqlite3_column_type(sqlite3_stmt*, int iCol);

   function finalize
     (stmt : Statement_Reference)
      return Return_Value;
   -- sqlite3.h:848
   -- int sqlite3_finalize(sqlite3_stmt *pStmt);

   function reset
     (stmt : Statement_Reference)
      return Return_Value;
   -- sqlite3.h:857
   -- int sqlite3_reset(sqlite3_stmt *pStmt);

   ----------------------------------------------------------------------------
   -- PRIVATE -----------------------------------------------------------------
   ----------------------------------------------------------------------------

   private

   type DB_Type is limited null record;
   type DB_Access is access all DB_Type;
   for DB_Access'Storage_Size use 0;
   pragma Convention (C, DB_Access);

   type Object is record
      db : aliased DB_Access := null;
   end record;
   -- sqlite3.h:47
   -- typedef struct sqlite3 sqlite3;

   type Stmt_Type is limited null record;
   type Stmt_Access is access all Stmt_Type;
   for Stmt_Access'Storage_Size use 0;
   pragma Convention (C, Stmt_Access);

   type Statement is record
      stmt : aliased Stmt_Access := null;
   end record;

end GNU.DB.SQLite3;