MantisBase v0.4.9
Loading...
Searching...
No Matches
soci_wrappers.h
Go to the documentation of this file.
1
8
9#ifndef MANTISAPP_SOCI_WRAPPERS_H
10#define MANTISAPP_SOCI_WRAPPERS_H
11
12#include "utils.h"
13#include "../mantisbase.h"
16#include "soci/values.h"
17
18namespace mb {
25 MANTISBASE_API inline soci::values json2SociValue(const json &entity, const json &fields) {
26 if (!fields.is_array()) throw std::invalid_argument("Fields must be an array");
27
28 soci::values vals;
29 // Bind parameters dynamically
30 for (const auto &field: fields) {
31 const auto field_name = field.at("name").get<std::string>();
32
33 if (field_name == "id" || field_name == "created" || field_name == "updated") {
34 continue;
35 }
36 // Skip fields that are not in the JSON object
37 if (!entity.contains(field_name)) continue;
38
39 // For password types, hash before binding (null password = OAuth-only user)
40 if (field_name == "password") {
41 if (entity[field_name].is_null() || (entity[field_name].is_string() && entity[field_name].get<std::string>().empty())) {
42 std::optional<int> val;
43 vals.set(field_name, val, soci::i_null);
44 continue;
45 }
46 auto hashed_password = hashPassword(entity.at(field_name).get<std::string>());
47 vals.set(field_name, hashed_password);
48 continue;
49 }
50
51 // If the value is null, set i_null and continue
52 if (entity[field_name].is_null()) {
53 std::optional<int> val; // Set to optional, no value is set in db
54 vals.set(field_name, val, soci::i_null);
55 continue;
56 }
57
58 // For non-null values, set the value accordingly
59 const auto field_type = field.at("type").get<std::string>();
60 if (field_type == "string" || field_type == "file") {
61 vals.set(field_name, entity.value(field_name, ""));
62 } else if (field_type == "double") {
63 vals.set(field_name, entity.value(field_name, 0.0));
64 } else if (field_type == "date") {
65 auto dt_str = entity.value(field_name, "");
66 if (dt_str.empty()) {
67 vals.set(field_name, 0, soci::i_null);
68 } else {
69 std::tm tm{};
70 std::istringstream ss{dt_str};
71 ss >> std::get_time(&tm, "%Y-%m-%dT%H:%M:%S");
72
73 vals.set(field_name, tm);
74 }
75 } else if (field_type == "int") {
76 bindIntFieldValue(vals, field_name, entity.at(field_name), intPrecisionFromField(field));
77 } else if (field_type == "json") {
78 vals.set(field_name, entity.value(field_name, json::object()));
79 } else if (field_type == "bool") {
80 vals.set(field_name, entity.value(field_name, false));
81 } else if (field_type == "files") {
82 vals.set(field_name, entity.value(field_name, json::array()));
83 }
84 }
85
86 return vals;
87 }
88
90 MANTISBASE_API inline MbIntPrecision getColumnIntPrecision(const std::string &column_name, const std::vector<json> &fields) {
91 for (const auto &field: fields) {
92 if (field.value("name", "") == column_name) {
93 return intPrecisionFromField(field);
94 }
95 }
96 return defaultIntPrecision();
97 }
98
100 MANTISBASE_API inline std::string getColumnType(const std::string &column_name, const std::vector<json> &fields) {
101 if (column_name.empty()) throw std::invalid_argument("Column name can't be empty!");
102
103 for (const auto &field: fields) {
104 if (field.value("name", "") == column_name)
105 return field.at("type").get<std::string>();
106 }
107
108 throw std::runtime_error("No field type found matching column `" + column_name + "'");
109 }
110
117 MANTISBASE_API inline json sociRow2Json(const std::string& db_type, const soci::row &row, const std::vector<json> &entity_fields) {
118 // Guard against empty reference schema fields
119 if (entity_fields.empty())
120 throw std::invalid_argument("Reference schema fields can't be empty!");
121
122 // Build response json object
123 json res_json;
124 for (size_t i = 0; i < row.size(); i++) {
125 const auto colName = row.get_properties(i).get_name();
126 const auto colType = getColumnType(colName, entity_fields);
127
128 // Check column type is valid type
129 if (colType.empty() || !MbEntitySchemaField::isValidFieldType(colType)) // Or not in expected types
130 {
131 // Throw an error for unknown types
132 throw std::runtime_error(std::format("Unknown column type `{}` for column `{}`", colType, colName));
133 }
134
135 // Handle null values immediately
136 if (row.get_indicator(i) == soci::i_null) {
137 // Handle null value in JSON
138 res_json[colName] = nullptr;
139 continue;
140 }
141
142 // Handle type conversions
143 if (colType == "string") {
144 res_json[colName] = row.get<std::string>(i, "");
145 } else if (colType == "double") {
146 res_json[colName] = row.get<double>(i);
147 } else if (colType == "date") {
148 res_json[colName] = mb::dbDateToString(db_type, row, i);
149 } else if (colType == "int") {
150 res_json[colName] = readIntFieldValue(row, i, getColumnIntPrecision(colName, entity_fields));
151 } else if (colType == "json" || colType == "list") {
152 res_json[colName] = row.get<json>(i);
153 } else if (colType == "bool") {
154 res_json[colName] = row.get<bool>(i);
155 } else if (colType == "file") {
156 res_json[colName] = row.get<std::string>(i);
157 } else if (colType == "files") {
158 res_json[colName] = row.get<json>(i);
159 }
160 }
161
162 return res_json;
163 }
164}
165
166#endif //MANTISAPP_SOCI_WRAPPERS_H
static bool isValidFieldType(const std::string &type)
Check if field type is valid.
Definition entity_schema_field.cpp:414
MbEntity schema field definition and validation.
#define MANTISBASE_API
Definition export.h:31
Signed/unsigned integer width tokens for entity int fields (i8..u64).
The main application for mantisbase.
Definition api_keys.h:35
MANTISBASE_API MbIntPrecision getColumnIntPrecision(const std::string &column_name, const std::vector< json > &fields)
Definition soci_wrappers.h:90
nlohmann::json json
Shorten JSON namespace.
Definition api_keys.h:36
MANTISBASE_API json sociRow2Json(const std::string &db_type, const soci::row &row, const std::vector< json > &entity_fields)
Convert a SOCI result row to JSON using entity field metadata.
Definition soci_wrappers.h:117
MANTISBASE_API void bindIntFieldValue(soci::values &vals, const std::string &field_name, const nlohmann::json &value, MbIntPrecision precision)
Definition int_precision.cpp:262
MANTISBASE_API std::string dbDateToString(const std::string &db_type, const soci::row &row, int index)
Convert database date value from SOCI row to string.
Definition date_utils.cpp:59
MANTISBASE_API std::string getColumnType(const std::string &column_name, const std::vector< json > &fields)
Definition soci_wrappers.h:100
MANTISBASE_API MbIntPrecision intPrecisionFromField(const nlohmann::json &field)
Definition int_precision.cpp:71
MANTISBASE_API soci::values json2SociValue(const json &entity, const json &fields)
Bind a JSON entity payload to SOCI values using field metadata.
Definition soci_wrappers.h:25
MbIntPrecision
Definition int_precision.h:24
MANTISBASE_API std::string hashPassword(const std::string &password)
Digests user password + a generated salt to yield a hashed password.
Definition auth_utils.cpp:8
MANTISBASE_API nlohmann::json readIntFieldValue(const soci::row &row, size_t index, MbIntPrecision precision)
Definition int_precision.cpp:304
MANTISBASE_API MbIntPrecision defaultIntPrecision()
Definition int_precision.cpp:10
Collection of utility functions that are re-used across different files.