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 23 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 35 of file querying.py.

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

126  def _cms_frontier_string(database, schema="cms_conditions"):
127  """
128  Get database string for frontier.
129  """
130  import urllib
131  return 'oracle+frontier://@%s/%s' % (urllib.quote_plus(connection._get_CMS_frontier_connection_string(database)), schema)
132 
def _cms_frontier_string(database, schema="cms_conditions")
Definition: querying.py:126
def querying.connection._cms_oracle_string (   user,
  pwd,
  db_name 
)
staticprivate
Get database string for oracle.

Definition at line 134 of file querying.py.

134  def _cms_oracle_string(user, pwd, db_name):
135  """
136  Get database string for oracle.
137  """
138  return 'oracle://%s:%s@%s' % (user, pwd, db_name)
139 
def _cms_oracle_string(user, pwd, db_name)
Definition: querying.py:134
def querying.connection._get_CMS_frontier_connection_string (   database)
staticprivate

Definition at line 118 of file querying.py.

References communicate(), and digitizers_cfi.strip.

119  try:
120  import subprocess
121  return subprocess.Popen(['cmsGetFnConnect', 'frontier://%s' % database], stdout = subprocess.PIPE).communicate()[0].strip()
122  except:
123  raise Exception("Frontier connections can only be constructed when inside a CMSSW environment.")
124 
static void * communicate(void *obj)
Definition: DQMNet.cc:1251
def _get_CMS_frontier_connection_string(database)
Definition: querying.py:118
def querying.connection._oracle_match_format (   self,
  string 
)
private

Definition at line 225 of file querying.py.

Referenced by querying.connection.search_everything().

225  def _oracle_match_format(self, string):
226  return "%%%s%%" % string
227 
def _oracle_match_format(self, string)
Definition: querying.py:225
def querying.connection.build_frontier_url (   db_name,
  schema 
)
static

Definition at line 157 of file querying.py.

157  def build_frontier_url(db_name, schema):
158  database_url = connection._cms_frontier_string(db_name, schema)
159 
160  try:
161  url = sqlalchemy.engine.url.make_url(database_url)
162  except sqlalchemy.exc.ArgumentError:
163  """
164  Is this needed for a use case?
165  """
166  url = sqlalchemy.engine.url.make_url('sqlite:///%s' % db_name)
167  return url
168 
def build_frontier_url(db_name, schema)
Definition: querying.py:157
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 141 of file querying.py.

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

Definition at line 178 of file querying.py.

Referenced by querying.connection.tear_down().

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

Definition at line 270 of file querying.py.

Referenced by querying.connection.write_and_commit().

270  def commit(self):
271  try:
272  self.session.commit()
273  except:
274  traceback.print_exc()
275  self.session.rollback()
276 
def commit(self)
Definition: querying.py:270
def querying.connection.global_tag (   self,
  pkargs 
)

Definition at line 203 of file querying.py.

203  def global_tag(self, **pkargs):
204  return self.factory.object("globaltag", **pkargs)
205 
def global_tag(self, pkargs)
Definition: querying.py:203
def querying.connection.global_tag_map (   self,
  pkargs 
)

Definition at line 206 of file querying.py.

206  def global_tag_map(self, **pkargs):
207  return self.factory.object("globaltagmap", **pkargs)
208 
def global_tag_map(self, pkargs)
Definition: querying.py:206
def querying.connection.hard_close (   self)

Definition at line 182 of file querying.py.

182  def hard_close(self):
183  self.engine.dispose()
184  return True
185 
def hard_close(self)
Definition: querying.py:182
def querying.connection.iov (   self,
  pkargs 
)

Definition at line 215 of file querying.py.

215  def iov(self, **pkargs):
216  return self.factory.object("iov", **pkargs)
217 
def iov(self, pkargs)
Definition: querying.py:215
def querying.connection.model (   self,
  model_name 
)

Definition at line 187 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().

187  def model(self, model_name):
188  if model_name.__class__ == sqlalchemy.ext.declarative.api.DeclarativeMeta:
189  model_name = model_name.__name__
190  model_name = model_name.replace("_", "")
191  return self.models[model_name]
192 
def model(self, model_name)
Definition: querying.py:187
def querying.connection.object (   self,
  model,
  pk_to_value 
)

Definition at line 195 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().

195  def object(self, model, pk_to_value):
196  if self.session == None:
197  return None
198  model_data = self.session.query(model)
199  for pk in pk_to_value:
200  model_data = model_data.filter(model.__dict__[pk] == pk_to_value[pk])
201  return model_data.first()
202 
def object(self, model, pk_to_value)
Definition: querying.py:195
def querying.connection.payload (   self,
  pkargs 
)

Definition at line 218 of file querying.py.

218  def payload(self, **pkargs):
219  return self.factory.object("payload", **pkargs)
220 
def payload(self, pkargs)
Definition: querying.py:218
def querying.connection.rollback (   self)

Definition at line 286 of file querying.py.

References edm.print().

286  def rollback(self):
287  try:
288  self.session.rollback()
289  except:
290  traceback.print_exc()
291  print("Session couldn't be rolled back.")
292 
def rollback(self)
Definition: querying.py:286
S & print(S &os, JobReport::InputFile const &f)
Definition: JobReport.cc:65
def querying.connection.search_everything (   self,
  string,
  amount = 10 
)

Definition at line 229 of file querying.py.

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

229  def search_everything(self, string, amount=10):
230  string = self._oracle_match_format(string)
231 
232  gt = self.model("globaltag")
233  global_tags = self.session.query(gt).filter(or_(
234  gt.name.ilike(string),
235  gt.description.ilike(string),
236  gt.release.ilike(string)
237  )).limit(amount)
238  tag = self.model("tag")
239  tags = self.session.query(tag).filter(or_(
240  tag.name.ilike(string),
241  tag.object_type.ilike(string),
242  tag.description.ilike(string))
243  ).limit(amount)
244  iov = self.model("iov")
245  iovs = self.session.query(iov).filter(or_(
246  iov.tag_name.ilike(string),
247  iov.since.ilike(string),
248  iov.payload_hash.ilike(string),
249  iov.insertion_time.ilike(string)
250  )).limit(amount)
251  payload = self.model("payload")
252  payloads = self.session.query(payload).filter(or_(
253  payload.hash.ilike(string),
254  payload.object_type.ilike(string),
255  payload.insertion_time.ilike(string)
256  )).limit(amount)
257 
258  return json_data_node.make({
259  "global_tags" : global_tags.all(),
260  "tags" : tags.all(),
261  "iovs" : iovs.all(),
262  "payloads" : payloads.all()
263  })
264 
def model(self, model_name)
Definition: querying.py:187
def _oracle_match_format(self, string)
Definition: querying.py:225
def search_everything(self, string, amount=10)
Definition: querying.py:229
def querying.connection.setup (   self)
Setup engine with given credentials from netrc file, and make a session maker.

Definition at line 76 of file querying.py.

References querying.connection.connection_data.

Referenced by o2olib.O2OTool.execute().

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

Definition at line 212 of file querying.py.

212  def tag(self, **pkargs):
213  return self.factory.object("tag", **pkargs)
214 
def tag(self, pkargs)
Definition: querying.py:212
def querying.connection.tear_down (   self)

Definition at line 171 of file querying.py.

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

171  def tear_down(self):
172  try:
173  self.session.commit()
174  self.close_session()
175  except:
176  return "Couldn't tear down connection on engine %s." % str(self.engine)
177 
def close_session(self)
Definition: querying.py:178
def tear_down(self)
Definition: querying.py:171
#define str(s)
def querying.connection.write (   self,
  object 
)

Definition at line 265 of file querying.py.

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

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

265  def write(self, object):
266  new_object = models.session_independent_object(object, schema=self.schema)
267  self.session.add(new_object)
268  return new_object
269 
def session_independent_object(object, schema=None)
Definition: models.py:31
def write(self, object)
Definition: querying.py:265
def querying.connection.write_and_commit (   self,
  object 
)

Member Data Documentation

querying.connection._pooling
private

Definition at line 37 of file querying.py.

querying.connection.connection_data

Definition at line 49 of file querying.py.

Referenced by querying.connection.setup().

querying.connection.engine
querying.connection.factory

Definition at line 90 of file querying.py.

querying.connection.models

Definition at line 73 of file querying.py.

Referenced by querying.connection.model().

querying.connection.radius

Definition at line 43 of file querying.py.

querying.connection.range

Definition at line 42 of file querying.py.

querying.connection.regexp

Definition at line 44 of file querying.py.

querying.connection.schema

Definition at line 50 of file querying.py.

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

querying.connection.session

Definition at line 89 of file querying.py.

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

querying.connection.sessionmaker

Definition at line 88 of file querying.py.