diff --git a/src/db/abstract/abstract-db.cpp b/src/db/abstract/abstract-db.cpp
index 39101ff00..81df3a33f 100644
--- a/src/db/abstract/abstract-db.cpp
+++ b/src/db/abstract/abstract-db.cpp
@@ -18,7 +18,6 @@
#include "abstract-db-p.h"
#include "db/provider/db-session-provider.h"
-#include "logger/logger.h"
#include "abstract-db.h"
@@ -60,12 +59,12 @@ void AbstractDb::init () {
// -----------------------------------------------------------------------------
-string AbstractDb::primaryKeyAutoIncrementStr () const {
+string AbstractDb::primaryKeyAutoIncrementStr (const string &type) const {
L_D(const AbstractDb);
switch (d->backend) {
case Mysql:
- return " BIGINT UNSIGNED PRIMARY KEY AUTO_INCREMENT";
+ return type + "UNSIGNED PRIMARY KEY AUTO_INCREMENT";
case Sqlite3:
return " INTEGER PRIMARY KEY AUTOINCREMENT";
}
diff --git a/src/db/abstract/abstract-db.h b/src/db/abstract/abstract-db.h
index 1e54943b2..3eac6971f 100644
--- a/src/db/abstract/abstract-db.h
+++ b/src/db/abstract/abstract-db.h
@@ -50,7 +50,7 @@ protected:
virtual void init ();
- std::string primaryKeyAutoIncrementStr () const;
+ std::string primaryKeyAutoIncrementStr (const std::string &type = "INT") const;
private:
L_DECLARE_PRIVATE(AbstractDb);
diff --git a/src/db/events-db.cpp b/src/db/events-db.cpp
index e8d0fa857..f9aaf4685 100644
--- a/src/db/events-db.cpp
+++ b/src/db/events-db.cpp
@@ -16,6 +16,8 @@
* along with this program. If not, see .
*/
+#include
+
#ifdef SOCI_ENABLED
#include
#endif // ifdef SOCI_ENABLED
@@ -44,22 +46,29 @@ EventsDb::EventsDb () : AbstractDb(*new EventsDbPrivate) {}
// Helpers.
// -----------------------------------------------------------------------------
-inline string mapFilterToSqlEvent (EventsDb::Filter filter) {
- switch (filter) {
- case EventsDb::NoFilter:
- break;
- case EventsDb::MessageFilter:
- return "0";
- case EventsDb::CallFilter:
- return "1";
- case EventsDb::ConferenceFilter:
- return "2";
- }
-
- return "";
+inline constexpr const char *mapFilterToSqlEvent (EventsDb::Filter filter) {
+ // Ugly. Yes. But constexpr...
+ return filter == EventsDb::MessageFilter
+ ? "0"
+ : (filter == EventsDb::CallFilter
+ ? "1"
+ : (filter == EventsDb::ConferenceFilter
+ ? "2"
+ : ""
+ )
+ );
}
static string buildSqlEventFilter (const list &filters, EventsDb::FilterMask mask) {
+ L_ASSERT(
+ find_if(filters.cbegin(), filters.cend(), [](const EventsDb::Filter &filter) {
+ return filter == EventsDb::NoFilter;
+ }) == filters.cend()
+ );
+
+ if (mask == EventsDb::NoFilter)
+ return "";
+
bool isStart = true;
string sql;
for (const auto &filter : filters) {
@@ -71,7 +80,8 @@ static string buildSqlEventFilter (const list &filters, Events
sql += " WHERE ";
} else
sql += " OR ";
- sql += " type = " + mapFilterToSqlEvent(filter);
+ sql += " event_type_id = ";
+ sql += mapFilterToSqlEvent(filter);
}
return sql;
@@ -93,28 +103,39 @@ static string buildSqlEventFilter (const list &filters, Events
" value VARCHAR(255) NOT NULL"
")";
+ *session <<
+ "CREATE TABLE IF NOT EXISTS event_type ("
+ " id" + primaryKeyAutoIncrementStr("TINYINT") + ","
+ " value VARCHAR(255) NOT NULL"
+ ")";
+
*session <<
"CREATE TABLE IF NOT EXISTS event ("
" id" + primaryKeyAutoIncrementStr() + ","
- " timestamp TIMESTAMP NOT NULL"
+ " event_type_id TINYINT UNSIGNED NOT NULL,"
+ " timestamp TIMESTAMP NOT NULL,"
+ " FOREIGN KEY (event_type_id)"
+ " REFERENCES event_type(id)"
+ " ON DELETE CASCADE"
")";
*session <<
"CREATE TABLE IF NOT EXISTS message_status ("
- " id" + primaryKeyAutoIncrementStr() + ","
+ " id" + primaryKeyAutoIncrementStr("TINYINT") + ","
" status VARCHAR(255) NOT NULL"
")";
*session <<
"CREATE TABLE IF NOT EXISTS message_direction ("
- " id" + primaryKeyAutoIncrementStr() + ","
+ " id" + primaryKeyAutoIncrementStr("TINYINT") + ","
" direction VARCHAR(255) NOT NULL"
")";
*session <<
"CREATE TABLE IF NOT EXISTS dialog ("
- " local_sip_address_id BIGINT UNSIGNED NOT NULL," // Sip address used to communicate.
- " remote_sip_address_id BIGINT UNSIGNED NOT NULL," // Server (for conference) or user sip address.
+ " id" + primaryKeyAutoIncrementStr() + ","
+ " local_sip_address_id INT UNSIGNED NOT NULL," // Sip address used to communicate.
+ " remote_sip_address_id INT UNSIGNED NOT NULL," // Server (for conference) or user sip address.
" creation_timestamp TIMESTAMP NOT NULL," // Dialog creation date.
" last_update_timestamp TIMESTAMP NOT NULL," // Last event timestamp (call, message...).
" FOREIGN KEY (local_sip_address_id)"
@@ -128,7 +149,7 @@ static string buildSqlEventFilter (const list &filters, Events
*session <<
"CREATE TABLE IF NOT EXISTS message_event ("
" id" + primaryKeyAutoIncrementStr() + ","
- " dialog_id BIGINT UNSIGNED NOT NULL,"
+ " dialog_id INT UNSIGNED NOT NULL,"
" status_id TINYINT UNSIGNED NOT NULL,"
" direction_id TINYINT UNSIGNED NOT NULL,"
" imdn_message_id VARCHAR(255) NOT NULL," // See: https://tools.ietf.org/html/rfc5438#section-6.3
@@ -190,9 +211,24 @@ static string buildSqlEventFilter (const list &filters, Events
}
int EventsDb::getMessagesCount (const string &remoteAddress) const {
- // TODO.
- (void)remoteAddress;
- return 0;
+ L_D(const EventsDb);
+
+ string query = "SELECT COUNT(*) FROM message_event"
+ " WHERE dialog_id = ("
+ " SELECT id FROM dialog WHERE remote_sip_address_id =("
+ " SELECT id FROM sip_address WHERE value = :remote_address"
+ " )"
+ " )", use(remoteAddress);
+ int count = 0;
+
+ L_BEGIN_LOG_EXCEPTION
+
+ soci::session *session = d->dbSession.getBackendSession();
+ *session << query, soci::into(count);
+
+ L_END_LOG_EXCEPTION
+
+ return count;
}
int EventsDb::getUnreadMessagesCount (const string &remoteAddress) const {
diff --git a/src/db/events-db.h b/src/db/events-db.h
index 5a1b1bfb3..d6013ed2e 100644
--- a/src/db/events-db.h
+++ b/src/db/events-db.h
@@ -50,11 +50,11 @@ public:
int getEventsCount (FilterMask mask = NoFilter) const;
// Messages, calls and conferences.
- int getMessagesCount (const std::string &remoteAddress) const;
- int getUnreadMessagesCount (const std::string &remoteAddress) const;
+ int getMessagesCount (const std::string &remoteAddress = "") const;
+ int getUnreadMessagesCount (const std::string &remoteAddress = "") const;
std::list getHistory (const std::string &remoteAddress, int nLast, FilterMask mask = NoFilter) const;
std::list getHistory (const std::string &remoteAddress, int begin, int end, FilterMask mask = NoFilter) const;
- void cleanHistory (const std::string &remoteAddress);
+ void cleanHistory (const std::string &remoteAddress = "");
protected:
void init () override;