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 24 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 36 of file querying.py.

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

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

Definition at line 135 of file querying.py.

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

Definition at line 119 of file querying.py.

References communicate(), and digitizers_cfi.strip.

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

Definition at line 226 of file querying.py.

Referenced by querying.connection.search_everything().

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

Definition at line 158 of file querying.py.

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

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

Definition at line 179 of file querying.py.

Referenced by querying.connection.tear_down().

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

Definition at line 271 of file querying.py.

Referenced by querying.connection.write_and_commit().

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

Definition at line 204 of file querying.py.

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

Definition at line 207 of file querying.py.

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

Definition at line 183 of file querying.py.

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

Definition at line 216 of file querying.py.

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

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

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

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

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

Definition at line 219 of file querying.py.

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

Definition at line 287 of file querying.py.

References edm.print().

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

Definition at line 230 of file querying.py.

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

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

Definition at line 77 of file querying.py.

References querying.connection.connection_data.

Referenced by o2olib.O2OTool.execute().

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

Definition at line 213 of file querying.py.

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

Definition at line 172 of file querying.py.

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

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

Definition at line 266 of file querying.py.

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

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

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

Member Data Documentation

querying.connection._pooling
private

Definition at line 38 of file querying.py.

querying.connection.connection_data

Definition at line 50 of file querying.py.

Referenced by querying.connection.setup().

querying.connection.engine
querying.connection.factory

Definition at line 91 of file querying.py.

querying.connection.models

Definition at line 74 of file querying.py.

Referenced by querying.connection.model().

querying.connection.radius

Definition at line 44 of file querying.py.

querying.connection.range

Definition at line 43 of file querying.py.

querying.connection.regexp

Definition at line 45 of file querying.py.

querying.connection.schema

Definition at line 51 of file querying.py.

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

querying.connection.session

Definition at line 90 of file querying.py.

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

querying.connection.sessionmaker

Definition at line 89 of file querying.py.