Function findBy

Summary

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

(1) std::vector< T > findBy(const Criteria &criteria) noexcept(false)

(2) void findBy(const Criteria &criteria, const MultipleRowsCallback &rcb, const ExceptionCallback &ecb) noexcept

Function overload

Synopsis

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

std::vector< T > findBy(const Criteria &criteria) noexcept(false)

Description

Select the rows that match the given criteria.

Parameters:

criteria - The criteria.

Return
std::vector<T> The vector of rows that match the given criteria.

Mentioned in

  • Database General / DataBase ORM / Criteria

Source

Lines 894-944 in orm_lib/inc/drogon/orm/Mapper.h. Line 463 in orm_lib/inc/drogon/orm/Mapper.h.

template <typename T>
inline std::vector<T> Mapper<T>::findBy(const Criteria &criteria) noexcept(
    false)
{
    std::string sql = "select * from ";
    sql += T::tableName;
    bool hasParameters = false;
    if (criteria)
    {
        hasParameters = true;
        sql += " where ";
        sql += criteria.criteriaString();
    }
    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;
    }
    std::vector<T> ret;
    for (auto const &row : r)
    {
        ret.push_back(T(row));
    }
    return ret;
}

Synopsis

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

void findBy(const Criteria &criteria, const MultipleRowsCallback &rcb, const ExceptionCallback &ecb) noexcept

Description

Asynchronously select the rows that match the given criteria.

Parameters:

criteria - The criteria.

rcb - is called with the result.

ecb - is called when an error occurs.

Mentioned in

  • Database General / DataBase ORM / Criteria

Source

Lines 945-993 in orm_lib/inc/drogon/orm/Mapper.h. Line 472 in orm_lib/inc/drogon/orm/Mapper.h.

template <typename T>
inline void Mapper<T>::findBy(const Criteria &criteria,
                              const MultipleRowsCallback &rcb,
                              const ExceptionCallback &ecb) noexcept
{
    std::string sql = "select * from ";
    sql += T::tableName;
    bool hasParameters = false;
    if (criteria)
    {
        hasParameters = true;
        sql += " where ";
        sql += criteria.criteriaString();
    }
    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) {
        std::vector<T> ret;
        for (auto const &row : r)
        {
            ret.push_back(T(row));
        }
        rcb(ret);
    };
    binder >> ecb;
}





Add Discussion as Guest

Log in to DocsForge