def find_or_create(self, table: str, insert_hash: dict) -> Dict[str, Any]: """Select a single row from the database matching the hash or insert a row with the hash values and return the inserted row as a hash.""" # FIXME probably do this in a serialized transaction? table = decode_object_from_bytes_if_needed(table) insert_hash = decode_object_from_bytes_if_needed(insert_hash) insert_hash = insert_hash.copy() # To be able to safely modify it if len(insert_hash) == 0: raise McFindOrCreateException("Hash to INSERT or SELECT is empty") # MC_REWRITE_TO_PYTHON: remove after getting rid of Catalyst if "submit" in insert_hash: del insert_hash["submit"] row = self.select(table=table, what_to_select='*', condition_hash=insert_hash) if row is not None and row.rows() > 0: return row.hash() else: # try to create it, but if some other process has created it because we don't have a lock, just use that one try: return self.create(table=table, insert_hash=insert_hash) except McUniqueConstraintException: return self.select(table=table, what_to_select='*', condition_hash=insert_hash).hash()
def find_or_create(self, table: str, insert_hash: dict) -> Dict[str, Any]: """Select a single row from the database matching the hash or insert a row with the hash values and return the inserted row as a hash.""" table = decode_object_from_bytes_if_needed(table) insert_hash = decode_object_from_bytes_if_needed(insert_hash) insert_hash = insert_hash.copy() # To be able to safely modify it if len(insert_hash) == 0: raise McFindOrCreateException("Hash to INSERT or SELECT is empty") # MC_REWRITE_TO_PYTHON: remove after getting rid of Catalyst if "submit" in insert_hash: del insert_hash["submit"] row = self.select(table=table, what_to_select='*', condition_hash=insert_hash) if row is not None and row.rows() > 0: return row.hash() else: return self.create(table=table, insert_hash=insert_hash)