def update_entity(entity): """Update values of an object into the database. :param entity: the object to update values. """ LOGGER.debug("Update entity %s", entity) try: entity.updated_at = datetime.datetime.now() db.session.commit() return entity except exc.SQLAlchemyError as err: errors.stracktrace() db.session.rollback() raise exceptions.DatabaseError(UPDATE_ERR) from err
def delete_entity(entity, soft=True): """Remove an object from the database. :param entity: the object to remove """ LOGGER.debug("Delete entity %s", entity) try: entity.deleted_at = datetime.datetime.now() if soft is None: db.session.delete(entity) db.session.commit() return entity except exc.SQLAlchemyError as err: errors.stracktrace() db.session.rollback() raise exceptions.DatabaseError(DELETE_ERR) from err
def check_sql(): """Check SQL connection. Raises: exceptions.DatabaseError: if an error occurs during SQL execution Returns: version: which version of SQL server is """ try: LOGGER.info("Check SQL version: %s", db.engine.name) if db.engine.name == "sqlite": return db.engine.execute("select sqlite_version()") return db.engine.execute("SELECT version() AS value") except exc.SQLAlchemyError as err: errors.stracktrace() raise exceptions.DatabaseError("Can't check SQL version") from err
def get_user_by_name(name): """Retrieve a user. Args: name ([string]): the name of the user to retrieve Raises: exceptions.DatabaseError: if an errors occurs during SQL execution Returns: [User]: A User model """ LOGGER.info("Get a User with name %s", name) try: return User.query.filter_by(name=name).first() except exc.SQLAlchemyError as err: errors.stracktrace() raise exceptions.DatabaseError(GET_USER_ERR) from err
def list_entities(model, err_msg): """List some SQL entities. Args: model ([db.Model]): the database model to use err_msg ([string]): the error message if an error occurs Raises: exceptions.DatabaseError: if an error occurs during SQL execution Returns: A list of database models """ LOGGER.debug("List entities %s", model) try: return model.query.order_by(model.id).all() except exc.SQLAlchemyError as err: errors.stracktrace() raise exceptions.DatabaseError(err_msg) from err
def create_User(name, email): """Create a new User. Args: name ([string]): The name of the User email ([string]): An email Raises: exceptions.DatabaseError: if an errors occurs during SQL execution Returns: [User]: A User model """ LOGGER.info("Create a User %s %s", name, email) try: return commons.add_entity(User(name, email)) except exc.SQLAlchemyError as err: errors.stracktrace() raise exceptions.DatabaseError(CREATE_USER_ERR) from err
def get_entity(model, id, err_msg): """Retrieve a SQL entity. Args: model ([db.Model]): the database model to use id ([int]): the entity ID err_msg ([string]): the error message if an error occurs Raises: exceptions.DatabaseError: if an error occurs during SQL execution Returns: A database model entity """ LOGGER.debug("Get entity %s id=%s", model, id) try: return model.query.filter_by(id=id).first() except exc.SQLAlchemyError as err: errors.stracktrace() raise exceptions.DatabaseError(err_msg) from err
def add_entity(entity): """Add into database an entity. Args: entity ([db.Model]): the object to add into the database Raises: exceptions.DatabaseError: if an error occurs during SQL execution Returns: A database model entity """ LOGGER.debug("Add entity %s", entity) try: entity.created_at = datetime.datetime.now() db.session.add(entity) db.session.commit() return entity except exc.SQLAlchemyError as err: errors.stracktrace() db.session.rollback() raise exceptions.DatabaseError(CREATE_ERR) from err