ThinSQLite++
A thin, safe and convenient modern C++ wrapper for the SQLite API.
Loading...
Searching...
No Matches
keywords.hpp
1/*
2 Copyright 2019 Eugene Gershnik
3
4 Use of this source code is governed by a BSD-style
5 license that can be found in the LICENSE file or at
6 https://github.com/gershnik/thinsqlitepp/blob/master/LICENSE
7*/
8
9#ifndef HEADER_SQLITEPP_KEYWORDS_INCLUDED
10#define HEADER_SQLITEPP_KEYWORDS_INCLUDED
11
12#include "exception_iface.hpp"
13
14#include <string_view>
15#include <iterator>
16
17namespace thinsqlitepp
18{
19#if SQLITE_VERSION_NUMBER >= SQLITEPP_SQLITE_VERSION(3, 24, 0)
24
33 SQLITEPP_EXPORTED
34 class keywords
35 {
36 public:
37 using value_type = std::string_view;
38 using size_type = int;
39 using difference_type = int;
40 using reference = value_type;
41 using pointer = void;
42
43 class const_iterator
44 {
45 friend class keywords;
46 public:
47 using value_type = keywords::value_type;
48 using size_type = keywords::size_type;
49 using difference_type = keywords::difference_type;
50 using reference = keywords::reference;
51 using pointer = void;
52 using iterator_category = std::random_access_iterator_tag;
53
54 public:
55 const_iterator() noexcept = default;
56
57 std::string_view operator*() const
58 {
59 const char * name = nullptr;
60 int len = 0;
61 int res = sqlite3_keyword_name(_idx, &name, &len);
62 if (res != SQLITE_OK)
63 throw exception(res);
64 return std::string_view(name, size_t(len));
65 }
66 std::string_view operator[](size_type idx) const noexcept
67 { return *(*this + idx); }
68
69 const_iterator & operator++()
70 { ++_idx; return *this; }
71 const_iterator operator++(int)
72 { return const_iterator(_idx++); }
73 const_iterator & operator+=(int val) noexcept
74 { _idx += val; return *this; }
75 const_iterator & operator--()
76 { --_idx; return *this; }
77 const_iterator operator--(int)
78 { return const_iterator(_idx--); }
79 const_iterator & operator-=(int val) noexcept
80 { _idx -= val; return *this; }
81
82 friend const_iterator operator+(const const_iterator & lhs, int rhs) noexcept
83 { return const_iterator(lhs._idx + rhs); }
84 friend const_iterator operator+(int lhs, const const_iterator & rhs) noexcept
85 { return const_iterator(rhs._idx + lhs); }
86
87 friend int operator-(const const_iterator & lhs, const const_iterator & rhs) noexcept
88 { return lhs._idx - rhs._idx; }
89 friend const_iterator operator-(const const_iterator & lhs, int rhs) noexcept
90 { return const_iterator(lhs._idx - rhs); }
91
92 friend bool operator==(const const_iterator & lhs, const const_iterator & rhs) noexcept
93 { return lhs._idx == rhs._idx; }
94 friend bool operator!=(const const_iterator & lhs, const const_iterator & rhs) noexcept
95 { return lhs._idx != rhs._idx; }
96 friend bool operator<(const const_iterator & lhs, const const_iterator & rhs) noexcept
97 { return lhs._idx < rhs._idx; }
98 friend bool operator<=(const const_iterator & lhs, const const_iterator & rhs) noexcept
99 { return lhs._idx <= rhs._idx; }
100 friend bool operator>(const const_iterator & lhs, const const_iterator & rhs) noexcept
101 { return lhs._idx > rhs._idx; }
102 friend bool operator>=(const const_iterator & lhs, const const_iterator & rhs) noexcept
103 { return lhs._idx >= rhs._idx; }
104
105 private:
106 const_iterator(int idx) noexcept : _idx(idx)
107 {}
108 private:
109 int _idx = 0;
110 };
111
112 using iterator = const_iterator;
113 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
114 using reverse_iterator = std::reverse_iterator<iterator>;
115
116 public:
117 int size() const noexcept
118 { return sqlite3_keyword_count(); }
119 bool empty() const noexcept
120 { return size() == 0; }
121
122 std::string_view operator[](int idx) const noexcept
123 { return *const_iterator(idx); }
124
125 const_iterator begin() const noexcept
126 { return const_iterator(0); }
127 const_iterator cbegin() const noexcept
128 { return const_iterator(0); }
129 const_iterator end() const noexcept
130 { return const_iterator(size()); }
131 const_iterator cend() const noexcept
132 { return const_iterator(size()); }
133
134 const_reverse_iterator rbegin() const noexcept
135 { return const_reverse_iterator(end()); }
136 const_reverse_iterator crbegin() const noexcept
137 { return const_reverse_iterator(end()); }
138 const_reverse_iterator rend() const noexcept
139 { return const_reverse_iterator(begin()); }
140 const_reverse_iterator crend() const noexcept
141 { return const_reverse_iterator(begin()); }
142
143
149 static bool check(std::string_view text)
150 { return sqlite3_keyword_check(text.data(), int_size(text.size())); }
151 };
152
154#endif
155}
156
157#endif
158
Exception used to report any SQLite errors.
Definition exception_iface.hpp:180
STL interface to SQLite keywords.
Definition keywords.hpp:35
static bool check(std::string_view text)
Checks to see whether or not the text argument is a keyword.
Definition keywords.hpp:149
T data(T... args)
sqlite3_keyword_name
ThinSQLite++ namespace.
Definition backup_iface.hpp:17
#define SQLITE_OK
T size(T... args)