21
21
22
22
#include " UserQuerySelectCountStar.h"
23
23
24
+ // Third-party headers
25
+ #include " boost/algorithm/string/replace.hpp"
24
26
#include " boost/lexical_cast.hpp"
25
27
26
28
// LSST headers
30
32
#include " cconfig/CzarConfig.h"
31
33
#include " ccontrol/UserQueryError.h"
32
34
#include " ccontrol/UserQueryType.h"
35
+ #include " global/LogContext.h"
33
36
#include " qdisp/MessageStore.h"
34
37
#include " qmeta/QInfo.h"
35
38
#include " qmeta/QMetaSelect.h"
40
43
41
44
namespace {
42
45
43
- std::string g_nextResultTableId (std::string const & userQueryId) {
44
- return " qserv_result_countstar_" + userQueryId;
45
- }
46
-
47
46
LOG_LOGGER _log = LOG_GET(" lsst.qserv.ccontrol.UserQuerySelectCountStar" );
48
47
49
48
} // end namespace
@@ -63,7 +62,6 @@ UserQuerySelectCountStar::UserQuerySelectCountStar(std::string query,
63
62
: _qMetaSelect(qMetaSelect),
64
63
_queryMetadata (queryMetadata),
65
64
_messageStore(std::make_shared<qdisp::MessageStore>()),
66
- _resultTableName(::g_nextResultTableId(userQueryId)),
67
65
_userQueryId(userQueryId),
68
66
_rowsTable(rowsTable),
69
67
_resultDb(resultDb),
@@ -124,7 +122,7 @@ void UserQuerySelectCountStar::submit() {
124
122
}
125
123
126
124
// Create a result table, with one column (row_count) and one row (the total number of rows):
127
- std::string createTable = " CREATE TABLE " + _resultTableName + " (row_count BIGINT UNSIGNED)" ;
125
+ std::string createTable = " CREATE TABLE " + _resultTable + " (row_count BIGINT UNSIGNED)" ;
128
126
LOGS (_log, LOG_LVL_DEBUG, " creating result table: " << createTable);
129
127
auto const czarConfig = cconfig::CzarConfig::instance ();
130
128
auto const resultDbConn = sql::SqlConnectionFactory::make (czarConfig->getMySqlResultConfig ());
@@ -137,7 +135,7 @@ void UserQuerySelectCountStar::submit() {
137
135
}
138
136
139
137
// Insert the total row count into the result table:
140
- std::string insertRow = " INSERT INTO " + _resultTableName + " VALUES (" ;
138
+ std::string insertRow = " INSERT INTO " + _resultTable + " VALUES (" ;
141
139
try {
142
140
insertRow += lexical_cast<std::string>(row_count);
143
141
} catch (bad_lexical_cast const & exc) {
@@ -171,13 +169,38 @@ void UserQuerySelectCountStar::qMetaRegister(std::string const& resultLocation,
171
169
std::string const & msgTableName) {
172
170
qmeta::QInfo::QType qType = _async ? qmeta::QInfo::ASYNC : qmeta::QInfo::SYNC;
173
171
std::string user = " anonymous" ; // we do not have access to that info yet
174
- std::string qTemplate = " template" ;
175
- std::string qMerge = " merge" ;
172
+ std::string qTemplate;
173
+ std::string qMerge;
174
+ if (_resultLoc.empty ()) {
175
+ // Special token #QID# is replaced with query ID later.
176
+ _resultLoc = " table:result_#QID#" ;
177
+ }
176
178
int chunkCount = 0 ;
177
- qmeta::QInfo qInfo (qType, _qMetaCzarId, user, _query, qTemplate, qMerge, getResultLocation (),
178
- msgTableName, getResultQuery (), chunkCount);
179
+ qmeta::QInfo qInfo (qType, _qMetaCzarId, user, _query, qTemplate, qMerge, _resultLoc, msgTableName,
180
+ getResultQuery (), chunkCount);
181
+ // register query, save its ID
179
182
qmeta::QMeta::TableNames tableNames;
180
183
_qMetaQueryId = _queryMetadata->registerQuery (qInfo, tableNames);
184
+ _queryIdStr = QueryIdHelper::makeIdStr (_qMetaQueryId);
185
+ // Add logging context with query ID
186
+ QSERV_LOGCONTEXT_QUERY (_qMetaQueryId);
187
+
188
+ // update #QID# with actual query ID
189
+ boost::replace_all (_resultLoc, " #QID#" , std::to_string (_qMetaQueryId));
190
+
191
+ // guess query result location
192
+ if (_resultLoc.compare (0 , 6 , " table:" ) == 0 ) {
193
+ _resultTable = _resultLoc.substr (6 );
194
+ } else {
195
+ // we only support results going to tables for now, abort for anything else
196
+ std::string const msg = " Unexpected result location '" + _resultLoc + " '" ;
197
+ _messageStore->addMessage (-1 , " SYSTEM" , 1146 , msg, MessageSeverity::MSG_ERROR);
198
+ throw UserQueryError (getQueryIdString ());
199
+ }
200
+ }
201
+
202
+ void UserQuerySelectCountStar::saveResultQuery () {
203
+ _queryMetadata->saveResultQuery (_qMetaQueryId, getResultQuery ());
181
204
}
182
205
183
206
QueryState UserQuerySelectCountStar::join () {
0 commit comments