[rygel] tracker: Refined class hierarchy for Tracker queries



commit 4c66fb0498159e9c798088a0a715d9aa3b921a4c
Author: Zeeshan Ali (Khattak) <zeeshanak gnome org>
Date:   Wed Feb 3 17:19:48 2010 +0200

    tracker: Refined class hierarchy for Tracker queries
    
    Now we have two classes to represent a tracker query: TrackerQuery &
    TrackerSelectionQuery.

 src/plugins/tracker/Makefile.am                    |    1 +
 .../tracker/rygel-tracker-metadata-values.vala     |   10 +-
 src/plugins/tracker/rygel-tracker-query.vala       |   91 ++---------------
 .../tracker/rygel-tracker-search-container.vala    |   22 ++--
 .../tracker/rygel-tracker-selection-query.vala     |  110 ++++++++++++++++++++
 src/rygel/rygel-http-post.vala                     |    1 -
 6 files changed, 136 insertions(+), 99 deletions(-)
---
diff --git a/src/plugins/tracker/Makefile.am b/src/plugins/tracker/Makefile.am
index 503a75e..76cd327 100644
--- a/src/plugins/tracker/Makefile.am
+++ b/src/plugins/tracker/Makefile.am
@@ -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 \
diff --git a/src/plugins/tracker/rygel-tracker-metadata-values.vala b/src/plugins/tracker/rygel-tracker-metadata-values.vala
index 315cfbb..64d1ba9 100644
--- a/src/plugins/tracker/rygel-tracker-metadata-values.vala
+++ b/src/plugins/tracker/rygel-tracker-metadata-values.vala
@@ -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 {
diff --git a/src/plugins/tracker/rygel-tracker-query.vala b/src/plugins/tracker/rygel-tracker-query.vala
index 28afac6..19ccdb8 100644
--- a/src/plugins/tracker/rygel-tracker-query.vala
+++ b/src/plugins/tracker/rygel-tracker-query.vala
@@ -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;
-    }
 }
diff --git a/src/plugins/tracker/rygel-tracker-search-container.vala b/src/plugins/tracker/rygel-tracker-search-container.vala
index 9dc2d6d..1e8823f 100644
--- a/src/plugins/tracker/rygel-tracker-search-container.vala
+++ b/src/plugins/tracker/rygel-tracker-search-container.vala
@@ -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
index 0000000..dd2ccba
--- /dev/null
+++ b/src/plugins/tracker/rygel-tracker-selection-query.vala
@@ -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;
+    }
+}
diff --git a/src/rygel/rygel-http-post.vala b/src/rygel/rygel-http-post.vala
index 7864db3..497e51d 100644
--- a/src/rygel/rygel-http-post.vala
+++ b/src/rygel/rygel-http-post.vala
@@ -83,7 +83,6 @@ internal class Rygel.HTTPPost : HTTPRequest {
     }
 
     private void on_got_chunk (Message msg, Buffer chunk) {
-
         this.write_chunk.begin (chunk);
     }
 



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