@@ -91,10 +91,15 @@ unsigned int DbManager::insertWord(std::string word)
91
91
{
92
92
try {
93
93
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
+ );
96
98
tx.commit ();
97
- return r.inserted_oid ();
99
+ if (!r.empty ()) {
100
+ return r[0 ][0 ].as <unsigned int >();
101
+ }
102
+ return 0 ;
98
103
99
104
} catch (std::exception const & e) {
100
105
return 0 ;
@@ -105,10 +110,15 @@ unsigned int DbManager::insertUrl(std::string url)
105
110
{
106
111
try {
107
112
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
+ );
110
117
tx.commit ();
111
- return r.inserted_oid ();
118
+ if (!r.empty ()) {
119
+ return r[0 ][0 ].as <unsigned int >();
120
+ }
121
+ return 0 ;
112
122
113
123
} catch (std::exception const & e) {
114
124
return 0 ;
@@ -118,29 +128,27 @@ unsigned int DbManager::insertUrl(std::string url)
118
128
bool DbManager::insertPresence (WordPresence presence)
119
129
{
120
130
try {
121
- pqxx::transaction<> tx{ *conn };
122
131
std::string word = presence.word ;
123
132
std::string url = presence.url ;
124
133
unsigned short frequency = presence.frequency ;
125
134
126
135
// если слова нет в таблице слов, добавляем
127
136
unsigned int wordId = getWordId (word);
128
137
if (wordId == 0 ) {
129
- wordId = insertWord (word); // здесь почему-то всегда 0, потому получаем отдельным запросом ниже
130
- wordId = getWordId (word);
138
+ wordId = insertWord (word);
131
139
}
132
140
133
141
// если ресурса нет в таблице ресурсов, добавляем
134
142
unsigned int urlId = getUrlId (url);
135
143
if (urlId == 0 ) {
136
- urlId = insertWord (url); // здесь почему-то всегда 0, потому получаем отдельным запросом ниже
137
- urlId = getUrlId (url);
144
+ urlId = insertUrl (url);
138
145
}
139
146
140
147
std::string wordIdStr = std::to_string (wordId);
141
148
std::string urlIdStr = std::to_string (urlId);
142
149
143
150
// добавляем новую частоту слова на ресурсе
151
+ pqxx::transaction<> tx{ *conn };
144
152
tx.exec (" insert into frequencies (word_id, url_id, frequency)"
145
153
" values ('" + tx.esc (wordIdStr) + " ', '" + tx.esc (urlIdStr) + " ', '" + tx.esc (std::to_string (frequency)) + " ') RETURNING id;" );
146
154
tx.commit ();
0 commit comments