24 oracle::occi::Connection* conn) :
LMFUnique(env, conn) {
39 std::map<std::string, LMFUnique*>::iterator
i =
58 std::map<unsigned int, std::string>
m;
59 std::map<std::string, unsigned int>::const_iterator
i =
m_keys.begin();
60 std::map<std::string, unsigned int>::const_iterator
e =
m_keys.end();
62 m[i->second] = i->first;
79 cout <<
"Stored data: " << s << endl;
81 std::map<int, std::vector<float> >::const_iterator
p =
m_data.begin();
82 std::map<int, std::vector<float> >::const_iterator
end =
m_data.end();
85 while ((p != end) && (c < max)) {
87 std::vector<float>
x = p->second;
88 cout << c <<
" -------------------------------------------" << endl;
89 cout <<
" ID: " <<
id << endl;
90 for (
unsigned int j = 0; j < x.size(); j++) {
94 cout << rm[j] <<
":" << x[j] <<
"\t";
105 std::stringstream sql;
107 unsigned int nParameters =
m_keys.size() + 2;
108 for (
unsigned int i = 0;
i < nParameters - 1;
i++) {
109 sql <<
":" <<
i + 1 <<
", ";
111 sql <<
":" << nParameters <<
")";
142 std::stringstream sql;
145 if (
_where.length() > 0) {
151 sql <<
"SELECT * FROM CMS_ECAL_LASER_COND." <<
getTableName() <<
" WHERE " 167 sql <<
"SELECT * FROM (SELECT CMS_ECAL_LASER_COND." 170 <<
" JOIN LMF_RUN_IOV ON " 171 <<
"LMF_RUN_IOV.LMF_IOV_ID = " 173 <<
"WHERE SUBRUN_START " << op <<
"= TO_DATE(:" <<
count;
175 sql <<
", 'YYYY-MM-DD HH24:MI:SS') ORDER BY SUBRUN_START " 176 << order <<
") WHERE ROWNUM <= 1";
179 sql <<
" AND LOGIC_ID = :" <<
count;
206 int logic_id =
m_data.begin()->first;
208 lastMeasuredOn +=
which;
209 dat->fetch(logic_id, &lastMeasuredOn,
which);
210 dat->setMaxDataToDump(
m_max);
213 throw(std::runtime_error(
m_className +
"::getPrevious: Too many LOGIC_IDs in " 221 fetch(
id.getLogicID());
227 fetch(
id.getLogicID(), &tm, 1);
234 fetch(
id.getLogicID(), &tm, direction);
246 fetch(logic_id,
nullptr, 0);
252 fetch(logic_id, &tm, 1);
260 std::size_t
n = sql.find(
":");
261 for (
int done = 1; done <
count; done++) {
263 n = sql.find(
":", n + 1);
265 while (n != std::string::npos) {
266 char type = sql.at(n + 1);
270 }
else if (type ==
'F') {
271 stmt->setFloat(nw + count, atof(
_wherePars[nw].c_str()));
273 }
else if (type ==
'I') {
274 stmt->setInt(nw + count, atoi(
_wherePars[nw].c_str()));
277 n = sql.find(
":", n + 1);
286 throw(std::runtime_error(
m_className +
"::fetch: Cannot fetch data with " 287 "timestamp = 0 and LMFRunIOV = 0"));
291 std::cout <<
"[LMFDat] This object is valid..." << std::endl;
297 std::cout <<
"[LMFDat] Executing query " << std::endl;
298 std::cout <<
" " << sql << std::endl << std::flush;
302 stmt->setPrefetchRowCount(10000);
308 stmt->setString(count,
timestamp->str());
311 stmt->setInt(count++, logic_id);
315 std::vector<float>
x;
316 int nData =
m_keys.size();
318 while (rset->next() != 0) {
319 for (
int i = 0;
i < nData;
i++) {
320 x.push_back(rset->getFloat(
i + 3));
322 int id = rset->getInt(2);
329 stmt->setPrefetchRowCount(0);
330 m_conn->terminateStatement(stmt);
332 catch (oracle::occi::SQLException &
e) {
333 throw(std::runtime_error(
m_className +
"::fetch: " + e.getMessage()));
343 m_Error +=
" Can't find lmfRunIOV within foreign keys.";
356 std::map<int, std::vector<float> >
s =
m_data;
357 std::string sql =
"SELECT LOGIC_ID FROM CMS_ECAL_LASER_COND." +
371 stmt->setPrefetchRowCount(10000);
373 std::map<int, std::vector<float> >::iterator
i = s.end();
374 std::map<int, std::vector<float> >::iterator
e = s.end();
375 while (rset->next() != 0) {
380 i = s.find(rset->getInt(1));
385 stmt->setPrefetchRowCount(0);
386 m_conn->terminateStatement(stmt);
388 catch (oracle::occi::SQLException &
e) {
389 throw(std::runtime_error(
m_className +
"::fetchData: "+e.getMessage()));
411 std::map<int, std::vector<float> > data2write =
fetchData();
412 if (!data2write.empty()) {
417 std::list<dvoid *> bufPointers;
418 int nParameters =
m_keys.size();
419 int nData = data2write.size();
424 int * iovid_vec =
new int[nData];
425 int * logicid_vec =
new int[nData];
426 int *intArray =
new int[nData];
427 float *floatArray =
new float[nData];
428 ub2 * intSize =
new ub2[nData];
429 ub2 * floatSize =
new ub2[nData];
430 size_t intTotalSize =
sizeof(
int)*nData;
431 size_t floatTotalSize =
sizeof(
float)*nData;
437 for (
int i = 0;
i < nData;
i++) {
438 intSize[
i] =
sizeof(
int);
439 floatSize[
i] =
sizeof(
int);
443 int iov_id = runiov->
getID();
444 std::map<int, std::vector<float> >::const_iterator
b = data2write.begin();
445 std::map<int, std::vector<float> >::const_iterator
e = data2write.end();
446 for (
int i = 0;
i < nData;
i++) {
447 iovid_vec[
i] = iov_id;
449 stmt->setDataBuffer(1, (dvoid*)iovid_vec, oracle::occi::OCCIINT,
450 sizeof(iovid_vec[0]), intSize);
455 logicid_vec[c++] =
id;
458 stmt->setDataBuffer(2, (dvoid*)logicid_vec, oracle::occi::OCCIINT,
459 sizeof(logicid_vec[0]), intSize);
462 for (
int i = 0;
i < nParameters;
i++) {
463 b = data2write.begin();
467 std::vector<float>
x = b->second;
469 intArray[
c] = (
int)rint(x[
i]);
471 floatArray[
c] = x[
i];
473 throw(std::runtime_error(
"ERROR: LMFDat::writeDB: unsupported type"));
480 type = oracle::occi::OCCIINT;
481 ub2 *sizeArray = intSize;
482 int size =
sizeof(intArray[0]);
484 buffer = (dvoid *)malloc(
sizeof(
float)*nData);
485 memcpy(buffer, floatArray, floatTotalSize);
486 type = oracle::occi::OCCIFLOAT;
487 sizeArray = floatSize;
488 size =
sizeof(floatArray[0]);
490 buffer = (dvoid *)malloc(
sizeof(
int)*nData);
491 memcpy(buffer, intArray, intTotalSize);
493 bufPointers.push_back(buffer);
495 for (
int k = 0; ((
k < nData) && (
k <
m_max));
k++) {
500 << ((
float *)(buffer))[
k] << endl;
503 stmt->setDataBuffer(
i + 3, buffer, type, size, sizeArray);
505 stmt->executeArrayUpdate(nData);
507 delete [] floatArray;
510 delete [] logicid_vec;
512 std::list<dvoid *>::const_iterator bi = bufPointers.begin();
513 std::list<dvoid *>::const_iterator be = bufPointers.end();
519 m_conn->terminateStatement(stmt);
521 }
catch (oracle::occi::SQLException &
e) {
526 int iov_id = runiov->
getID();
527 std::cout <<
"==== This object refers to IOV " << iov_id << std::endl;
530 throw(std::runtime_error(
m_className +
"::writeDB: " +
546 for (
unsigned int i = 0;
i <
m_keys.size();
i++) {
553 sql =
"SELECT * FROM TABLE(CMS_ECAL_LASER_COND.LMF_TAB_COLS(:1, :2))";
564 while (rset->next() != 0) {
569 m_conn->terminateStatement(stmt);
570 }
catch (oracle::occi::SQLException &
e) {
571 throw(std::runtime_error(
m_className +
"::getKeyTypes: " + e.getMessage() +
582 m_Error =
"class name not set ";
587 m_Error +=
"table name not set ";
594 m_Error +=
"key size does not correspond to table definition";
604 std::vector<float> ret;
616 return getData(
id.getLogicID());
631 return getData(
id.getLogicID(), ret);
647 return getData(
id.getLogicID(), k);
662 std::vector<float>
v;
664 if ((retval) && (v.size() >
k)) {
674 return getData(
id.getLogicID(), k, ret);
687 return getData(
id.getLogicID(), key, ret);
std::vector< std::string > _wherePars
LMFUnique & setInt(std::string key, int value)
void getNeighbour(LMFDat *dat, int which) noexcept(false)
oracle::occi::Connection * m_conn
void adjustParameters(int n, std::string &sql, Statement *stmt)
void getPrevious(LMFDat *dat) noexcept(false)
int writeDB() noexcept(false) override
std::map< unsigned int, std::string > getReverseMap() const
std::string buildInsertSql()
oracle::occi::ResultSet ResultSet
std::map< int, std::vector< float > > m_data
void dump() const override
LMFDat & setData(int logic_id, const std::vector< float > &data)
virtual void dump() const
std::vector< std::string > m_type
void getNext(LMFDat *dat) noexcept(false)
int getInt(std::string fieldname) const
std::map< int, std::vector< float > > fetchData() noexcept(false)
void checkConnection() const noexcept(false)
void fetch() noexcept(false)
oracle::occi::Statement Statement
std::map< std::string, LMFUnique * > m_foreignKeys
Tm getSubrunStart() const
std::map< int, std::vector< float > > getData()
LMFDat & setMaxDataToDump(int n)
void setWhereClause(std::string w)
std::vector< float > operator[](int id)
virtual std::string getIovIdFieldName() const
virtual std::string foreignKeyName() const
std::map< std::string, unsigned int > m_keys
virtual int writeForeignKeys() noexcept(false)
virtual std::string getTableName() const
oracle::occi::Connection * getConn() const
std::string buildSelectSql(int logic_id=0, int direction=0)
oracle::occi::Environment * getEnv() const
int fetchID() noexcept(false) override
void getKeyTypes() noexcept(false)