Function update

Summary

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

(1) size_t update(const T &obj) noexcept(false)

(2) void update(const T &obj, const CountCallback &rcb, const ExceptionCallback &ecb) noexcept

Function overload

Synopsis

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

size_t update(const T &obj) noexcept(false)

Description

Update a record.

Parameters:

obj - The record.

Return
size_t The number of updated records. It only could be 0 or 1.
Note
The table must have a primary key.

Source

Lines 1259-1288 in orm_lib/inc/drogon/orm/Mapper.h. Line 521 in orm_lib/inc/drogon/orm/Mapper.h.

template <typename T>
inline size_t Mapper<T>::update(const T &obj) noexcept(false)
{
    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, "$?");
    Result r(nullptr);
    {
        auto binder = *client_ << std::move(sql);
        obj.updateArgs(binder);
        outputPrimeryKeyToBinder(obj.getPrimaryKey(), binder);
        binder << Mode::Blocking;
        binder >> [&r](const Result &result) { r = result; };
        binder.exec();  // Maybe throw exception;
    }
    return r.affectedRows();
}

Synopsis

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

void update(const T &obj, const CountCallback &rcb, const ExceptionCallback &ecb) noexcept

Description

Asynchronously update a record.

Parameters:

obj - The record.

rcb - is called with the number of updated records.

ecb - is called when an error occurs.

Note
The table must have a primary key.

Source

Lines 1289-1315 in orm_lib/inc/drogon/orm/Mapper.h. Line 531 in orm_lib/inc/drogon/orm/Mapper.h.

template <typename T>
inline void Mapper<T>::update(const T &obj,
                              const CountCallback &rcb,
                              const ExceptionCallback &ecb) 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);
    binder >> [=](const Result &r) { rcb(r.affectedRows()); };
    binder >> ecb;
}





Add Discussion as Guest

Log in to DocsForge