Ejemplo n.º 1
0
def item_exists(item_type, item_id, session):
    """Check if the instance with type 'item_type' and id 'item_id'
    exists in the database. Returns True if exists else False

    :param item_type: The @type of the instance
    :type item_type: str
    :param item_id: The id of the instance in the database
    :type item_id: str
    :param session: sqlalchemy scoped session
    """
    database_class = get_database_class(item_type)
    return session.query(database_class.id).filter_by(id=item_id).scalar() is not None
Ejemplo n.º 2
0
def insert_single(object_: Dict[str, Any], session: scoped_session) -> Any:
    """Insert instance of classes with single objects.
    :param object_: object to be inserted
    :param session: sqlalchemy scoped session
    :return:
    Raises:
        ClassNotFound: If `type_` does not represt a valid/defined RDFClass.
        Instance: If an Instance of type `type_` already exists.
    """
    type_ = object_["@type"]
    database_class = get_database_class(type_)

    try:
        session.query(database_class).all()[-1]
    except (NoResultFound, IndexError, ValueError):
        return insert(object_, session=session)

    raise InstanceExists(type_)