9 #include "RelationalAccess/ISchema.h"
10 #include "RelationalAccess/ITable.h"
11 #include "RelationalAccess/ITableDataEditor.h"
12 #include "RelationalAccess/IQuery.h"
13 #include "RelationalAccess/ICursor.h"
14 #include "RelationalAccess/TableDescription.h"
15 #include "RelationalAccess/ITablePrivilegeManager.h"
16 #include "CoralBase/Attribute.h"
17 #include "CoralBase/AttributeList.h"
18 #include "CoralBase/AttributeSpecification.h"
20 #include <boost/date_time/posix_time/posix_time_types.hpp>
34 m_sessionHandle(sessionHandle),
36 m_statusEditorHandle(0),
38 m_logTableExists(
false){
44 m_sessionHandle.open( logConnectionString, role, readOnly );
49 if(m_logTableExists)
return;
54 m_logTableExists=
true;
67 coral::AttributeSpecification::typeNameForType<unsigned long long>() );
68 description.setPrimaryKey( std::vector<std::string>( 1,
std::string(
"LOGID")));
70 coral::AttributeSpecification::typeNameForType<std::string>() );
71 description.setNotNullConstraint(
std::string(
"EXECTIME"));
74 coral::AttributeSpecification::typeNameForType<std::string>() );
75 description.setNotNullConstraint(
std::string(
"IOVTAG"));
77 description.insertColumn(
std::string(
"IOVTIMETYPE"),
78 coral::AttributeSpecification::typeNameForType<std::string>() );
79 description.setNotNullConstraint(
std::string(
"IOVTIMETYPE"));
81 description.insertColumn(
std::string(
"PAYLOADCLASS"),
82 coral::AttributeSpecification::typeNameForType<std::string>() );
83 description.setNotNullConstraint(
std::string(
"PAYLOADCLASS"));
85 description.insertColumn(
std::string(
"PAYLOADTOKEN"),
86 coral::AttributeSpecification::typeNameForType<std::string>() );
87 description.setNotNullConstraint(
std::string(
"PAYLOADTOKEN"));
89 description.insertColumn(
std::string(
"PAYLOADINDEX"),
90 coral::AttributeSpecification::typeNameForType<unsigned int>() );
91 description.setNotNullConstraint(
std::string(
"PAYLOADINDEX"));
94 coral::AttributeSpecification::typeNameForType<unsigned long long>() );
95 description.setNotNullConstraint(
std::string(
"LASTSINCE"));
97 description.insertColumn(
std::string(
"DESTINATIONDB"),
98 coral::AttributeSpecification::typeNameForType<std::string>() );
99 description.setNotNullConstraint(
std::string(
"DESTINATIONDB"));
101 description.insertColumn(
std::string(
"PROVENANCE"),
102 coral::AttributeSpecification::typeNameForType<std::string>() );
104 coral::AttributeSpecification::typeNameForType<std::string>() );
105 description.insertColumn(
std::string(
"EXECMESSAGE"),
106 coral::AttributeSpecification::typeNameForType<std::string>() );
107 m_sessionHandle.nominalSchema().createTable( description );
108 m_logTableExists=
true;
119 unsigned int payloadIdx,
120 unsigned long long lastSince
126 boost::posix_time::ptime
p=boost::posix_time::microsec_clock::universal_time();
129 if(!m_sequenceManager){
134 this->insertLogRecord(targetLogId,now,destDB,payloadClass,payloadToken,userlogInfo,iovtag,iovtimetype,payloadIdx,lastSince,
"OK");
145 unsigned int payloadIdx,
146 unsigned long long lastSince,
152 boost::posix_time::ptime
p=boost::posix_time::microsec_clock::universal_time();
155 if(!m_sequenceManager){
160 this->insertLogRecord(targetLogId,now,destDB,payloadClass,payloadToken,userlogInfo,iovtag,iovtimetype,payloadIdx,lastSince,exceptionMessage);
167 bool filterFailedOp)
const{
170 whereClause+=
".PROVENANCE=:provenance";
174 whereClause+=
std::string(
".EXECMESSAGE=:execmessage");
176 coral::AttributeList BindVariableList;
177 BindVariableList.extend(
"provenance",
typeid(
std::string) );
178 BindVariableList.extend(
"execmessage",
typeid(
std::string) );
179 BindVariableList[
"provenance"].data<
std::string>()=provenance;
180 BindVariableList[
"execmessage"].data<
std::string>()=
"OK";
181 m_sessionHandle.transaction().start(
true);
185 query->addToOutputList(
"LOGID" );
186 query->defineOutputType(
"LOGID",
"unsigned long long" );
187 query->addToOutputList(
"DESTINATIONDB" );
188 query->addToOutputList(
"PROVENANCE" );
189 query->addToOutputList(
"USERTEXT" );
190 query->addToOutputList(
"IOVTAG" );
191 query->addToOutputList(
"IOVTIMETYPE" );
192 query->addToOutputList(
"PAYLOADINDEX" );
193 query->defineOutputType(
"PAYLOADINDEX",
"unsigned int" );
194 query->addToOutputList(
"LASTSINCE" );
195 query->defineOutputType(
"LASTSINCE",
"unsigned long long" );
196 query->addToOutputList(
"PAYLOADCLASS" );
197 query->addToOutputList(
"PAYLOADTOKEN" );
198 query->addToOutputList(
"EXECTIME" );
199 query->addToOutputList(
"EXECMESSAGE" );
201 query->setCondition( whereClause, BindVariableList );
203 query->limitReturnedRows();
204 coral::ICursor& cursor =
query->execute();
205 if( cursor.next() ) {
206 const coral::AttributeList& row = cursor.currentRow();
207 logentry.
logId=row[
"LOGID"].data<
unsigned long long>();
213 logentry.
payloadIdx=row[
"PAYLOADINDEX"].data<
unsigned int>();
214 logentry.
lastSince=row[
"LASTSINCE"].data<
unsigned long long>();
221 m_sessionHandle.transaction().commit();
227 bool filterFailedOp)
const{
228 coral::AttributeList BindVariableList;
229 BindVariableList.extend(
"IOVTAG",
typeid(
std::string) );
230 BindVariableList[
"IOVTAG"].data<
std::string>()=iovtag;
233 if(connectionStr!=
""){
235 whereClause+=
std::string(
"DESTINATIONDB=:DESTINATIONDB");
236 BindVariableList.extend(
"DESTINATIONDB",
typeid(
std::string) );
237 BindVariableList[
"DESTINATIONDB"].data<
std::string>()=connectionStr;
241 whereClause+=
std::string(
"EXECMESSAGE=:EXECMESSAGE");
242 BindVariableList.extend(
"EXECMESSAGE",
typeid(
std::string) );
243 BindVariableList[
"EXECMESSAGE"].data<
std::string>()=
"OK";
245 m_sessionHandle.transaction().start(
true);
249 query->addToOutputList(
"LOGID" );
250 query->defineOutputType(
"LOGID",
"unsigned long long" );
251 query->addToOutputList(
"DESTINATIONDB" );
252 query->addToOutputList(
"PROVENANCE" );
253 query->addToOutputList(
"USERTEXT" );
254 query->addToOutputList(
"IOVTAG" );
255 query->addToOutputList(
"IOVTIMETYPE" );
256 query->addToOutputList(
"PAYLOADINDEX" );
257 query->defineOutputType(
"PAYLOADINDEX",
"unsigned int" );
258 query->addToOutputList(
"LASTSINCE" );
259 query->defineOutputType(
"LASTSINCE",
"unsigned long long" );
260 query->addToOutputList(
"PAYLOADCLASS" );
261 query->addToOutputList(
"PAYLOADTOKEN" );
262 query->addToOutputList(
"EXECTIME" );
263 query->addToOutputList(
"EXECMESSAGE" );
265 query->setCondition( whereClause, BindVariableList );
266 query->addToOrderList(
"LOGID DESC" );
267 query->limitReturnedRows();
268 coral::ICursor& cursor =
query->execute();
269 if( cursor.next() ) {
270 const coral::AttributeList& row = cursor.currentRow();
271 logentry.
logId=row[
"LOGID"].data<
unsigned long long>();
277 logentry.
payloadIdx=row[
"PAYLOADINDEX"].data<
unsigned int>();
278 logentry.
lastSince=row[
"LASTSINCE"].data<
unsigned long long>();
286 m_sessionHandle.transaction().commit();
291 bool filterFailedOp )
const{
292 LookupLastEntryByTag(iovtag,
"",logentry,filterFailedOp);
303 unsigned int payloadIdx,
304 unsigned long long lastSince,
307 coral::AttributeList rowData;
308 rowData.extend<
unsigned long long>(
"LOGID");
317 rowData.extend<
unsigned int>(
"PAYLOADINDEX");
318 rowData.extend<
unsigned long long>(
"LASTSINCE");
320 rowData[
"LOGID"].data<
unsigned long long >() = logId;
321 rowData[
"EXECTIME"].data<
std::string >() = utctime;
322 rowData[
"DESTINATIONDB"].data<
std::string >() = destDB;
323 rowData[
"PAYLOADCLASS"].data<
std::string >() = payloadClass;
325 if( payloadToken.empty() ) ptok =
"NA";
326 rowData[
"PAYLOADTOKEN"].data<
std::string >() = ptok;
330 rowData[
"IOVTIMETYPE"].data<
std::string >() = iovtimetype;
331 rowData[
"PAYLOADINDEX"].data<
unsigned int >() = payloadIdx;
332 rowData[
"LASTSINCE"].data<
unsigned long long >() = lastSince;
333 rowData[
"EXECMESSAGE"].data<
std::string >() = exceptionMessage;
341 if( m_sequenceManager ){
342 delete m_sequenceManager;
void logFailedOperationNow(const cond::UserLogInfo &userlogInfo, const std::string &destDB, const std::string &payloadToken, const std::string &payloadClass, const std::string &iovtag, const std::string &iovtimetype, unsigned int payloadIdx, unsigned long long lastSince, const std::string &exceptionMessage)
void connect(const std::string &logConnectionString, bool readOnly=false)
void LookupLastEntryByProvenance(const std::string &provenance, LogDBEntry &logentry, bool filterFailedOp=true) const
bool existSequencesTable()
Whether sequence table exists.
std::string destinationDB
std::string to_string(const T &t)
void createSequencesTable()
Creates the table holding the sequences.
unsigned long long lastSince
static const std::string COND_WRITER_ROLE
int start(bool readOnly=false)
start transaction
void logOperationNow(const cond::UserLogInfo &userlogInfo, const std::string &destDB, const std::string &payloadToken, const std::string &payloadClass, const std::string &iovtag, const std::string &iovtimetype, unsigned int payloadIdx, unsigned long long lastSince)
static const std::string COND_READER_ROLE
void createLogDBIfNonExist()
static std::string SequenceTableName()
void LookupLastEntryByTag(const std::string &iovtag, LogDBEntry &logentry, bool filterFailedOp=true) const
volatile std::atomic< bool > shutdown_flag false
Logger(DbSession &sessionHandle)
int commit()
commit transaction. Will disconnect from database if connection timeout==0 or connectted time close t...
void insertLogRecord(unsigned long long logId, const std::string &utctime, const std::string &destDB, const std::string &payloadToken, const std::string &payloadClass, const cond::UserLogInfo &userLogInfo, const std::string &iovtag, const std::string &iovtimetype, unsigned int payloadIdx, unsigned long long lastSince, const std::string &exceptionMessage)
static std::string LogTableName()