<commit_before>from __future__ import unicode_literals

import itertools
import logging
import operator
import os
import sqlite3

from mopidy.models import Artist, Album, Track, Ref

_BROWSE_QUERIES = {
    None: """
    SELECT CASE WHEN album.uri IS NULL THEN '%s' ELSE '%s' END AS type,
           coalesce(album.uri, track.uri) AS uri,
           coalesce(album.name, track.name) AS name
      FROM track LEFT OUTER JOIN album ON track.album = album.uri
     WHERE %%s
     GROUP BY coalesce(album.uri, track.uri)
     ORDER BY %%s
    """ % (Ref.TRACK, Ref.ALBUM),
    Ref.ALBUM: """
    SELECT '%s' AS type, uri AS uri, name AS name
      FROM album
     WHERE %%s
     ORDER BY %%s
    """ % Ref.ALBUM,
    Ref.ARTIST: """
    SELECT '%s' AS type, uri AS uri, name AS name
      FROM artist
     WHERE %%s
     ORDER BY %%s
    """ % Ref.ARTIST,
    Ref.TRACK: """
    SELECT '%s' AS type, uri AS uri, name AS name
      FROM track
     WHERE %%s
     ORDER BY %%s
    """ % Ref.TRACK
}

_BROWSE_FILTERS = {
    None: {
        'album': 'track.album = ?',
        'albumartist': 'album.artists = ?',
        'artist': 'track.artists = ?',
        'composer': 'track.composers = ?',
        'date': "track.date LIKE ? || '%'",
        'genre': 'track.genre = ?',
        'performer': 'track.performers = ?',
        'max-age': "track.last_modified > strftime('%s', 'now') - ?",
    },
    Ref.ARTIST: {
        'role': {
            'albumartist': """EXISTS (
                SELECT * FROM album WHERE album.artists = artist.uri
            )""",
            'artist': """EXISTS (
                SELECT * FROM track WHERE track.artists = artist.uri
            )""",
            'composer': """EXISTS (
                SELECT * FROM track WHERE track.composers = artist.uri
            )""",
            'performer': """EXISTS (
                SELECT * FROM track WHERE track.performers = artist.uri
            )"""
        },
    },
    Ref.ALBUM: {
        'albumartist': 'artists = ?',
        'artist': """? IN (
            SELECT artists FROM track WHERE album = album.uri
        )""",
        'composer': """? IN (
            SELECT composers FROM track WHERE album = album.uri
        )""",
        'date': """EXISTS (
            SELECT * FROM track WHERE album = album.uri AND date LIKE ? || '%'
        )""",
        'genre': """? IN (
            SELECT genre FROM track WHERE album = album.uri
        )""",
        'performer': """? IN (
            SELECT performers FROM track WHERE album = album.uri
        )""",
        'max-age': """EXISTS (
            SELECT *
              FROM track
             WHERE album = album.uri
               AND last_modified > strftime('%s', 'now') - ?
        )""",
    },
    Ref.TRACK: {
        'album': 'album = ?',
        'albumartist': """? IN (
            SELECT artists FROM album WHERE uri = track.album
        )""",
        'artist': 'artists = ?',
        'composer': 'composers = ?',
        'date': "date LIKE ? || '%'",
        'genre': 'genre = ?',
        'performer': 'performers = ?',
        'max-age': "last_modified > strftime('%s', 'now') - ?",
    }
}

_SEARCH_SQL = """
SELECT *
  FROM tracks
 WHERE docid IN (SELECT docid FROM %s WHERE %s)
"""

_SEARCH_FILTERS = {
    'album': 'album_uri = ?',
    'albumartist': 'albumartist_uri = ?',
    'artist': 'artist_uri = ?',
    'composer': 'composer_uri = ?',
    'date': "date LIKE ? || '%'",
    'genre': 'genre = ?',
    'performer': 'performer_uri = ?',
    'uri': 'uri GLOB ?',
    'max-age': "last_modified > strftime('%s', 'now') - ?",
}

_SEARCH_FIELDS = {
    'uri',
    'track_name',
    'album',<commit_msg>Fix #54: Track.last_modified changed to milliseconds.<commit_after>from __future__ import unicode_literals

import itertools
import logging
import operator
import os
import sqlite3

from mopidy.models import Artist, Album, Track, Ref

_BROWSE_QUERIES = {
    None: """
    SELECT CASE WHEN album.uri IS NULL THEN '%s' ELSE '%s' END AS type,
           coalesce(album.uri, track.uri) AS uri,
           coalesce(album.name, track.name) AS name
      FROM track LEFT OUTER JOIN album ON track.album = album.uri
     WHERE %%s
     GROUP BY coalesce(album.uri, track.uri)
     ORDER BY %%s
    """ % (Ref.TRACK, Ref.ALBUM),
    Ref.ALBUM: """
    SELECT '%s' AS type, uri AS uri, name AS name
      FROM album
     WHERE %%s
     ORDER BY %%s
    """ % Ref.ALBUM,
    Ref.ARTIST: """
    SELECT '%s' AS type, uri AS uri, name AS name
      FROM artist
     WHERE %%s
     ORDER BY %%s
    """ % Ref.ARTIST,
    Ref.TRACK: """
    SELECT '%s' AS type, uri AS uri, name AS name
      FROM track
     WHERE %%s
     ORDER BY %%s
    """ % Ref.TRACK
}

_BROWSE_FILTERS = {
    None: {
        'album': 'track.album = ?',
        'albumartist': 'album.artists = ?',
        'artist': 'track.artists = ?',
        'composer': 'track.composers = ?',
        'date': "track.date LIKE ? || '%'",
        'genre': 'track.genre = ?',
        'performer': 'track.performers = ?',
        'max-age': "track.last_modified >= (strftime('%s', 'now') - ?) * 1000",
    },
    Ref.ARTIST: {
        'role': {
            'albumartist': """EXISTS (
                SELECT * FROM album WHERE album.artists = artist.uri
            )""",
            'artist': """EXISTS (
                SELECT * FROM track WHERE track.artists = artist.uri
            )""",
            'composer': """EXISTS (
                SELECT * FROM track WHERE track.composers = artist.uri
            )""",
            'performer': """EXISTS (
                SELECT * FROM track WHERE track.performers = artist.uri
            )"""
        },
    },
    Ref.ALBUM: {
        'albumartist': 'artists = ?',
        'artist': """? IN (
            SELECT artists FROM track WHERE album = album.uri
        )""",
        'composer': """? IN (
            SELECT composers FROM track WHERE album = album.uri
        )""",
        'date': """EXISTS (
            SELECT * FROM track WHERE album = album.uri AND date LIKE ? || '%'
        )""",
        'genre': """? IN (
            SELECT genre FROM track WHERE album = album.uri
        )""",
        'performer': """? IN (
            SELECT performers FROM track WHERE album = album.uri
        )""",
        'max-age': """EXISTS (
            SELECT *
              FROM track
             WHERE album = album.uri
               AND last_modified >= (strftime('%s', 'now') - ?) * 1000
        )""",
    },
    Ref.TRACK: {
        'album': 'album = ?',
        'albumartist': """? IN (
            SELECT artists FROM album WHERE uri = track.album
        )""",
        'artist': 'artists = ?',
        'composer': 'composers = ?',
        'date': "date LIKE ? || '%'",
        'genre': 'genre = ?',
        'performer': 'performers = ?',
        'max-age': "last_modified >= (strftime('%s', 'now') - ?) * 1000",
    }
}

_SEARCH_SQL = """
SELECT *
  FROM tracks
 WHERE docid IN (SELECT docid FROM %s WHERE %s)
"""

_SEARCH_FILTERS = {
    'album': 'album_uri = ?',
    'albumartist': 'albumartist_uri = ?',
    'artist': 'artist_uri = ?',
    'composer': 'composer_uri = ?',
    'date': "date LIKE ? || '%'",
    'genre': 'genre = ?',
    'performer': 'performer_uri = ?',
    'uri': 'uri GLOB ?',
    'max-age': "last_modified >= (strftime('%s', 'now') - ?) * 1000",
}

_SEARCH_FIELDS = {
    'uri',
    'track_name',
    'album',
{'fraction_non_alphanumeric': 0.16562922868741542, 'fraction_numerical': 0.0027063599458728013, 'mean_word_length': 1.8916275430359937, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '750426', 'n_tokens_mistral': 2504, 'n_tokens_neox': 2373, 'n_words': 618}