File: sqlite.hpp

package info (click to toggle)
mapnik 3.0.12%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 17,084 kB
  • ctags: 18,454
  • sloc: cpp: 142,516; python: 1,416; sh: 769; makefile: 170; xml: 140; lisp: 13
file content (192 lines) | stat: -rw-r--r-- 5,459 bytes parent folder | download | duplicates (4)
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
/*****************************************************************************
 *
 * This file is part of Mapnik (c++ mapping toolkit)
 *
 * Copyright (C) 2015 Artem Pavlenko
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *****************************************************************************/

#include <mapnik/util/noncopyable.hpp>
#include <mapnik/util/variant.hpp>
// boost
#include <memory>

#pragma GCC diagnostic push
#include <mapnik/warning_ignore.hpp>
#include <sqlite3.h>
#pragma GCC diagnostic pop

//stl
#ifdef MAPNIK_DEBUG
#include <cassert>
#endif

#include <iostream>
#include <string>
#include <vector>

namespace mapnik {  namespace sqlite {

    class database : private util::noncopyable
    {
        friend class prepared_statement;

        struct database_closer
        {
            void operator () (sqlite3 * db)
            {
#ifdef MAPNIK_DEBUG
                std::cerr << "close database " << db << "\n";
#endif
                sqlite3_close(db);
            }
        };

        using sqlite_db = std::shared_ptr<sqlite3>;
        sqlite_db db_;

    public:
        database(std::string const& name);
        ~database();
        bool execute(std::string const& sql);
    };

    struct null_type {};
    struct blob
    {
        blob(const char* buf, unsigned size)
            : buf_(buf), size_(size) {}

        const char * buf_;
        unsigned size_;
    };

    using value_type = mapnik::util::variant<int,double,std::string, blob,null_type>;
    using record_type = std::vector<value_type>;

    class prepared_statement : util::noncopyable
    {
        struct binder
        {
            binder(sqlite3_stmt * stmt, unsigned index)
                : stmt_(stmt), index_(index) {}

            bool operator() (null_type )
            {
                if (sqlite3_bind_null(stmt_, index_) != SQLITE_OK)
                {
                    std::cerr << "cannot bind nullptr\n";
                    return false;
                }
                return true;
            }

            bool operator() (int val)
            {
                if (sqlite3_bind_int(stmt_, index_ , val ) != SQLITE_OK)
                {
                    std::cerr << "cannot bind " << val << "\n";
                    return false;
                }
                return true;
            }

            bool operator() (double val)
            {
                if (sqlite3_bind_double(stmt_, index_ , val ) != SQLITE_OK)
                {
                    std::cerr << "cannot bind " << val << "\n";
                    return false;
                }
                return true;
            }

            bool operator() (std::string const& val)
            {
                if (sqlite3_bind_text(stmt_, index_, val.c_str(), val.length(), SQLITE_STATIC) != SQLITE_OK)
                {
                    std::cerr << "cannot bind " << val << "\n";
                    return false;
                }
                return true;
            }

            bool operator() (blob const& val)
            {
                if (sqlite3_bind_blob(stmt_, index_, val.buf_, val.size_, SQLITE_STATIC) != SQLITE_OK)
                {
                    std::cerr << "cannot bind BLOB\n";
                    return false;
                }
                return true;
            }

            sqlite3_stmt * stmt_;
            unsigned index_;
        };
    public:
        prepared_statement(database & db, std::string const& sql)
            : db_(db.db_.get()), stmt_(0)
        {
            const char * tail;
            //char * err_msg;
            int res = sqlite3_prepare_v2(db_, sql.c_str(),-1, &stmt_,&tail);
            if (res != SQLITE_OK)
            {
                std::cerr << "ERR:"<< res << "\n";
                throw;
            }
        }

        ~prepared_statement()
        {
            int res = sqlite3_finalize(stmt_);
            if (res != SQLITE_OK)
            {
                std::cerr << "ERR:" << res << "\n";
            }
        }

        bool insert_record(record_type const& rec) const
        {
#ifdef MAPNIK_DEBUG
            assert( unsigned(sqlite3_bind_parameter_count(stmt_)) == rec.size());
#endif
            record_type::const_iterator itr = rec.begin();
            record_type::const_iterator end = rec.end();
            int count = 1;
            for (; itr!=end;++itr)
            {
                binder op(stmt_,count++);
                if (!util::apply_visitor(op,*itr))
                {
                    return false;
                }
            }

            sqlite3_step(stmt_);
            sqlite3_reset(stmt_);

            return true;
        }

    private:
        sqlite3 * db_;
        sqlite3_stmt * stmt_;
    };
    }
}