Function updateFuture

Synopsis

#include <orm_lib/inc/drogon/orm/Mapper.h>

std::future< size_t > updateFuture(const T &obj) noexcept

Description

Asynchronously update a record.

Parameters

obj - The record.

Returns
std::future<size_t> The future object with which user can get the number of updated records.
Note
The table must have a primary key.

Source

Lines 1294-1323 in orm_lib/inc/drogon/orm/Mapper.h. Line 532 in orm_lib/inc/drogon/orm/Mapper.h.

template <typename T>
inline std::future<size_t> Mapper<T>::updateFuture(const T &obj) noexcept
{
    clear();
    static_assert(!std::is_same<typename T::PrimaryKeyType, void>::value,
                  "No primary key in the table!");
    std::string sql = "update ";
    sql += T::tableName;
    sql += " set ";
    for (auto const &colName : obj.updateColumns())
    {
        sql += colName;
        sql += " = $?,";
    }
    sql[sql.length() - 1] = ' ';  // Replace the last ','

    makePrimaryKeyCriteria(sql);

    sql = replaceSqlPlaceHolder(sql, "$?");
    auto binder = *client_ << std::move(sql);
    obj.updateArgs(binder);
    outputPrimeryKeyToBinder(obj.getPrimaryKey(), binder);

    std::shared_ptr<std::promise<size_t>> prom =
        std::make_shared<std::promise<size_t>>();
    binder >> [prom](const Result &r) { prom->set_value(r.affectedRows()); };
    binder >> [prom](const std::exception_ptr &e) { prom->set_exception(e); };
    binder.exec();
    return prom->get_future();
}





Add Discussion as Guest

Log in