Closed UInSomnia closed 5 months ago
Я нашёл решение nullptr (C++) -> NULL (AnySQL)
If I want to pass several fields that can be either std::string or NULL, depending on the situation. What should we do then? Maybe I don't understand something, but it would be better to take a pointer to a string or another value from the user - the parameters of the sql query. Then it would be possible to pass null ptr if NULL or a pointer to an existing entity is needed. In this case, the parameters would be in equilibrium. And so it turns out that in one case it is a pointer, and in the other an object
const drogon::orm::Result result = clientPtr->execSqlSync(sql_query, str_param.empty() ? nullptr : str_param, str_param1.empty() ? nullptr : str_param1);
You could use the std::optional<>
Thank you very much! I'm going to edit my code I am very grateful to you for creating this library - Drogon
Result
const std::optional<std::string> opt_id_file = [&id_file]() -> std::optional<std::string>
{
if(id_file.empty())
return std::nullopt;
else
return id_file;
}();
const std::optional<std::string> opt_id_node_children = [&id_node_children]() -> std::optional<std::string>
{
if(id_node_children.empty())
return std::nullopt;
else
return id_node_children;
}();
const std::optional<std::string> opt_encrypt_url = [&encrypt_url]() -> std::optional<std::string>
{
if(encrypt_url.empty())
return std::nullopt;
else
return encrypt_url;
}();
clientPtr->execSqlSync(sql_query_insert_content,
str_id_gen_content,
id_content_type,
opt_id_file, // id_file,
opt_id_node_children, // id_node_children,
opt_encrypt_url, // encrypt_url,
encrypt_name,
new_number_sequence);
Why not to create a makeStringOptional
function for doing the same thing?
How can I specify the insertion of a null value when composing an INSERT SQL query?