Function execSqlAsync

Synopsis

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

template <typename FUNCTION1, typename FUNCTION2, typename... Arguments>
void execSqlAsync(const std::string &sql, FUNCTION1 &&rCallback, FUNCTION2 &&exceptCallback, Arguments &&... args) noexcept

Description

Async and nonblocking method.

Parameters:

sql - is the SQL statement to be executed;

FUNCTION1 - is usually the ResultCallback type;

FUNCTION2 - is usually the ExceptionCallback type;

args - are parameters that are bound to placeholders in the sql parameter;

Note
If the number of args parameters is not zero, make sure that all criteria in the sql parameter set by bind parameters, for example:
  1. select * from users where user_id > 10 limit 10 offset 10; //Not bad, no bind parameters are used.
  2. select * from users where user_id > ? limit ? offset ?; //Good, fully use bind parameters.
  3. select * from users where user_id > ? limit ? offset 10; //Bad, partially use bind parameters.

Strictly speaking, try not to splice SQL statements dynamically, Instead, use the constant sql string with placeholders and the bind parameters to execute sql. This rule makes the sql execute faster and more securely, and users should follow this rule when calling all methods of DbClient.

Mentioned in

Source

Lines 104-115 in orm_lib/inc/drogon/orm/DbClient.h.

template <typename FUNCTION1, typename FUNCTION2, typename... Arguments>
void execSqlAsync(const std::string &sql,
                  FUNCTION1 &&rCallback,
                  FUNCTION2 &&exceptCallback,
                  Arguments &&... args) noexcept
{
    auto binder = *this << sql;
    (void)std::initializer_list<int>{
        (binder << std::forward<Arguments>(args), 0)...};
    binder >> std::forward<FUNCTION1>(rCallback);
    binder >> std::forward<FUNCTION2>(exceptCallback);
}





Add Discussion as Guest

Log in to DocsForge