CMS 3D CMS Logo

List of all members | Public Member Functions | Static Public Member Functions | Public Attributes | Private Member Functions | Static Private Member Functions | Private Attributes
querying.connection Class Reference
Inheritance diagram for querying.connection:

Public Member Functions

def __init__ (self, connection_data, mode=None, map_blobs=False, secrets=None, pooling=False)
 
def close_session (self)
 
def commit (self)
 
def global_tag (self, **pkargs)
 
def global_tag_map (self, **pkargs)
 
def hard_close (self)
 
def iov (self, **pkargs)
 
def model (self, model_name)
 
def object (self, model, pk_to_value)
 
def payload (self, **pkargs)
 
def rollback (self)
 
def search_everything (self, string, amount=10)
 
def setup (self)
 
def tag (self, **pkargs)
 
def tear_down (self)
 
def write (self, object)
 
def write_and_commit (self, object)
 

Static Public Member Functions

def build_frontier_url (db_name, schema)
 
def build_oracle_url (user, pwd, db_name)
 

Public Attributes

 connection_data
 
 engine
 
 factory
 
 models
 
 radius
 
 range
 
 regexp
 
 schema
 
 session
 
 sessionmaker
 

Private Member Functions

def _oracle_match_format (self, string)
 

Static Private Member Functions

def _cms_frontier_string (database, schema="cms_conditions")
 
def _cms_oracle_string (user, pwd, db_name)
 
def _get_CMS_frontier_connection_string (database)
 

Private Attributes

 _pooling
 

Detailed Description

Definition at line 25 of file querying.py.

Constructor & Destructor Documentation

◆ __init__()

def querying.connection.__init__ (   self,
  connection_data,
  mode = None,
  map_blobs = False,
  secrets = None,
  pooling = False 
)

Definition at line 37 of file querying.py.

37  def __init__(self, connection_data, mode=None, map_blobs=False, secrets=None, pooling=False):
38 
39  self._pooling = pooling
40 
41  # add querying utility properties
42  # these must belong to the connection since the way in which their values are handled
43  # depends on the database being connected to.
44  self.range = models.Range
45  self.radius = models.Radius
46  self.regexp = models.RegExp
47  self.regexp.connection_object = self
48 
49  if type(connection_data) in [str, unicode]:
50  # if we've been given a connection string, process it
51  self.connection_data = new_connection_dictionary(connection_data, secrets=secrets, mode=mode)
52  self.schema = self.connection_data.get("schema") if self.connection_data.get("schema") != None else ""
53 
54  self.range.database_type = self.connection_data["host"]
55  self.radius.database_type = self.connection_data["host"]
56  self.regexp.database_type = self.connection_data["host"]
57  else:
58  self.connection_data = connection_data
59  # assume we have an engine
60  # we need to take the string representation so we know which type of db we're aiming at
61  engine_string = str(connection_data)
62  db_type = None
63  if "oracle" in engine_string:
64  db_type = "oracle"
65  elif "frontier" in engine_string:
66  db_type = "frontier"
67  elif "sqlite" in engine_string:
68  db_type = "sqlite"
69 
70  self.range.database_type = db_type
71  self.radius.database_type = db_type
72  self.regexp.database_type = db_type
73 
74  from . import models as ms
75  self.models = ms.generate(map_blobs)
76  #self.base = self.models["Base"]
77 

Member Function Documentation

◆ _cms_frontier_string()

def querying.connection._cms_frontier_string (   database,
  schema = "cms_conditions" 
)
staticprivate
Get database string for frontier.

Definition at line 128 of file querying.py.

128  def _cms_frontier_string(database, schema="cms_conditions"):
129  """
130  Get database string for frontier.
131  """
132  import urllib
133  return 'oracle+frontier://@%s/%s' % (urllib.quote_plus(connection._get_CMS_frontier_connection_string(database)), schema)
134 

◆ _cms_oracle_string()

def querying.connection._cms_oracle_string (   user,
  pwd,
  db_name 
)
staticprivate
Get database string for oracle.

Definition at line 136 of file querying.py.

136  def _cms_oracle_string(user, pwd, db_name):
137  """
138  Get database string for oracle.
139  """
140  return 'oracle://%s:%s@%s' % (user, pwd, db_name)
141 

◆ _get_CMS_frontier_connection_string()

def querying.connection._get_CMS_frontier_connection_string (   database)
staticprivate

Definition at line 120 of file querying.py.

120  def _get_CMS_frontier_connection_string(database):
121  try:
122  import subprocess
123  return subprocess.Popen(['cmsGetFnConnect', 'frontier://%s' % database], stdout = subprocess.PIPE).communicate()[0].strip()
124  except:
125  raise Exception("Frontier connections can only be constructed when inside a CMSSW environment.")
126 

References communicate(), and digitizers_cfi.strip.

◆ _oracle_match_format()

def querying.connection._oracle_match_format (   self,
  string 
)
private

Definition at line 227 of file querying.py.

227  def _oracle_match_format(self, string):
228  return "%%%s%%" % string
229 

Referenced by querying.connection.search_everything().

◆ build_frontier_url()

def querying.connection.build_frontier_url (   db_name,
  schema 
)
static

Definition at line 159 of file querying.py.

159  def build_frontier_url(db_name, schema):
160  database_url = connection._cms_frontier_string(db_name, schema)
161 
162  try:
163  url = sqlalchemy.engine.url.make_url(database_url)
164  except sqlalchemy.exc.ArgumentError:
165  """
166  Is this needed for a use case?
167  """
168  url = sqlalchemy.engine.url.make_url('sqlite:///%s' % db_name)
169  return url
170 

◆ build_oracle_url()

def querying.connection.build_oracle_url (   user,
  pwd,
  db_name 
)
static
Build the connection url, and get credentials from self.secrets dictionary.

Definition at line 143 of file querying.py.

143  def build_oracle_url(user, pwd, db_name):
144  """
145  Build the connection url, and get credentials from self.secrets dictionary.
146  """
147 
148  database_url = connection._cms_oracle_string(user, pwd, db_name)
149 
150  try:
151  url = sqlalchemy.engine.url.make_url(database_url)
152  if url.password is None:
153  url.password = pwd
154  except sqlalchemy.exc.ArgumentError:
155  url = sqlalchemy.engine.url.make_url('sqlite:///%s' % db_name)
156  return url
157 

◆ close_session()

def querying.connection.close_session (   self)

Definition at line 180 of file querying.py.

180  def close_session(self):
181  self.session.close()
182  return True
183 

References querying.connection.session.

Referenced by querying.connection.tear_down().

◆ commit()

def querying.connection.commit (   self)

Definition at line 272 of file querying.py.

272  def commit(self):
273  try:
274  self.session.commit()
275  except:
276  traceback.print_exc()
277  self.session.rollback()
278 

References querying.connection.rollback(), and querying.connection.session.

Referenced by querying.connection.tear_down(), and querying.connection.write_and_commit().

◆ global_tag()

def querying.connection.global_tag (   self,
**  pkargs 
)

Definition at line 205 of file querying.py.

205  def global_tag(self, **pkargs):
206  return self.factory.object("globaltag", **pkargs)
207 

References querying.connection.factory, and querying.connection.object().

◆ global_tag_map()

def querying.connection.global_tag_map (   self,
**  pkargs 
)

Definition at line 208 of file querying.py.

208  def global_tag_map(self, **pkargs):
209  return self.factory.object("globaltagmap", **pkargs)
210 

References querying.connection.factory, and querying.connection.object().

◆ hard_close()

def querying.connection.hard_close (   self)

Definition at line 184 of file querying.py.

184  def hard_close(self):
185  self.engine.dispose()
186  return True
187 

References cms::xerces.dispose(), and querying.connection.engine.

◆ iov()

def querying.connection.iov (   self,
**  pkargs 
)

Definition at line 217 of file querying.py.

217  def iov(self, **pkargs):
218  return self.factory.object("iov", **pkargs)
219 

References querying.connection.factory, and querying.connection.object().

◆ model()

def querying.connection.model (   self,
  model_name 
)

Definition at line 189 of file querying.py.

189  def model(self, model_name):
190  if model_name.__class__ == sqlalchemy.ext.declarative.api.DeclarativeMeta:
191  model_name = model_name.__name__
192  model_name = model_name.replace("_", "")
193  return self.models[model_name]
194 

References querying.connection.models.

Referenced by querying.connection.search_everything().

◆ object()

def querying.connection.object (   self,
  model,
  pk_to_value 
)

Definition at line 197 of file querying.py.

197  def object(self, model, pk_to_value):
198  if self.session == None:
199  return None
200  model_data = self.session.query(model)
201  for pk in pk_to_value:
202  model_data = model_data.filter(model.__dict__[pk] == pk_to_value[pk])
203  return model_data.first()
204 

References querying.connection.session.

Referenced by querying.connection.global_tag(), querying.connection.global_tag_map(), querying.connection.iov(), querying.connection.payload(), and querying.connection.tag().

◆ payload()

def querying.connection.payload (   self,
**  pkargs 
)

Definition at line 220 of file querying.py.

220  def payload(self, **pkargs):
221  return self.factory.object("payload", **pkargs)
222 

References querying.connection.factory, and querying.connection.object().

◆ rollback()

def querying.connection.rollback (   self)

Definition at line 288 of file querying.py.

288  def rollback(self):
289  try:
290  self.session.rollback()
291  except:
292  traceback.print_exc()
293  print("Session couldn't be rolled back.")
294 

References print(), and querying.connection.session.

Referenced by querying.connection.commit().

◆ search_everything()

def querying.connection.search_everything (   self,
  string,
  amount = 10 
)

Definition at line 231 of file querying.py.

231  def search_everything(self, string, amount=10):
232  string = self._oracle_match_format(string)
233 
234  gt = self.model("globaltag")
235  global_tags = self.session.query(gt).filter(or_(
236  gt.name.ilike(string),
237  gt.description.ilike(string),
238  gt.release.ilike(string)
239  )).limit(amount)
240  tag = self.model("tag")
241  tags = self.session.query(tag).filter(or_(
242  tag.name.ilike(string),
243  tag.object_type.ilike(string),
244  tag.description.ilike(string))
245  ).limit(amount)
246  iov = self.model("iov")
247  iovs = self.session.query(iov).filter(or_(
248  iov.tag_name.ilike(string),
249  iov.since.ilike(string),
250  iov.payload_hash.ilike(string),
251  iov.insertion_time.ilike(string)
252  )).limit(amount)
253  payload = self.model("payload")
254  payloads = self.session.query(payload).filter(or_(
255  payload.hash.ilike(string),
256  payload.object_type.ilike(string),
257  payload.insertion_time.ilike(string)
258  )).limit(amount)
259 
260  return json_data_node.make({
261  "global_tags" : global_tags.all(),
262  "tags" : tags.all(),
263  "iovs" : iovs.all(),
264  "payloads" : payloads.all()
265  })
266 

References querying.connection._oracle_match_format(), ALCARECOTkAlBeamHalo_cff.filter, remoteMonitoring_LED_IterMethod_cfg.limit, TritonService::Module.model, querying.connection.model(), and querying.connection.session.

◆ setup()

def querying.connection.setup (   self)
Setup engine with given credentials from netrc file, and make a session maker.

Definition at line 78 of file querying.py.

78  def setup(self):
79  """
80  Setup engine with given credentials from netrc file, and make a session maker.
81  """
82 
83  if isinstance(self.connection_data, dict):
84  self.engine = engine_from_dictionary(self.connection_data, pooling=self._pooling)
85  else:
86  # we've been given an engine by the user
87  # use it as the engine
88  self.engine = self.connection_data
89 
90  self.sessionmaker = sessionmaker(bind=self.engine)
91  self.session = self.sessionmaker()
92  self.factory = factory(self)
93 
94  # assign correct schema for database name to each model
95  tmp_models_dict = {}
96  for key in self.models:
97  if self.models[key].__class__ == sqlalchemy.ext.declarative.api.DeclarativeMeta\
98  and str(self.models[key].__name__) != "Base":
99 
100  if isinstance(self.connection_data, dict):
101  # we can only extract the secrets and schema individuall
102  # if we were given a dictionary... if we were given an engine
103  # we can't do this without parsing the connection string from the engine
104  # - a wide range of which it will be difficult to support!
105  self.models[key].__table__.schema = self.connection_data["schema"]
106  self.models[key].secrets = self.connection_data["secrets"]
107 
108  self.models[key].session = self.session
109  # isn't used anywhere - comment it out for now
110  #self.models[key].authentication = self.netrc_authenticators
111  self.models[key].connection = self
112  tmp_models_dict[key.lower()] = self.models[key]
113  tmp_models_dict[key.lower()].empty = False
114 
115  self.models = tmp_models_dict
116 
117  return self
118 

References querying.connection.connection_data.

Referenced by o2olib.O2OTool.execute().

◆ tag()

def querying.connection.tag (   self,
**  pkargs 
)

Definition at line 214 of file querying.py.

214  def tag(self, **pkargs):
215  return self.factory.object("tag", **pkargs)
216 

References querying.connection.factory, and querying.connection.object().

◆ tear_down()

def querying.connection.tear_down (   self)

Definition at line 173 of file querying.py.

173  def tear_down(self):
174  try:
175  self.session.commit()
176  self.close_session()
177  except:
178  return "Couldn't tear down connection on engine %s." % str(self.engine)
179 

References querying.connection.close_session(), querying.connection.commit(), querying.connection.engine, querying.connection.session, and str.

◆ write()

def querying.connection.write (   self,
  object 
)

Definition at line 267 of file querying.py.

267  def write(self, object):
268  new_object = models.session_independent_object(object, schema=self.schema)
269  self.session.add(new_object)
270  return new_object
271 

References PVValHelper.add(), querying.connection.schema, querying.connection.session, and models.session_independent_object().

Referenced by pkg.AbstractPkg.generate(), and querying.connection.write_and_commit().

◆ write_and_commit()

def querying.connection.write_and_commit (   self,
  object 
)

Member Data Documentation

◆ _pooling

querying.connection._pooling
private

Definition at line 39 of file querying.py.

◆ connection_data

querying.connection.connection_data

Definition at line 51 of file querying.py.

Referenced by querying.connection.setup().

◆ engine

querying.connection.engine

◆ factory

querying.connection.factory

◆ models

querying.connection.models

Definition at line 75 of file querying.py.

Referenced by querying.connection.model().

◆ radius

querying.connection.radius

Definition at line 45 of file querying.py.

◆ range

querying.connection.range

Definition at line 44 of file querying.py.

◆ regexp

querying.connection.regexp

Definition at line 46 of file querying.py.

◆ schema

querying.connection.schema

Definition at line 52 of file querying.py.

Referenced by o2o_db_manager.DbManager.get_url(), and querying.connection.write().

◆ session

querying.connection.session

◆ sessionmaker

querying.connection.sessionmaker

Definition at line 90 of file querying.py.

resolutioncreator_cfi.object
object
Definition: resolutioncreator_cfi.py:4
digitizers_cfi.strip
strip
Definition: digitizers_cfi.py:19
cms::xerces::dispose
void dispose(XMLCh *ptr)
Definition: XercesStrUtils.h:15
querying.new_connection_dictionary
def new_connection_dictionary(connection_data, secrets=None, mode="r")
Definition: querying.py:349
models.Radius
Definition: models.py:102
models.RegExp
Definition: models.py:132
singleTopDQM_cfi.setup
setup
Definition: singleTopDQM_cfi.py:37
ReggeGribovPartonMC_EposLHC_2760GeV_PbPb_cfi.model
model
Definition: ReggeGribovPartonMC_EposLHC_2760GeV_PbPb_cfi.py:11
query
Definition: query.py:1
GlobalPosition_Frontier_DevDB_cff.tag
tag
Definition: GlobalPosition_Frontier_DevDB_cff.py:11
str
#define str(s)
Definition: TestProcessor.cc:52
communicate
static void * communicate(void *obj)
Definition: DQMNet.cc:1049
jets_cff.payload
payload
Definition: jets_cff.py:32
ALCARECOTkAlBeamHalo_cff.filter
filter
Definition: ALCARECOTkAlBeamHalo_cff.py:27
print
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:46
PVValHelper::add
void add(std::map< std::string, TH1 * > &h, TH1 *hist)
Definition: PVValidationHelpers.cc:12
Exception
writeEcalDQMStatus.write
write
Definition: writeEcalDQMStatus.py:48
querying.engine_from_dictionary
def engine_from_dictionary(dictionary, pooling=True)
Definition: querying.py:428
models.Range
Definition: models.py:118
remoteMonitoring_LED_IterMethod_cfg.limit
limit
Definition: remoteMonitoring_LED_IterMethod_cfg.py:427
models.session_independent_object
def session_independent_object(object, schema=None)
Definition: models.py:33