CMS 3D CMS Logo

Classes | Functions
querying Namespace Reference

Classes

class  connection
 
class  factory
 

Functions

def _get_netrc_data (netrc_file, key)
 
def connect (connection_data, mode="r", map_blobs=False, secrets=None, pooling=True)
 
def engine_from_dictionary (dictionary, pooling=True)
 
def new_connection_dictionary (connection_data, secrets=None, mode="r")
 

Detailed Description

connection class translates either a connection string for sqlite, oracle of frontier into a connection object.
Also sets up ORM with SQLAlchemy.

connection class can also take a pre-constructed engine - useful for web services.

Function Documentation

◆ _get_netrc_data()

def querying._get_netrc_data (   netrc_file,
  key 
)
private
Returns a dictionary {login : ..., account : ..., password : ...}

Definition at line 337 of file querying.py.

References reco.zip().

Referenced by new_connection_dictionary().

337 def _get_netrc_data(netrc_file, key):
338  """
339  Returns a dictionary {login : ..., account : ..., password : ...}
340  """
341  try:
342  headers = ["login", "account", "password"]
343  authenticator_tuple = netrc.netrc(netrc_file).authenticators(key)
344  if authenticator_tuple == None:
345  raise Exception("netrc file must contain key '%s'." % key)
346  except:
347  raise Exception("Couldn't get credentials from netrc file.")
348  return dict(list(zip(headers, authenticator_tuple)))
349 
ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE constexpr float zip(ConstView const &tracks, int32_t i)
Definition: TracksSoA.h:90
def _get_netrc_data(netrc_file, key)
Definition: querying.py:337

◆ connect()

def querying.connect (   connection_data,
  mode = "r",
  map_blobs = False,
  secrets = None,
  pooling = True 
)
Utility method for user - set up a connection object.

Definition at line 453 of file querying.py.

Referenced by shell.connect(), command_line.copy_global_tag(), command_line.copy_tag(), command_line.diff_of_gts(), command_line.diff_of_tags(), command_line.list_object(), command_line.search(), uploads.uploader.send_payloads(), and payload_tests.payload_tests.test_write_blob_to_sqlite().

453 def connect(connection_data, mode="r", map_blobs=False, secrets=None, pooling=True):
454  """
455  Utility method for user - set up a connection object.
456  """
457  con = connection(connection_data=connection_data, mode=mode, map_blobs=map_blobs, secrets=secrets, pooling=pooling)
458  con = con.setup()
459  return con
def connect(connection_data, mode="r", map_blobs=False, secrets=None, pooling=True)
Definition: querying.py:453

◆ engine_from_dictionary()

def querying.engine_from_dictionary (   dictionary,
  pooling = True 
)

Definition at line 429 of file querying.py.

429 def engine_from_dictionary(dictionary, pooling=True):
430  if dictionary["host"] != "sqlite":
431  if dictionary["host"] != "frontier":
432  # probably oracle
433  # if not frontier, we have to authenticate
434  user = dictionary["secrets"]["login"]
435  pwd = dictionary["secrets"]["password"]
436  # set max label length for oracle
437  if pooling:
438  return create_engine(connection.build_oracle_url(user, pwd, dictionary["database_name"]), label_length=6)
439  else:
440  return create_engine(connection.build_oracle_url(user, pwd, dictionary["database_name"]), label_length=6, poolclass=NullPool)
441  else:
442  # if frontier, no need to authenticate
443  # set max label length for frontier
444  if pooling:
445  return create_engine(connection.build_frontier_url(dictionary["database_name"], dictionary["schema"]), label_length=6)
446  else:
447  return create_engine(connection.build_frontier_url(dictionary["database_name"], dictionary["schema"]), label_length=6, poolclass=NullPool)
448  else:
449  # if host is sqlite, making the url is easy - no authentication
450  return create_engine("sqlite:///%s" % dictionary["database_name"])
451 
452 
def engine_from_dictionary(dictionary, pooling=True)
Definition: querying.py:429

◆ new_connection_dictionary()

def querying.new_connection_dictionary (   connection_data,
  secrets = None,
  mode = "r" 
)
Function used to construct connection data dictionaries - internal to framework.

Definition at line 350 of file querying.py.

References _get_netrc_data(), input, submitPVValidationJobs.split(), and str.

350 def new_connection_dictionary(connection_data, secrets=None, mode="r"):
351  """
352  Function used to construct connection data dictionaries - internal to framework.
353  """
354  frontier_str_length = len("frontier://")
355  sqlite_str_length = len("sqlite://")
356  #sqlite_file_str_length = len("sqlite_file://")
357  oracle_str_length = len("oracle://")
358 
359  if type(connection_data) in [str, str] and connection_data[0:frontier_str_length] == "frontier://":
360  """
361  frontier://database_name/schema
362  """
363  db_name = connection_data[frontier_str_length:].split("/")[0]
364  schema = connection_data[frontier_str_length:].split("/")[1]
365  connection_data = {}
366  connection_data["database_name"] = db_name
367  connection_data["schema"] = schema
368  connection_data["host"] = "frontier"
369  connection_data["secrets"] = None
370  elif type(connection_data) in [str, str] and connection_data[0:sqlite_str_length] == "sqlite://":
371  """
372  sqlite://database_file_name
373  """
374  # for now, just support "sqlite://" format for sqlite connection strings
375  db_name = connection_data[sqlite_str_length:]
376  schema = ""
377  connection_data = {}
378  connection_data["database_name"] = os.path.abspath(db_name)
379  connection_data["schema"] = schema
380  connection_data["host"] = "sqlite"
381  connection_data["secrets"] = None
382  elif type(connection_data) in [str, str] and connection_data[0:oracle_str_length] == "oracle://":
383  """
384  oracle://account:password@database_name
385  or
386  oracle://database_name/schema (requires a separate method of authentication - either dictionary or netrc)
387  """
388  new_connection_string = connection_data[oracle_str_length:]
389 
390  if ":" in new_connection_string:
391  # the user has given a password - usually in the case of the db upload service
392  database_name = new_connection_string[new_connection_string.index("@")+1:]
393  schema_name = new_connection_string[0:new_connection_string.index(":")]
394  # set username based on connection string
395  username = new_connection_string[0:new_connection_string.index(":")]
396  password = new_connection_string[new_connection_string.index(":")+1:new_connection_string.index("@")]
397  else:
398  mode_to_netrc_key_suffix = {"r" : "read", "w" : "write"}
399  database_name = new_connection_string[0:new_connection_string.index("/")]
400  schema_name = new_connection_string[new_connection_string.index("/")+1:]
401  if secrets == None:
402  username = str(input("Enter the username you want to connect to the schema '%s' with: " % (schema_name)))
403  password = str(input("Enter the password for the user '%s' in database '%s': " % (username, database_name)))
404  else:
405  if type(secrets) == str:
406  netrc_key = "%s/%s/%s" % (database_name, schema_name, mode_to_netrc_key_suffix[mode])
407  netrc_data = _get_netrc_data(secrets, key=netrc_key)
408  # take the username from the netrc entry corresponding to the mode the database is opened in
409  # eg, if the user has given mode="read", the database_name/schema_name/read entry will be taken
410  username = netrc_data["login"]
411  password = netrc_data["password"]
412  elif type(secrets) == dict:
413  username = secrets["user"]
414  password = secrets["password"]
415  else:
416  raise Exception("Invalid type given for secrets. Either an str or a dict must be given.")
417 
418  #print("Connected to database %s, schema %s, with username %s." % (database_name, schema_name, username))
419 
420  connection_data = {}
421  connection_data["database_name"] = database_name
422  connection_data["schema"] = schema_name
423  connection_data["password"] = password
424  connection_data["host"] = "oracle"
425  connection_data["secrets"] = {"login" : username, "password" : password}
426 
427  return connection_data
428 
static std::string const input
Definition: EdmProvDump.cc:50
def new_connection_dictionary(connection_data, secrets=None, mode="r")
Definition: querying.py:350
#define str(s)
def _get_netrc_data(netrc_file, key)
Definition: querying.py:337