summaryrefslogtreecommitdiffstats
path: root/ml/dlib/dlib/sqlite/sqlite.h
blob: 7eefbb21a69aade08d1ebc3e5ae9b22b3b2d9782 (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
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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
// Copyright (C) 2011  Davis E. King (davis@dlib.net)
// License: Boost Software License   See LICENSE.txt for the full license.
#ifndef DLIB_SQLiTE_H_
#define DLIB_SQLiTE_H_

#include "sqlite_abstract.h"

#include <iostream>
#include <limits>
#include <memory>
#include <vector>

#include "../algs.h"
#include <sqlite3.h>
#include "../serialize.h"


// --------------------------------------------------------------------------------------------

namespace dlib
{

// --------------------------------------------------------------------------------------------

    struct sqlite_error : public error
    {
        sqlite_error(const std::string& message): error(message) {}
    };

// --------------------------------------------------------------------------------------------

    namespace impl
    {
        struct db_deleter
        {
            void operator()(
                sqlite3* db
            )const 
            { 
                sqlite3_close(db);
            }
        };
    }

// --------------------------------------------------------------------------------------------

    class database : noncopyable
    {
    public:
        database(
        ) 
        {
        }

        database (
            const std::string& file
        ) 
        {
            open(file);
        }

        bool is_open (
        ) const
        {
            return db.get() != 0;
        }

        void open (
            const std::string& file
        )
        {
            filename = file;
            sqlite3* ptr = 0;
            int status = sqlite3_open(file.c_str(), &ptr);
            db.reset(ptr, impl::db_deleter());
            if (status != SQLITE_OK)
            {
                throw sqlite_error(sqlite3_errmsg(db.get()));
            }
        }

        const std::string& get_database_filename (
        ) const
        {
            // make sure requires clause is not broken
            DLIB_ASSERT(is_open() == true,
                "\t std::string database::get_database_filename()"
                << "\n\t The database must be opened before calling this routine."
                << "\n\t this: " << this
                );

            return filename;
        }

        inline void exec (
            const std::string& sql_statement
        );

        int64 last_insert_rowid (
        ) const
        {
            return sqlite3_last_insert_rowid(db.get());
        }

    private:

        friend class statement;

        std::string filename;
        std::shared_ptr<sqlite3> db;
    };

// --------------------------------------------------------------------------------------------

    class statement : noncopyable
    {
    public:
        statement (
            database& db_,
            const std::string sql_statement
        ) : 
            needs_reset(false),
            step_status(SQLITE_DONE),
            at_first_step(true),
            db(db_.db),
            stmt(0),
            sql_string(sql_statement)
        {
            // make sure requires clause is not broken
            DLIB_ASSERT(db_.is_open() == true,
                        "\t statement::statement()"
                        << "\n\t The database must be opened before calling this routine."
                        << "\n\t this: " << this
            );

            int status = sqlite3_prepare_v2(db.get(), 
                                         sql_string.c_str(),
                                         sql_string.size()+1,
                                         &stmt,
                                         NULL);

            if (status != SQLITE_OK)
            {
                sqlite3_finalize(stmt);
                throw sqlite_error(sqlite3_errmsg(db.get()));
            }
            if (stmt == 0)
            {
                throw sqlite_error("Invalid SQL statement");
            }
        }

        ~statement(
        )
        {
            sqlite3_finalize(stmt);
        }

        void exec(
        )
        {
            reset();

            step_status = sqlite3_step(stmt);
            needs_reset = true;
            if (step_status != SQLITE_DONE && step_status != SQLITE_ROW)
            {
                if (step_status == SQLITE_ERROR)
                    throw sqlite_error(sqlite3_errmsg(db.get()));
                else if (step_status == SQLITE_BUSY)
                    throw sqlite_error("statement::exec() failed.  SQLITE_BUSY returned");
                else
                    throw sqlite_error("statement::exec() failed.");
            }
        }

        bool move_next (
        )
        {
            if (step_status == SQLITE_ROW)
            {
                if (at_first_step)
                {
                    at_first_step = false;
                    return true;
                }
                else
                {
                    step_status = sqlite3_step(stmt);
                    if (step_status == SQLITE_DONE)
                    {
                        return false;
                    }
                    else if (step_status == SQLITE_ROW)
                    {
                        return true;
                    }
                    else
                    {
                        throw sqlite_error(sqlite3_errmsg(db.get()));
                    }
                }
            }
            else
            {
                return false;
            }
        }

        unsigned long get_num_columns(
        ) const
        {
            if( (at_first_step==false) && (step_status==SQLITE_ROW))
            {
                return sqlite3_column_count(stmt);
            }
            else
            {
                return 0;
            }
        }

        const std::string& get_sql_string (
        ) const
        {
            return sql_string;
        }

        template <typename T>
        typename enable_if_c<std::numeric_limits<T>::is_integer>::type get_column (
            unsigned long idx,
            T& item
        ) const
        {
            // unsigned ints won't fit into int all the time so put those into 64bit ints.
            if (sizeof(T) < sizeof(int) || (sizeof(T)==sizeof(int) && is_signed_type<T>::value))
                item = get_column_as_int(idx);
            else
                item = get_column_as_int64(idx);
        }

        void get_column(unsigned long idx, std::string& item) const { item = get_column_as_text(idx); }
        void get_column(unsigned long idx, float& item      ) const { item = get_column_as_double(idx); }
        void get_column(unsigned long idx, double& item     ) const { item = get_column_as_double(idx); }
        void get_column(unsigned long idx, long double& item) const { item = get_column_as_double(idx); }

        template <typename T>
        typename disable_if_c<std::numeric_limits<T>::is_integer>::type get_column (
            unsigned long idx,
            T& item
        ) const
        {
            get_column_as_object(idx, item);
        }

        const std::vector<char> get_column_as_blob (
            unsigned long idx
        ) const
        {
            // make sure requires clause is not broken
            DLIB_ASSERT(idx < get_num_columns(),
                        "\t std::vector<char> statement::get_column_as_blob()"
                        << "\n\t Invalid column index."
                        << "\n\t idx:  " << idx 
                        << "\n\t this: " << this
            );

            const char* data = static_cast<const char*>(sqlite3_column_blob(stmt, idx));
            const int size = sqlite3_column_bytes(stmt, idx);

            return std::vector<char>(data, data+size);
        }

        template <typename T>
        void get_column_as_object (
            unsigned long idx,
            T& item
        ) const
        {
            // make sure requires clause is not broken
            DLIB_ASSERT(idx < get_num_columns(),
                        "\t void statement::get_column_as_object()"
                        << "\n\t Invalid column index."
                        << "\n\t idx:  " << idx 
                        << "\n\t this: " << this
            );

            const char* data = static_cast<const char*>(sqlite3_column_blob(stmt, idx));
            const int size = sqlite3_column_bytes(stmt, idx);
            std::istringstream sin(std::string(data,size));
            deserialize(item, sin);
        }

        const std::string get_column_as_text (
            unsigned long idx
        ) const
        {
            // make sure requires clause is not broken
            DLIB_ASSERT(idx < get_num_columns(),
                        "\t std::string statement::get_column_as_text()"
                        << "\n\t Invalid column index."
                        << "\n\t idx:  " << idx 
                        << "\n\t this: " << this
            );

            const char* data = reinterpret_cast<const char*>(sqlite3_column_text(stmt, idx));
            if (data != 0)
                return std::string(data);
            else
                return std::string();
        }

        double get_column_as_double (
            unsigned long idx
        ) const
        {
            // make sure requires clause is not broken
            DLIB_ASSERT(idx < get_num_columns(),
                        "\t double statement::get_column_as_double()"
                        << "\n\t Invalid column index."
                        << "\n\t idx:  " << idx 
                        << "\n\t this: " << this
            );

            return sqlite3_column_double(stmt, idx);
        }

        int get_column_as_int (
            unsigned long idx
        ) const
        {
            // make sure requires clause is not broken
            DLIB_ASSERT(idx < get_num_columns(),
                        "\t int statement::get_column_as_int()"
                        << "\n\t Invalid column index."
                        << "\n\t idx:  " << idx 
                        << "\n\t this: " << this
            );

            return sqlite3_column_int(stmt, idx);
        }

        int64 get_column_as_int64 (
            unsigned long idx
        ) const
        {
            // make sure requires clause is not broken
            DLIB_ASSERT(idx < get_num_columns(),
                        "\t int64 statement::get_column_as_int64()"
                        << "\n\t Invalid column index."
                        << "\n\t idx:  " << idx 
                        << "\n\t this: " << this
            );

            return sqlite3_column_int64(stmt, idx);
        }

        const std::string get_column_name (
            unsigned long idx
        ) const
        {
            // make sure requires clause is not broken
            DLIB_ASSERT(idx < get_num_columns(),
                        "\t std::string statement::get_column_name()"
                        << "\n\t Invalid column index."
                        << "\n\t idx:  " << idx 
                        << "\n\t this: " << this
            );

            return std::string(sqlite3_column_name(stmt,idx));
        }

        unsigned long get_max_parameter_id (
        ) const
        {
            return sqlite3_limit(db.get(), SQLITE_LIMIT_VARIABLE_NUMBER, -1);
        }

        unsigned long get_parameter_id (
            const std::string& name
        ) const
        {
            return sqlite3_bind_parameter_index(stmt, name.c_str());
        }

        template <typename T>
        typename enable_if_c<std::numeric_limits<T>::is_integer>::type bind (
            unsigned long idx,
            const T& item
        ) 
        {
            // unsigned ints won't fit into int all the time so put those into 64bit ints.
            if (sizeof(T) < sizeof(int) || (sizeof(T)==sizeof(int) && is_signed_type<T>::value))
                bind_int(idx, item);
            else
                bind_int64(idx, item);
        }

        void bind(unsigned long idx, const std::string& item) { bind_text(idx, item); }
        void bind(unsigned long idx, const float& item      ) { bind_double(idx, item); }
        void bind(unsigned long idx, const double& item     ) { bind_double(idx, item); }
        void bind(unsigned long idx, const long double& item) { bind_double(idx, item); }

        template <typename T>
        typename disable_if_c<std::numeric_limits<T>::is_integer>::type bind (
            unsigned long idx,
            const T& item
        ) 
        {
            bind_object(idx, item);
        }

        void bind_blob (
            unsigned long parameter_id,
            const std::vector<char>& item
        )
        {
            // make sure requires clause is not broken
            DLIB_ASSERT(1 <= parameter_id && parameter_id <= get_max_parameter_id(),
                        "\t void statement::bind_blob()"
                        << "\n\t Invalid parameter id."
                        << "\n\t parameter_id:           " << parameter_id 
                        << "\n\t get_max_parameter_id(): " << get_max_parameter_id() 
                        << "\n\t this:                   " << this
            );

            reset();
            int status = sqlite3_bind_blob(stmt, parameter_id, &item[0], item.size(), SQLITE_TRANSIENT);

            if (status != SQLITE_OK)
            {
                throw sqlite_error(sqlite3_errmsg(db.get()));
            }
        }

        template <typename T>
        void bind_object (
            unsigned long parameter_id,
            const T& item
        )
        {
            // make sure requires clause is not broken
            DLIB_ASSERT(1 <= parameter_id && parameter_id <= get_max_parameter_id(),
                        "\t void statement::bind_object()"
                        << "\n\t Invalid parameter id."
                        << "\n\t parameter_id:           " << parameter_id 
                        << "\n\t get_max_parameter_id(): " << get_max_parameter_id() 
                        << "\n\t this:                   " << this
            );

            reset();
            std::ostringstream sout;
            serialize(item, sout);
            const std::string& str = sout.str();
            int status = sqlite3_bind_blob(stmt, parameter_id, str.data(), str.size(), SQLITE_TRANSIENT);

            if (status != SQLITE_OK)
            {
                throw sqlite_error(sqlite3_errmsg(db.get()));
            }
        }

        void bind_double (
            unsigned long parameter_id,
            const double& item
        )
        {
            // make sure requires clause is not broken
            DLIB_ASSERT(1 <= parameter_id && parameter_id <= get_max_parameter_id(),
                        "\t void statement::bind_double()"
                        << "\n\t Invalid parameter id."
                        << "\n\t parameter_id:           " << parameter_id 
                        << "\n\t get_max_parameter_id(): " << get_max_parameter_id() 
                        << "\n\t this:                   " << this
            );

            reset();
            int status = sqlite3_bind_double(stmt, parameter_id, item);

            if (status != SQLITE_OK)
            {
                throw sqlite_error(sqlite3_errmsg(db.get()));
            }
        }

        void bind_int (
            unsigned long parameter_id,
            const int& item
        )
        {
            // make sure requires clause is not broken
            DLIB_ASSERT(1 <= parameter_id && parameter_id <= get_max_parameter_id(),
                        "\t void statement::bind_int()"
                        << "\n\t Invalid parameter id."
                        << "\n\t parameter_id:           " << parameter_id 
                        << "\n\t get_max_parameter_id(): " << get_max_parameter_id() 
                        << "\n\t this:                   " << this
            );

            reset();
            int status = sqlite3_bind_int(stmt, parameter_id, item);

            if (status != SQLITE_OK)
            {
                throw sqlite_error(sqlite3_errmsg(db.get()));
            }
        }

        void bind_int64 (
            unsigned long parameter_id,
            const int64& item
        )
        {
            // make sure requires clause is not broken
            DLIB_ASSERT(1 <= parameter_id && parameter_id <= get_max_parameter_id(),
                        "\t void statement::bind_int64()"
                        << "\n\t Invalid parameter id."
                        << "\n\t parameter_id:           " << parameter_id 
                        << "\n\t get_max_parameter_id(): " << get_max_parameter_id() 
                        << "\n\t this:                   " << this
            );

            reset();
            int status = sqlite3_bind_int64(stmt, parameter_id, item);

            if (status != SQLITE_OK)
            {
                throw sqlite_error(sqlite3_errmsg(db.get()));
            }
        }

        void bind_null (
            unsigned long parameter_id
        )
        {
            // make sure requires clause is not broken
            DLIB_ASSERT(1 <= parameter_id && parameter_id <= get_max_parameter_id(),
                        "\t void statement::bind_null()"
                        << "\n\t Invalid parameter id."
                        << "\n\t parameter_id:           " << parameter_id 
                        << "\n\t get_max_parameter_id(): " << get_max_parameter_id() 
                        << "\n\t this:                   " << this
            );

            reset();
            int status = sqlite3_bind_null(stmt, parameter_id);

            if (status != SQLITE_OK)
            {
                throw sqlite_error(sqlite3_errmsg(db.get()));
            }
        }

        void bind_text (
            unsigned long parameter_id,
            const std::string& item
        )
        {
            // make sure requires clause is not broken
            DLIB_ASSERT(1 <= parameter_id && parameter_id <= get_max_parameter_id(),
                        "\t void statement::bind_text()"
                        << "\n\t Invalid parameter id."
                        << "\n\t parameter_id:           " << parameter_id 
                        << "\n\t get_max_parameter_id(): " << get_max_parameter_id() 
                        << "\n\t this:                   " << this
            );

            reset();
            int status = sqlite3_bind_text(stmt, parameter_id, item.c_str(), -1, SQLITE_TRANSIENT);

            if (status != SQLITE_OK)
            {
                throw sqlite_error(sqlite3_errmsg(db.get()));
            }
        }

    private:

        void reset()
        {
            if (needs_reset)
            {
                if (sqlite3_reset(stmt) != SQLITE_OK)
                {
                    step_status = SQLITE_DONE;
                    throw sqlite_error(sqlite3_errmsg(db.get()));
                }
                needs_reset = false;
                step_status = SQLITE_DONE;
                at_first_step = true;
            }
        }

        bool needs_reset; // true if sqlite3_step() has been called more recently than sqlite3_reset() 
        int step_status;
        bool at_first_step;

        std::shared_ptr<sqlite3> db;
        sqlite3_stmt* stmt;
        std::string sql_string;
    };

// --------------------------------------------------------------------------------------------

    void database::
    exec (
        const std::string& sql_statement
    )
    {
        // make sure requires clause is not broken
        DLIB_ASSERT(is_open() == true,
                    "\t void database::exec()"
                    << "\n\t The database must be opened before calling this routine."
                    << "\n\t this: " << this
        );

        statement(*this, sql_statement).exec();
    }

// --------------------------------------------------------------------------------------------

}

#endif // DLIB_SQLiTE_H_