def set_metadata_bulk(self, scope, name, meta, recursive=False, session=None): if not self.json_implemented(session): raise NotImplementedError if session.query(models.DataIdentifier).filter_by(scope=scope, name=name).one_or_none() is None: raise exception.DataIdentifierNotFound("Data identifier '%s:%s' not found" % (scope, name)) row_did_meta = session.query(models.DidMeta).filter_by(scope=scope, name=name).scalar() if row_did_meta is None: # Add metadata column to new table (if not already present) row_did_meta = models.DidMeta(scope=scope, name=name) row_did_meta.save(session=session, flush=False) existing_meta = {} if hasattr(row_did_meta, 'meta'): if row_did_meta.meta: existing_meta = row_did_meta.meta # Oracle returns a string instead of a dict if session.bind.dialect.name in ['oracle', 'sqlite'] and existing_meta: existing_meta = json_lib.loads(existing_meta) for key, value in meta.items(): existing_meta[key] = value row_did_meta.meta = None session.flush() # Oracle insert takes a string as input if session.bind.dialect.name in ['oracle', 'sqlite']: existing_meta = json_lib.dumps(existing_meta) row_did_meta.meta = existing_meta row_did_meta.save(session=session, flush=True)
def set_metadata(self, scope, name, key, value, recursive, session=None): """ Add or update the given metadata to the given did :param scope: the scope of the did :param name: the name of the did :param meta: the metadata to be added or updated """ if not self.json_implemented(session): raise NotImplementedError try: row_did = session.query(models.DataIdentifier).filter_by( scope=scope, name=name).one() row_did_meta = session.query(models.DidMeta).filter_by( scope=scope, name=name).scalar() if row_did_meta is None: # Add metadata column to new table (if not already present) row_did_meta = models.DidMeta(scope=scope, name=name) row_did_meta.save(session=session, flush=False) existing_meta = getattr(row_did_meta, 'meta') # Oracle returns a string instead of a dict if session.bind.dialect.name in ['oracle', 'sqlite' ] and existing_meta is not None: existing_meta = json_lib.loads(existing_meta) if existing_meta is None: existing_meta = {} # for k, v in iteritems(meta): # existing_meta[k] = v existing_meta[key] = value row_did_meta.meta = None session.flush() # Oracle insert takes a string as input if session.bind.dialect.name in ['oracle', 'sqlite']: existing_meta = json_lib.dumps(existing_meta) row_did_meta.meta = existing_meta row_did_meta.save(session=session, flush=True) except NoResultFound: raise exception.DataIdentifierNotFound( "Data identifier '%(scope)s:%(name)s' not found" % locals())