Commit 353411e1 authored by Ronan's avatar Ronan

feat(MainDb): fetch partially history

parent e97343cb
...@@ -66,13 +66,9 @@ MainDb::MainDb () : AbstractDb(*new MainDbPrivate) {} ...@@ -66,13 +66,9 @@ MainDb::MainDb () : AbstractDb(*new MainDbPrivate) {}
} }
static constexpr EnumToSql<MainDb::Filter> eventFilterToSql[] = { static constexpr EnumToSql<MainDb::Filter> eventFilterToSql[] = {
{ MainDb::ConferenceCallFilter, "type = 1 OR type = 2" }, { MainDb::ConferenceCallFilter, "1, 2" },
{ MainDb::ConferenceChatMessageFilter, "type = 5" }, { MainDb::ConferenceChatMessageFilter, "5" },
{ { MainDb::ConferenceInfoFilter, "3, 4, 6, 7, 8, 9, 10, 11, 12" }
MainDb::ConferenceInfoFilter,
"type = 3 OR type = 4 OR type = 6 OR type = 7 OR type = 8 OR "
"type = 9 OR type = 10 OR type = 11 OR type = 12"
}
}; };
static constexpr const char *mapEventFilterToSql (MainDb::Filter filter) { static constexpr const char *mapEventFilterToSql (MainDb::Filter filter) {
...@@ -83,7 +79,11 @@ MainDb::MainDb () : AbstractDb(*new MainDbPrivate) {} ...@@ -83,7 +79,11 @@ MainDb::MainDb () : AbstractDb(*new MainDbPrivate) {}
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
static string buildSqlEventFilter (const list<MainDb::Filter> &filters, MainDb::FilterMask mask) { static string buildSqlEventFilter (
const list<MainDb::Filter> &filters,
MainDb::FilterMask mask,
const string &condKeyWord = "WHERE"
) {
L_ASSERT( L_ASSERT(
find_if(filters.cbegin(), filters.cend(), [](const MainDb::Filter &filter) { find_if(filters.cbegin(), filters.cend(), [](const MainDb::Filter &filter) {
return filter == MainDb::NoFilter; return filter == MainDb::NoFilter;
...@@ -101,12 +101,15 @@ MainDb::MainDb () : AbstractDb(*new MainDbPrivate) {} ...@@ -101,12 +101,15 @@ MainDb::MainDb () : AbstractDb(*new MainDbPrivate) {}
if (isStart) { if (isStart) {
isStart = false; isStart = false;
sql += " WHERE "; sql += " " + condKeyWord + " type IN (";
} else } else
sql += " OR "; sql += ", ";
sql += mapEventFilterToSql(filter); sql += mapEventFilterToSql(filter);
} }
if (!isStart)
sql += ") ";
return sql; return sql;
} }
...@@ -738,9 +741,6 @@ MainDb::MainDb () : AbstractDb(*new MainDbPrivate) {} ...@@ -738,9 +741,6 @@ MainDb::MainDb () : AbstractDb(*new MainDbPrivate) {}
} }
// TODO. // TODO.
(void)peerAddress;
(void)nLast;
(void)mask;
return list<shared_ptr<EventLog>>(); return list<shared_ptr<EventLog>>();
} }
...@@ -750,17 +750,56 @@ MainDb::MainDb () : AbstractDb(*new MainDbPrivate) {} ...@@ -750,17 +750,56 @@ MainDb::MainDb () : AbstractDb(*new MainDbPrivate) {}
int end, int end,
FilterMask mask FilterMask mask
) const { ) const {
L_D();
list<shared_ptr<EventLog>> events;
if (!isConnected()) { if (!isConnected()) {
lWarning() << "Unable to get history. Not connected."; lWarning() << "Unable to get history. Not connected.";
return list<shared_ptr<EventLog>>(); return events;
} }
// TODO. if (begin < 0)
(void)peerAddress; begin = 0;
(void)begin;
(void)end; if (end > 0 && begin > end) {
(void)mask; lWarning() << "Unable to get history. Invalid range.";
return list<shared_ptr<EventLog>>(); return events;
}
string query = "SELECT id, type, date FROM event"
" WHERE id IN ("
" SELECT event_id FROM conference_event WHERE chat_room_id = ("
" SELECT id FROM sip_address WHERE value = :peerAddress"
" )"
" )";
query += buildSqlEventFilter({
ConferenceCallFilter, ConferenceChatMessageFilter, ConferenceInfoFilter
}, mask, "AND");
query += " ORDER BY id DESC";
if (end >= 0)
query += " LIMIT " + Utils::toString(end + 1 - begin);
else
query += " LIMIT -1";
if (begin > 0)
query += " OFFSET " + Utils::toString(begin);
L_BEGIN_LOG_EXCEPTION
soci::session *session = d->dbSession.getBackendSession<soci::session>();
soci::transaction tr(*session);
soci::rowset<soci::row> rows = (session->prepare << query, soci::use(peerAddress));
for (const auto &row : rows) {
(void)row;
events.push_back(std::make_shared<EventLog>());
}
L_END_LOG_EXCEPTION
return events;
} }
void MainDb::cleanHistory (const string &peerAddress, FilterMask mask) { void MainDb::cleanHistory (const string &peerAddress, FilterMask mask) {
...@@ -771,6 +810,8 @@ MainDb::MainDb () : AbstractDb(*new MainDbPrivate) {} ...@@ -771,6 +810,8 @@ MainDb::MainDb () : AbstractDb(*new MainDbPrivate) {}
return; return;
} }
// TODO: Deal with mask.
string query; string query;
if (mask == MainDb::NoFilter || mask & ConferenceChatMessageFilter) if (mask == MainDb::NoFilter || mask & ConferenceChatMessageFilter)
query += "SELECT event_id FROM conference_event WHERE chat_room_id = (" query += "SELECT event_id FROM conference_event WHERE chat_room_id = ("
......
...@@ -64,11 +64,23 @@ static void get_unread_messages_count () { ...@@ -64,11 +64,23 @@ static void get_unread_messages_count () {
BC_ASSERT_EQUAL(eventsDb.getUnreadMessagesCount("sip:test-7@sip.linphone.org"), 0, int, "%d"); BC_ASSERT_EQUAL(eventsDb.getUnreadMessagesCount("sip:test-7@sip.linphone.org"), 0, int, "%d");
} }
static void get_history () {
MainDb eventsDb;
BC_ASSERT_TRUE(eventsDb.connect(MainDb::Sqlite3, getDatabasePath()));
BC_ASSERT_EQUAL(
eventsDb.getHistory("sip:test-7@sip.linphone.org", 0, -1, MainDb::Filter::ConferenceChatMessageFilter).size(),
3,
int,
"%d"
);
}
test_t events_db_tests[] = { test_t events_db_tests[] = {
TEST_NO_TAG("Open database", open_database), TEST_NO_TAG("Open database", open_database),
TEST_NO_TAG("Get events count", get_events_count), TEST_NO_TAG("Get events count", get_events_count),
TEST_NO_TAG("Get messages count", get_messages_count), TEST_NO_TAG("Get messages count", get_messages_count),
TEST_NO_TAG("Get unread messages count", get_unread_messages_count) TEST_NO_TAG("Get unread messages count", get_unread_messages_count),
TEST_NO_TAG("Get history", get_history)
}; };
test_suite_t events_db_test_suite = { test_suite_t events_db_test_suite = {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment