'Helper function returns a query object that will find items
according to the library query type provided and the key-value
pairs specified. The any_query_type is used for queries of
type "any"; if None, then an error is thrown.'

def _metadata_query(self, query_type, any_query_type, kv):

    if kv:
        queries = []
        it = iter(kv)
        for (tag, value) in zip(it, it):
            if (tag.lower() == u'any'):
                if any_query_type:
                    queries.append(any_query_type(value, ITEM_KEYS_WRITABLE, query_type))
                else:
                    raise BPDError(ERROR_UNKNOWN, u'no    such    tagtype')
            else:
                (_, key) = self._tagtype_lookup(tag)
                queries.append(query_type(key, value))
        return dbcore.query.AndQuery(queries)
    else:
        return dbcore.query.TrueQuery()
