[gnome-music] query: Use functions for queries



commit d39cc3b9a41c544b21f3728621b66d3cd5ba16c3
Author: Arnel Borja <arnelborja src gnome org>
Date:   Sun May 4 22:21:12 2014 +0800

    query: Use functions for queries
    
    This merges the queries for each view and search.

 gnomemusic/grilo.py |    6 +-
 gnomemusic/query.py |  266 +++++++++++----------------------------------------
 2 files changed, 57 insertions(+), 215 deletions(-)
---
diff --git a/gnomemusic/grilo.py b/gnomemusic/grilo.py
index ca58804..7da8e0c 100644
--- a/gnomemusic/grilo.py
+++ b/gnomemusic/grilo.py
@@ -150,15 +150,15 @@ class Grilo(GObject.GObject):
 
     @log
     def populate_artists(self, offset, callback, count=-1):
-        self.populate_items(Query.ARTISTS, offset, callback, count)
+        self.populate_items(Query.all_artists(), offset, callback, count)
 
     @log
     def populate_albums(self, offset, callback, count=-1):
-        self.populate_items(Query.ALBUMS, offset, callback, count)
+        self.populate_items(Query.all_albums(), offset, callback, count)
 
     @log
     def populate_songs(self, offset, callback, count=-1):
-        self.populate_items(Query.SONGS, offset, callback, count)
+        self.populate_items(Query.all_songs(), offset, callback, count)
 
     @log
     def populate_album_songs(self, album_id, callback, count=-1):
diff --git a/gnomemusic/query.py b/gnomemusic/query.py
index 9723969..8c4510b 100644
--- a/gnomemusic/query.py
+++ b/gnomemusic/query.py
@@ -30,7 +30,38 @@ from gi.repository import GLib, Tracker
 
 class Query():
 
-    ALBUMS = '''
+    @staticmethod
+    def all_albums():
+        return Query.albums('?album a nmm:MusicAlbum .')
+
+    @staticmethod
+    def all_artists():
+        return Query.artists('?album a nmm:MusicAlbum .')
+
+    @staticmethod
+    def all_songs():
+        return Query.songs('?song a nmm:MusicPiece ; a nfo:FileDataObject .')
+
+    SONGS_COUNT = '''
+    SELECT
+        COUNT(?song) AS childcount
+    WHERE {
+        ?song a nmm:MusicPiece ;
+              a nfo:FileDataObject
+        FILTER (
+            NOT EXISTS {
+                ?song a nmm:Video
+            } &&
+            NOT EXISTS {
+                ?song a nmm:Playlist
+            }
+        )
+    }
+    '''.replace('\n', ' ').strip()
+
+    @staticmethod
+    def albums(where_clause):
+        query = '''
     SELECT DISTINCT
         rdf:type(?album)
         tracker:id(?album) AS id
@@ -112,7 +143,7 @@ class Query():
             LIMIT 1
         ) AS creation-date
         {
-            ?album a nmm:MusicAlbum .
+            %(where_clause)s
             FILTER (
                 EXISTS {
                     ?_3 nmm:musicAlbum ?album ;
@@ -129,9 +160,13 @@ class Query():
             )
         }
     ORDER BY fn:lower-case(?title) ?author ?albumyear
-    '''.replace('\n', ' ').strip()
+    '''.replace('\n', ' ').strip() % {'where_clause': where_clause.replace('\n', ' ').strip()}
+
+        return query
 
-    ARTISTS = '''
+    @staticmethod
+    def artists(where_clause):
+        query = '''
     SELECT DISTINCT
         rdf:type(?album)
         tracker:id(?album) AS id
@@ -221,220 +256,19 @@ class Query():
             LIMIT 1
         ) AS creation-date
         {
-            ?album a nmm:MusicAlbum .
-            FILTER (
-                EXISTS {
-                    ?_3 nmm:musicAlbum ?album ;
-                        tracker:available 'true'
-                FILTER (
-                    NOT EXISTS {
-                        ?_3 a nmm:Video
-                    } &&
-                    NOT EXISTS {
-                        ?_3 a nmm:Playlist
-                    }
-                )
-                }
-            )
-        }
-    ORDER BY fn:lower-case(?author) ?albumyear nie:title(?album)
-    '''.replace('\n', ' ').strip()
-
-    SONGS = '''
-    SELECT DISTINCT
-        rdf:type(?song)
-        tracker:id(?song) AS id
-        nie:url(?song) AS url
-        nie:title(?song) AS title
-        nmm:artistName(nmm:performer(?song)) AS artist
-        nie:title(nmm:musicAlbum(?song)) AS album
-        nfo:duration(?song) AS duration
-        {
-            ?song a nmm:MusicPiece ;
-                  a nfo:FileDataObject
-            FILTER (
-                NOT EXISTS {
-                    ?song a nmm:Video
-                } &&
-                NOT EXISTS {
-                    ?song a nmm:Playlist
-                }
-            )
-        }
-    ORDER BY tracker:added(?song)
-    '''.replace('\n', ' ').strip()
-
-    SONGS_COUNT = '''
-    SELECT
-        COUNT(?song) AS childcount
-    WHERE {
-        ?song a nmm:MusicPiece ;
-              a nfo:FileDataObject
-        FILTER (
-            NOT EXISTS {
-                ?song a nmm:Video
-            } &&
-            NOT EXISTS {
-                ?song a nmm:Playlist
-            }
-        )
-    }
-    '''.replace('\n', ' ').strip()
-
-    @staticmethod
-    def albums(where_clause):
-        query = '''
-    SELECT DISTINCT
-        rdf:type(?album)
-        tracker:id(?album) AS id
-        (
-            SELECT
-                nmm:artistName(?artist)
-            WHERE {
-                ?album nmm:albumArtist ?artist
-            }
-            LIMIT 1
-        ) AS artist
-        nie:title(?album) AS title
-        nie:title(?album) AS album
-        tracker:coalesce(
-            (
-                SELECT
-                    GROUP_CONCAT(
-                        nmm:artistName(?artist),
-                        ','
-                    )
-                WHERE {
-                    ?album nmm:albumArtist ?artist
-                }
-            ),
-            (
-                SELECT
-                    GROUP_CONCAT(
-                        (
-                            SELECT
-                                nmm:artistName(nmm:performer(?_12)) AS perf
-                            WHERE {
-                                ?_12 nmm:musicAlbum ?album
-                            }
-                            GROUP BY ?perf
-                        ),
-                        ','
-                    ) AS album_performer
-                WHERE {
-                }
-            )
-        ) AS author
-        xsd:integer(
-            tracker:coalesce(
-                nmm:albumTrackCount(?album),
-                (
-                    SELECT
-                        COUNT(?_1)
-                    WHERE {
-                        ?_1 nmm:musicAlbum ?album ;
-                            tracker:available 'true'
-                    }
-                )
-            )
-        ) AS childcount
-        (
-            SELECT
-                fn:year-from-dateTime(?c)
-            WHERE {
-                ?_2 nmm:musicAlbum ?album ;
-                    nie:contentCreated ?c ;
-                    tracker:available 'true'
-            }
-            LIMIT 1
-        ) AS creation-date
-        {
             %(where_clause)s
             FILTER (
                 EXISTS {
                     ?_3 nmm:musicAlbum ?album ;
                         tracker:available 'true'
-                }
-            )
-        }
-    ORDER BY fn:lower-case(?title) ?author ?albumyear
-    '''.replace('\n', ' ').strip() % {'where_clause': where_clause.replace('\n', ' ').strip()}
-
-        return query
-
-    @staticmethod
-    def artists(where_clause):
-        query = '''
-    SELECT DISTINCT
-        rdf:type(?album)
-        tracker:id(?album) AS id
-        (
-            SELECT
-                nmm:artistName(?artist)
-            WHERE {
-                ?album nmm:albumArtist ?artist
-            }
-            LIMIT 1
-        ) AS artist
-        nie:title(?album) AS title
-        nie:title(?album) AS album
-        tracker:coalesce(
-            (
-                SELECT
-                    GROUP_CONCAT(
-                        nmm:artistName(?artist),
-                        ','
+                    FILTER (
+                        NOT EXISTS {
+                            ?_3 a nmm:Video
+                        } &&
+                        NOT EXISTS {
+                            ?_3 a nmm:Playlist
+                        }
                     )
-                WHERE {
-                    ?album nmm:albumArtist ?artist
-                }
-            ),
-            (
-                SELECT
-                    GROUP_CONCAT(
-                        (
-                            SELECT
-                                nmm:artistName(nmm:performer(?_12)) AS perf
-                            WHERE {
-                                ?_12 nmm:musicAlbum ?album
-                            }
-                            GROUP BY ?perf
-                        ),
-                        ','
-                    ) AS album_performer
-                WHERE {
-                }
-            )
-        ) AS author
-        xsd:integer(
-            tracker:coalesce(
-                nmm:albumTrackCount(?album),
-                (
-                    SELECT
-                        COUNT(?_1)
-                    WHERE {
-                        ?_1 nmm:musicAlbum ?album ;
-                        tracker:available 'true'
-                    }
-                )
-            )
-        ) AS childcount
-        (
-            SELECT
-                fn:year-from-dateTime(?c)
-            WHERE {
-                ?_2 nmm:musicAlbum ?album ;
-                    nie:contentCreated ?c ;
-                    tracker:available 'true'
-            }
-            LIMIT 1
-        ) AS creation-date
-        {
-            %(where_clause)s
-            FILTER (
-                EXISTS {
-                    ?_3 nmm:musicAlbum ?album ;
-                        tracker:available 'true'
                 }
             )
         }
@@ -456,6 +290,14 @@ class Query():
         nfo:duration(?song) AS duration
         {
             %(where_clause)s
+            FILTER (
+                NOT EXISTS {
+                    ?song a nmm:Video
+                } &&
+                NOT EXISTS {
+                    ?song a nmm:Playlist
+                }
+            )
         }
     ORDER BY tracker:added(?song)
     '''.replace('\n', ' ').strip() % {'where_clause': where_clause.replace('\n', ' ').strip()}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]