예제 #1
0
def _get_collection(model, insecure=False, limit=None, marker=None,
                    sort_keys=None, sort_dirs=None, fields=None, **filters):
    columns = (
        tuple([getattr(model, f) for f in fields if hasattr(model, f)])
        if fields else ()
    )

    query = (db_base.model_query(model, columns) if insecure
             else _secure_query(model, *columns))
    query = db_filters.apply_filters(query, model, **filters)

    query = _paginate_query(
        model,
        limit,
        marker,
        sort_keys,
        sort_dirs,
        query
    )

    try:
        return query.all()
    except Exception as e:
        raise exc.DBError(
            "Failed when querying database, error type: %s, "
            "error message: %s" % (e.__class__.__name__, str(e))
        )
예제 #2
0
def conditional_update(model, values, expected_values, insecure=False,
                       filters=None, session=None):
    """Compare-and-swap conditional update SQLAlchemy implementation."""
    filters = filters or {}
    filters.update(expected_values)
    query = (db_base.model_query(model) if insecure else _secure_query(model))
    query = db_filters.apply_filters(query, model, **filters)
    update_args = {'synchronize_session': False}

    # Return True if we were able to change any DB entry, False otherwise
    result = query.update(values, **update_args)

    return 0 != result