result_iteration_helper.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 
33 
34 #include <sqlite3.h>
35 #include <cassert>
36 
37 #include <cstring>
38 #include <boost/make_shared.hpp>
39 
41 {
42  if (!stmt_)
43  return false;
44  switch (sqlite3_step(stmt_.get()))
45  {
46  case SQLITE_ROW:
47 
48  return true;
49  case SQLITE_DONE:
50  stmt_.reset();
51  return false;
52  default:
53  throw InternalError("next() failed", stmt_.get());
54  }
55 }
57 {
58  if (!stmt_)
59  return false;
60  switch (sqlite3_column_type(stmt_.get(), schema::DATA_COLUMN_INDEX))
61  {
62  case SQLITE_BLOB:
63  return sqlite3_column_bytes(stmt_.get(), schema::DATA_COLUMN_INDEX) != 0;
64  case SQLITE_NULL:
65  return false;
66  default:
67  throw DatatypeMismatch("Data Column has wrong data type");
68  }
69 }
70 
72 {
73  assert(static_cast<bool>(stmt_));
74  auto ans = boost::make_shared<warehouse_ros_sqlite::Metadata>();
75  for (const auto& col_pair : metadata_cols_)
76  {
77  ans->append(std::get<0>(col_pair), stmt_.get(), std::get<1>(col_pair));
78  }
79  return ans;
80 }
81 
83 {
84  if (!hasData())
85  return std::string();
86  return std::string(reinterpret_cast<const char*>(sqlite3_column_blob(stmt_.get(), schema::DATA_COLUMN_INDEX)),
87  sqlite3_column_bytes(stmt_.get(), schema::DATA_COLUMN_INDEX));
88 }
89 
90 namespace
91 {
92 int constexpr strlength(const char* str)
93 {
94  return *str ? 1 + strlength(str + 1) : 0;
95 }
96 } // namespace
97 
99 {
100  if (!stmt_)
101  return;
102  constexpr int max_length = strlength(schema::METADATA_COLUMN_PREFIX);
103  for (int i = 0; i < sqlite3_column_count(stmt_.get()); ++i)
104  {
105  const char* col_name = sqlite3_column_name(stmt_.get(), i);
106  if (std::strncmp(schema::METADATA_COLUMN_PREFIX, col_name, max_length) == 0)
107  {
108  // strip off prefix
109  metadata_cols_.emplace_back(col_name + max_length, i);
110  }
111  }
112 }
std::vector< std::pair< std::string, int > > metadata_cols_
const int DATA_COLUMN_INDEX
Definition: utils.h:93
warehouse_ros::Metadata::ConstPtr metadata() const override
constexpr const char * METADATA_COLUMN_PREFIX
Definition: utils.h:84


warehouse_ros_sqlite
Author(s): Bjarne von Horn
autogenerated on Fri Nov 11 2022 03:44:33