drogonframework / drogon

Drogon: A C++14/17/20 based HTTP web application framework running on Linux/macOS/Unix/Windows
MIT License
11.44k stars 1.1k forks source link

SQL null set #1881

Closed UInSomnia closed 4 months ago

UInSomnia commented 9 months ago

How can I specify the insertion of a null value when composing an INSERT SQL query?

UInSomnia commented 9 months ago

Я нашёл решение nullptr (C++) -> NULL (AnySQL)

UInSomnia commented 9 months ago

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);

an-tao commented 9 months ago

You could use the std::optional<>

UInSomnia commented 9 months ago

Thank you very much! I'm going to edit my code I am very grateful to you for creating this library - Drogon

UInSomnia commented 9 months ago

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);
an-tao commented 9 months ago

Why not to create a makeStringOptional function for doing the same thing?