query.cpp
Go to the documentation of this file.
1 // SPDX-License-Identifier: BSD-3-Clause
2 
3 /*
4  * Copyright (c) 2020, Bjarne von Horn
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  * * Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  * * Neither the name of the copyright holder nor the names of its contributors
15  * may be used to endorse or promote products derived from this software
16  * without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL BJARNE VON HORN BE LIABLE FOR ANY DIRECT,
22  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
30 
31 #include <sqlite3.h>
32 #include <iomanip>
33 #include <boost/variant/static_visitor.hpp>
36 #include <cassert>
37 #include <ros/console.h>
38 
40  const std::string& outro,
41  int bind_start_col) const
42 {
43  sqlite3_stmt* stmt = nullptr;
44  const auto query = intro + query_.str() + outro + ";";
46  ROS_DEBUG_NAMED("warehouse_ros_sqlite", "query query: %s", query.c_str());
47  if (sqlite3_prepare_v2(db_conn, query.c_str(), query.size() + 1 /* null terminator*/, &stmt, nullptr) != SQLITE_OK)
48  {
49  // TODO: check if all column exists and return nullptr if not
50  // not throwing an exception, missing column means empty result
51  ROS_ERROR_NAMED("warehouse_ros_sqlite", "Preparing Query failed: %s", sqlite3_errmsg(db_conn));
52  return ans;
53  }
54  ans.reset(stmt);
55  assert(static_cast<size_t>(sqlite3_bind_parameter_count(stmt)) == (values_.size() + bind_start_col - 1));
56 
57  warehouse_ros_sqlite::BindVisitor visitor(stmt, bind_start_col);
58  for (const auto& value : values_)
59  {
60  if (boost::apply_visitor(visitor, value) != SQLITE_OK)
61  {
62  throw InternalError("Binding parameter to query failed", db_conn);
63  }
64  }
65 
66  return ans;
67 }
68 
69 void warehouse_ros_sqlite::Query::append(const std::string& name, const std::string& val)
70 {
71  doappend(name, " == ", val);
72 }
73 void warehouse_ros_sqlite::Query::append(const std::string& name, const double val)
74 {
75  doappend(name, " == ", val);
76 }
77 void warehouse_ros_sqlite::Query::append(const std::string& name, const int val)
78 {
79  doappend(name, " == ", val);
80 }
81 void warehouse_ros_sqlite::Query::append(const std::string& name, const bool val)
82 {
83  doappend(name, " == ", static_cast<int>(val));
84 }
85 void warehouse_ros_sqlite::Query::appendLT(const std::string& name, const double val)
86 {
87  doappend(name, " < ", val);
88 }
89 void warehouse_ros_sqlite::Query::appendLT(const std::string& name, const int val)
90 {
91  doappend(name, " < ", val);
92 }
93 void warehouse_ros_sqlite::Query::appendLTE(const std::string& name, const double val)
94 {
95  doappend(name, " <= ", val);
96 }
97 void warehouse_ros_sqlite::Query::appendLTE(const std::string& name, const int val)
98 {
99  doappend(name, " <= ", val);
100 }
101 void warehouse_ros_sqlite::Query::appendGT(const std::string& name, const double val)
102 {
103  doappend(name, " > ", val);
104 }
105 void warehouse_ros_sqlite::Query::appendGT(const std::string& name, const int val)
106 {
107  doappend(name, " > ", val);
108 }
109 void warehouse_ros_sqlite::Query::appendGTE(const std::string& name, const double val)
110 {
111  doappend(name, " >= ", val);
112 }
113 void warehouse_ros_sqlite::Query::appendGTE(const std::string& name, const int val)
114 {
115  doappend(name, " >= ", val);
116 }
117 void warehouse_ros_sqlite::Query::appendRange(const std::string& name, const double lower, const double upper)
118 {
119  doappend(name, " > ", lower);
120  doappend(name, " < ", upper);
121 }
122 void warehouse_ros_sqlite::Query::appendRange(const std::string& name, const int lower, const int upper)
123 {
124  doappend(name, " > ", lower);
125  doappend(name, " < ", upper);
126 }
127 void warehouse_ros_sqlite::Query::appendRangeInclusive(const std::string& name, const double lower, const double upper)
128 {
129  doappend(name, " >= ", lower);
130  doappend(name, " <= ", upper);
131 }
132 void warehouse_ros_sqlite::Query::appendRangeInclusive(const std::string& name, const int lower, const int upper)
133 {
134  doappend(name, " >= ", lower);
135  doappend(name, " <= ", upper);
136 }
warehouse_ros_sqlite::Query::appendGT
void appendGT(const std::string &name, const double val) override
Definition: query.cpp:101
ROS_ERROR_NAMED
#define ROS_ERROR_NAMED(name,...)
query.h
warehouse_ros_sqlite::Query::appendLTE
void appendLTE(const std::string &name, const double val) override
Definition: query.cpp:93
console.h
ROS_DEBUG_NAMED
#define ROS_DEBUG_NAMED(name,...)
warehouse_ros_sqlite::sqlite3_stmt_ptr
std::unique_ptr< sqlite3_stmt, Sqlite3StmtDeleter > sqlite3_stmt_ptr
Definition: utils.h:50
warehouse_ros_sqlite::BindVisitor
Definition: variant.h:41
warehouse_ros_sqlite::InternalError
Definition: exceptions.h:42
warehouse_ros_sqlite::Query::append
void append(const std::string &name, const std::string &val) override
Definition: query.cpp:69
warehouse_ros_sqlite::Query::appendRange
void appendRange(const std::string &name, const double lower, const double upper) override
Definition: query.cpp:117
warehouse_ros_sqlite::Query::values_
std::vector< Variant > values_
Definition: query.h:78
warehouse_ros_sqlite::Query::appendRangeInclusive
void appendRangeInclusive(const std::string &name, const double lower, const double upper) override
Definition: query.cpp:127
warehouse_ros_sqlite::Query::appendGTE
void appendGTE(const std::string &name, const double val) override
Definition: query.cpp:109
warehouse_ros_sqlite::Query::query_
std::stringstream query_
Definition: query.h:79
exceptions.h
variant.h
warehouse_ros_sqlite::Query::prepare
sqlite3_stmt_ptr prepare(sqlite3 *db_conn, const std::string &intro, const std::string &outro="", int bind_start_col=1) const
Definition: query.cpp:39
warehouse_ros_sqlite::Query::appendLT
void appendLT(const std::string &name, const double val) override
Definition: query.cpp:85


warehouse_ros_sqlite
Author(s): Bjarne von Horn
autogenerated on Mon Oct 14 2024 02:16:58