9#include "CoralBase/Exception.h"
10#include "CoralBase/Blob.h"
12#include "RelationalAccess/ConnectionService.h"
13#include "RelationalAccess/IConnectionServiceConfiguration.h"
14#include "RelationalAccess/ISessionProxy.h"
15#include "RelationalAccess/ISchema.h"
17#include "CrestApi/CrestApiBase.h"
18#include "CrestApi/CrestRequest.h"
20#include "boost/property_tree/ptree.hpp"
24using ptree = boost::property_tree::ptree;
40 static const std::string versionTagPrefix(
"Trigger-Run3-Schema-v");
43 if(! session->nominalSchema().existsTable(
"TRIGGER_SCHEMA") ) {
44 throw std::runtime_error(
"Trigger schema has no schema version table" );
54 auto & cursor =
query->execute();
55 if ( ! cursor.next() ) {
56 throw std::runtime_error(
"Trigger schema has schema version table but it is empty" );
59 const coral::AttributeList& row = cursor.currentRow();
60 std::string versionTag = row[
"TS_TAG"].data<std::string>();
61 if( ! versionTag.starts_with(versionTagPrefix)) {
62 throw std::runtime_error(std::format(
"Tag format error: Trigger schema version tag {} does not start with {}", versionTag, versionTagPrefix));
65 std::string vstr = versionTag.substr(versionTagPrefix.size());
70 catch (
const std::invalid_argument& ia) {
71 TRG_MSG_ERROR(
"Invalid argument when interpreting the version part " << vstr <<
" of schema tag " << versionTag <<
". " << ia.what());
106std::unique_ptr<coral::ISessionProxy>
109 coral::ConnectionService connSvc;
110 coral::IConnectionServiceConfiguration& csc = connSvc.configuration();
125 auto proxy = std::unique_ptr<coral::ISessionProxy>( connSvc.connect(
m_connection, coral::AccessMode::ReadOnly) );
155 Crest::CrestApi capi = Crest::CrestApi(url);
156 std::string payload = capi.getPayload(
query);
163 Crest::CrestRequest request = Crest::CrestRequest();
165 std::string current_path =
"payloads/data?format=BLOB&hash=" +
query;
166 nlohmann::json js =
nullptr;
167 std::string payload = request.performRequest(current_path, Crest::Action::GET, js,
"TrigDbLoader");
175 const std::map<size_t, TrigConf::QueryDefinition> & queries)
const
178 size_t maxDefVersion = 0;
179 for(
auto & entry : queries) {
180 size_t vkey = entry.first;
182 maxDefVersion = vkey;
186 if( maxDefVersion==0 ) {
190 return queries.at(maxDefVersion);
195 const std::string & outFileName,
const std::string &
description,
196 const std::string & query_type)
const
202 catch(Crest::CrestException & ex) {
203 TRG_MSG_ERROR(
"When reading " <<
description <<
" for key " << key <<
" from crest a CrestException was caught ( " << ex.what() <<
" )" );
206 if(!outFileName.empty()) {
213 catch(boost::property_tree::json_parser_error & ex) {
214 TRG_MSG_ERROR(
"When reading " <<
description <<
" for key " << key <<
" from crest a ptree json parser error was caught ( " << ex.what() <<
" )" );
221 const std::string & outFileName,
const std::string &
description,
222 const std::map<size_t, QueryDefinition> & queries)
const
225 session->transaction().start(
true);
231 auto & cursor = q->execute();
232 if ( ! cursor.next() ) {
236 const coral::AttributeList& row = cursor.currentRow();
237 const coral::Blob& dataBlob = row[qdef.
dataName()].data<coral::Blob>();
239 if(!outFileName.empty()) {
245 catch(coral::QueryException & ex) {
246 TRG_MSG_ERROR(
"When reading " <<
description <<
" for key " << key <<
" a coral::QueryException was caught ( " << ex.what() <<
" )" );
249 catch(boost::property_tree::json_parser_error & ex) {
250 TRG_MSG_ERROR(
"When reading " <<
description <<
" for key " << key <<
" a ptree json parser error was caught ( " << ex.what() <<
" )" );
boost::property_tree::ptree ptree
Loader class for Trigger configuration from the Trigger DB.
std::unique_ptr< coral::IQuery > createQuery(coral::ISessionProxy *session)
const std::string & dataName()
void setBoundValue(const std::string &fieldName, const T &value)
void extendOutput(const std::string &fieldName)
void addToTableList(const std::string &table, const std::string &table_short="")
const std::string & getName() const
name accessor
TrigConfMessaging(const std::string &name)
Constructor with parameters.
std::unique_ptr< coral::ISessionProxy > createDBSession() const
create (if needed) DB session and return the session proxy
QueryDefinition getQueryDefinition(size_t schemaVersion, const std::map< size_t, QueryDefinition > &queries) const
return query for given schemaVersion from possible queries
std::string getTrigDataCrest(const std::string &type, int key) const
Get trigger configuration from the TriggerDB through Crest.
std::string m_crestVersion
TrigDBLoader(const std::string &loaderName, const std::string &connection)
Constructor.
std::string m_crestTrigDb
std::string m_crestServer
void setCrestTrigDB(const std::string &crestTrigDB)
set trigger db for the crest connection
virtual ~TrigDBLoader()
Destructor - cannot be defined here because QueryDefinition is an incomplete type.
void loadFromOracle(unsigned int key, boost::property_tree::ptree &pt, const std::string &outFileName, const std::string &description, const std::map< size_t, QueryDefinition > &queries) const
size_t schemaVersion(coral::ISessionProxy *session) const
access to TriggerDB schema version
void setCrestConnection(const std::string &server, const std::string &version="")
declare CREST as the source of the configuration An empty crest server makes it use Oracle
void loadFromCrest(unsigned int key, boost::property_tree::ptree &pt, const std::string &outFileName, const std::string &description, const std::string &query_type) const
std::string description
glabal timer - how long have I taken so far?
void stringToPtree(const std::string &json_string, boost::property_tree::ptree &pt)
void writeRawFile(const coral::Blob &data, const std::string &outFileName)
write coral data blob to file
void blobToPtree(const coral::Blob &blob, boost::property_tree::ptree &pt)