ATLAS Offline Software
Loading...
Searching...
No Matches
CoraCoolSequence.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3*/
4
5// CoraCoolSequence.cxx
6// implementation for CoraCoolSequence class
7// Richard Hawkings, started 24/10/06
8
9#include "CoralBase/AttributeList.h"
10#include "CoralBase/Attribute.h"
11#include "RelationalAccess/ISessionProxy.h"
12#include "RelationalAccess/ITransaction.h"
13#include "RelationalAccess/ITable.h"
14#include "RelationalAccess/TableDescription.h"
15#include "RelationalAccess/ITableDataEditor.h"
16#include "RelationalAccess/ISchema.h"
17#include "RelationalAccess/IQuery.h"
18#include "RelationalAccess/ICursor.h"
19#include "RelationalAccess/SchemaException.h"
21
22#include "CoraCoolSequence.h"
24#include <unistd.h>
25
26CoraCoolSequence::CoraCoolSequence(const std::string& dbname,
27 const std::string& seqname,
28 coral::ISessionProxy* proxy,
29 bool create) :
30 m_dbname(dbname), m_seqname(seqname), m_proxy(proxy), m_table(0) {
31
32 // derive key table name from CoraCool database name
33 const std::string keytblname=m_dbname+"_CORACOOLKEYS";
34 // check for existance of key table
35 try {
36 m_table=&(m_proxy->nominalSchema().tableHandle(keytblname));
37 }
38 catch (coral::SchemaException& e) {
39 // try to create the table if requested
40 if (create) {
41 coral::TableDescription tdesc(keytblname);
42 tdesc.setName(keytblname);
43 tdesc.insertColumn("SEQNAME","string",63,false);
44 tdesc.insertColumn("SEQVAL","int");
45 tdesc.setPrimaryKey("SEQNAME");
46 m_table=&(m_proxy->nominalSchema().createTable(tdesc));
47 // pause for thought
48 m_proxy->transaction().commit();
49 sleep(1);
50 m_proxy->transaction().start(false);
51 m_table=&(m_proxy->nominalSchema().tableHandle(keytblname));
52 }
53 }
54 // throw exception here? FIXME
55 if (m_table==0) throw CoraCoolException("Cannot create "+keytblname+" table",
56 "CoraCoolSequence::CoraCoolSequence");
57 // if not creating, finish here in any case - have pointer to table
58 if (!create) return;
59
60 int ifk;
61 if (!querySeq(ifk,false)) {
62 // update table with sequence starting at 0
63 coral::AttributeList data;
64 data.extend<std::string>("SEQNAME");
65 data.extend<int>("SEQVAL");
66 data[0].data<std::string>()=m_seqname;
67 data[1].data<int>()=0;
68 coral::ITableDataEditor& editor=m_table->dataEditor();
69 editor.insertRow(data);
70 }
71}
72
73int CoraCoolSequence::fetch(const int inc) {
74 int key=0;
75 // query sequence setting lock on row
76 if (querySeq(key,true)) {
77 // got result, now update rows
78 coral::AttributeList bindvar;
79 bindvar.extend<std::string>("SKEY");
80 bindvar[0].data<std::string>()=m_seqname;
81 bindvar.extend<int>("SINC");
82 bindvar[1].data<int>()=inc;
83 coral::ITableDataEditor& editor=m_table->dataEditor();
84 int rowsupdated=editor.updateRows("SEQVAL=SEQVAL+:SINC",
85 "SEQNAME=:SKEY",bindvar);
86 if (rowsupdated!=1) throw CoraCoolException(
87 "Unexpected number of rows locked in keytable",
88 "CoraCoolSequence::CoraCoolSequence");
89 } else {
90 throw CoraCoolException ("Problem generating next key value for "+m_seqname,
91 "CoraCoolSequence::fetch");
92 }
93 return key;
94}
95
96bool CoraCoolSequence::querySeq(int& keyval,bool update,bool gettable) {
97 if (gettable) {
98 const std::string keytblname=m_dbname+"_CORACOOLKEYS";
99 m_table=&(m_proxy->nominalSchema().tableHandle(keytblname));
100 }
101 coral::IQuery* query=m_table->newQuery();
102 coral::AttributeList bindvar;
103 bindvar.extend<std::string>("SKEY");
104 bindvar[0].data<std::string>()=m_seqname;
105 query->setCondition("SEQNAME=:SKEY",bindvar);
106 query->setRowCacheSize(2);
107 query->defineOutputType("SEQVAL","int");
108 if (update) query->setForUpdate();
109 coral::ICursor& cursor=query->execute();
110 bool res=true;
111 if (!cursor.next()) {
112 res=false;
113 } else {
114 const coral::AttributeList& res=cursor.currentRow();
115 keyval=res["SEQVAL"].data<int>();
116 }
117 if (cursor.next()) {
118 res=false;
119 }
120 delete query;
121 return res;
122}
123
125 coral::AttributeList bindvar;
126 bindvar.extend<std::string>("SKEY");
127 bindvar[0].data<std::string>()=m_seqname;
128 coral::ITableDataEditor& editor=m_table->dataEditor();
129 long rows=editor.deleteRows("SEQNAME=:SKEY",bindvar);
130 return (rows==1);
131}
char data[hepevt_bytes_allocation_ATLAS]
Definition HepEvt.cxx:11
std::pair< std::vector< unsigned int >, bool > res
Define macros for attributes used to control the static checker.
coral::ITable * m_table
bool querySeq(int &keyval, bool update=false, bool gettable=false)
coral::ISessionProxy * m_proxy
std::string m_seqname
int fetch(const int inc=1)
Definition query.py:1