9#ifndef MANTISAPP_SOCI_WRAPPERS_H
10#define MANTISAPP_SOCI_WRAPPERS_H
16#include "soci/values.h"
26 if (!fields.is_array())
throw std::invalid_argument(
"Fields must be an array");
30 for (
const auto &field: fields) {
31 const auto field_name = field.at(
"name").get<std::string>();
33 if (field_name ==
"id" || field_name ==
"created" || field_name ==
"updated") {
37 if (!entity.contains(field_name))
continue;
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);
46 auto hashed_password =
hashPassword(entity.at(field_name).get<std::string>());
47 vals.set(field_name, hashed_password);
52 if (entity[field_name].is_null()) {
53 std::optional<int> val;
54 vals.set(field_name, val, soci::i_null);
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,
"");
67 vals.set(field_name, 0, soci::i_null);
70 std::istringstream ss{dt_str};
71 ss >> std::get_time(&tm,
"%Y-%m-%dT%H:%M:%S");
73 vals.set(field_name, tm);
75 }
else if (field_type ==
"int") {
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()));
91 for (
const auto &field: fields) {
92 if (field.value(
"name",
"") == column_name) {
101 if (column_name.empty())
throw std::invalid_argument(
"Column name can't be empty!");
103 for (
const auto &field: fields) {
104 if (field.value(
"name",
"") == column_name)
105 return field.at(
"type").get<std::string>();
108 throw std::runtime_error(
"No field type found matching column `" + column_name +
"'");
119 if (entity_fields.empty())
120 throw std::invalid_argument(
"Reference schema fields can't be empty!");
124 for (
size_t i = 0; i < row.size(); i++) {
125 const auto colName = row.get_properties(i).get_name();
132 throw std::runtime_error(std::format(
"Unknown column type `{}` for column `{}`", colType, colName));
136 if (row.get_indicator(i) == soci::i_null) {
138 res_json[colName] =
nullptr;
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") {
149 }
else if (colType ==
"int") {
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);
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.
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.