[gnome-photos/sam/tracker3: 24/27] Fix single item queries




commit 6335a681a7a922b3b05c6d05a9218094851d4ec1
Author: Sam Thursfield <sam afuera me uk>
Date:   Sun Aug 2 16:58:23 2020 +0200

    Fix single item queries
    
    Use 'VALUES' to bind ?urn to a specific value, instead of doing a
    search-and-replace on the whole query. The latter approach was
    generating invalid SPARQL, such as `GROUP BY <urn:...>`.

 src/photos-query-builder.c              | 21 ++++++++++-----------
 src/queries/all.sparql.template         |  2 ++
 src/queries/collections.sparql.template |  1 +
 src/queries/photos.sparql.template      |  1 +
 4 files changed, 14 insertions(+), 11 deletions(-)
---
diff --git a/src/photos-query-builder.c b/src/photos-query-builder.c
index d029f80e..088dd69d 100644
--- a/src/photos-query-builder.c
+++ b/src/photos-query-builder.c
@@ -50,7 +50,7 @@ const gchar *photos_default_filter = \
 
 static gchar *
 photos_query_builder_query (PhotosSearchContextState *state,
-                            gboolean global,
+                            const gchar *values,
                             gint flags,
                             PhotosOffsetController *offset_cntrlr)
 {
@@ -107,7 +107,7 @@ photos_query_builder_query (PhotosSearchContextState *state,
 
   order = "ORDER BY DESC (?mtime)";
 
-  if (global && (flags & PHOTOS_QUERY_FLAGS_UNLIMITED) == 0)
+  if (values == NULL && (flags & PHOTOS_QUERY_FLAGS_UNLIMITED) == 0)
     {
       gint offset = 0;
       gint step = 60;
@@ -125,6 +125,7 @@ photos_query_builder_query (PhotosSearchContextState *state,
                                               "miner_fs_busname", miner_fs_busname,
                                               "outer_projection", outer_projection,
                                               "inner_projection", inner_projection,
+                                              "values", values ? values : "",
                                               "collections_default_filter", collections_default_filter,
                                               "item_pattern", item_pattern,
                                               "photos_default_filter", photos_default_filter,
@@ -220,6 +221,7 @@ photos_query_builder_count_query (PhotosSearchContextState *state, gint flags)
                                               "miner_fs_busname", miner_fs_busname,
                                               "outer_projection", outer_projection,
                                               "inner_projection", inner_projection,
+                                              "values", "",
                                               "collections_default_filter", collections_default_filter,
                                               "item_pattern", item_pattern,
                                               "photos_default_filter", photos_default_filter,
@@ -283,7 +285,7 @@ photos_query_builder_fetch_collections_local (PhotosSearchContextState *state)
   g_autofree gchar *sparql = NULL;
 
   sparql = photos_query_builder_query (state,
-                                       TRUE,
+                                       NULL,
                                        PHOTOS_QUERY_FLAGS_COLLECTIONS
                                        | PHOTOS_QUERY_FLAGS_LOCAL
                                        | PHOTOS_QUERY_FLAGS_UNLIMITED,
@@ -303,7 +305,7 @@ photos_query_builder_global_query (PhotosSearchContextState *state,
   PhotosQuery *query;
   g_autofree gchar *sparql = NULL;
 
-  sparql = photos_query_builder_query (state, TRUE, flags, offset_cntrlr);
+  sparql = photos_query_builder_query (state, NULL, flags, offset_cntrlr);
   query = photos_query_new (state, sparql);
 
   return query;
@@ -345,17 +347,14 @@ photos_query_builder_set_collection_query (PhotosSearchContextState *state,
 PhotosQuery *
 photos_query_builder_single_query (PhotosSearchContextState *state, gint flags, const gchar *resource)
 {
-  g_autoptr (GRegex) regex = NULL;
   PhotosQuery *query;
-  g_autofree gchar *replacement = NULL;
   g_autofree gchar *sparql = NULL;
-  g_autofree gchar *tmp = NULL;
+  g_autofree gchar *values = NULL;
 
-  tmp = photos_query_builder_query (state, FALSE, flags, NULL);
+  values = g_strdup_printf ("VALUES ?urn { <%s> }", resource);
+
+  sparql = photos_query_builder_query (state, values, flags, NULL);
 
-  regex = g_regex_new ("\\?urn", 0, 0, NULL);
-  replacement = g_strconcat ("<", resource, ">", NULL);
-  sparql = g_regex_replace (regex, tmp, -1, 0, replacement, 0, NULL);
   query = photos_query_new (state, sparql);
 
   return query;
diff --git a/src/queries/all.sparql.template b/src/queries/all.sparql.template
index 44ab2d66..4188faed 100644
--- a/src/queries/all.sparql.template
+++ b/src/queries/all.sparql.template
@@ -7,6 +7,7 @@ FROM NAMED tracker:Pictures
             {
                 SELECT ?urn COUNT(?item) AS ?count
                 {
+                    {{values}}
                     ?urn a nfo:DataContainer.
                     ?item a nmm:Photo; nie:isPartOf ?urn.
                 } GROUP BY ?urn
@@ -21,6 +22,7 @@ FROM NAMED tracker:Pictures
         {
             SERVICE <dbus:{{miner_fs_busname}}>
             {
+                {{values}}
                 ?urn a nmm:Photo ;
                     nie:isStoredAs ?file .
                 OPTIONAL { ?urn nco:creator ?creator . }
diff --git a/src/queries/collections.sparql.template b/src/queries/collections.sparql.template
index dfd0ab9f..e2d7137b 100644
--- a/src/queries/collections.sparql.template
+++ b/src/queries/collections.sparql.template
@@ -3,6 +3,7 @@ SELECT {{inner_projection}}
     {
         SELECT ?urn COUNT(?item) AS ?count
         {
+            {{values}}
             ?urn a nfo:DataContainer.
             ?item a nmm:Photo ;
                nie:isPartOf ?urn ;
diff --git a/src/queries/photos.sparql.template b/src/queries/photos.sparql.template
index 021609fd..862668a0 100644
--- a/src/queries/photos.sparql.template
+++ b/src/queries/photos.sparql.template
@@ -5,6 +5,7 @@ FROM tracker:Pictures
     {
         SELECT {{inner_projection}}
         {
+            {{values}}
             ?urn a nmm:Photo ;
                 nie:isStoredAs ?file .
             OPTIONAL { ?urn nco:creator ?creator . }


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