/* * @file CalllogTable.cpp * @author Aleksander Rudnik (aleksander.rudnik@mudita.com) * @date 23.09.2019 * @brief Call Log DB Table * @copyright Copyright (C) 2019 mudita.com * @details */ #include "CalllogTable.hpp" #include "log/log.hpp" #include CalllogTable::CalllogTable(Database *db) : Table(db) {} CalllogTable::~CalllogTable() {} bool CalllogTable::create() { return db->execute(createTableQuery); } bool CalllogTable::add(CalllogTableRow entry) { return db->execute( "INSERT or ignore INTO calls (number, e164number, presentation, date, duration, type, name, contactId, " "isRead) VALUES ('%q', '%q', %lu, %q, %q, %lu, '%q', '%q', %d);", entry.number.c_str(), entry.e164number.c_str(), static_cast(entry.presentation), utils::to_string(entry.date).c_str(), utils::to_string(entry.duration).c_str(), static_cast(entry.type), entry.name.c_str(), entry.contactId.c_str(), entry.isRead); } bool CalllogTable::removeById(uint32_t id) { return db->execute("DELETE FROM calls where _id = %lu;", id); } bool CalllogTable::removeByField(CalllogTableFields field, const char *str) { return false; // not implemented // TODO: alek: check this } bool CalllogTable::update(CalllogTableRow entry) { return db->execute("UPDATE calls SET number = '%q', e164number = '%q', presentation = %lu, date = %lu, duration = " "%lu, type = %lu, " "name = '%q', contactId = '%q', isRead = " "%d WHERE _id = %lu;", entry.number.c_str(), entry.e164number.c_str(), static_cast(entry.presentation), static_cast(entry.date), static_cast(entry.duration), static_cast(entry.type), entry.name.c_str(), entry.contactId.c_str(), entry.isRead, entry.ID); } CalllogTableRow CalllogTable::getById(uint32_t id) { auto retQuery = db->query("SELECT * FROM calls WHERE _id= %u;", id); if ((retQuery == nullptr) || (retQuery->getRowCount() == 0)) { return CalllogTableRow(); } return CalllogTableRow{ (*retQuery)[0].getUInt32(), // ID (*retQuery)[1].getString(), // number (*retQuery)[2].getString(), // e164number static_cast((*retQuery)[3].getUInt32()), // presentation static_cast((*retQuery)[4].getUInt64()), // date static_cast((*retQuery)[5].getUInt64()), // duration static_cast((*retQuery)[6].getUInt32()), // type (*retQuery)[7].getString(), // name (*retQuery)[8].getString(), // contactID static_cast((*retQuery)[9].getUInt64()), // isRead }; } std::vector CalllogTable::getLimitOffset(uint32_t offset, uint32_t limit) { auto retQuery = db->query("SELECT * from calls ORDER BY date DESC LIMIT %lu OFFSET %lu;", limit, offset); if ((retQuery == nullptr) || (retQuery->getRowCount() == 0)) { return std::vector(); } std::vector ret; do { ret.push_back(CalllogTableRow{ (*retQuery)[0].getUInt32(), // ID (*retQuery)[1].getString(), // number (*retQuery)[2].getString(), // e164number static_cast((*retQuery)[3].getUInt32()), // presentation static_cast((*retQuery)[4].getUInt64()), // date static_cast((*retQuery)[5].getUInt64()), // duration static_cast((*retQuery)[6].getUInt32()), // type (*retQuery)[7].getString(), // name (*retQuery)[8].getString(), // contactID static_cast((*retQuery)[9].getUInt64()), // isRead }); } while (retQuery->nextRow()); return ret; } std::vector CalllogTable::getLimitOffsetByField(uint32_t offset, uint32_t limit, CalllogTableFields field, const char *str) { std::string fieldName; // TODO: alek: add more fields? switch (field) { case CalllogTableFields::DATE: fieldName = "date"; break; case CalllogTableFields::TYPE: fieldName = "type"; break; default: return std::vector(); } auto retQuery = db->query( "SELECT * from calls WHERE %q='%q' ORDER BY date LIMIT %lu OFFSET %lu;", fieldName.c_str(), str, limit, offset); if ((retQuery == nullptr) || (retQuery->getRowCount() == 0)) { return std::vector(); } std::vector ret; do { ret.push_back(CalllogTableRow{ (*retQuery)[0].getUInt32(), // ID (*retQuery)[1].getString(), // number (*retQuery)[2].getString(), // e164number static_cast((*retQuery)[3].getUInt32()), // presentation static_cast((*retQuery)[4].getUInt64()), // date static_cast((*retQuery)[5].getUInt64()), // duration static_cast((*retQuery)[6].getUInt32()), // type (*retQuery)[7].getString(), // name (*retQuery)[8].getString(), // contactID static_cast((*retQuery)[9].getUInt64()), // isRead }); } while (retQuery->nextRow()); return ret; } uint32_t CalllogTable::count(EntryState state) { std::string query = "SELECT COUNT(*) FROM calls "; switch (state) { case EntryState::ALL: break; case EntryState::UNREAD: query += "WHERE calls.isRead = 0"; break; case EntryState::READ: query += "WHERE calls.isRead = 1"; break; } query += ";"; LOG_DEBUG("> %s", query.c_str()); auto queryRet = db->query(query.c_str()); if (queryRet == nullptr || queryRet->getRowCount() == 0) { return 0; } return uint32_t{(*queryRet)[0].getUInt32()}; } uint32_t CalllogTable::count() { return count(EntryState::ALL); } uint32_t CalllogTable::countByFieldId(const char *field, uint32_t id) { auto queryRet = db->query("SELECT COUNT(*) FROM calls WHERE %q=%lu;", field, id); if ((queryRet == nullptr) || (queryRet->getRowCount() == 0)) { return 0; } return uint32_t{(*queryRet)[0].getUInt32()}; } bool CalllogTable::SetAllRead() { return db->execute("UPDATE calls SET isRead = 1;"); }