test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ODScanConfig.cc
Go to the documentation of this file.
1 #include <stdexcept>
2 #include <cstdlib>
3 #include <string>
5 
7 
8 using namespace std;
9 using namespace oracle::occi;
10 
12 {
13  m_env = NULL;
14  m_conn = NULL;
15  m_writeStmt = NULL;
16  m_readStmt = NULL;
17  m_config_tag="";
18  m_ID=0;
19  clear();
20 }
21 
22 
23 
25 {
26 }
27 
29 
30  m_type_id=0;
31  m_type="";
32  m_from_val=0;
33  m_to_val=0;
34  m_step=0;
35 
36 }
37 
38 int ODScanConfig::fetchNextId() throw(std::runtime_error) {
39 
40  int result=0;
41  try {
42  this->checkConnection();
43 
44  m_readStmt = m_conn->createStatement();
45  m_readStmt->setSQL("select ecal_scan_config_sq.NextVal from dual");
46  ResultSet* rset = m_readStmt->executeQuery();
47  while (rset->next ()){
48  result= rset->getInt(1);
49  }
50  m_conn->terminateStatement(m_readStmt);
51  return result;
52 
53  } catch (SQLException &e) {
54  throw(std::runtime_error("ODScanConfig::fetchNextId(): "+e.getMessage()));
55  }
56 
57 }
58 
59 
60 void ODScanConfig::setParameters(const std::map<string,string>& my_keys_map){
61 
62  // parses the result of the XML parser that is a map of
63  // string string with variable name variable value
64 
65  for( std::map<std::string, std::string >::const_iterator ci=
66  my_keys_map.begin(); ci!=my_keys_map.end(); ci++ ) {
67 
68  if(ci->first== "SCAN_ID") setConfigTag(ci->second);
69  if(ci->first== "TYPE_ID") setTypeId(atoi(ci->second.c_str()) );
70  if(ci->first== "TYPE" || ci->first== "SCAN_TYPE") setScanType(ci->second.c_str() );
71  if(ci->first== "FROM" ||ci->first== "FROM_VAL" ) setFromVal(atoi(ci->second.c_str() ));
72  if(ci->first== "TO" ||ci->first== "TO_VAL" ) setToVal(atoi(ci->second.c_str() ));
73  if(ci->first== "STEP") setStep(atoi(ci->second.c_str() ));
74 
75  }
76 
77 }
78 
80  throw(std::runtime_error)
81 {
82  this->checkConnection();
83  int next_id=fetchNextId();
84 
85  try {
86  m_writeStmt = m_conn->createStatement();
87  m_writeStmt->setSQL("INSERT INTO ECAL_scan_dat ( scan_id, scan_tag ,"
88  " type_id, scan_type , FROM_VAL , TO_VAL, STEP )"
89  " VALUES ( :1, :2, :3, :4, :5, :6, :7)");
90  m_writeStmt->setInt(1, next_id);
91  m_ID=next_id;
92 
93  } catch (SQLException &e) {
94  throw(std::runtime_error("ODScanConfig::prepareWrite(): "+e.getMessage()));
95  }
96 }
97 
99  throw(std::runtime_error)
100 {
101  this->checkConnection();
102  this->checkPrepare();
103 
104  try {
105 
106  // number 1 is the id
107  m_writeStmt->setString(2, this->getConfigTag());
108 
109  m_writeStmt->setInt(3, this->getTypeId());
110  m_writeStmt->setString(4, this->getScanType() );
111  m_writeStmt->setInt(5, this->getFromVal() );
112  m_writeStmt->setInt(6, this->getToVal() );
113  m_writeStmt->setInt(7, this->getStep() );
114 
115  m_writeStmt->executeUpdate();
116 
117 
118  } catch (SQLException &e) {
119  throw(std::runtime_error("ODScanConfig::writeDB(): "+e.getMessage()));
120  }
121  // Now get the ID
122  if (!this->fetchID()) {
123  throw(std::runtime_error("ODScanConfig::writeDB: Failed to write"));
124  }
125 
126 }
127 
128 
129 
130 
132  throw(std::runtime_error)
133 {
134  this->checkConnection();
135  result->clear();
136  if(result->getId()==0 && (result->getConfigTag()=="") ){
137  throw(std::runtime_error("ODScanConfig::fetchData(): no Id defined for this ODScanConfig "));
138  }
139 
140  try {
141 
142  m_readStmt->setSQL("SELECT * "
143  "FROM ECAL_SCAN_DAT "
144  " where (scan_id = :1 or scan_tag=:2 )" );
145  m_readStmt->setInt(1, result->getId());
146  m_readStmt->setString(2, result->getConfigTag());
147 
148  ResultSet* rset = m_readStmt->executeQuery();
149 
150  rset->next();
151 
152  // id 1 is the scan_id
153  result->setId(rset->getInt(1));
154  result->setConfigTag(rset->getString(2));
155  result->setTypeId( rset->getInt(3) );
156  result->setScanType( rset->getString(4) );
157  result->setFromVal( rset->getInt(5) );
158  result->setToVal( rset->getInt(6) );
159  result->setStep( rset->getInt(7) );
160 
161  } catch (SQLException &e) {
162  throw(std::runtime_error("ODScanConfig::fetchData(): "+e.getMessage()));
163  }
164 }
165 
166 int ODScanConfig::fetchID() throw(std::runtime_error)
167 {
168  // Return from memory if available
169  if (m_ID!=0) {
170  return m_ID;
171  }
172 
173  this->checkConnection();
174 
175  try {
176  Statement* stmt = m_conn->createStatement();
177  stmt->setSQL("SELECT scan_id FROM ecal_scan_dat "
178  "WHERE scan_tag=:scan_tag ");
179 
180  stmt->setString(1, getConfigTag() );
181 
182  ResultSet* rset = stmt->executeQuery();
183 
184  if (rset->next()) {
185  m_ID = rset->getInt(1);
186  } else {
187  m_ID = 0;
188  }
189  m_conn->terminateStatement(stmt);
190  } catch (SQLException &e) {
191  throw(std::runtime_error("ODScanConfig::fetchID: "+e.getMessage()));
192  }
193 
194  return m_ID;
195 }
void prepareWrite()
Definition: ODScanConfig.cc:79
void fetchData(ODScanConfig *result)
#define NULL
Definition: scimark2.h:8
tuple result
Definition: mps_fire.py:84
void setParameters(const std::map< std::string, std::string > &my_keys_map)
Definition: ODScanConfig.cc:60
void clear(CLHEP::HepGenMatrix &m)
Helper function: Reset all elements of a matrix to 0.
Definition: matutil.cc:167
oracle::occi::Statement Statement
Definition: IODConfig.h:23
int fetchNextId()
Definition: ODScanConfig.cc:38
oracle::occi::ResultSet ResultSet
Definition: HcalDbOmds.cc:26
oracle::occi::SQLException SQLException
Definition: IODConfig.h:22
void writeDB()
Definition: ODScanConfig.cc:98