Skip to content

Commit 21a8761

Browse files
committed
bugfix getting id after insert
1 parent 6d524e8 commit 21a8761

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

DbManager.cpp

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,15 @@ unsigned int DbManager::insertWord(std::string word)
9191
{
9292
try {
9393
pqxx::transaction<> tx{ *conn };
94-
pqxx::result r = tx.exec("insert into words (word)"
95-
"values ('" + tx.esc(word) + "') returning id;");
94+
pqxx::result r = tx.exec(
95+
"insert into words (word)"
96+
"values ('" + tx.esc(word) + "') returning id;"
97+
);
9698
tx.commit();
97-
return r.inserted_oid();
99+
if (!r.empty()) {
100+
return r[0][0].as<unsigned int>();
101+
}
102+
return 0;
98103

99104
} catch(std::exception const& e) {
100105
return 0;
@@ -105,10 +110,15 @@ unsigned int DbManager::insertUrl(std::string url)
105110
{
106111
try {
107112
pqxx::transaction<> tx{ *conn };
108-
pqxx::result r = tx.exec("insert into urls (url)"
109-
"values ('" + tx.esc(url) + "') RETURNING id;");
113+
pqxx::result r = tx.exec(
114+
"insert into urls (url)"
115+
"values ('" + tx.esc(url) + "') RETURNING id;"
116+
);
110117
tx.commit();
111-
return r.inserted_oid();
118+
if (!r.empty()) {
119+
return r[0][0].as<unsigned int>();
120+
}
121+
return 0;
112122

113123
} catch(std::exception const& e) {
114124
return 0;
@@ -118,29 +128,27 @@ unsigned int DbManager::insertUrl(std::string url)
118128
bool DbManager::insertPresence(WordPresence presence)
119129
{
120130
try {
121-
pqxx::transaction<> tx{ *conn };
122131
std::string word = presence.word;
123132
std::string url = presence.url;
124133
unsigned short frequency = presence.frequency;
125134

126135
// если слова нет в таблице слов, добавляем
127136
unsigned int wordId = getWordId(word);
128137
if (wordId == 0) {
129-
wordId = insertWord(word); // здесь почему-то всегда 0, потому получаем отдельным запросом ниже
130-
wordId = getWordId(word);
138+
wordId = insertWord(word);
131139
}
132140

133141
// если ресурса нет в таблице ресурсов, добавляем
134142
unsigned int urlId = getUrlId(url);
135143
if (urlId == 0) {
136-
urlId = insertWord(url); // здесь почему-то всегда 0, потому получаем отдельным запросом ниже
137-
urlId = getUrlId(url);
144+
urlId = insertUrl(url);
138145
}
139146

140147
std::string wordIdStr = std::to_string(wordId);
141148
std::string urlIdStr = std::to_string(urlId);
142149

143150
// добавляем новую частоту слова на ресурсе
151+
pqxx::transaction<> tx{ *conn };
144152
tx.exec("insert into frequencies (word_id, url_id, frequency)"
145153
"values ('" + tx.esc(wordIdStr) + "', '" + tx.esc(urlIdStr) + "', '" + tx.esc(std::to_string(frequency)) + "') RETURNING id;");
146154
tx.commit();

main.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,17 @@ int main()
3131

3232
// dbManager.insertWord("привет");
3333
// dbManager.insertWord("мир");
34-
// dbManager.insertWord("пока");
34+
// unsigned int id = dbManager.insertWord("пока3");
35+
// std::cout << id << std::endl;
3536
// dbManager.insertUrl("www.yandex.ru");
3637
// dbManager.insertUrl("www.google.com");
3738
// dbManager.insertUrl("www.yahoo.com");
3839

39-
unsigned int id = dbManager.getWordId("привет");
40-
std::cout << id << std::endl;
40+
// unsigned int id = dbManager.getWordId("привет");
41+
// std::cout << id << std::endl;
4142

42-
std::string url = "www.yandex.ru";
43-
std::string word = "привет";
43+
std::string url = "www.yandex3.ru";
44+
std::string word = "новое3";
4445
unsigned short frequency = 2;
4546
WordPresence presence = {word, url, frequency};
4647
dbManager.insertPresence(presence);

0 commit comments

Comments
 (0)