CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
List of all members | Public Member Functions | Public Attributes | Private Attributes
conddblib.Connection Class Reference
Inheritance diagram for conddblib.Connection:

Public Member Functions

def __init__
 
def get_dbtype
 
def init
 
def is_frontier
 
def is_official
 
def is_oracle
 
def is_read_only
 
def is_sqlite
 
def is_valid
 
def metadata
 
def session
 

Public Attributes

 engine
 

Private Attributes

 _backendName
 
 _is_frontier
 
 _is_official
 
 _is_oracle
 
 _is_read_only
 
 _is_sqlite
 
 _schemaName
 
 _session
 
 _url
 

Detailed Description

Definition at line 308 of file conddblib.py.

Constructor & Destructor Documentation

def conddblib.Connection.__init__ (   self,
  url,
  init = False 
)

Definition at line 310 of file conddblib.py.

311  def __init__(self, url, init=False):
312  # Workaround to avoid creating files if not present.
313  # Python's sqlite3 module does not use sqlite3_open_v2(),
314  # and therefore we cannot disable SQLITE_OPEN_CREATE.
315  # Only in the case of creating a new database we skip the check.
316  if url.drivername == 'sqlite':
317 
318  #if not init and url.database is not None and not os.path.isfile(url.database):
319  # # url.database is None if opening a in-memory DB, e.g. 'sqlite://'
320  # raise Exception('SQLite database %s not found.' % url.database)
322  self.engine = sqlalchemy.create_engine(url)
323 
324  enabled_foreign_keys = self.engine.execute('pragma foreign_keys').scalar()
325  supports_foreign_keys = enabled_foreign_keys is not None
326  if not supports_foreign_keys:
327  logger.warning('Your SQLite database does not support foreign keys, so constraints will not be checked. Please upgrade.')
328  elif not enabled_foreign_keys:
329  self.engine.execute('pragma foreign_keys = on')
330 
331  else:
332  self.engine = sqlalchemy.create_engine(url)
334  self._session = sqlalchemy.orm.scoped_session(sqlalchemy.orm.sessionmaker(bind=self.engine))
336  self._is_frontier = url.drivername == 'oracle+frontier'
337  self._is_oracle = url.drivername == 'oracle'
338  self._is_sqlite = url.drivername == 'sqlite'
340  self._is_read_only = self._is_frontier or url.host in {
341  'cms_orcon_adg',
342  'cmsarc_lb',
343  }
345  self._is_official = self._is_frontier or url.host in {
346  'cms_orcon_adg',
347  'cmsarc_lb',
348  'cms_orcoff_int',
349  'cms_orcoff_prep',
350  'cms_orcon_prod',
351  'cmsintr_lb',
352  }
353  self._url = url
354  self._backendName = ('sqlite' if self._is_sqlite else 'oracle' )
355  self._schemaName = ( None if self._is_sqlite else schema_name )
356  logging.debug(' ... using db "%s", schema "%s"' % (url, self._schemaName) )
357  logging.debug('Loading db types...')
358  self.get_dbtype(Tag).__name__
359  self.get_dbtype(Payload)
360  self.get_dbtype(IOV)
361  self.get_dbtype(TagLog)
362  self.get_dbtype(GlobalTag)
363  self.get_dbtype(GlobalTagMap)
double scalar(const CLHEP::HepGenMatrix &m)
Return the matrix as a scalar. Raise an assertion if the matris is not .
Definition: matutil.cc:183

Member Function Documentation

def conddblib.Connection.get_dbtype (   self,
  theType 
)

Definition at line 364 of file conddblib.py.

References conddblib.Connection._backendName, conddblib.Connection._schemaName, relativeConstraints.keys, and conddblib.make_dbtype().

Referenced by conddblib.Connection.init(), conddblib.Connection.is_valid(), and conddblib.Connection.session().

365  def get_dbtype(self,theType):
366  basename = theType.__name__
367  if self._backendName not in db_models.keys() or basename not in db_models[self._backendName].keys():
368  return make_dbtype( self._backendName, self._schemaName, theType )
369  else:
370  return db_models[self._backendName][basename]
def make_dbtype
Definition: conddblib.py:203
def conddblib.Connection.init (   self,
  drop = False 
)
Initializes a database.

Definition at line 415 of file conddblib.py.

References querying.connection.engine, conddblib.Connection.engine, conddblib.Connection.get_dbtype(), conddblib.Connection.is_sqlite(), and conddblib.Connection.is_valid().

416  def init(self, drop=False):
417  '''Initializes a database.
418  '''
419  logging.info('Initializing database...')
420  if drop:
421  logging.debug('Dropping tables...')
422  self.metadata.drop_all(self.engine)
423  else:
424  if self.is_valid():
425  raise Exception('Looks like the database is already a valid CMS Conditions one.')
426 
427  logging.debug('Creating tables...')
428  self.get_dbtype(Tag).__table__.create(bind = self.engine)
429  self.get_dbtype(Payload).__table__.create(bind = self.engine)
430  self.get_dbtype(IOV).__table__.create(bind = self.engine)
431  self.get_dbtype(TagLog).__table__.create(bind = self.engine)
432  self.get_dbtype(GlobalTag).__table__.create(bind = self.engine)
433  self.get_dbtype(GlobalTagMap).__table__.create(bind = self.engine)
434  #self.metadata.create_all(self.engine)
435  if self.is_sqlite:
436  # horrible hack, but no choice because of the sqlalchemy bug ( see comment in the model)
437  import sqlite3
438  import string
439  conn = sqlite3.connect( self._url.database )
440  c = conn.cursor()
441  stmt = string.Template('ALTER TABLE $before RENAME TO $after')
442  c.execute( stmt.substitute( before=GlobalTag.__tablename__, after='TMP0' ) )
443  c.execute( stmt.substitute( before='TMP0', after=GlobalTag.__tablename__.upper() ) )
444  c.execute( stmt.substitute( before=GlobalTagMap.__tablename__, after='TMP1' ) )
445  c.execute( stmt.substitute( before='TMP1', after=GlobalTagMap.__tablename__.upper() ) )
446  conn.commit()
447  conn.close()
448  # TODO: Create indexes
449  #logger.debug('Creating indexes...')
450 
451 
# Connection helpers
def conddblib.Connection.is_frontier (   self)

Definition at line 382 of file conddblib.py.

References conddblib.Connection._is_frontier.

383  def is_frontier(self):
384  return self._is_frontier
def conddblib.Connection.is_official (   self)

Definition at line 398 of file conddblib.py.

References conddblib.Connection._is_official.

399  def is_official(self):
400  return self._is_official
def conddblib.Connection.is_oracle (   self)

Definition at line 386 of file conddblib.py.

References conddblib.Connection._is_oracle.

387  def is_oracle(self):
388  return self._is_oracle
def conddblib.Connection.is_read_only (   self)

Definition at line 394 of file conddblib.py.

References conddblib.Connection._is_read_only.

395  def is_read_only(self):
396  return self._is_read_only
def conddblib.Connection.is_sqlite (   self)

Definition at line 390 of file conddblib.py.

References conddblib.Connection._is_sqlite.

Referenced by conddblib.Connection.init().

391  def is_sqlite(self):
392  return self._is_sqlite
def conddblib.Connection.is_valid (   self)
Tests whether the current DB looks like a valid CMS Conditions one.

Definition at line 401 of file conddblib.py.

References Vispa.Plugins.EdmBrowser.EdmDataAccessor.all(), conddblib.Connection.get_dbtype(), and conddblib.getSchema().

Referenced by conddblib.Connection.init().

402  def is_valid(self):
403  '''Tests whether the current DB looks like a valid CMS Conditions one.
404  '''
405  engine_connection = self.engine.connect()
406  #ret = all([self.engine.dialect.has_table(engine_connection, table.__tablename__) for table in [Tag, IOV, Payload, GlobalTag, GlobalTagMap]])
407  # temporarely avoid the check on the GT tables - there are releases in use where C++ does not create these tables.
408  #ret = all([self.engine.dialect.has_table(engine_connection, table.__tablename__,table.__table_args__['schema']) for table in [Tag, IOV, Payload]])
409  _Tag = self.get_dbtype(Tag)
410  _IOV = self.get_dbtype(IOV)
411  _Payload = self.get_dbtype(Payload)
412  ret = all([self.engine.dialect.has_table(engine_connection, table.__tablename__,getSchema(table)) for table in [_Tag, _IOV, _Payload]])
413  engine_connection.close()
414  return ret
def getSchema
Definition: conddblib.py:237
def conddblib.Connection.metadata (   self)

Definition at line 378 of file conddblib.py.

379  def metadata(self):
380  return _Base.metadata
def conddblib.Connection.session (   self)

Definition at line 371 of file conddblib.py.

References conddblib.Connection._is_sqlite, conddblib.Connection._session, and conddblib.Connection.get_dbtype().

372  def session(self):
373  s = self._session()
374  s.get_dbtype = self.get_dbtype
375  s._is_sqlite = self._is_sqlite
376  return s

Member Data Documentation

conddblib.Connection._backendName
private

Definition at line 353 of file conddblib.py.

Referenced by conddblib.Connection.get_dbtype().

conddblib.Connection._is_frontier
private

Definition at line 335 of file conddblib.py.

Referenced by conddblib.Connection.is_frontier().

conddblib.Connection._is_official
private

Definition at line 344 of file conddblib.py.

Referenced by conddblib.Connection.is_official().

conddblib.Connection._is_oracle
private

Definition at line 336 of file conddblib.py.

Referenced by conddblib.Connection.is_oracle().

conddblib.Connection._is_read_only
private

Definition at line 339 of file conddblib.py.

Referenced by conddblib.Connection.is_read_only().

conddblib.Connection._is_sqlite
private

Definition at line 337 of file conddblib.py.

Referenced by conddblib.Connection.is_sqlite(), and conddblib.Connection.session().

conddblib.Connection._schemaName
private

Definition at line 354 of file conddblib.py.

Referenced by conddblib.Connection.get_dbtype().

conddblib.Connection._session
private

Definition at line 333 of file conddblib.py.

Referenced by conddblib.Connection.session().

conddblib.Connection._url
private

Definition at line 352 of file conddblib.py.

Referenced by cmstc.TagCollector._open().

conddblib.Connection.engine

Definition at line 321 of file conddblib.py.

Referenced by conddblib.Connection.init().