QPair<int, HistoryKeeper::ChatType> HistoryKeeper::getChatID(const QString &id_str, ChatType ct) { auto it = chats.find(id_str); if (it != chats.end()) return it.value(); db->exec(QString("INSERT INTO chats (name, ctype) VALUES ('%1', '%2');").arg(id_str).arg(ct)); updateChatsID(); return getChatID(id_str, ct); }
HistoryKeeper::HistoryKeeper(GenericDdInterface *db_) : db(db_) { /* DB format chats: * name -> id map id -- auto-incrementing number name -- chat's name (for user to user conversation it is opposite user public key) ctype -- chat type, reserved for group chats alisases: * user_id -> id map id -- auto-incrementing number name -- user's public key history: id -- auto-incrementing number timestamp chat_id -- current chat ID (resolves from chats table) sender -- sender's ID (resolves from aliases table) message */ // for old tables: QSqlQuery ans = db->exec("select seq from sqlite_sequence where name=\"history\";"); if (ans.first()) { int idMax = ans.value(0).toInt(); QSqlQuery ret = db->exec("select seq from sqlite_sequence where name=\"sent_status\";"); int idCur = 0; if (ret.first()) { idCur = ret.value(0).toInt(); } if (idCur != idMax) { QString cmd = QString("INSERT INTO sent_status (id, status) VALUES (%1, 1);").arg(idMax); db->exec(cmd); } } updateChatsID(); updateAliases(); setSyncType(Settings::getInstance().getDbSyncType()); messageID = 0; QSqlQuery sqlAnswer = db->exec("select seq from sqlite_sequence where name=\"history\";"); if (sqlAnswer.first()) messageID = sqlAnswer.value(0).toLongLong(); }
HistoryKeeper::HistoryKeeper(GenericDdInterface *db_) : db(db_) { /* DB format chats: * name -> id map id -- auto-incrementing number name -- chat's name (for user to user conversation it is opposite user public key) ctype -- chat type, reserved for group chats aliases: * user_id -> id map id -- auto-incrementing number user_id -- user's public key av_hash -- hash of user's avatar avatar -- user's avatar history: id -- auto-incrementing number timestamp chat_id -- current chat ID (resolves from chats table) sender -- sender's ID (resolves from aliases table) message alias -- sender's alias in */ // for old tables: QSqlQuery ans = db->exec("SELECT seq FROM sqlite_sequence WHERE name=\"history\";"); if (ans.first()) { int idMax = ans.value(0).toInt(); QSqlQuery ret = db->exec("SELECT seq FROM sqlite_sequence WHERE name=\"sent_status\";"); int idCur = 0; if (ret.first()) idCur = ret.value(0).toInt(); if (idCur != idMax) { QString cmd = QString("INSERT INTO sent_status (id, status) VALUES (%1, 1);").arg(idMax); db->exec(cmd); } } //check table stuct ans = db->exec("PRAGMA table_info (\"history\")"); ans.seek(5); if (!ans.value(1).toString().contains("alias")) { //add collum in table db->exec("ALTER TABLE history ADD COLUMN alias TEXT"); qDebug() << "Struct DB updated: Added column alias in table history."; } ans.clear(); ans = db->exec("PRAGMA table_info('aliases')"); ans.seek(2); if (!ans.value(1).toString().contains("av_hash")) { //add collum in table db->exec("ALTER TABLE aliases ADD COLUMN av_hash BLOB"); qDebug() << "Struct DB updated: Added column av_hash in table aliases."; } ans.seek(3); if (!ans.value(1).toString().contains("avatar")) { //add collum in table needImport = true; db->exec("ALTER TABLE aliases ADD COLUMN avatar BLOB"); qDebug() << "Struct DB updated: Added column avatar in table aliases."; } updateChatsID(); updateAliases(); setSyncType(Settings::getInstance().getDbSyncType()); messageID = 0; QSqlQuery sqlAnswer = db->exec("SELECT seq FROM sqlite_sequence WHERE name=\"history\";"); if (sqlAnswer.first()) messageID = sqlAnswer.value(0).toLongLong(); }