tracker: Refined class hierarchy for Tracker queries
authorZeeshan Ali (Khattak) <zeeshanak@gnome.org>
Wed, 3 Feb 2010 15:19:48 +0000 (17:19 +0200)
committerZeeshan Ali (Khattak) <zeeshanak@gnome.org>
Fri, 12 Feb 2010 15:53:25 +0000 (17:53 +0200)
Now we have two classes to represent a tracker query: TrackerQuery &
TrackerSelectionQuery.

src/plugins/tracker/Makefile.am
src/plugins/tracker/rygel-tracker-metadata-values.vala
src/plugins/tracker/rygel-tracker-query.vala
src/plugins/tracker/rygel-tracker-search-container.vala
src/plugins/tracker/rygel-tracker-selection-query.vala [new file with mode: 0644]
src/rygel/rygel-http-post.vala

index 503a75e..76cd327 100644 (file)
@@ -21,6 +21,7 @@ librygel_media_tracker_la_SOURCES = \
                                    rygel-tracker-years.vala \
                                    rygel-tracker-search-container.vala \
                                    rygel-tracker-query.vala \
+                                   rygel-tracker-selection-query.vala \
                                    rygel-tracker-query-triplet.vala \
                                    rygel-tracker-query-triplets.vala \
                                    rygel-tracker-item-factory.vala \
index 315cfbb..64d1ba9 100644 (file)
@@ -121,11 +121,11 @@ public class Rygel.TrackerMetadataValues : Rygel.SimpleContainer {
         var last_variable = variables[num_keys - 1];
         selected.add ("DISTINCT " + last_variable);
 
-        var query = new TrackerQuery (selected,
-                                      mandatory,
-                                      null,
-                                      null,
-                                      last_variable);
+        var query = new TrackerSelectionQuery (selected,
+                                               mandatory,
+                                               null,
+                                               null,
+                                               last_variable);
 
         string[,] values;
         try {
index 28afac6..19ccdb8 100644 (file)
@@ -25,25 +25,12 @@ using Gee;
 /**
  * Represents Tracker SPARQL query
  */
-public class Rygel.TrackerQuery {
+public abstract class Rygel.TrackerQuery {
     public TrackerQueryTriplets mandatory;
     public TrackerQueryTriplets optional;
 
-    public ArrayList<string> variables;
-    public ArrayList<string> filters;
-
-    public string order_by;
-    public int offset;
-    public int max_count;
-
-    public TrackerQuery (ArrayList<string>     variables,
-                         TrackerQueryTriplets  mandatory,
-                         TrackerQueryTriplets? optional,
-                         ArrayList<string>?    filters,
-                         string?               order_by = null,
-                         int                   offset = 0,
-                         int                   max_count = -1) {
-        this.variables = variables;
+    public TrackerQuery (TrackerQueryTriplets  mandatory,
+                         TrackerQueryTriplets? optional) {
         this.mandatory = mandatory;
 
         if (optional != null) {
@@ -51,27 +38,6 @@ public class Rygel.TrackerQuery {
         } else {
             this.optional = new TrackerQueryTriplets ();
         }
-
-        if (filters != null) {
-            this.filters = filters;
-        } else {
-            this.filters = new ArrayList<string> ();
-        }
-
-        this.order_by = order_by;
-
-        this.offset = offset;
-        this.max_count = max_count;
-    }
-
-    public TrackerQuery.clone (TrackerQuery query) {
-        this (this.copy_str_list (query.variables),
-              new TrackerQueryTriplets.clone (query.mandatory),
-              new TrackerQueryTriplets.clone (query.optional),
-              this.copy_str_list (query.filters),
-              query.order_by,
-              query.offset,
-              query.max_count);
     }
 
     public async string[,] execute (TrackerResourcesIface resources)
@@ -82,43 +48,12 @@ public class Rygel.TrackerQuery {
         return yield resources.sparql_query (str);
     }
 
-    public string to_string () {
-        string query = "SELECT";
-
-        foreach (var variable in this.variables) {
-            query += " " + variable;
-        }
-
-        query += " WHERE { " +
-                 this.serialize_triplets (this.mandatory) +
-                 " . " +
-                 this.serialize_triplets (this.optional);
-
-        if (this.filters.size > 0) {
-            query += " FILTER (";
-            for (var i = 0; i < this.filters.size; i++) {
-                query += this.filters[i];
-
-                if (i < this.filters.size - 1) {
-                    query += " && ";
-                }
-            }
-            query += ")";
-        }
-
-        query += " }";
-
-        if (this.order_by != null) {
-            query += " ORDER BY " + order_by;
-        }
-
-        query += " OFFSET " + this.offset.to_string ();
-
-        if (this.max_count != -1) {
-            query += " LIMIT " + this.max_count.to_string ();
-        }
-
-        return query;
+    // Deriving classes should override this method and complete it by
+    // adding the first part of the query
+    public virtual string to_string () {
+        return this.serialize_triplets (this.mandatory) +
+               " . " +
+               this.serialize_triplets (this.optional);
     }
 
     private string serialize_triplets (TrackerQueryTriplets triplets) {
@@ -140,12 +75,4 @@ public class Rygel.TrackerQuery {
 
         return str;
     }
-
-    private ArrayList<string> copy_str_list (Gee.List<string> str_list) {
-        var copy = new ArrayList<string> ();
-
-        copy.add_all (str_list);
-
-        return copy;
-    }
 }
index 9dc2d6d..1e8823f 100644 (file)
@@ -37,7 +37,7 @@ public class Rygel.TrackerSearchContainer : Rygel.MediaContainer {
     private const string MODIFIED_PREDICATE = "nfo:fileLastModified";
     private const string MODIFIED_VARIABLE = "?modified";
 
-    public TrackerQuery query;
+    public TrackerSelectionQuery query;
     public TrackerItemFactory item_factory;
 
     private TrackerResourcesIface resources;
@@ -87,11 +87,11 @@ public class Rygel.TrackerSearchContainer : Rygel.MediaContainer {
             }
         }
 
-        this.query = new TrackerQuery (variables,
-                                       our_mandatory,
-                                       optional,
-                                       filters,
-                                       MODIFIED_VARIABLE);
+        this.query = new TrackerSelectionQuery (variables,
+                                                our_mandatory,
+                                                optional,
+                                                filters,
+                                                MODIFIED_VARIABLE);
 
         try {
             this.create_proxies ();
@@ -186,7 +186,7 @@ public class Rygel.TrackerSearchContainer : Rygel.MediaContainer {
 
     private async void get_children_count () {
         try {
-            var query = new TrackerQuery.clone (this.query);
+            var query = new TrackerSelectionQuery.clone (this.query);
 
             query.variables = new ArrayList<string> ();
             query.variables.add ("COUNT(" + ITEM_VARIABLE + ") AS x");
@@ -205,15 +205,15 @@ public class Rygel.TrackerSearchContainer : Rygel.MediaContainer {
         }
     }
 
-    private TrackerQuery? create_query (SearchExpression expression,
-                                        int              offset,
-                                        int              max_count) {
+    private TrackerSelectionQuery? create_query (SearchExpression expression,
+                                                 int              offset,
+                                                 int              max_count) {
         if (expression == null || !(expression is RelationalExpression)) {
             return null;
         }
 
         var rel_expression = expression as RelationalExpression;
-        var query = new TrackerQuery.clone (this.query);
+        var query = new TrackerSelectionQuery.clone (this.query);
 
         if (rel_expression.operand1 == "@id") {
             var filter = create_filter_for_id (rel_expression);
diff --git a/src/plugins/tracker/rygel-tracker-selection-query.vala b/src/plugins/tracker/rygel-tracker-selection-query.vala
new file mode 100644 (file)
index 0000000..dd2ccba
--- /dev/null
@@ -0,0 +1,110 @@
+/*
+ * Copyright (C) 20010 Nokia Corporation.
+ *
+ * Author: Zeeshan Ali <zeenix@gmail.com>
+ *
+ * This file is part of Rygel.
+ *
+ * Rygel is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Rygel is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+using Gee;
+
+/**
+ * Represents Tracker SPARQL Selection query
+ */
+public class Rygel.TrackerSelectionQuery : Rygel.TrackerQuery {
+    public ArrayList<string> variables;
+    public ArrayList<string> filters;
+
+    public string order_by;
+    public int offset;
+    public int max_count;
+
+    public TrackerSelectionQuery (ArrayList<string>     variables,
+                                  TrackerQueryTriplets  mandatory,
+                                  TrackerQueryTriplets? optional,
+                                  ArrayList<string>?    filters,
+                                  string?               order_by = null,
+                                  int                   offset = 0,
+                                  int                   max_count = -1) {
+        base (mandatory, optional);
+
+        if (filters != null) {
+            this.filters = filters;
+        } else {
+            this.filters = new ArrayList<string> ();
+        }
+
+        this.variables = variables;
+        this.order_by = order_by;
+        this.offset = offset;
+        this.max_count = max_count;
+    }
+
+    public TrackerSelectionQuery.clone (TrackerSelectionQuery query) {
+        this (this.copy_str_list (query.variables),
+              new TrackerQueryTriplets.clone (query.mandatory),
+              new TrackerQueryTriplets.clone (query.optional),
+              this.copy_str_list (query.filters),
+              query.order_by,
+              query.offset,
+              query.max_count);
+    }
+
+    public override string to_string () {
+        var query = "SELECT ";
+
+        foreach (var variable in this.variables) {
+            query += " " + variable;
+        }
+
+        query += " WHERE { " + base.to_string ();
+
+        if (this.filters.size > 0) {
+            query += " FILTER (";
+            for (var i = 0; i < this.filters.size; i++) {
+                query += this.filters[i];
+
+                if (i < this.filters.size - 1) {
+                    query += " && ";
+                }
+            }
+            query += ")";
+        }
+
+        query += " }";
+
+        if (this.order_by != null) {
+            query += " ORDER BY " + order_by;
+        }
+
+        query += " OFFSET " + this.offset.to_string ();
+
+        if (this.max_count != -1) {
+            query += " LIMIT " + this.max_count.to_string ();
+        }
+
+        return query;
+    }
+
+    private ArrayList<string> copy_str_list (Gee.List<string> str_list) {
+        var copy = new ArrayList<string> ();
+
+        copy.add_all (str_list);
+
+        return copy;
+    }
+}
index 7864db3..497e51d 100644 (file)
@@ -83,7 +83,6 @@ internal class Rygel.HTTPPost : HTTPRequest {
     }
 
     private void on_got_chunk (Message msg, Buffer chunk) {
-
         this.write_chunk.begin (chunk);
     }