Skip to content

Commit 2a17a2a

Browse files
committed
bugfix getting data from db
1 parent 21a8761 commit 2a17a2a

File tree

2 files changed

+30
-13
lines changed

2 files changed

+30
-13
lines changed

DbManager.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ std::vector<int> DbManager::getUrlsIdsByWord(std::string word)
197197
std::string wordIdStr = std::to_string(wordId);
198198

199199
pqxx::work tx{ *conn };
200-
for (auto [id] : tx.query<int>("select id from frequencies " "where word_id = '" + tx.esc(wordIdStr) + "';")) {
200+
for (auto [id] : tx.query<int>("select url_id from frequencies " "where word_id = '" + tx.esc(wordIdStr) + "';")) {
201201
urlIds.push_back(id);
202202
}
203203
return urlIds;
@@ -209,7 +209,7 @@ std::vector<int> DbManager::getUrlsIdsByWords(std::vector<std::string> words)
209209
std::vector<int> urlIdsAccepted;
210210
std::vector<int> word_ids = getWordsIds(words);
211211
pqxx::work tx{ *conn };
212-
for (auto [urlIdd] : tx.query<int>("select url_id from frequencies " "where word_id in '" + getStringFromVector(word_ids) + "';")) {
212+
for (auto [urlIdd] : tx.query<int>("select url_id from frequencies " "where word_id in (" + getStringFromVector(word_ids) + ");")) {
213213
urlIds.push_back(urlIdd);
214214
}
215215

@@ -230,12 +230,12 @@ std::vector<std::string> DbManager::getSortedUrlsByWords(std::vector<std::string
230230

231231
std::vector<std::string> sortedUrls;
232232
pqxx::work tx{ *conn };
233-
for (auto& [url, freq] : tx.query<std::string, int>("select u.url, sum(f.frequency) sum_freq from frequencies f"
234-
"join words w on f.word_id = w.id"
235-
"join urls u on f.url_id = u.id"
236-
"where word_id in (" + getStringFromVector(word_ids) + ")"
237-
"and url_id in (" + getStringFromVector(url_ids) + ")"
238-
"group by url"
233+
for (auto& [url, freq] : tx.query<std::string, int>("select u.url, sum(f.frequency) sum_freq from frequencies f "
234+
"join words w on f.word_id = w.id "
235+
"join urls u on f.url_id = u.id "
236+
"where word_id in (" + getStringFromVector(word_ids) + ") "
237+
"and url_id in (" + getStringFromVector(url_ids) + ") "
238+
"group by url "
239239
"order by sum_freq DESC")) {
240240
sortedUrls.push_back(url);
241241
}

main.cpp

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,28 @@ int main()
4040
// unsigned int id = dbManager.getWordId("привет");
4141
// std::cout << id << std::endl;
4242

43-
std::string url = "www.yandex3.ru";
44-
std::string word = "новое3";
45-
unsigned short frequency = 2;
46-
WordPresence presence = {word, url, frequency};
47-
dbManager.insertPresence(presence);
43+
// std::string url = "www.yahoo.com";
44+
// std::string word = "мир";
45+
// unsigned short frequency = 1;
46+
// WordPresence presence = {word, url, frequency};
47+
// dbManager.insertPresence(presence);
48+
49+
// std::vector<int> urlIds = dbManager.getUrlsIdsByWord("привет");
50+
// for (auto& i : urlIds) {
51+
// std::cout << i << std::endl;
52+
// }
53+
54+
std::vector<std::string> words;
55+
words.push_back("привет");
56+
words.push_back("мир");
57+
// std::vector<int> urlIds = dbManager.getUrlsIdsByWords(words);
58+
// for (auto& i : urlIds) {
59+
// std::cout << i << std::endl;
60+
// }
61+
std::vector<std::string> urls = dbManager.getSortedUrlsByWords(words);
62+
for (auto& i : urls) {
63+
std::cout << i << std::endl;
64+
}
4865

4966
return 0;
5067
}

0 commit comments

Comments
 (0)