drogonframework / drogon

Drogon: A C++14/17/20 based HTTP web application framework running on Linux/macOS/Unix/Windows
MIT License
11.04k stars 1.06k forks source link

orm limit,offset使用好象有bug,提个建议orm执行的时候打印一下sql语句 #2083

Closed zh7314 closed 6 days ago

zh7314 commented 6 days ago

Notice 使用的是docker,ide是vscode,不方便debug,提个建议就是 orm执行的时候打印一下sql语句

        auto jsonPtr = req->getJsonObject();

        // 转换指针
        auto pJson = *jsonPtr;
        LOG_DEBUG << pJson.toStyledString();
        // 获取对应的参数
        std::string page = pJson.get("page", 1).asString();
        std::string pageSize = pJson.get("pageSize", 10).asString();

        auto clientPtr = drogon::app().getDbClient();
        Mapper<Admin> mp(clientPtr);

        LOG_DEBUG << std::stoi(pageSize);
        LOG_DEBUG << std::stoi(page);
        std::vector<Admin> admin_list = mp.orderBy(Admin::Cols::_id).limit(std::stoi(pageSize)).offset(std::stoi(page)).findAll();

在分页 时候,limit,offset是需要自己计算还是Mapper维护?

https://github.com/drogonframework/drogon/wiki/ENG-08-3-Database-ORM#mappers-chain-interface

Mapper<Users> mp(dbClientPtr);
auto users = mp.orderBy(Users::Cols::_join_time).limit(25).offset(0).findAll();

这段程序是从users表中选择用户列表,每页25行的第一页

但是第二页

数据库有 2,3,4,5四条数据

mp.orderBy(Admin::Cols::_id).limit(5).offset(0).findAll();  这个返回是对的是 2,3,4,5 四条数据

mp.orderBy(Admin::Cols::_id).limit(5).offset(1).findAll(); 这个返回是对的是 3,4,5  三条数据,正常理解应该是返回为空才对

记得以前是可以打印执行sql,但是好象1.9.1版本找不到在哪打印orm执行的sql,方便开发和debug,vscode debug不是很熟

dg_ctl -v
     _
  __| |_ __ ___   __ _  ___  _ __
 / _` | '__/ _ \ / _` |/ _ \| '_ \
| (_| | | | (_) | (_| | (_) | | | |
 \__,_|_|  \___/ \__, |\___/|_| |_|
                 |___/

A utility for drogon
Version: 1.9.1
Git commit: 41b740f649859e37cf1650ecc8876e1a12a81460
Compilation: 
  Compiler: g++-11
  Compiler ID: GNU
  Compilation flags: -O3 -DNDEBUG -std=c++17 -I/usr/include/jsoncpp -I/usr/local/include
Libraries: 
  postgresql: yes  (pipeline mode: yes)
  mariadb: yes
  sqlite3: yes
  ssl/tls backend: OpenSSL
  brotli: no
  hiredis: yes
  c-ares: yes
  yaml-cpp: no
hwc0919 commented 6 days ago

limit offset均为sql标准含义

zh7314 commented 6 days ago

limit offset均为sql标准含义

ok,知道了,自己去计算一下就ok了

zh7314 commented 6 days ago
      std::string page = pJson.get("page", 1).asString();
      std::string pageSize = pJson.get("pageSize", 10).asString();

        auto offset = (std::stoi(page) - 1) * std::stoi(pageSize);
std::vector<Admin> admin_list = mp.orderBy(Admin::Cols::_id).limit(std::stoi(pageSize)).offset(offset).findAll();