Exemple #1
0
def entity_hard_delete(kls, entity_id, session=None):
    if not session:
        session = get_session()

    try:
        with session.begin(subtransactions=True):
            query = model_query(kls, session)
            entity = query.filter_by(id=entity_id).first()
            if entity is None:
                raise exc.NotFound(
                    _("%(name)s %(id)s not found") % {
                        'name': kls.__name__,
                        'id': entity_id
                    })

            session.delete(entity)

    except db_exc.DBReferenceError as re:
        raise exc.DBReferenceError(object_name=kls.__name__,
                                   value=re.constraint,
                                   key=re.key)
    except db_exc.DBConnectionError:
        raise exc.DBConnectionError()
    except db_exc.ColumnError:
        raise exc.ColumnError()
    except db_exc.DBDeadlock:
        raise exc.DBDeadLock()
    except db_exc.DBInvalidUnicodeParameter:
        raise exc.DBInvalidUnicodeParameter()
Exemple #2
0
def __entity_get(kls, entity_id, session):
    try:
        query = model_query(kls, session)
        return query.filter_by(id=entity_id).first()
    except db_exc.DBConnectionError:
        raise exc.DBConnectionError()
    except db_exc.ColumnError:
        raise exc.ColumnError()
    except db_exc.DBDeadlock:
        raise exc.DBDeadLock()
    except db_exc.DBInvalidUnicodeParameter:
        raise exc.DBInvalidUnicodeParameter()
Exemple #3
0
def entity_get_all(kls,
                   filter_non_public=False,
                   marker=None,
                   offset=None,
                   limit=None,
                   sort_field='id',
                   sort_dir='asc',
                   session=None,
                   **kwargs):
    # Sanity checks, in case someone accidentally explicitly passes in 'None'
    if not sort_field:
        sort_field = 'id'
    if not sort_dir:
        sort_dir = 'asc'

    # Construct the query
    query = model_query(kls, session)

    # Sanity check on input parameters
    query = apply_query_filters(query=query, model=kls, **kwargs)

    # Construct the query
    try:
        query = paginate_query(query=query,
                               model=kls,
                               limit=limit,
                               sort_key=sort_field,
                               marker=marker,
                               offset=offset,
                               sort_dir=sort_dir)

        # Execute the query
        entities = query.all()
    except db_exc.DBConnectionError:
        raise exc.DBConnectionError()
    except db_exc.DBDeadlock:
        raise exc.DBDeadLock()
    except db_exc.DBInvalidUnicodeParameter:
        raise exc.DBInvalidUnicodeParameter()

    if len(entities) > 0 and filter_non_public:
        sample_entity = entities[0]
        public_fields = getattr(sample_entity, "_public_fields", [])

        entities = [
            _filter_non_public_fields(entity, public_fields)
            for entity in entities
        ]

    return entities
Exemple #4
0
def entity_get_count(kls, session=None, **kwargs):
    # Construct the query
    query = model_query(kls, session)

    # Sanity check on input parameters
    query = apply_query_filters(query=query, model=kls, **kwargs)

    try:
        count = query.count()
    except db_exc.DBConnectionError:
        raise exc.DBConnectionError()
    except db_exc.DBDeadlock:
        raise exc.DBDeadLock()
    except db_exc.DBInvalidUnicodeParameter:
        raise exc.DBInvalidUnicodeParameter()

    return count
Exemple #5
0
def model_query(model, session=None):
    """Query helper.

    :param model: base model to query
    """
    session = session or get_session()

    try:
        query = session.query(model)
    except db_exc.DBConnectionError:
        raise exc.DBConnectionError()
    except db_exc.ColumnError:
        raise exc.ColumnError()
    except db_exc.DBDeadlock:
        raise exc.DBDeadLock()
    except db_exc.DBInvalidUnicodeParameter:
        raise exc.DBInvalidUnicodeParameter()
    return query