VSQLite++ 0.3
Loading...
Searching...
No Matches
query.hpp
Go to the documentation of this file.
1/*##############################################################################
2 VSQLite++ - virtuosic bytes SQLite3 C++ wrapper
3
4 Copyright (c) 2006-2014 Vinzenz Feenstra vinzenz.feenstra@gmail.com
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without modification,
8 are permitted provided that the following conditions are met:
9
10 * Redistributions of source code must retain the above copyright notice,
11 this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
15 * Neither the name of virtuosic bytes nor the names of its contributors may
16 be used to endorse or promote products derived from this software without
17 specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 POSSIBILITY OF SUCH DAMAGE.
30
31##############################################################################*/
32#ifndef GUARD_SQLITE_QUERY_HPP_INCLUDED
33#define GUARD_SQLITE_QUERY_HPP_INCLUDED
34
35#include <iterator>
36#include <memory>
37#include <stdexcept>
38#include <string>
39#include <string_view>
40#include <unordered_map>
41#include <vector>
42#include <type_traits>
43#include <memory>
44#include <sqlite/command.hpp>
45#include <sqlite/result.hpp>
46
55namespace sqlite {
56inline namespace v2 {
57
61 struct query : command {
66 query(connection &con, std::string const &sql);
67
71 virtual ~query();
72
74 public:
75 struct column_cache {
76 std::vector<std::string> names;
77 std::unordered_map<std::string, int> lookup;
78 int index_of(std::string_view name) const;
79 };
80
81 class row_view {
82 public:
83 row_view() = default;
84 row_view(result *res, std::shared_ptr<column_cache> cache) :
85 res_(res), cache_(std::move(cache)) {}
86
87 bool valid() const noexcept {
88 return res_ != nullptr;
89 }
90
91 result &raw() const {
92 if (!res_) {
93 throw std::runtime_error("row_view is not bound to a row");
94 }
95 return *res_;
96 }
97
98 template <typename T> T get(std::string_view name) const {
99 return raw().get<T>(column(name));
100 }
101
102 template <typename T> T get(int idx) const {
103 return raw().get<T>(idx);
104 }
105
106 template <typename T> T operator[](std::string_view name) const {
107 return get<T>(name);
108 }
109
110 private:
111 int column(std::string_view name) const;
112
113 result *res_ = nullptr;
114 std::shared_ptr<column_cache> cache_;
115 };
116
117 class iterator {
118 public:
119 using iterator_category = std::input_iterator_tag;
121 using difference_type = std::ptrdiff_t;
122 using pointer = row_view *;
124
126 iterator(result_type res, std::shared_ptr<column_cache> cache, bool end);
131 bool operator==(iterator const &other) const;
132 bool operator!=(iterator const &other) const;
133
134 private:
135 void advance();
138 bool end_ = true;
139 std::shared_ptr<column_cache> cache_;
141 };
142
145
147 iterator end() const;
148
149 private:
151 bool begin_called_ = false;
152 std::shared_ptr<column_cache> cache_;
153 };
154
158 VSQLITE_DEPRECATED result_type emit_result();
159
164
167
174 template <typename... Args, typename = std::enable_if_t<(sizeof...(Args) > 0)>>
175 result_range each(Args &&...args) {
176 ((void)(*this % std::forward<Args>(args)), ...);
177 return each();
178 }
179
180 private:
181 friend struct result;
183 bool step();
184 };
185} // namespace v2
186} // namespace sqlite
187
188#endif // GUARD_SQLITE_QUERY_HPP_INCLUDED
std::input_iterator_tag iterator_category
Definition query.hpp:119
std::shared_ptr< column_cache > cache_
Definition query.hpp:139
bool operator!=(iterator const &other) const
iterator(result_type res, std::shared_ptr< column_cache > cache, bool end)
bool operator==(iterator const &other) const
int column(std::string_view name) const
T get(std::string_view name) const
Definition query.hpp:98
T operator[](std::string_view name) const
Definition query.hpp:106
row_view(result *res, std::shared_ptr< column_cache > cache)
Definition query.hpp:84
std::shared_ptr< column_cache > cache_
Definition query.hpp:114
std::shared_ptr< column_cache > cache_
Definition query.hpp:152
Parameter binding helpers and the sqlite::command base class for executing statements.
std::shared_ptr< result > result_type
Shared-pointer alias used by legacy APIs that transfer result ownership.
Definition result.hpp:252
Row-oriented cursor and typed accessors returned by sqlite::query.
command(connection &con, std::string const &sql)
command constructor
connection is used to open, close, attach and detach a database. Further it has to be passed to all c...
std::vector< std::string > names
Definition query.hpp:76
int index_of(std::string_view name) const
std::unordered_map< std::string, int > lookup
Definition query.hpp:77
query should be used to execute SQL queries An object of this class is not copyable
Definition query.hpp:61
result_range each(Args &&...args)
Binds any supplied parameters and returns a range view.
Definition query.hpp:175
friend struct result
Definition query.hpp:181
query(connection &con, std::string const &sql)
constructor
result_range each()
Returns a range view for range-based for loops.
VSQLITE_DEPRECATED result_type emit_result()
executes the sql command (deprecated, prefer each())
virtual ~query()
destructor
result_type get_result()
returns the results (needs a previous step_once() call)
T get(int idx)
Extracts the column at idx into an arbitrary C++ type.
Definition result.hpp:261