Function findByPrimaryKey

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, T >::type findByPrimaryKey(const TraitsPKType &key) noexcept(false)

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

(3) template <typename U = T>
    std::enable_if< !std::is_same< typename U::PrimaryKeyType, void >::value, void >::type findByPrimaryKey(const TraitsPKType &key, const SingleRowCallback &rcb, const ExceptionCallback &ecb) noexcept

(4) template <typename U = T>
    std::enable_if< std::is_same< typename U::PrimaryKeyType, void >::value, void >::type findByPrimaryKey(const TraitsPKType &key, const SingleRowCallback &rcb, const ExceptionCallback &ecb) 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, T >::type findByPrimaryKey(const TraitsPKType &key) noexcept(false)

Description

Find a record by the primary key.

Parameters:

key - The value of the primary key.

Return
T The record of the primary key.
Note
If no hit record exists, an UnexpectedRows exception is thrown.

Mentioned in

Source

Lines 183-221 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,
    T>::type
findByPrimaryKey(const TraitsPKType &key) noexcept(false)
{
    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 findOne(Criteria(T::primaryKeyName, key));
    std::string sql = T::sqlForFindingByPrimaryKey();
    if (forUpdate_)
    {
        sql += " for update";
    }
    clear();
    Result r(nullptr);
    {
        auto binder = *client_ << std::move(sql);
        outputPrimeryKeyToBinder(key, binder);
        binder << Mode::Blocking;
        binder >> [&r](const Result &result) { r = result; };
        binder.exec();  // exec may be throw exception;
    }
    if (r.size() == 0)
    {
        throw UnexpectedRows("0 rows found");
    }
    else if (r.size() > 1)
    {
        throw UnexpectedRows("Found more than one row");
    }
    auto row = r[0];
    return T(row);
}

Synopsis

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

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

Description

No description yet.

Mentioned in

Source

Lines 223-231 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,
    T>::type
findByPrimaryKey(const TraitsPKType &key) noexcept(false)
{
    LOG_FATAL << "The table must have a primary key";
    abort();
}

Synopsis

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

template <typename U = T>
std::enable_if< !std::is_same< typename U::PrimaryKeyType, void >::value, void >::type findByPrimaryKey(const TraitsPKType &key, const SingleRowCallback &rcb, const ExceptionCallback &ecb) noexcept

Description

Asynchronously find a record by the primary key.

Parameters:

key - The value of the primary key.

rcb - Is called when a record is found.

ecb - Is called when an error occurs or a record cannot be found.

Mentioned in

Source

Lines 240-279 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,
    void>::type
findByPrimaryKey(const TraitsPKType &key,
                 const SingleRowCallback &rcb,
                 const ExceptionCallback &ecb) 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");
    // findOne(Criteria(T::primaryKeyName, key), rcb, ecb);
    std::string sql = T::sqlForFindingByPrimaryKey();
    if (forUpdate_)
    {
        sql += " for update";
    }
    clear();
    auto binder = *client_ << std::move(sql);
    outputPrimeryKeyToBinder(key, binder);
    binder >> [=](const Result &r) {
        if (r.size() == 0)
        {
            ecb(UnexpectedRows("0 rows found"));
        }
        else if (r.size() > 1)
        {
            ecb(UnexpectedRows("Found more than one row"));
        }
        else
        {
            rcb(T(r[0]));
        }
    };
    binder >> ecb;
}

Synopsis

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

template <typename U = T>
std::enable_if< std::is_same< typename U::PrimaryKeyType, void >::value, void >::type findByPrimaryKey(const TraitsPKType &key, const SingleRowCallback &rcb, const ExceptionCallback &ecb) noexcept

Description

No description yet.

Mentioned in

Source

Lines 280-290 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,
    void>::type
findByPrimaryKey(const TraitsPKType &key,
                 const SingleRowCallback &rcb,
                 const ExceptionCallback &ecb) noexcept
{
    LOG_FATAL << "The table must have a primary key";
    abort();
}





Add Discussion as Guest

Log in to DocsForge