2121
2222#include " UserQuerySelectCountStar.h"
2323
24+ // Third-party headers
25+ #include " boost/algorithm/string/replace.hpp"
2426#include " boost/lexical_cast.hpp"
2527
2628// LSST headers
3032#include " cconfig/CzarConfig.h"
3133#include " ccontrol/UserQueryError.h"
3234#include " ccontrol/UserQueryType.h"
35+ #include " global/LogContext.h"
3336#include " qdisp/MessageStore.h"
3437#include " qmeta/QInfo.h"
3538#include " qmeta/QMetaSelect.h"
4043
4144namespace {
4245
43- std::string g_nextResultTableId (std::string const & userQueryId) {
44- return " qserv_result_countstar_" + userQueryId;
45- }
46-
4746LOG_LOGGER _log = LOG_GET(" lsst.qserv.ccontrol.UserQuerySelectCountStar" );
4847
4948} // end namespace
@@ -63,7 +62,6 @@ UserQuerySelectCountStar::UserQuerySelectCountStar(std::string query,
6362 : _qMetaSelect(qMetaSelect),
6463 _queryMetadata (queryMetadata),
6564 _messageStore(std::make_shared<qdisp::MessageStore>()),
66- _resultTableName(::g_nextResultTableId(userQueryId)),
6765 _userQueryId(userQueryId),
6866 _rowsTable(rowsTable),
6967 _resultDb(resultDb),
@@ -124,7 +122,7 @@ void UserQuerySelectCountStar::submit() {
124122 }
125123
126124 // 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)" ;
128126 LOGS (_log, LOG_LVL_DEBUG, " creating result table: " << createTable);
129127 auto const czarConfig = cconfig::CzarConfig::instance ();
130128 auto const resultDbConn = sql::SqlConnectionFactory::make (czarConfig->getMySqlResultConfig ());
@@ -137,7 +135,7 @@ void UserQuerySelectCountStar::submit() {
137135 }
138136
139137 // 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 (" ;
141139 try {
142140 insertRow += lexical_cast<std::string>(row_count);
143141 } catch (bad_lexical_cast const & exc) {
@@ -171,13 +169,38 @@ void UserQuerySelectCountStar::qMetaRegister(std::string const& resultLocation,
171169 std::string const & msgTableName) {
172170 qmeta::QInfo::QType qType = _async ? qmeta::QInfo::ASYNC : qmeta::QInfo::SYNC;
173171 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+ }
176178 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
179182 qmeta::QMeta::TableNames tableNames;
180183 _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 ());
181204}
182205
183206QueryState UserQuerySelectCountStar::join () {
0 commit comments