ATLAS Offline Software
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 
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
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 
73 int 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 
96 bool 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 }
CaloNoise_fillDB.dbname
dbname
Definition: CaloNoise_fillDB.py:43
data
char data[hepevt_bytes_allocation_ATLAS]
Definition: HepEvt.cxx:11
StateLessPT_NewConfig.proxy
proxy
Definition: StateLessPT_NewConfig.py:392
CoraCoolSequence::querySeq
bool querySeq(int &keyval, bool update=false, bool gettable=false)
Definition: CoraCoolSequence.cxx:96
CoraCoolSequence::dropSeq
bool dropSeq()
Definition: CoraCoolSequence.cxx:124
python.PyKernel.AttributeList
AttributeList
Definition: PyKernel.py:36
query
Definition: query.py:1
CoraCoolSequence::fetch
int fetch(const int inc=1)
Definition: CoraCoolSequence.cxx:73
CoraCoolSequence::m_dbname
std::string m_dbname
Definition: CoraCoolSequence.h:40
res
std::pair< std::vector< unsigned int >, bool > res
Definition: JetGroupProductTest.cxx:14
beamspotnt.rows
list rows
Definition: bin/beamspotnt.py:1112
query_example.query
query
Definition: query_example.py:15
CoraCoolSequence::m_proxy
coral::ISessionProxy * m_proxy
Definition: CoraCoolSequence.h:42
CoraCoolSequence::m_seqname
std::string m_seqname
Definition: CoraCoolSequence.h:41
CoraCoolSequence::m_table
coral::ITable * m_table
Definition: CoraCoolSequence.h:43
dqt_zlumi_pandas.update
update
Definition: dqt_zlumi_pandas.py:42
CoraCoolException
Definition: CoraCoolException.h:13
DiTauMassTools::MaxHistStrategyV2::e
e
Definition: PhysicsAnalysis/TauID/DiTauMassTools/DiTauMassTools/HelperFunctions.h:26
query_example.cursor
cursor
Definition: query_example.py:21
CoraCoolSequence::CoraCoolSequence
CoraCoolSequence()
CoraCoolSequence.h
checker_macros.h
Define macros for attributes used to control the static checker.
CoraCoolException.h
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37