コード例 #1
0
    def is_study_inside_database(self, study: StudyDTO) -> bool:
        """Get the study with selected primary key from the database

        Args:
            study: the study that will be looked for in the DB

        Returns:
            True if the study has been found and is unique inside the database, False otherwise
        """
        key = self.db_primary_key
        value = study.__getattribute__(key)
        self.logger.info(f"Checking if study {study.path}: is inside database")

        found_studies = self.db.search(where(key=key) == value)
        return len(found_studies) == 1
コード例 #2
0
    def save_study(self, study: StudyDTO):
        """Saves the selected study inside the database. If the study already exists inside the
        database then the content of the database is updated, otherwise the new study is added to the database

        Args:
            study: The study data transfer object that will be saved
        """
        if self.is_study_inside_database(study=study):
            self.logger.info(
                f"Updating study already existing inside database with"
                f"{DB_PRIMARY_KEY}: {study.__getattribute__(DB_PRIMARY_KEY)}"
            )
            self.db.update(
                study.__dict__,
                where(DB_PRIMARY_KEY) == study.__getattribute__(DB_PRIMARY_KEY),
            )
        else:
            self.logger.info(
                f"Inserting new study with {DB_PRIMARY_KEY}: {study.__getattribute__(DB_PRIMARY_KEY)} inside database"
            )
            self.db.insert(study.__dict__)