def filter_by_checksums(field, checksums):
    query = "IN ("
    # trick to workaround postgresql, it does not allow returning ():
    empty_query = "IS NULL"
    if checksums:
        if len(checksums) > 10000:
            logger.warn("""
                More than 10000 UUIDs passed to filter by checksums method,
                these should be batched into separate querysets to avoid SQL Query too large errors in SQLite
            """)
        try:
            _validate_checksums(checksums)
            checksums_list = [
                "'{}'".format(identifier) for identifier in checksums
            ]
            return UnaryExpression(
                field,
                modifier=operators.custom_op(query + ",".join(checksums_list) +
                                             ")"),
            )
        except ValueError:
            # the value is not a valid hex code for a checksum, so fall through to the
            # empty case and don't return any results
            pass
    return UnaryExpression(field, modifier=operators.custom_op(empty_query))
Exemple #2
0
def filter_by_checksums(field, checksums):
    query = "IN ("
    # trick to workaround postgresql, it does not allow returning ():
    empty_query = "IS NULL"
    if checksums:
        checksums_list = ["'{}'".format(identifier) for identifier in checksums]
        return UnaryExpression(
            field, modifier=operators.custom_op(query + ",".join(checksums_list) + ")")
        )
    return UnaryExpression(field, modifier=operators.custom_op(empty_query))
Exemple #3
0
def _by_uuids(field, ids, validate, include):
    query = "IN (" if include else "NOT IN ("
    # trick to workaround postgresql, it does not allow returning ():
    empty_query = "IS NULL" if include else "IS NOT NULL"
    if ids:
        try:
            validate_uuids(ids)
            ids_list = [_format_uuid(identifier) for identifier in ids]
            return UnaryExpression(
                field, modifier=operators.custom_op(query + ",".join(ids_list) + ")")
            )
        except UUIDValidationError:
            # the value is not a valid hex code for a UUID, so fall through to the
            # empty case and don't return any results
            pass
    return UnaryExpression(field, modifier=operators.custom_op(empty_query))
Exemple #4
0
def filter_by_checksums(field, checksums):
    query = "IN ("
    # trick to workaround postgresql, it does not allow returning ():
    empty_query = "IS NULL"
    if checksums:
        if len(checksums) > 10000:
            logger.warn("""
                More than 10000 UUIDs passed to filter by checksums method,
                these should be batched into separate querysets to avoid SQL Query too large errors in SQLite
            """)
        checksums_list = [
            "'{}'".format(identifier) for identifier in checksums
        ]
        return UnaryExpression(
            field,
            modifier=operators.custom_op(query + ",".join(checksums_list) +
                                         ")"))
    return UnaryExpression(field, modifier=operators.custom_op(empty_query))
Exemple #5
0
def _by_uuids(field, ids, validate, include, vendor=None):
    query = "IN (" if include else "NOT IN ("
    # trick to workaround postgresql, it does not allow returning ():
    empty_query = "IS NULL" if include else "IS NOT NULL"
    if ids:
        if len(ids) > 10000:
            logger.warn(
                """
                More than 10000 UUIDs passed to filter by uuids method,
                these should be batched into separate querysets to avoid SQL Query too large errors in SQLite
            """
            )
        try:
            validate_uuids(ids)
            ids_list = [_format_uuid(identifier, vendor=vendor) for identifier in ids]
            return UnaryExpression(
                field, modifier=operators.custom_op(query + ",".join(ids_list) + ")")
            )
        except UUIDValidationError:
            # the value is not a valid hex code for a UUID, so fall through to the
            # empty case and don't return any results
            pass
    return UnaryExpression(field, modifier=operators.custom_op(empty_query))
Exemple #6
0
def _by_uuids(field, ids, validate, include):
    ids_list = list(ids)
    query = "IN (" if include else "NOT IN ("
    # trick to workaround postgresql, it does not allow returning ():
    empty_query = "IS NULL" if include else "IS NOT NULL"
    for (idx, identifier) in enumerate(ids_list):
        if validate:
            try:
                UUID(identifier, version=4)
            except (TypeError, ValueError):
                # the value is not a valid hex code for a UUID, so we don't return any results
                return UnaryExpression(
                    field, modifier=operators.custom_op(empty_query))
        # wrap the uuids in string quotations
        if django_connection.vendor == "postgresql" and validate:
            ids_list[idx] = "'{}'::uuid".format(identifier)
        else:  # sqlite or it's not an uuid
            ids_list[idx] = "'{}'".format(identifier)
    if ids_list:
        placeholder = query + ",".join(ids_list) + ")"
    else:
        placeholder = empty_query
    return UnaryExpression(field, modifier=operators.custom_op(placeholder))