summaryrefslogtreecommitdiff
path: root/src/sqlite3.adb
blob: d39fd6c4623ab4402bb4ef40374694fbd6f4a6d1 (plain)
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


with

    Interfaces.C.Strings;


package body SQLite3 is


    procedure Finalize
           (This : in out SQLite3_DB) is
    begin
        This.Close;
    end Finalize;


    procedure Finalize
           (This : in out SQLite3_Statement) is
    begin
        This.Finish;
    end Finalize;




    procedure Open
           (Handle   :    out SQLite3_DB;
            Filename : in     String)
    is
        function sqlite3_open
               (Filename  : in     Interfaces.C.char_array;
                DB_Handle : access DB_Private_Access)
            return Interfaces.C.int
        with Import => True, Convention => C;

        Code : Interfaces.C.int;
    begin
        Code := sqlite3_open (Interfaces.C.To_C (Filename), Handle.Ptr'Access);
        Handle.Prep := True;
        if Status_Code (Code) /= SQLITE_OK then
            raise Program_Error with Error_Message (Handle);
        end if;
    end Open;


    procedure Open
           (Handle   :    out SQLite3_DB;
            Filename : in     String;
            Flags    : in     Open_Flag)
    is
        function sqlite3_open_v2
               (Filename  : in     Interfaces.C.char_array;
                DB_Handle : access DB_Private_Access;
                Flags     : in     Interfaces.C.int;
                VFS       : in     Interfaces.C.char_array)
            return Interfaces.C.int
        with Import => True, Convention => C;

        Code : Interfaces.C.int;
    begin
        Code := sqlite3_open_v2
           (Interfaces.C.To_C (Filename),
            Handle.Ptr'Access,
            Interfaces.C.int (Flags),
            Interfaces.C.To_C (""));
        Handle.Prep := True;
        if Status_Code (Code) /= SQLITE_OK then
            raise Program_Error with Error_Message (Handle);
        end if;
    end Open;


    procedure Close
           (Handle : in out SQLite3_DB)
    is
        function sqlite3_close
               (DB_Handle : in DB_Private_Access)
            return Interfaces.C.int
        with Import => True, Convention => C;

        Code : Interfaces.C.int;
    begin
        if Handle.Prep = False then
            return;
        end if;
        Code := sqlite3_close (Handle.Ptr);
        Handle.Prep := False;
        if Status_Code (Code) /= SQLITE_OK then
            raise Program_Error with Error_Message (Handle);
        end if;
    end Close;




    procedure Prepare
           (Handle     : in     SQLite3_DB;
            SQL        : in     String;
            SQL_Handle :    out SQLite3_Statement'Class)
    is
        type Chars_Ptr_Ptr is access all Interfaces.C.Strings.chars_ptr;

        --  int sqlite3_prepare_v2(
        --    sqlite3 *db,            /* Database handle */
        --    const char *zSql,       /* SQL statement, UTF-8 encoded */
        --    int nByte,              /* Maximum length of zSql in bytes. */
        --    sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
        --    const char **pzTail     /* OUT: Pointer to unused portion of zSql */
        --  );
        function sqlite3_prepare_v2
               (DB_Handle : in     DB_Private_Access;
                zSql      : in     Interfaces.C.char_array;
                nByte     : in     Interfaces.C.int;
                ppStmt    : access Statement_Private_Access;
                pzTail    : in     Chars_Ptr_Ptr)
            return Interfaces.C.int
        with Import => True, Convention => C;

        SQL_Str : Interfaces.C.char_array := Interfaces.C.To_C (SQL);
        Code : Interfaces.C.int;
    begin
        Code := sqlite3_prepare_v2
           (DB_Handle => Handle.Ptr,
            zSql      => SQL_Str,
            nByte     => SQL_Str'Length,
            ppStmt    => SQL_Handle.Ptr'Access,
            pzTail    => null);
        SQL_Handle.Prep := True;
        SQL_Handle.Status := Status_Code (Code);
        if SQL_Handle.Status /= SQLITE_OK then
            raise Program_Error with Error_Message (Handle);
        end if;
    end Prepare;


    procedure Prepare
           (Handle     : in     SQLite3_DB;
            SQL        : in     String;
            Flags      : in     Prepare_Flag;
            SQL_Handle :    out SQLite3_Statement'Class)
    is
        type Chars_Ptr_Ptr is access all Interfaces.C.Strings.chars_ptr;

        --  int sqlite3_prepare_v3(
        --    sqlite3 *db,            /* Database handle */
        --    const char *zSql,       /* SQL statement, UTF-8 encoded */
        --    int nByte,              /* Maximum length of zSql in bytes. */
        --    unsigned int prepFlags  /* Zero or more SQLITE_PREPARE_ flags */
        --    sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
        --    const char **pzTail     /* OUT: Pointer to unused portion of zSql */
        --  );
        function sqlite3_prepare_v2
               (DB_Handle : in     DB_Private_Access;
                zSql      : in     Interfaces.C.char_array;
                nByte     : in     Interfaces.C.int;
                prepFlags : in     Interfaces.C.unsigned;
                ppStmt    : access Statement_Private_Access;
                pzTail    : in     Chars_Ptr_Ptr)
            return Interfaces.C.int
        with Import => True, Convention => C;

        SQL_Str : Interfaces.C.char_array := Interfaces.C.To_C (SQL);
        Code : Interfaces.C.int;
    begin
        Code := sqlite3_prepare_v2
           (DB_Handle => Handle.Ptr,
            zSql      => SQL_Str,
            nByte     => SQL_Str'Length,
            prepFlags => Interfaces.C.unsigned (Flags),
            ppStmt    => SQL_Handle.Ptr'Access,
            pzTail    => null);
        SQL_Handle.Prep := True;
        SQL_Handle.Status := Status_Code (Code);
        if SQL_Handle.Status /= SQLITE_OK then
            raise Program_Error with Error_Message (Handle);
        end if;
    end Prepare;


    procedure Step
           (SQL_Handle : in out SQLite3_Statement)
    is
        --  int sqlite3_step(sqlite3_stmt*);
        function sqlite3_step
               (Stmt : in Statement_Private_Access)
            return Interfaces.C.int
        with Import => True, Convention => C;

        Code : Interfaces.C.int;
    begin
        Code := sqlite3_step (SQL_Handle.Ptr);
        SQL_Handle.Status := Status_Code (Code);
        if SQL_Handle.Status /= SQLITE_OK and
            SQL_Handle.Status /= SQLITE_ROW and
            SQL_Handle.Status /= SQLITE_DONE
        then
            raise Program_Error with Error_String (SQL_Handle.Status);
        end if;
    end Step;


    function Status
           (SQL_Handle : in SQLite3_Statement)
        return Status_Code is
    begin
        return SQL_Handle.Status;
    end Status;


    procedure Finish
           (SQL_Handle : in out SQLite3_Statement)
    is
        --  int sqlite3_finalize(sqlite3_stmt*);
        function sqlite3_finalize
               (Stmt : in Statement_Private_Access)
            return Interfaces.C.int
        with Import => True, Convention => C;

        Code : Interfaces.C.int;
    begin
        if SQL_Handle.Prep = False then
            return;
        end if;
        Code := sqlite3_finalize (SQL_Handle.Ptr);
        SQL_Handle.Prep := False;
        SQL_Handle.Status := Status_Code (Code);
        if SQL_Handle.Status /= SQLITE_OK then
            raise Program_Error with Error_String (SQL_Handle.Status);
        end if;
    end Finish;


    procedure Reset
           (SQL_Handle : in out SQLite3_Statement)
    is
        --  int sqlite3_reset(sqlite3_stmt*);
        function sqlite3_reset
               (Stmt : in Statement_Private_Access)
            return Interfaces.C.int
        with Import => True, Convention => C;

        Code : Interfaces.C.int;
    begin
        Code := sqlite3_reset (SQL_Handle.Ptr);
        SQL_Handle.Status := Status_Code (Code);
        if SQL_Handle.Status /= SQLITE_OK then
            raise Program_Error with Error_String (SQL_Handle.Status);
        end if;
    end Reset;


    procedure Bind
           (SQL_Handle : in out SQLite3_Statement;
            Index      : in     SQL_Parameter_Index;
            Value      : in     Integer) is
    begin
        Bind (SQL_Handle, Index, Long_Integer (Value));
    end Bind;


    procedure Bind
           (SQL_Handle : in out SQLite3_Statement;
            Index      : in     SQL_Parameter_Index;
            Value      : in     Long_Integer)
    is
        --  int sqlite3_bind_int(sqlite3_stmt*, int, int);
        function sqlite3_bind_int
               (Stmt  : in Statement_Private_Access;
                Index : in Interfaces.C.int;
                Value : in Interfaces.C.int)
            return Interfaces.C.int
        with Import => True, Convention => C;

        Code : Interfaces.C.int;
    begin
        Code := sqlite3_bind_int
           (Stmt  => SQL_Handle.Ptr,
            Index => Interfaces.C.int (Index),
            Value => Interfaces.C.int (Value));
        SQL_Handle.Status := Status_Code (Code);
        if SQL_Handle.Status /= SQLITE_OK then
            raise Program_Error with Error_String (SQL_Handle.Status);
        end if;
    end Bind;


    procedure Bind
           (SQL_Handle : in out SQLite3_Statement;
            Index      : in     SQL_Parameter_Index;
            Value      : in     Ada.Strings.Unbounded.Unbounded_String)
    is
        use Ada.Strings.Unbounded;

        --  int sqlite3_bind_text
        --       (sqlite3_stmt*,
        --        int,
        --        const char*,
        --        int,
        --        void(*)(void*));
        function sqlite3_bind_text
               (Stmt  : in Statement_Private_Access;
                Index : in Interfaces.C.int;
                Value : in Interfaces.C.char_array;
                Bytes : in Interfaces.C.int;
                Opt   : in Interfaces.C.long)
            return Interfaces.C.int
        with Import => True, Convention => C;

        Code : Interfaces.C.int;
    begin
        Code := sqlite3_bind_text
           (Stmt  => SQL_Handle.Ptr,
            Index => Interfaces.C.int (Index),
            Value => Interfaces.C.To_C (To_String (Value)),
            Bytes => Interfaces.C.int (Length (Value)),
            Opt   => SQLITE_TRANSIENT);
        SQL_Handle.Status := Status_Code (Code);
        if SQL_Handle.Status /= SQLITE_OK then
            raise Program_Error with Error_String (SQL_Handle.Status);
        end if;
    end Bind;


    function Column
           (SQL_Handle : in SQLite3_Statement;
            Index      : in SQL_Column_Index)
        return Integer
    is
        --  int sqlite3_column_int(sqlite3_stmt*, int iCol);
        function sqlite3_column_int
               (Stmt  : in Statement_Private_Access;
                Index : in Interfaces.C.int)
            return Interfaces.C.int
        with Import => True, Convention => C;

        Ret_Val : Interfaces.C.int;
    begin
        Ret_Val := sqlite3_column_int
           (Stmt => SQL_Handle.Ptr,
            Index => Interfaces.C.int (Index));
        return Integer (Ret_Val);
    end Column;


    function Column
           (SQL_Handle : in SQLite3_Statement;
            Index      : in SQL_Column_Index)
        return Ada.Strings.Unbounded.Unbounded_String
    is
        use Ada.Strings.Unbounded;
        use Interfaces;
        use type Interfaces.C.Strings.chars_ptr;

        --  const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol);
        function sqlite3_column_text
               (Stmt  : in Statement_Private_Access;
                Index : in Interfaces.C.int)
            return Interfaces.C.Strings.chars_ptr
        with Import => True, Convention => C;

        Ret_Val : Interfaces.C.Strings.chars_ptr;
    begin
        Ret_Val := sqlite3_column_text
           (Stmt  => SQL_Handle.Ptr,
            Index => Interfaces.C.int (Index));
        if Ret_Val = C.Strings.Null_Ptr then
            return Null_Unbounded_String;
        else
            return To_Unbounded_String (C.To_Ada (C.Strings.Value (Ret_Val)));
        end if;
    end Column;




    function Error_Message
           (Handle : in SQLite3_DB)
        return String
    is
        use Interfaces;

        --  const char *sqlite3_errmsg(sqlite3*);
        function sqlite3_errmsg
               (DB_Handle : in DB_Private_Access)
            return C.Strings.chars_ptr
        with Import => True, Convention => C;
    begin
        return C.To_Ada (C.Strings.Value (sqlite3_errmsg (Handle.Ptr)));
    end Error_Message;


    function Error_String
           (Status : in Status_Code)
        return String
    is
        use Interfaces;

        function sqlite3_errstr
               (Code : in Interfaces.C.int)
            return C.Strings.chars_ptr
        with Import => True, Convention => C;
    begin
        return C.To_Ada (C.Strings.Value (sqlite3_errstr (Interfaces.C.int (Status))));
    end Error_String;


end SQLite3;