CMS 3D CMS Logo

Public Types | Public Member Functions | Protected Member Functions | Protected Attributes | Private Member Functions | Friends

LMFUnique Class Reference

#include <LMFUnique.h>

Inheritance diagram for LMFUnique:
IUniqueDBObject IDBObject LMFColor LMFDat LMFIOV LMFLmrSubIOV LMFPrimVers LMFRunIOV LMFRunTag LMFSeqDat LMFTrigType

List of all members.

Public Types

typedef oracle::occi::ResultSet ResultSet
typedef oracle::occi::Statement Statement

Public Member Functions

void attach (std::string name, LMFUnique *u)
void debug ()
virtual void dump () const
virtual void dump (int n) const
virtual bool exists ()
virtual boost::ptr_list
< LMFUnique
fetchAll () const throw (std::runtime_error)
int fetchID () throw (std::runtime_error)
std::string getClassName ()
std::string getClassName () const
int getID () const
int getInt (std::string fieldname) const
std::string getString (std::string fieldname) const
virtual bool isValid () const
virtual bool isValid ()
 LMFUnique ()
 LMFUnique (oracle::occi::Environment *env, oracle::occi::Connection *conn)
 LMFUnique (EcalDBConnection *c)
void nodebug ()
virtual bool operator< (const LMFUnique &r)
virtual bool operator<= (const LMFUnique &r)
std::string sequencePostfix (Tm t)
void setByID (int id) throw (std::runtime_error)
LMFUniquesetInt (std::string key, int value)
LMFUniquesetString (std::string key, std::string value)
void startProfiling ()
void stopProfiling ()
virtual ~LMFUnique ()

Protected Member Functions

virtual void setClassName (std::string s)
virtual int writeDB () throw (std::runtime_error)
virtual int writeForeignKeys () throw (std::runtime_error)

Protected Attributes

bool _profiling
std::string m_className
char m_debug
std::map< std::string,
LMFUnique * > 
m_foreignKeys
std::map< std::string, int > m_intFields
std::map< std::string,
std::string > 
m_stringFields

Private Member Functions

virtual LMFUniquecreateObject () const
virtual std::string fetchAllSql (Statement *stmt) const
virtual std::string fetchIdSql (Statement *stmt)
virtual void fetchParentIDs ()
virtual void getParameters (ResultSet *rset)
virtual std::string setByIDSql (Statement *stmt, int id)
virtual std::string writeDBSql (Statement *stmt)

Friends

class EcalCondDBInterface

Detailed Description

Definition at line 17 of file LMFUnique.h.


Member Typedef Documentation

Reimplemented in LMFClsDat, and LMFRunTag.

Definition at line 19 of file LMFUnique.h.

typedef oracle::occi::Statement LMFUnique::Statement

Reimplemented in LMFClsDat.

Definition at line 20 of file LMFUnique.h.


Constructor & Destructor Documentation

LMFUnique::LMFUnique ( ) [inline]

Definition at line 23 of file LMFUnique.h.

References _profiling, IDBObject::m_conn, IDBObject::m_env, IUniqueDBObject::m_ID, nodebug(), NULL, and setClassName().

              { 
    _profiling = false;
    m_env = NULL;
    m_conn = NULL;
    m_ID = 0;
    setClassName("LMFUnique"); nodebug(); 
  }
LMFUnique::LMFUnique ( oracle::occi::Environment *  env,
oracle::occi::Connection *  conn 
) [inline]

Definition at line 30 of file LMFUnique.h.

References _profiling, IUniqueDBObject::m_ID, nodebug(), setClassName(), and IDBObject::setConnection().

                                        {
    _profiling = false;
    m_ID = 0;
    setClassName("LMFUnique"); nodebug(); 
    setConnection(env, conn);
  }
LMFUnique::LMFUnique ( EcalDBConnection c) [inline]
LMFUnique::~LMFUnique ( ) [virtual]

Definition at line 7 of file LMFUnique.cc.

                      {
}

Member Function Documentation

void LMFUnique::attach ( std::string  name,
LMFUnique u 
)

Definition at line 50 of file LMFUnique.cc.

References i, and mergeVDriftHistosByStation::name.

Referenced by LMFCorrCoefDatComponent::setLMFLmrSubIOV(), LMFDat::setLMFRunIOV(), and LMFRunIOV::setSequence().

                                                   {
  std::map<std::string, LMFUnique *>::const_iterator i = 
    m_foreignKeys.find(name);
  if (i != m_foreignKeys.end()) {
    if (i->second != u) {
      m_foreignKeys[name] = u;
      m_ID = 0;
    }
  } else {
    m_foreignKeys[name] = u;
    m_ID = 0;
  }
}
LMFUnique * LMFUnique::createObject ( ) const [private, virtual]

Reimplemented in LMFColor, LMFCorrVers, LMFIOV, LMFPrimVers, LMFRunTag, LMFSeqVers, and LMFTrigType.

Definition at line 189 of file LMFUnique.cc.

References NULL.

                                         {
  /* this method should return a pointer to a newly created object */
  return NULL;
}
void LMFUnique::debug ( ) [inline]
void LMFUnique::dump ( int  n) const [virtual]

Reimplemented in LMFDat.

Definition at line 116 of file LMFUnique.cc.

References gather_cfg::cout, and combine::key.

                                {
  /*
    This method is used to dump the content of an object 
    Indent data if the object is contained inside another object
  */
  std::string m_indent = "";
  std::string m_trail = "";
  m_trail.resize(70 - 31 - n * 2, '#');
  m_indent.resize(n*2, ' ');
  m_indent += "|";
  // start of object
  cout << m_indent << "#################" << setw(15) << m_className 
       << " " << m_trail << endl;
  cout << m_indent << "Address: " << this << endl;
  cout << m_indent << "Connection params : " << m_env << ", " << m_conn << endl;
  // object ID in the DB
  cout << m_indent << "ID" << setw(18) << ": " << m_ID;
  if (m_ID == 0) {
    cout << " *** NULL ID ***";
  }
  if (!isValid()) {
    cout << " INVALID ***";
  }
  cout << endl;
  // iterate over string fields
  std::map<std::string, std::string>::const_iterator is = 
    m_stringFields.begin();
  std::map<std::string, std::string>::const_iterator es = 
    m_stringFields.end();
  while (is != es) {
    std::string key = is->first;
    cout << m_indent << key << setw(20 - key.length()) << ": " << is->second
         << endl;
    is++;
  }
  // iterate over integer fields
  std::map<std::string, int>::const_iterator ii = m_intFields.begin();
  std::map<std::string, int>::const_iterator ei = m_intFields.end();
  while (ii != ei) {
    std::string key = ii->first;
    cout << m_indent << key << setw(20 - key.length()) << ": " << ii->second
         << endl;
    ii++;
  }
  cout << m_indent << "#################" << setw(15) << m_className 
       << " " << m_trail << endl;
  // iterate over foreign keys
  std::map<std::string, LMFUnique*>::const_iterator ik = m_foreignKeys.begin();
  std::map<std::string, LMFUnique*>::const_iterator ek = m_foreignKeys.end();
  m_indent.clear();
  m_indent.resize((n + 1) * 2, ' ');
  while (ik != ek) {
    cout << m_indent << "Foreign Key: " << ik->first << endl;
    ik->second->dump(n + 1);
    ik++;
  }
}
void LMFUnique::dump ( void  ) const [virtual]

Reimplemented in LMFDat, LMFIOV, and LMFRunIOV.

Definition at line 112 of file LMFUnique.cc.

References hcal_timing_source_file_cfg::dump.

Referenced by LMFCorrCoefDat::fetch(), fetchAll(), and LMFSeqDat::writeDBSql().

                           {
  dump(0);
}
bool LMFUnique::exists ( ) [virtual]

Definition at line 174 of file LMFUnique.cc.

References run_regression::ret.

                       {
  fetchID();
  bool ret = false;
  if (m_ID > 0) {
    ret = true;
  }
  return ret;
}
boost::ptr_list< LMFUnique > LMFUnique::fetchAll ( ) const throw (std::runtime_error) [virtual]

Definition at line 64 of file LMFUnique.cc.

References gather_cfg::cout, debug(), dump(), alignCSCRings::e, prof2calltree::l, NULL, python::connectstrParser::o, and setByID().

Referenced by LMFDefFabric::initialize(), and LMFColor::isValid().

{
  /*
    Returns a list of pointers to DB objects
   */
  boost::ptr_list<LMFUnique> l;
  this->checkConnection();

  try {
    Statement* stmt = m_conn->createStatement();
    std::string sql = fetchAllSql(stmt);
    if (sql != "") {
      if (m_debug) {
        cout << m_className + ": Query " + sql << endl;
      }
      ResultSet* rset = stmt->executeQuery();
      while (rset->next() != 0) {
        LMFUnique *o = createObject();
        if (m_debug) {
          o->debug();
        }
        if (o != NULL) {
          o->setByID(rset->getInt(1));
          if (m_debug) {
            o->dump();
          }
          try {
            l.push_back(o);
          }
          catch (boost::bad_pointer &e) {
            throw(std::runtime_error(m_className + "::fetchAll:  " + 
                                     e.what()));
          }
        }
      }
    }
    m_conn->terminateStatement(stmt);
  }
  catch (SQLException &e) {
    throw(std::runtime_error(m_className + "::fetchAll:  "+e.getMessage()));
  }
  if (m_debug) {
    cout << m_className << ": list size = " << l.size() << endl;
  }
  return l;
}
std::string LMFUnique::fetchAllSql ( Statement stmt) const [private, virtual]

Reimplemented in LMFColor, LMFCorrVers, LMFPrimVers, LMFRunTag, LMFSeqVers, and LMFTrigType.

Definition at line 183 of file LMFUnique.cc.

                                                      {
  /* this method should setup a Statement to select the unique IDs of the
     objects to return */
  return "";
}
int LMFUnique::fetchID ( ) throw (std::runtime_error) [virtual]

Implements IUniqueDBObject.

Definition at line 213 of file LMFUnique.cc.

References gather_cfg::cout, alignCSCRings::e, i, and n.

Referenced by LMFDat::getLMFRunIOVID(), LMFColor::setColor(), LMFColor::setName(), and LMFTrigType::setName().

{
  /*
    This method fetch the ID of the object from the database according
    to the given specifications.

    It is assumed that there is only one object in the database with the
    given specifications. In case more than one object can be retrieved
    this method throws an exception.

    Since not all the specifications can define completely the object
    itself, at the end, we setup the object based on its ID.
   */
  // Return tag from memory if available
  if (m_ID) {
    return m_ID;
  }
 
  this->checkConnection();

  // fetch this ID
  try {
    Statement* stmt = m_conn->createStatement();
    // prepare the sql query
    std::string sql = fetchIdSql(stmt);
    if (sql != "") {
      if (m_debug) {
        cout << m_className + ": Query " + sql << endl;
      }
      
      ResultSet* rset = stmt->executeQuery();
      if (rset->next() != 0) {
        m_ID = rset->getInt(1);
      } else {
        m_ID = 0;
      }
      if (m_debug) {
        cout << m_className + ": ID set to " << m_ID << endl;
      }
      int n = rset->getNumArrayRows();
      if (m_debug) {
        cout << m_className + ": Returned " << n << " rows"  << endl;
      }
      if (n > 1) {
        throw(std::runtime_error(m_className + "::fetchID: too many rows returned " +
                            "executing query " + sql));
        m_ID = 0;
      }
    }
    m_conn->terminateStatement(stmt);
  } catch (SQLException &e) {
    throw(std::runtime_error(m_className + "::fetchID:  "+e.getMessage()));
  }
  // given the ID of this object setup it completely
  if (m_ID > 0) {
    setByID(m_ID);
  }
  // if foreignKeys are there, set these objects too
  map<string, LMFUnique*>::iterator i = m_foreignKeys.begin();
  map<string, LMFUnique*>::iterator e = m_foreignKeys.end();
  while (i != e) {
    if (i->second->getID() == 0) {
      i->second->fetchID();
    }
    i++;
  }
  if (m_debug) {
    cout << m_className << ": fetchID:: returning " << m_ID << endl;
  }
  return m_ID;
}
virtual std::string LMFUnique::fetchIdSql ( Statement stmt) [inline, private, virtual]

Reimplemented in LMFColor, LMFCorrVers, LMFIOV, LMFLmrSubIOV, LMFPrimVers, LMFRunIOV, LMFRunTag, LMFSeqDat, LMFSeqVers, and LMFTrigType.

Definition at line 85 of file LMFUnique.h.

{ return ""; }
virtual void LMFUnique::fetchParentIDs ( ) [inline, private, virtual]

Reimplemented in LMFIOV, and LMFSeqDat.

Definition at line 91 of file LMFUnique.h.

{}
std::string LMFUnique::getClassName ( ) const [inline]

Definition at line 55 of file LMFUnique.h.

References m_className.

{ return m_className; }
std::string LMFUnique::getClassName ( ) [inline]

Definition at line 54 of file LMFUnique.h.

References m_className.

{ return m_className; }
int LMFUnique::getID ( ) const [inline]
int LMFUnique::getInt ( std::string  fieldname) const
virtual void LMFUnique::getParameters ( ResultSet rset) [inline, private, virtual]

Reimplemented in LMFColor, LMFCorrVers, LMFIOV, LMFLmrSubIOV, LMFPrimVers, LMFRunIOV, LMFRunTag, LMFSeqDat, LMFSeqVers, and LMFTrigType.

Definition at line 90 of file LMFUnique.h.

{}
std::string LMFUnique::getString ( std::string  fieldname) const
virtual bool LMFUnique::isValid ( void  ) [inline, virtual]

Reimplemented in LMFClsDat, LMFColor, LMFDat, LMFLaserPulseDat, LMFPnPrimDat, LMFPrimDat, LMFRunIOV, and LMFRunTag.

Definition at line 47 of file LMFUnique.h.

{ return true; }
virtual bool LMFUnique::isValid ( void  ) const [inline, virtual]

Reimplemented in LMFSeqDat.

Definition at line 46 of file LMFUnique.h.

{ return true; }
void LMFUnique::nodebug ( ) [inline]

Definition at line 69 of file LMFUnique.h.

References m_debug.

Referenced by LMFUnique().

{ m_debug = 0; }
virtual bool LMFUnique::operator< ( const LMFUnique r) [inline, virtual]

Definition at line 74 of file LMFUnique.h.

References IUniqueDBObject::m_ID.

                                             {
    return (m_ID < r.m_ID);
  }
virtual bool LMFUnique::operator<= ( const LMFUnique r) [inline, virtual]

Definition at line 77 of file LMFUnique.h.

References IUniqueDBObject::m_ID.

                                              {
    return (m_ID <= r.m_ID);
  }
std::string LMFUnique::sequencePostfix ( Tm  t)

Definition at line 10 of file LMFUnique.cc.

References Tm::str().

Referenced by LMFLmrSubIOV::writeDBSql(), LMFRunIOV::writeDBSql(), and LMFSeqDat::writeDBSql().

                                         {
  std::string ts = t.str();
  return ts.substr(2, 2);
}
void LMFUnique::setByID ( int  id) throw (std::runtime_error) [virtual]

Implements IUniqueDBObject.

Definition at line 286 of file LMFUnique.cc.

References gather_cfg::cout, alignCSCRings::e, cmsPerfRegress::getParameters(), and errorMatrix2Lands_multiChannel::id.

Referenced by LMFCorrCoefDat::fetch(), fetchAll(), LMFSeqDat::fetchByRunIOV(), LMFRunIOV::getLMFColor(), LMFLmrSubIOV::getLMFIOV(), LMFCorrCoefDatComponent::getLMFLmrSubIOV(), LMFDat::getLMFRunIOV(), LMFRunIOV::getLMFRunTag(), LMFCorrCoefDatComponent::getSequence(), LMFRunIOV::getSequence(), and LMFRunIOV::getTriggerType().

{
  /*
    Given the ID of an object setup it
   */
  if (m_debug) {
    cout << m_className << ": Setting this object as ID = " << id << endl;
  }
  this->checkConnection();
  try {
    Statement* stmt = m_conn->createStatement();
    std::string sql = setByIDSql(stmt, id);
    if (sql == "") {
      throw(std::runtime_error(m_className + "::setByID: [empty sql])"));
    }
    if (m_debug) {
      cout << m_className + ": " + sql << endl;
    }

    ResultSet* rset = stmt->executeQuery();
    if (rset->next() != 0) {
      // setup the concrete object
      getParameters(rset);
      m_ID = id;
      if (m_debug) {
        cout << m_className + ": Setting done. ID set to " << m_ID << endl;
      }
    } else {
      throw(std::runtime_error(m_className + "::setByID:  Given id is not in the database"));
    }
    m_conn->terminateStatement(stmt);
  } catch (SQLException &e) {
   throw(std::runtime_error(m_className + "::setByID:  "+e.getMessage()));
  }
}
virtual std::string LMFUnique::setByIDSql ( Statement stmt,
int  id 
) [inline, private, virtual]

Reimplemented in LMFColor, LMFCorrVers, LMFIOV, LMFLmrSubIOV, LMFPrimVers, LMFRunIOV, LMFRunTag, LMFSeqDat, LMFSeqVers, and LMFTrigType.

Definition at line 87 of file LMFUnique.h.

                                         { return ""; }
virtual void LMFUnique::setClassName ( std::string  s) [inline, protected, virtual]
LMFUnique & LMFUnique::setInt ( std::string  key,
int  value 
)

Definition at line 33 of file LMFUnique.cc.

References i, combine::key, and relativeConstraints::value.

Referenced by LMFDat::getLMFRunIOVID(), LMFSeqDat::getParameters(), LMFRunTag::getParameters(), LMFRunIOV::getParameters(), LMFColor::getParameters(), LMFColor::setColor(), LMFRunIOV::setColor(), LMFRunIOV::setColorIndex(), LMFCorrCoefDatComponent::setLMFLmrSubIOV(), LMFDat::setLMFRunIOV(), LMFRunIOV::setLMFRunTag(), LMFRunIOV::setLmr(), LMFColor::setName(), LMFRunIOV::setSequence(), LMFSeqDat::setSequenceNumber(), LMFRunIOV::setTriggerType(), LMFRunTag::setVersion(), LMFSeqDat::setVmax(), and LMFSeqDat::setVmin().

                                                     {
  // check if this key exists
  std::map<std::string, int>::const_iterator i = m_intFields.find(key);
  if (i != m_intFields.end()) {
    // the key exist: check if it changed: reset the ID of the object
    if (i->second != value) {
      m_intFields[key] = value;
      m_ID = 0;
    }
  } else {
    // create this key and reset the ID of the object
    m_intFields[key] = value;
    m_ID = 0;    
  }
  return *this;
}
LMFUnique & LMFUnique::setString ( std::string  key,
std::string  value 
)
void LMFUnique::startProfiling ( ) [inline]

Definition at line 80 of file LMFUnique.h.

References _profiling.

{ _profiling = true; }
void LMFUnique::stopProfiling ( ) [inline]

Definition at line 81 of file LMFUnique.h.

References _profiling.

{ _profiling = false; }
int LMFUnique::writeDB ( ) throw (std::runtime_error) [protected, virtual]

Reimplemented in LMFColoredTable, LMFCorrCoefDatComponent, and LMFDat.

Definition at line 339 of file LMFUnique.cc.

References gather_cfg::cout, debug, hcal_timing_source_file_cfg::dump, alignCSCRings::e, end, and errorMatrix2Lands_multiChannel::start.

{
  clock_t start = 0;
  clock_t end = 0;
  if (_profiling) {
    start = clock();
  }
  // write the associated objects first (foreign keys must exist before use)
  writeForeignKeys();
  // see if this data is already in the DB
  if (!(this->fetchID())) { 
    // check the connectioin
    this->checkConnection();
    
    // write new tag to the DB
    std::string sql = "";
    try {
      Statement* stmt = m_conn->createStatement();
      
      sql = writeDBSql(stmt);
      if (sql != "") {
        if (m_debug) {
          cout << m_className + ": " + sql << endl;
        }
        stmt->executeUpdate();
      }
      m_conn->commit();
      m_conn->terminateStatement(stmt);
    } catch (SQLException &e) {
      debug();
      dump();
      throw(std::runtime_error(m_className + "::writeDB:  " + e.getMessage() +
                               " while executing query " + sql));
    }
    // now get the id
    if (this->fetchID() == 0) {
      throw(std::runtime_error(m_className + "::writeDB:  Failed to write"));
    }
  }
  if (_profiling) {
    end = clock();
    if (m_debug) {
      std::cout << m_className << ":: Spent time in writeDB:" << 
        ((double) (end - start)) / CLOCKS_PER_SEC << " s" << endl;
    }
  }
  return m_ID;
}
virtual std::string LMFUnique::writeDBSql ( Statement stmt) [inline, private, virtual]

Reimplemented in LMFIOV, LMFLmrSubIOV, LMFRunIOV, and LMFSeqDat.

Definition at line 84 of file LMFUnique.h.

{ return ""; } 
int LMFUnique::writeForeignKeys ( ) throw (std::runtime_error) [protected, virtual]

Definition at line 323 of file LMFUnique.cc.

References prof2calltree::count, alignCSCRings::e, and i.

Referenced by LMFDat::writeDB().

{
  std::map<std::string, LMFUnique*>::const_iterator i = m_foreignKeys.begin();
  std::map<std::string, LMFUnique*>::const_iterator e = m_foreignKeys.end();
  int count = 0;
  while (i != e) {
    if (i->second->getID() == 0) {
      i->second->writeDB();
      count++;
    }
    i++;
  }
  return count;
}

Friends And Related Function Documentation

friend class EcalCondDBInterface [friend]

Reimplemented from IDBObject.

Reimplemented in LMFColor, LMFCorrCoefDatComponent, LMFDat, LMFIOV, LMFLmrSubIOV, LMFRunIOV, LMFSeqDat, and LMFTrigType.

Definition at line 21 of file LMFUnique.h.


Member Data Documentation

bool LMFUnique::_profiling [protected]

Definition at line 107 of file LMFUnique.h.

Referenced by LMFUnique(), startProfiling(), and stopProfiling().

std::string LMFUnique::m_className [protected]
char LMFUnique::m_debug [protected]
std::map<std::string, LMFUnique*> LMFUnique::m_foreignKeys [protected]
std::map<std::string, int> LMFUnique::m_intFields [protected]
std::map<std::string, std::string> LMFUnique::m_stringFields [protected]