VSQLite++
0.3
Toggle main menu visibility
Loading...
Searching...
No Matches
include
sqlite
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
55
namespace
sqlite
{
56
inline
namespace
v2
{
57
61
struct
query
:
command
{
66
query
(
connection
&con, std::string
const
&sql);
67
71
virtual
~query
();
72
73
class
result_range
{
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;
120
using
value_type
=
row_view
;
121
using
difference_type
= std::ptrdiff_t;
122
using
pointer
=
row_view
*;
123
using
reference
=
row_view
&;
124
125
iterator
();
126
iterator
(
result_type
res, std::shared_ptr<column_cache> cache,
bool
end
);
127
reference
operator*
()
const
;
128
pointer
operator->
()
const
;
129
iterator
&
operator++
();
130
iterator
operator++
(
int
);
131
bool
operator==
(
iterator
const
&other)
const
;
132
bool
operator!=
(
iterator
const
&other)
const
;
133
134
private
:
135
void
advance
();
136
void
prime_cache
();
137
result_type
result_
;
138
bool
end_
=
true
;
139
std::shared_ptr<column_cache>
cache_
;
140
row_view
current_
;
141
};
142
143
result_range
();
144
explicit
result_range
(
result_type
res);
145
146
iterator
begin
();
147
iterator
end
()
const
;
148
149
private
:
150
result_type
result_
;
151
bool
begin_called_
=
false
;
152
std::shared_ptr<column_cache>
cache_
;
153
};
154
158
VSQLITE_DEPRECATED
result_type
emit_result
();
159
163
result_type
get_result
();
164
166
result_range
each
();
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
;
182
void
access_check
();
183
bool
step
();
184
};
185
}
// namespace v2
186
}
// namespace sqlite
187
188
#endif
// GUARD_SQLITE_QUERY_HPP_INCLUDED
sqlite::v2::query::result_range::iterator
Definition
query.hpp:117
sqlite::v2::query::result_range::iterator::prime_cache
void prime_cache()
sqlite::v2::query::result_range::iterator::operator->
pointer operator->() const
sqlite::v2::query::result_range::iterator::iterator
iterator()
sqlite::v2::query::result_range::iterator::advance
void advance()
sqlite::v2::query::result_range::iterator::result_
result_type result_
Definition
query.hpp:137
sqlite::v2::query::result_range::iterator::iterator_category
std::input_iterator_tag iterator_category
Definition
query.hpp:119
sqlite::v2::query::result_range::iterator::end_
bool end_
Definition
query.hpp:138
sqlite::v2::query::result_range::iterator::reference
row_view & reference
Definition
query.hpp:123
sqlite::v2::query::result_range::iterator::cache_
std::shared_ptr< column_cache > cache_
Definition
query.hpp:139
sqlite::v2::query::result_range::iterator::difference_type
std::ptrdiff_t difference_type
Definition
query.hpp:121
sqlite::v2::query::result_range::iterator::operator++
iterator & operator++()
sqlite::v2::query::result_range::iterator::operator++
iterator operator++(int)
sqlite::v2::query::result_range::iterator::current_
row_view current_
Definition
query.hpp:140
sqlite::v2::query::result_range::iterator::operator*
reference operator*() const
sqlite::v2::query::result_range::iterator::operator!=
bool operator!=(iterator const &other) const
sqlite::v2::query::result_range::iterator::iterator
iterator(result_type res, std::shared_ptr< column_cache > cache, bool end)
sqlite::v2::query::result_range::iterator::pointer
row_view * pointer
Definition
query.hpp:122
sqlite::v2::query::result_range::iterator::operator==
bool operator==(iterator const &other) const
sqlite::v2::query::result_range::iterator::value_type
row_view value_type
Definition
query.hpp:120
sqlite::v2::query::result_range::row_view
Definition
query.hpp:81
sqlite::v2::query::result_range::row_view::row_view
row_view()=default
sqlite::v2::query::result_range::row_view::res_
result * res_
Definition
query.hpp:113
sqlite::v2::query::result_range::row_view::column
int column(std::string_view name) const
sqlite::v2::query::result_range::row_view::get
T get(std::string_view name) const
Definition
query.hpp:98
sqlite::v2::query::result_range::row_view::valid
bool valid() const noexcept
Definition
query.hpp:87
sqlite::v2::query::result_range::row_view::raw
result & raw() const
Definition
query.hpp:91
sqlite::v2::query::result_range::row_view::operator[]
T operator[](std::string_view name) const
Definition
query.hpp:106
sqlite::v2::query::result_range::row_view::get
T get(int idx) const
Definition
query.hpp:102
sqlite::v2::query::result_range::row_view::row_view
row_view(result *res, std::shared_ptr< column_cache > cache)
Definition
query.hpp:84
sqlite::v2::query::result_range::row_view::cache_
std::shared_ptr< column_cache > cache_
Definition
query.hpp:114
sqlite::v2::query::result_range
Definition
query.hpp:73
sqlite::v2::query::result_range::end
iterator end() const
sqlite::v2::query::result_range::cache_
std::shared_ptr< column_cache > cache_
Definition
query.hpp:152
sqlite::v2::query::result_range::result_range
result_range()
sqlite::v2::query::result_range::result_range
result_range(result_type res)
sqlite::v2::query::result_range::begin
iterator begin()
sqlite::v2::query::result_range::begin_called_
bool begin_called_
Definition
query.hpp:151
sqlite::v2::query::result_range::result_
result_type result_
Definition
query.hpp:150
command.hpp
Parameter binding helpers and the sqlite::command base class for executing statements.
sqlite::v2
Definition
backup.hpp:18
sqlite::v2::result_type
std::shared_ptr< result > result_type
Shared-pointer alias used by legacy APIs that transfer result ownership.
Definition
result.hpp:252
sqlite
Definition
backup.hpp:17
result.hpp
Row-oriented cursor and typed accessors returned by sqlite::query.
sqlite::v2::command::command
command(connection &con, std::string const &sql)
command constructor
sqlite::v2::connection
connection is used to open, close, attach and detach a database. Further it has to be passed to all c...
Definition
connection.hpp:64
sqlite::v2::query::result_range::column_cache
Definition
query.hpp:75
sqlite::v2::query::result_range::column_cache::names
std::vector< std::string > names
Definition
query.hpp:76
sqlite::v2::query::result_range::column_cache::index_of
int index_of(std::string_view name) const
sqlite::v2::query::result_range::column_cache::lookup
std::unordered_map< std::string, int > lookup
Definition
query.hpp:77
sqlite::query
query should be used to execute SQL queries An object of this class is not copyable
Definition
query.hpp:61
sqlite::v2::query::each
result_range each(Args &&...args)
Binds any supplied parameters and returns a range view.
Definition
query.hpp:175
sqlite::v2::query::result
friend struct result
Definition
query.hpp:181
sqlite::v2::query::query
query(connection &con, std::string const &sql)
constructor
sqlite::v2::query::each
result_range each()
Returns a range view for range-based for loops.
sqlite::v2::query::access_check
void access_check()
sqlite::v2::query::step
bool step()
sqlite::v2::query::emit_result
VSQLITE_DEPRECATED result_type emit_result()
executes the sql command (deprecated, prefer each())
sqlite::v2::query::~query
virtual ~query()
destructor
sqlite::v2::query::get_result
result_type get_result()
returns the results (needs a previous step_once() call)
sqlite::v2::result::get
T get(int idx)
Extracts the column at idx into an arbitrary C++ type.
Definition
result.hpp:261
Generated by
1.17.0