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

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.
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 
def new_connection_dictionary(connection_data, secrets=None, mode="r")
Definition: querying.py:349
def __init__(self, connection_data, mode=None, map_blobs=False, secrets=None, pooling=False)
Definition: querying.py:37
#define str(s)

Member Function Documentation

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 
def _cms_frontier_string(database, schema="cms_conditions")
Definition: querying.py:128
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 
def _cms_oracle_string(user, pwd, db_name)
Definition: querying.py:136
def querying.connection._get_CMS_frontier_connection_string (   database)
staticprivate

Definition at line 120 of file querying.py.

References communicate(), and digitizers_cfi.strip.

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 
static void * communicate(void *obj)
Definition: DQMNet.cc:1251
def _get_CMS_frontier_connection_string(database)
Definition: querying.py:120
def querying.connection._oracle_match_format (   self,
  string 
)
private

Definition at line 227 of file querying.py.

Referenced by querying.connection.search_everything().

227  def _oracle_match_format(self, string):
228  return "%%%s%%" % string
229 
def _oracle_match_format(self, string)
Definition: querying.py:227
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 
def build_frontier_url(db_name, schema)
Definition: querying.py:159
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 
def build_oracle_url(user, pwd, db_name)
Definition: querying.py:143
def querying.connection.close_session (   self)

Definition at line 180 of file querying.py.

Referenced by querying.connection.tear_down().

180  def close_session(self):
181  self.session.close()
182  return True
183 
def close_session(self)
Definition: querying.py:180
def querying.connection.commit (   self)

Definition at line 272 of file querying.py.

Referenced by querying.connection.write_and_commit().

272  def commit(self):
273  try:
274  self.session.commit()
275  except:
276  traceback.print_exc()
277  self.session.rollback()
278 
def commit(self)
Definition: querying.py:272
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 
def global_tag(self, pkargs)
Definition: querying.py:205
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 
def global_tag_map(self, pkargs)
Definition: querying.py:208
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 
def hard_close(self)
Definition: querying.py:184
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 
def iov(self, pkargs)
Definition: querying.py:217
def querying.connection.model (   self,
  model_name 
)

Definition at line 189 of file querying.py.

References querying.connection.models.

Referenced by querying.connection.search_everything(), Vispa.Gui.Header.Header.setText(), and Vispa.Gui.SimpleDraggableTreeWidget.SimpleDraggableTreeWidget.startDrag().

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 
def model(self, model_name)
Definition: querying.py:189
def querying.connection.object (   self,
  model,
  pk_to_value 
)

Definition at line 197 of file querying.py.

References querying.connection.session.

Referenced by Vispa.Views.LineDecayView.LineDecayContainer.createObject(), Vispa.Views.LineDecayView.LineDecayContainer.delete(), Vispa.Views.LineDecayView.DecayLine.delete(), Vispa.Views.LineDecayView.LineDecayContainer.mouseReleaseEvent(), and Vispa.Views.BoxDecayView.BoxDecayContainer.toggleCollapse().

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 
def object(self, model, pk_to_value)
Definition: querying.py:197
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 
def payload(self, pkargs)
Definition: querying.py:220
def querying.connection.rollback (   self)

Definition at line 288 of file querying.py.

References edm.print().

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 
def rollback(self)
Definition: querying.py:288
S & print(S &os, JobReport::InputFile const &f)
Definition: JobReport.cc:66
def querying.connection.search_everything (   self,
  string,
  amount = 10 
)

Definition at line 231 of file querying.py.

References querying.connection._oracle_match_format(), ALCARECOTkAlBeamHalo_cff.filter, MessageLogger_cfi.limit, and querying.connection.model().

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 
def model(self, model_name)
Definition: querying.py:189
def _oracle_match_format(self, string)
Definition: querying.py:227
def search_everything(self, string, amount=10)
Definition: querying.py:231
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.

References querying.connection.connection_data.

Referenced by o2olib.O2OTool.execute().

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):
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 
def setup(self)
Definition: querying.py:78
def engine_from_dictionary(dictionary, pooling=True)
Definition: querying.py:428
#define str(s)
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 
def tag(self, pkargs)
Definition: querying.py:214
def querying.connection.tear_down (   self)

Definition at line 173 of file querying.py.

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

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 
def close_session(self)
Definition: querying.py:180
def tear_down(self)
Definition: querying.py:173
#define str(s)
def querying.connection.write (   self,
  object 
)

Definition at line 267 of file querying.py.

References querying.connection.schema, and models.session_independent_object().

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

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 
def session_independent_object(object, schema=None)
Definition: models.py:33
def write(self, object)
Definition: querying.py:267
def querying.connection.write_and_commit (   self,
  object 
)

Member Data Documentation

querying.connection._pooling
private

Definition at line 39 of file querying.py.

querying.connection.connection_data

Definition at line 51 of file querying.py.

Referenced by querying.connection.setup().

querying.connection.engine
querying.connection.factory

Definition at line 92 of file querying.py.

querying.connection.models

Definition at line 75 of file querying.py.

Referenced by querying.connection.model().

querying.connection.radius

Definition at line 45 of file querying.py.

querying.connection.range

Definition at line 44 of file querying.py.

querying.connection.regexp

Definition at line 46 of file querying.py.

querying.connection.schema

Definition at line 52 of file querying.py.

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

querying.connection.session

Definition at line 91 of file querying.py.

Referenced by querying.connection.object(), and o2olib.O2OJobMgr.runManager().

querying.connection.sessionmaker

Definition at line 90 of file querying.py.