summaryrefslogtreecommitdiff
path: root/subprojects/boost-sqlite/example/ordered_map.cpp
blob: 26a285b2fbf0b434079387f75b2b4deaff0ca07e (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
//
// Copyright (c) 2022 Klemens Morgenstern (klemens.morgenstern@gmx.net)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//

#include <boost/sqlite.hpp>
#include <boost/container/flat_map.hpp>
#include <iostream>

using namespace boost;

// this examples shows how to expose an ordered map as a vtable.

// tag::cursor[]
struct ordered_map_cursor final : sqlite::vtab::cursor<sqlite::string_view> // <1>
{
  container::flat_map<std::string, std::string> &data;
  ordered_map_cursor(container::flat_map<std::string, std::string> &data) : data(data) {}
  bool inverse = false;

  using const_iterator = typename container::flat_map<std::string, std::string>::const_iterator;
  const_iterator begin{data.begin()}, end{data.end()}; // <2>

  sqlite::result<void> next() override { if (inverse) end--; else begin++; return {};} // <3>

  sqlite::result<sqlite3_int64> row_id() override
  {
    return {system::in_place_error, SQLITE_MISUSE, // <4>
            "this shouldn't be called, we're omitting the row id"};
  }
  sqlite::result<sqlite::string_view> column(int i, bool /*nochange*/) override // <5>
  {
    auto & elem = inverse ? *std::prev(end) : *begin;

    if (i == 0)
      return elem.first;
    else
      return elem.second;
  }
  // end::cursor[]
  //tag::filter[]
  sqlite::result<void> filter(int idx, const char * idxStr, span<sqlite::value> values) override
  {
    if (idx != 0) // <1>
      inverse = true;

    for (auto i = 0u; i < values.size(); i ++)
    {
      auto txt = values[i].get_text();
      switch (idxStr[i])
      {
        case SQLITE_INDEX_CONSTRAINT_EQ: // <2>
        {
          auto nw = data.equal_range(txt);
          if (nw.first > begin)
            begin = nw.first;
          if (nw.second < end)
            end = nw.second;
        }

          break;
        case SQLITE_INDEX_CONSTRAINT_GT: // <3>
        {
          auto new_begin = data.find(txt);
          new_begin ++;
          if (new_begin > begin)
            begin = new_begin;
        }
          break;
        case SQLITE_INDEX_CONSTRAINT_GE: // <3>
        {
          auto new_begin = data.find(txt);
          if (new_begin > begin)
            begin = new_begin;
        }
          break;
        case SQLITE_INDEX_CONSTRAINT_LE: // <4>
        {
          auto new_end = data.find(txt);
          new_end++;
          if (new_end < end)
            end = new_end;

        }
          break;
        case SQLITE_INDEX_CONSTRAINT_LT: // <4>
        {
          auto new_end = data.find(txt);
          if (new_end < end)
            end = new_end;
        }
          break;
      }

    }
    return {};
  }
  //end::filter[]
  // tag::cursor[]
  bool eof() noexcept override // <6>
  {
    return begin == end;
  }
};
// end::cursor[]


// tag::table[]
struct map_impl final
    : sqlite::vtab::table<ordered_map_cursor>,
      sqlite::vtab::modifiable // <1>

{
  container::flat_map<std::string, std::string> &data;
  map_impl(container::flat_map<std::string, std::string> &data) : data(data) {}

  const char * declaration() override // <2>
  {
    return R"(
          create table my_map(
              name text primary key unique not null,
              data text) WITHOUT ROWID;)";
  }


  sqlite::result<cursor_type> open() override // <3>
  {
    return cursor_type{data};
  }

  sqlite::result<void> delete_(sqlite::value key) override // <4>
  {
    data.erase(key.get_text());
    return {};
  }
  sqlite::result<sqlite_int64> insert(sqlite::value /*key*/, span<sqlite::value> values,
                                      int /*on_conflict*/) override // <5>
  {
    data.emplace(values[0].get_text(), values[1].get_text());
    return 0;
  }

  sqlite::result<sqlite_int64> update(sqlite::value old_key, sqlite::value new_key,
                                      span<sqlite::value> values,
                                      int /*on_conflict*/) override // <6>
  {
    if (new_key.get_int() != old_key.get_int())
      data.erase(old_key.get_text());
    data.insert_or_assign(values[0].get_text(), values[1].get_text());
    return 0;
  }

  // end::table[]
  // tag::best_index[]
  sqlite::result<void> best_index(sqlite::vtab::index_info & info) override
  {
    // we're using the index to encode the mode, because it's simple enough.
    // more complex application should use it as an index like intended

    int idx = 0;
    sqlite::unique_ptr<char[]> str; // <1>
    if (info.constraints().size() > 0)
    {
      const auto sz = info.constraints().size()+1;
      str.reset(static_cast<char*>(sqlite3_malloc(sz)));
      std::memset(str.get(), '\0', sz);
    }
    else
      return {};

    for (auto i = 0u; i < info.constraints().size(); i++)
    {
      if ((idx & SQLITE_INDEX_CONSTRAINT_EQ) != 0)
        break;
      auto ct = info.constraints()[i];
      if (ct.iColumn == 0
          && ct.usable != 0) // aye, that's us
      {
        switch (ct.op) //<2>
        {
          // we'll stick to these
          case SQLITE_INDEX_CONSTRAINT_EQ: BOOST_FALLTHROUGH;
          case SQLITE_INDEX_CONSTRAINT_GT: BOOST_FALLTHROUGH;
          case SQLITE_INDEX_CONSTRAINT_GE: BOOST_FALLTHROUGH;
          case SQLITE_INDEX_CONSTRAINT_LE: BOOST_FALLTHROUGH;
          case SQLITE_INDEX_CONSTRAINT_LT:
            str[idx] = ct.op;
            info.usage()[i].argvIndex = ++idx; // use it -> value in this position in `filter`.
            info.usage()[i].omit = 1; // tell sqlite that we're sure enough, so sqlite doesn't check
            break;
          default:
            break;
        }
      }
    }


    if (info.order_by().size() == 1 && info.order_by()[0].iColumn == 0)
    {
      idx |= info.order_by()[0].desc; // <3>
      info.set_already_ordered(); // <4>
    }

    // <5>
    info.set_index(idx);
    if (str)
      info.set_index_string(str.release(), true);

    return {};
  }
  // end::best_index[]
  // tag::table[]
};

// end::table[]

// tag::module[]
struct ordered_map_module final : sqlite::vtab::eponymous_module<map_impl>
{
  container::flat_map<std::string, std::string> data;
  template<typename ... Args>
  ordered_map_module(Args && ...args) : data(std::forward<Args>(args)...) {}

  sqlite::result<map_impl> connect(
      sqlite::connection /*conn*/, int /*argc*/, const char * const */*argv*/)
  {
    return map_impl{data};
  }
};
// end::module[]



std::initializer_list<std::pair<std::string, std::string>> init_data = {
    {"atomic",                    "1.53.0"},
    {"chrono",                    "1.47.0"},
    {"container",                 "1.48.0"},
    {"context",                   "1.51.0"},
    {"contract",                  "1.67.0"},
    {"coroutine",                 "1.53.0"},
    {"date_time",                 "1.29.0"},
    {"exception",                 "1.36.0"},
    {"fiber",                     "1.62.0"},
    {"filesystem",                "1.30.0"},
    {"graph",                     "1.18.0"},
    {"graph_parallel",            "1.40.0"},
    {"headers",                   "1.00.0"},
    {"iostreams",                 "1.33.0"},
    {"json",                      "1.75.0"},
    {"locale",                    "1.48.0"},
    {"log",                       "1.54.0"},
    {"math",                      "1.23.0"},
    {"mpi",                       "1.35.0"},
    {"nowide",                    "1.73.0"},
    {"program_options",           "1.32.0"},
    {"python",                    "1.19.0"},
    {"random",                    "1.15.0"},
    {"regex",                     "1.18.0"},
    {"serialization",             "1.32.0"},
    {"stacktrace",                "1.65.0"},
    {"system",                    "1.35.0"},
    {"test",                      "1.21.0"},
    {"thread",                    "1.25.0"},
    {"timer",                     "1.9.0"},
    {"type_erasure",              "1.54.0"},
    {"url",                       "1.81.0"},
    {"wave",                      "1.33.0"}
};

void print(std::ostream & os, sqlite::resultset rw)
{
  os << "[";
  for (auto & r : rw)
    os << r.at(0).get_text() << ", ";
  os << "]" << std::endl;
}

int main (int /*argc*/, char * /*argv*/[])
{
  sqlite::connection conn{":memory:"};

  // tag::module[]
  ordered_map_module & m = sqlite::create_module(conn, "my_map", ordered_map_module(init_data));
  // end::module[]
  boost::ignore_unused(m);

  print(std::cout, conn.query("select * from my_map order by name desc;"));
  print(std::cout, conn.query("select * from my_map where name = 'url';"));
  print(std::cout, conn.query("select * from my_map where name < 'url' and name >= 'system' ;"));
  print(std::cout, conn.query("select * from my_map where name >  'json';"));
  print(std::cout, conn.query("select * from my_map where name >= 'json';"));
  print(std::cout, conn.query("select * from my_map where name <  'json';"));
  print(std::cout, conn.query("select * from my_map where name == 'json' order by name  asc;"));
  print(std::cout, conn.query("select * from my_map where name == 'json' order by name desc;"));

  print(std::cout, conn.query("select * from my_map where name < 'url' and name >= 'system' order by name desc;"));
  print(std::cout, conn.query("select * from my_map where data == '1.81.0';"));

  conn.query("delete from my_map where data == '1.81.0';");

  return 0;
}