utils.h
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  */
29 #pragma once
30 
31 #include <memory>
32 #include <array>
33 #include <climits>
34 #include <cstdlib>
35 #include <warehouse_ros_sqlite/warehouse_ros_sqlite_export.h>
36 
37 extern "C" {
38 struct sqlite3_stmt;
39 struct sqlite3;
40 }
41 
42 namespace warehouse_ros_sqlite
43 {
44 struct WAREHOUSE_ROS_SQLITE_EXPORT Sqlite3StmtDeleter
45 {
46  void operator()(sqlite3_stmt* stmt) const;
47 };
48 WAREHOUSE_ROS_SQLITE_EXPORT void sqlite3_delete(sqlite3* db);
49 
50 using sqlite3_stmt_ptr = std::unique_ptr<sqlite3_stmt, Sqlite3StmtDeleter>;
51 using sqlite3_ptr = std::shared_ptr<sqlite3>;
52 
53 namespace schema
54 {
55 namespace detail
56 {
57 template <typename = void>
58 void check_do_escape(std::string& /*unused*/, char /*unused*/)
59 {
60 }
61 template <char escaped_char, char... other_chars>
62 void check_do_escape(std::string& s, char c)
63 {
64  if (c == escaped_char)
65  s.push_back(escaped_char);
66  check_do_escape<other_chars...>(s, c);
67 }
68 
69 template <char... escaped_chars>
70 std::string escape(const std::string& s)
71 {
72  std::string ans;
73  ans.reserve(4 * sizeof...(escaped_chars) + s.size());
74  for (const auto c : s)
75  {
76  ans.push_back(c);
77  check_do_escape<escaped_chars...>(ans, c);
78  }
79  return ans;
80 }
81 } // namespace detail
82 
83 constexpr const char* DB_NAME = "main";
84 constexpr const char* METADATA_COLUMN_PREFIX = "M_";
85 constexpr const char* DATA_COLUMN_NAME = "Data";
86 constexpr const char* TABLE_NAME_PREFIX = "T_";
87 constexpr const char* M_D5_TABLE_NAME = "WarehouseIndex";
88 constexpr const char* M_D5_TABLE_INDEX_COLUMN = "MangledTableName";
89 constexpr const char* M_D5_TABLE_M_D5_COLUMN = "MessageMD5";
90 constexpr const char* M_D5_TABLE_DATATYPE_COLUMN = "MessageDataType";
91 constexpr const char* M_D5_TABLE_TABLE_COLUMN = "WarehouseCollectionName";
92 constexpr const char* M_D5_TABLE_DATABASE_COLUMN = "WarehouseDatabaseName";
93 const int DATA_COLUMN_INDEX = 0;
94 const int VERSION = 10;
95 
96 using escaped_columnname = std::string;
97 using escaped_tablename = std::string;
98 inline std::string escape_identifier(const std::string& s)
99 {
100  return "`" + detail::escape<'`'>(s) + "`";
101 }
103 {
105 }
106 inline std::string escape_string_literal_without_quotes(const std::string& c)
107 {
108  return schema::detail::escape<'\''>(c);
109 }
110 inline std::string mangle_database_and_collection_name(const std::string& db_name, const std::string& collection_name)
111 {
112  return TABLE_NAME_PREFIX + detail::escape<'@'>(db_name) + "@" + detail::escape<'@'>(collection_name);
113 }
115  const std::string& collection_name)
116 {
117  return "\"" + detail::escape<'@', '"'>(TABLE_NAME_PREFIX + db_name) + "@" +
118  detail::escape<'@', '"'>(collection_name) + "\"";
119 }
120 
121 } // namespace schema
122 
123 struct WAREHOUSE_ROS_SQLITE_EXPORT NullValue
124 {
125 };
126 
127 inline WAREHOUSE_ROS_SQLITE_EXPORT std::array<unsigned char, 16> parse_md5_hexstring(const std::string& md5)
128 {
129  if (md5.size() != 32)
130  {
131  throw std::invalid_argument("md5.size() must equal 32");
132  }
133  std::array<unsigned char, 16> binary_md5;
134  size_t md5_idx = 0;
135  for (auto& c : binary_md5)
136  {
137  char* end;
138  const auto substr = md5.substr(md5_idx, 2);
139  const auto t = std::strtoul(substr.c_str(), &end, 16);
140  if (substr.c_str() + 2 != end)
141  throw std::invalid_argument("md5 is not hex string");
142  c = static_cast<unsigned char>(t);
143  md5_idx += 2;
144  }
145  return binary_md5;
146 }
147 } // namespace warehouse_ros_sqlite
warehouse_ros_sqlite
Definition: database_connection.h:36
warehouse_ros_sqlite::schema::escape_columnname_with_prefix
escaped_columnname escape_columnname_with_prefix(const std::string &c)
Definition: utils.h:102
warehouse_ros_sqlite::schema::escape_and_mangle_database_and_collection_name
escaped_tablename escape_and_mangle_database_and_collection_name(const std::string &db_name, const std::string &collection_name)
Definition: utils.h:114
warehouse_ros_sqlite::schema::escaped_tablename
std::string escaped_tablename
Definition: utils.h:97
warehouse_ros_sqlite::schema::M_D5_TABLE_DATABASE_COLUMN
constexpr const char * M_D5_TABLE_DATABASE_COLUMN
Definition: utils.h:92
warehouse_ros_sqlite::schema::M_D5_TABLE_NAME
constexpr const char * M_D5_TABLE_NAME
Definition: utils.h:87
s
XmlRpcServer s
warehouse_ros_sqlite::schema::mangle_database_and_collection_name
std::string mangle_database_and_collection_name(const std::string &db_name, const std::string &collection_name)
Definition: utils.h:110
warehouse_ros_sqlite::NullValue
Definition: utils.h:123
warehouse_ros_sqlite::schema::M_D5_TABLE_DATATYPE_COLUMN
constexpr const char * M_D5_TABLE_DATATYPE_COLUMN
Definition: utils.h:90
warehouse_ros_sqlite::schema::DATA_COLUMN_INDEX
const int DATA_COLUMN_INDEX
Definition: utils.h:93
warehouse_ros_sqlite::schema::TABLE_NAME_PREFIX
constexpr const char * TABLE_NAME_PREFIX
Definition: utils.h:86
warehouse_ros_sqlite::schema::detail::escape
std::string escape(const std::string &s)
Definition: utils.h:70
warehouse_ros_sqlite::schema::DB_NAME
constexpr const char * DB_NAME
Definition: utils.h:83
warehouse_ros_sqlite::parse_md5_hexstring
WAREHOUSE_ROS_SQLITE_EXPORT std::array< unsigned char, 16 > parse_md5_hexstring(const std::string &md5)
Definition: utils.h:127
warehouse_ros_sqlite::sqlite3_ptr
std::shared_ptr< sqlite3 > sqlite3_ptr
Definition: utils.h:51
warehouse_ros_sqlite::sqlite3_delete
WAREHOUSE_ROS_SQLITE_EXPORT void sqlite3_delete(sqlite3 *db)
Definition: database_connection.cpp:228
warehouse_ros_sqlite::sqlite3_stmt_ptr
std::unique_ptr< sqlite3_stmt, Sqlite3StmtDeleter > sqlite3_stmt_ptr
Definition: utils.h:50
warehouse_ros_sqlite::schema::detail::check_do_escape
void check_do_escape(std::string &, char)
Definition: utils.h:58
warehouse_ros_sqlite::schema::M_D5_TABLE_INDEX_COLUMN
constexpr const char * M_D5_TABLE_INDEX_COLUMN
Definition: utils.h:88
warehouse_ros_sqlite::schema::DATA_COLUMN_NAME
constexpr const char * DATA_COLUMN_NAME
Definition: utils.h:85
warehouse_ros_sqlite::schema::escaped_columnname
std::string escaped_columnname
Definition: utils.h:96
warehouse_ros_sqlite::schema::escape_identifier
std::string escape_identifier(const std::string &s)
Definition: utils.h:98
warehouse_ros_sqlite::Sqlite3StmtDeleter
Definition: utils.h:44
warehouse_ros_sqlite::schema::METADATA_COLUMN_PREFIX
constexpr const char * METADATA_COLUMN_PREFIX
Definition: utils.h:84
warehouse_ros_sqlite::schema::M_D5_TABLE_M_D5_COLUMN
constexpr const char * M_D5_TABLE_M_D5_COLUMN
Definition: utils.h:89
warehouse_ros_sqlite::schema::escape_string_literal_without_quotes
std::string escape_string_literal_without_quotes(const std::string &c)
Definition: utils.h:106
warehouse_ros_sqlite::schema::M_D5_TABLE_TABLE_COLUMN
constexpr const char * M_D5_TABLE_TABLE_COLUMN
Definition: utils.h:91
warehouse_ros_sqlite::schema::VERSION
const int VERSION
Definition: utils.h:94


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