metadata.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 
35 
36 template <typename R>
37 R warehouse_ros_sqlite::Metadata::doLookup(const std::string& name) const
38 {
39  const auto res = data_.find(name);
40  if (res == data_.end())
41  {
42  throw std::range_error("");
43  }
44  return boost::apply_visitor(warehouse_ros_sqlite::NullValueVisitor<R>(), res->second);
45 }
46 
47 void warehouse_ros_sqlite::Metadata::append(const std::string& name, const std::string& val)
48 {
49  data_[name] = val;
50 }
51 void warehouse_ros_sqlite::Metadata::append(const std::string& name, const double val)
52 {
53  data_[name] = val;
54 }
55 void warehouse_ros_sqlite::Metadata::append(const std::string& name, const int val)
56 {
57  data_[name] = val;
58 }
59 void warehouse_ros_sqlite::Metadata::append(const std::string& name, const bool val)
60 {
61  data_[name] = static_cast<int>(val);
62 }
63 std::string warehouse_ros_sqlite::Metadata::lookupString(const std::string& name) const
64 {
65  return doLookup<std::string>(name);
66 }
67 double warehouse_ros_sqlite::Metadata::lookupDouble(const std::string& name) const
68 {
69  return doLookup<double>(name);
70 }
71 int warehouse_ros_sqlite::Metadata::lookupInt(const std::string& name) const
72 {
73  return doLookup<int>(name);
74 }
75 bool warehouse_ros_sqlite::Metadata::lookupBool(const std::string& name) const
76 {
77  return static_cast<bool>(doLookup<int>(name));
78 }
79 bool warehouse_ros_sqlite::Metadata::lookupField(const std::string& name) const
80 {
81  return (data_.find(name) != data_.end());
82 }
84 {
85  std::set<std::string> ans;
86  std::transform(data_.begin(), data_.end(), std::inserter(ans, ans.end()), [](const auto& it) { return it.first; });
87  return ans;
88 }
89 
90 void warehouse_ros_sqlite::Metadata::append(const std::string& name, sqlite3_stmt* stmt, int col)
91 {
92  switch (sqlite3_column_type(stmt, col))
93  {
94  case SQLITE_NULL:
95  data_[name] = NullValue();
96  break;
97  case SQLITE_BLOB:
98  data_[name] =
99  std::string(reinterpret_cast<const char*>(sqlite3_column_blob(stmt, col)), sqlite3_column_bytes(stmt, col));
100  break;
101  case SQLITE_INTEGER:
102  data_[name] = sqlite3_column_int(stmt, col);
103  break;
104  case SQLITE_FLOAT:
105  data_[name] = sqlite3_column_double(stmt, col);
106  break;
107  default:
108  throw DatatypeMismatch("Unknown Datatype when reading Metadata from DB");
109  }
110 }
111 
112 void warehouse_ros_sqlite::Metadata::ensureColumns(sqlite3* db, const std::string& unescaped_table_name) const
113 {
114  warehouse_ros_sqlite::EnsureColumnVisitor visitor(db, unescaped_table_name);
115  for (const auto& kv : data_)
116  {
117  boost::apply_visitor(visitor.setColumnName(std::get<0>(kv)), std::get<1>(kv));
118  }
119 }
warehouse_ros_sqlite::Metadata::lookupDouble
double lookupDouble(const std::string &name) const override
Definition: metadata.cpp:67
warehouse_ros_sqlite::NullValue
Definition: utils.h:123
warehouse_ros_sqlite::Metadata::lookupBool
bool lookupBool(const std::string &name) const override
Definition: metadata.cpp:75
warehouse_ros_sqlite::Metadata::append
void append(const std::string &name, const std::string &val) override
Definition: metadata.cpp:47
utils.h
warehouse_ros_sqlite::EnsureColumnVisitor
Definition: variant.h:72
warehouse_ros_sqlite::Metadata::ensureColumns
void ensureColumns(sqlite3 *db, const std::string &unescaped_table_name) const
Definition: metadata.cpp:112
warehouse_ros_sqlite::EnsureColumnVisitor::setColumnName
EnsureColumnVisitor & setColumnName(const std::string &unescaped_column)
Definition: variant.h:122
warehouse_ros_sqlite::Metadata::lookupFieldNames
std::set< std::string > lookupFieldNames() const override
Definition: metadata.cpp:83
warehouse_ros_sqlite::Metadata::lookupInt
int lookupInt(const std::string &name) const override
Definition: metadata.cpp:71
warehouse_ros_sqlite::Metadata::data_
std::map< std::string, Variant > data_
Definition: metadata.h:67
warehouse_ros_sqlite::NullValueVisitor
Definition: variant.h:161
variant.h
warehouse_ros_sqlite::Metadata::doLookup
R doLookup(const std::string &name) const
Definition: metadata.cpp:37
warehouse_ros_sqlite::Metadata::lookupField
bool lookupField(const std::string &name) const override
Definition: metadata.cpp:79
metadata.h
warehouse_ros_sqlite::Metadata::lookupString
std::string lookupString(const std::string &name) const override
Definition: metadata.cpp:63
warehouse_ros_sqlite::DatatypeMismatch
Definition: exceptions.h:49


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