oatpp / oatpp-sqlite

SQLite adapter for oatpp ORM.
https://oatpp.io/
Apache License 2.0
23 stars 18 forks source link

How to implement aggregate queries? #9

Closed dsmyth closed 3 years ago

dsmyth commented 3 years ago

How best to implement aggregate function queries? I have it working, but it's pretty kludgy: In my DbClient class:

    QUERY(getCaptureInfoCount,"SELECT COUNT(*) FROM captures;")

In my service class:

    auto dto = oatpp::Object<CaptureInfoDto>::createShared();
    auto dbCountResult = m_database->getCaptureInfoCount();
    auto countResult = dbCountResult->fetch<oatpp::Vector<oatpp::Fields<oatpp::Any>>>();
    dto->captureCount = countResult->at(0).getValueByKey("COUNT(*)").retrieve<oatpp::Int64>();

There must be a more elegant way of doing this?

lganzzzo commented 3 years ago

Hello @dsmyth ,

In this case, you don't need Any. It's easier to map directly to oatpp::Int64. Instead of oatpp::Fields you may use oatpp::Vector - to omit column names. Also, it may be convenient to place this method into your DbClient itself:

  QUERY(queryCaptureInfoCount, "SELECT COUNT(*) FROM captures;")

  oatpp::Int64 getCaptureInfoCount() {
    auto dbResult = queryCaptureInfoCount();
    auto dataset = dbResult->fetch<oatpp::Vector<oatpp::Vector<oatpp::Int64>>>();
    return dataset[0][0];
  }
dsmyth commented 3 years ago

That works -- thank you!