Function findFutureByPrimaryKey

Summary

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

(1) template <typename U = T>
    std::enable_if< !std::is_same< typename U::PrimaryKeyType, void >::value, std::future< T > >::type findFutureByPrimaryKey(const TraitsPKType &key) noexcept

(2) template <typename U = T>
    std::enable_if< std::is_same< typename U::PrimaryKeyType, void >::value, std::future< U > >::type findFutureByPrimaryKey(const TraitsPKType &key) noexcept

Function overload

Synopsis

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

template <typename U = T>
std::enable_if< !std::is_same< typename U::PrimaryKeyType, void >::value, std::future< T > >::type findFutureByPrimaryKey(const TraitsPKType &key) noexcept

Description

Asynchronously find a record by the primary key.

Parameters:

key - The value of the primary key.

Return
std::future<T> The future object with which user can get the result.
Note
If no hit record exists, an UnexpectedRows exception is thrown when user calls the get() method of the future object.

Source

Lines 301-357 in orm_lib/inc/drogon/orm/Mapper.h.

template <typename U = T>
inline typename std::enable_if<
    !std::is_same<typename U::PrimaryKeyType, void>::value,
    std::future<T>>::type
findFutureByPrimaryKey(const TraitsPKType &key) noexcept
{
    static_assert(!std::is_same<typename T::PrimaryKeyType, void>::value,
                  "No primary key in the table!");
    static_assert(
        internal::has_sqlForFindingByPrimaryKey<T>::value,
        "No function member named sqlForFindingByPrimaryKey, please "
        "make sure that the model class is generated by the latest "
        "version of drogon_ctl");
    // return findFutureOne(Criteria(T::primaryKeyName, key));
    std::string sql = T::sqlForFindingByPrimaryKey();
    if (forUpdate_)
    {
        sql += " for update";
    }
    clear();
    auto binder = *client_ << std::move(sql);
    outputPrimeryKeyToBinder(key, binder);
    std::shared_ptr<std::promise<T>> prom =
        std::make_shared<std::promise<T>>();
    binder >> [=](const Result &r) {
        if (r.size() == 0)
        {
            try
            {
                throw UnexpectedRows("0 rows found");
            }
            catch (...)
            {
                prom->set_exception(std::current_exception());
            }
        }
        else if (r.size() > 1)
        {
            try
            {
                throw UnexpectedRows("Found more than one row");
            }
            catch (...)
            {
                prom->set_exception(std::current_exception());
            }
        }
        else
        {
            prom->set_value(T(r[0]));
        }
    };
    binder >> [=](const std::exception_ptr &e) { prom->set_exception(e); };
    binder.exec();
    return prom->get_future();
}

Synopsis

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

template <typename U = T>
std::enable_if< std::is_same< typename U::PrimaryKeyType, void >::value, std::future< U > >::type findFutureByPrimaryKey(const TraitsPKType &key) noexcept

Description

No description yet.

Source

Lines 359-367 in orm_lib/inc/drogon/orm/Mapper.h.

template <typename U = T>
inline typename std::enable_if<
    std::is_same<typename U::PrimaryKeyType, void>::value,
    std::future<U>>::type
findFutureByPrimaryKey(const TraitsPKType &key) noexcept
{
    LOG_FATAL << "The table must have a primary key";
    abort();
}





Add Discussion as Guest

Log in to DocsForge