Function findOne

Summary

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

(1) T findOne(const Criteria &criteria) noexcept(false)

(2) void findOne(const Criteria &criteria, const SingleRowCallback &rcb, const ExceptionCallback &ecb) noexcept

Function overload

Synopsis

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

T findOne(const Criteria &criteria) noexcept(false)

Description

Find one record that matches the given criteria.

Parameters:

criteria - The criteria.

Return
T The result record.
Note
if the number of rows is greater than one or equal to zero, an UnexpectedRows exception is thrown.

Mentioned in

Source

Lines 712-765 in orm_lib/inc/drogon/orm/Mapper.h. Line 432 in orm_lib/inc/drogon/orm/Mapper.h.

template <typename T>
inline T Mapper<T>::findOne(const Criteria &criteria) noexcept(false)
{
    std::string sql = "select * from ";
    sql += T::tableName;
    bool hasParameters = false;
    if (criteria)
    {
        sql += " where ";
        sql += criteria.criteriaString();
        hasParameters = true;
    }
    sql.append(orderByString_);
    if (limit_ > 0)
    {
        hasParameters = true;
        sql.append(" limit $?");
    }
    if (offset_ > 0)
    {
        hasParameters = true;
        sql.append(" offset $?");
    }
    if (hasParameters)
        sql = replaceSqlPlaceHolder(sql, "$?");
    if (forUpdate_)
    {
        sql += " for update";
    }
    Result r(nullptr);
    {
        auto binder = *client_ << std::move(sql);
        if (criteria)
            criteria.outputArgs(binder);
        if (limit_ > 0)
            binder << limit_;
        if (offset_)
            binder << offset_;
        clear();
        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>

void findOne(const Criteria &criteria, const SingleRowCallback &rcb, const ExceptionCallback &ecb) noexcept

Description

Asynchronously find one record that matches the given criteria.

Parameters:

criteria - The criteria.

rcb - is called with the result.

ecb - is called when an error occurs.

Mentioned in

Source

Lines 767-821 in orm_lib/inc/drogon/orm/Mapper.h. Line 441 in orm_lib/inc/drogon/orm/Mapper.h.

template <typename T>
inline void Mapper<T>::findOne(const Criteria &criteria,
                               const SingleRowCallback &rcb,
                               const ExceptionCallback &ecb) noexcept
{
    std::string sql = "select * from ";
    sql += T::tableName;
    bool hasParameters = false;
    if (criteria)
    {
        sql += " where ";
        sql += criteria.criteriaString();
        hasParameters = true;
    }
    sql.append(orderByString_);
    if (limit_ > 0)
    {
        hasParameters = true;
        sql.append(" limit $?");
    }
    if (offset_ > 0)
    {
        hasParameters = true;
        sql.append(" offset $?");
    }
    if (hasParameters)
        sql = replaceSqlPlaceHolder(sql, "$?");
    if (forUpdate_)
    {
        sql += " for update";
    }
    auto binder = *client_ << std::move(sql);
    if (criteria)
        criteria.outputArgs(binder);
    if (limit_ > 0)
        binder << limit_;
    if (offset_)
        binder << offset_;
    clear();
    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;
}





Add Discussion as Guest

Log in to DocsForge