__all__.append('ContentFilename')
+@session_wrapper
def get_or_set_contents_file_id(filename, session=None):
"""
Returns database id for given filename.
@rtype: int
@return: the database id for the given component
"""
- privatetrans = False
- if session is None:
- session = DBConn().session()
- privatetrans = True
q = session.query(ContentFilename).filter_by(filename=filename)
cf = ContentFilename()
cf.filename = filename
session.add(cf)
- if privatetrans:
- session.commit()
- else:
- session.flush()
+ session.commit_or_flush()
ret = cf.cafilename_id
- if privatetrans:
- session.close()
-
return ret
__all__.append('get_or_set_contents_file_id')
__all__.append('ContentFilepath')
+@session_wrapper
def get_or_set_contents_path_id(filepath, session=None):
"""
Returns database id for given path.
@rtype: int
@return: the database id for the given path
"""
- privatetrans = False
- if session is None:
- session = DBConn().session()
- privatetrans = True
q = session.query(ContentFilepath).filter_by(filepath=filepath)
cf = ContentFilepath()
cf.filepath = filepath
session.add(cf)
- if privatetrans:
- session.commit()
- else:
- session.flush()
+ session.commit_or_flush()
ret = cf.cafilepath_id
- if privatetrans:
- session.close()
-
return ret
__all__.append('get_or_set_contents_path_id')
__all__.append('Fingerprint')
+@session_wrapper
def get_or_set_fingerprint(fpr, session=None):
"""
Returns Fingerprint object for given fpr.
@rtype: Fingerprint
@return: the Fingerprint object for the given fpr
"""
- privatetrans = False
- if session is None:
- session = DBConn().session()
- privatetrans = True
q = session.query(Fingerprint).filter_by(fingerprint=fpr)
fingerprint = Fingerprint()
fingerprint.fingerprint = fpr
session.add(fingerprint)
- if privatetrans:
- session.commit()
- else:
- session.flush()
+ session.commit_or_flush()
ret = fingerprint
- if privatetrans:
- session.close()
-
return ret
__all__.append('get_or_set_fingerprint')
__all__.append('Keyring')
+@session_wrapper
def get_or_set_keyring(keyring, session=None):
"""
If C{keyring} does not have an entry in the C{keyrings} table yet, create one
@rtype: Keyring
@return: the Keyring object for this keyring
-
"""
- privatetrans = False
- if session is None:
- session = DBConn().session()
- privatetrans = True
- try:
- obj = session.query(Keyring).filter_by(keyring_name=keyring).first()
-
- if obj is None:
- obj = Keyring(keyring_name=keyring)
- session.add(obj)
- if privatetrans:
- session.commit()
- else:
- session.flush()
+ q = session.query(Keyring).filter_by(keyring_name=keyring)
+ try:
+ return q.one()
+ except NoResultFound:
+ obj = Keyring(keyring_name=keyring)
+ session.add(obj)
+ session.commit_or_flush()
return obj
- finally:
- if privatetrans:
- session.close()
__all__.append('get_or_set_keyring')
__all__.append('Maintainer')
+@session_wrapper
def get_or_set_maintainer(name, session=None):
"""
Returns Maintainer object for given maintainer name.
@rtype: Maintainer
@return: the Maintainer object for the given maintainer
"""
- privatetrans = False
- if session is None:
- session = DBConn().session()
- privatetrans = True
q = session.query(Maintainer).filter_by(name=name)
try:
maintainer = Maintainer()
maintainer.name = name
session.add(maintainer)
- if privatetrans:
- session.commit()
- else:
- session.flush()
+ session.commit_or_flush()
ret = maintainer
- if privatetrans:
- session.close()
-
return ret
__all__.append('get_or_set_maintainer')
+@session_wrapper
def get_maintainer(maintainer_id, session=None):
"""
Return the name of the maintainer behind C{maintainer_id} or None if that
@return: the Maintainer with this C{maintainer_id}
"""
- privatetrans = False
- if session is None:
- session = DBConn().session()
- privatetrans = True
-
- try:
- return session.query(Maintainer).get(maintainer_id)
- finally:
- if privatetrans:
- session.close()
+ return session.query(Maintainer).get(maintainer_id)
__all__.append('get_maintainer')
__all__.append('Uid')
+@session_wrapper
def add_database_user(uidname, session=None):
"""
Adds a database user
@return: the uid object for the given uidname
"""
- privatetrans = False
- if session is None:
- session = DBConn().session()
- privatetrans = True
-
session.execute("CREATE USER :uid", {'uid': uidname})
-
- if privatetrans:
- session.commit()
- session.close()
+ session.commit_or_flush()
__all__.append('add_database_user')
+@session_wrapper
def get_or_set_uid(uidname, session=None):
"""
Returns uid object for given uidname.
@return: the uid object for the given uidname
"""
- privatetrans = False
- if session is None:
- session = DBConn().session()
- privatetrans = True
-
q = session.query(Uid).filter_by(uid=uidname)
try:
uid = Uid()
uid.uid = uidname
session.add(uid)
- if privatetrans:
- session.commit()
- else:
- session.flush()
+ session.commit_or_flush()
ret = uid
- if privatetrans:
- session.close()
-
return ret
__all__.append('get_or_set_uid')