Function insert

Summary

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

(1) void insert(T &obj) noexcept(false)

(2) void insert(const T &obj, const SingleRowCallback &rcb, const ExceptionCallback &ecb) noexcept

Function overload

Synopsis

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

void insert(T &obj) noexcept(false)

Description

Insert a row into the table.

Parameters:

obj - The object to be inserted.

Note
The auto-increased primary key (if it exists) is set to the obj argument after the method returns.

Source

Lines 1136-1167 in orm_lib/inc/drogon/orm/Mapper.h. Line 492 in orm_lib/inc/drogon/orm/Mapper.h.

template <typename T>
inline void Mapper<T>::insert(T &obj) noexcept(false)
{
    clear();
    Result r(nullptr);
    bool needSelection = false;
    {
        auto binder = *client_ << obj.sqlForInserting(needSelection);
        obj.outputArgs(binder);
        binder << Mode::Blocking;
        binder >> [&r](const Result &result) { r = result; };
        binder.exec();  // Maybe throw exception;
    }
    assert(r.affectedRows() == 1);
    if (client_->type() == ClientType::PostgreSQL)
    {
        if (needSelection)
        {
            assert(r.size() == 1);
            obj = T(r[0]);
        }
    }
    else  // Mysql or Sqlite3
    {
        auto id = r.insertId();
        obj.updateId(id);
        if (needSelection)
        {
            obj = findByPrimaryKey(obj.getPrimaryKey());
        }
    }
}

Synopsis

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

void insert(const T &obj, const SingleRowCallback &rcb, const ExceptionCallback &ecb) noexcept

Description

Asynchronously insert a row into the table.

Parameters:

obj - The object to be inserted.

rcb - is called with the result (with the auto-increased primary key (if it exists)).

ecb - is called when an error occurs.

Source

Lines 1168-1209 in orm_lib/inc/drogon/orm/Mapper.h. Line 502 in orm_lib/inc/drogon/orm/Mapper.h.

template <typename T>
inline void Mapper<T>::insert(const T &obj,
                              const SingleRowCallback &rcb,
                              const ExceptionCallback &ecb) noexcept
{
    clear();
    bool needSelection = false;
    auto binder = *client_ << obj.sqlForInserting(needSelection);
    obj.outputArgs(binder);
    auto client = client_;
    binder >> [client, rcb, obj, needSelection, ecb](const Result &r) {
        assert(r.affectedRows() == 1);
        if (client->type() == ClientType::PostgreSQL)
        {
            if (needSelection)
            {
                assert(r.size() == 1);
                rcb(T(r[0]));
            }
            else
            {
                rcb(obj);
            }
        }
        else  // Mysql or Sqlite3
        {
            auto id = r.insertId();
            auto newObj = obj;
            newObj.updateId(id);
            if (needSelection)
            {
                auto tmp = Mapper<T>(client);
                tmp.findByPrimaryKey(newObj.getPrimaryKey(), rcb, ecb);
            }
            else
            {
                rcb(newObj);
            }
        }
    };
    binder >> ecb;
}





Add Discussion as Guest

Log in to DocsForge