[tracker/wip/carlosg/sparql1.1: 50/56] libtracker-data: Implement TZ sparql function



commit 15c0f3d80e1c9403c83c061b59f6f6dfb2eb5ff2
Author: Carlos Garnacho <carlosg gnome org>
Date:   Tue Jun 4 12:30:01 2019 +0200

    libtracker-data: Implement TZ sparql function

 src/libtracker-data/tracker-db-interface-sqlite.c | 40 +++++++++++++++++++++++
 src/libtracker-data/tracker-sparql.c              |  8 ++++-
 2 files changed, 47 insertions(+), 1 deletion(-)
---
diff --git a/src/libtracker-data/tracker-db-interface-sqlite.c 
b/src/libtracker-data/tracker-db-interface-sqlite.c
index 8c244c3d1..84216e2b3 100644
--- a/src/libtracker-data/tracker-db-interface-sqlite.c
+++ b/src/libtracker-data/tracker-db-interface-sqlite.c
@@ -559,6 +559,44 @@ function_sparql_time_zone_duration (sqlite3_context *context,
        }
 }
 
+static void
+function_sparql_time_zone_substr (sqlite3_context *context,
+                                  int              argc,
+                                  sqlite3_value   *argv[])
+{
+       if (argc != 1) {
+               sqlite3_result_error (context, "Invalid argument count", -1);
+               return;
+       }
+
+       if (sqlite3_value_type (argv[0]) == SQLITE_NULL) {
+               sqlite3_result_null (context);
+               return;
+       } else if (sqlite3_value_numeric_type (argv[0]) == SQLITE_INTEGER) {
+               sqlite3_result_text (context, "", -1, NULL);
+       } else if (sqlite3_value_type (argv[0]) == SQLITE_TEXT) {
+               const gchar *str;
+               gint len;
+
+               str = sqlite3_value_text (argv[0]);
+               len = strlen (str);
+
+               if (g_str_has_suffix (str, "Z")) {
+                       sqlite3_result_text (context, "Z", -1, NULL);
+               } else {
+                       const gchar *tz;
+
+                       tz = &str[len - 5];
+                       if (tz[0] != '+' && tz[0] != '-')
+                               tz = "";
+
+                       sqlite3_result_text (context, g_strdup (tz), -1, g_free);
+               }
+       } else {
+               sqlite3_result_error (context, "Invalid argument type", -1);
+       }
+}
+
 static void
 function_sparql_cartesian_distance (sqlite3_context *context,
                                     int              argc,
@@ -1570,6 +1608,8 @@ initialize_functions (TrackerDBInterface *db_interface)
                  function_sparql_time_sort },
                { "SparqlTimezoneDuration", 1, SQLITE_ANY | SQLITE_DETERMINISTIC,
                  function_sparql_time_zone_duration },
+               { "SparqlTimezoneString", 1, SQLITE_ANY | SQLITE_DETERMINISTIC,
+                 function_sparql_time_zone_substr },
                /* Paths and filenames */
                { "SparqlStringFromFilename", 1, SQLITE_ANY | SQLITE_DETERMINISTIC,
                  function_sparql_string_from_filename },
diff --git a/src/libtracker-data/tracker-sparql.c b/src/libtracker-data/tracker-sparql.c
index 181631b0f..6b976b6f3 100644
--- a/src/libtracker-data/tracker-sparql.c
+++ b/src/libtracker-data/tracker-sparql.c
@@ -5937,7 +5937,13 @@ translate_BuiltInCall (TrackerSparql  *sparql,
        } else if (_accept (sparql, RULE_TYPE_LITERAL, LITERAL_TIMEZONE)) {
                _unimplemented ("TIMEZONE");
        } else if (_accept (sparql, RULE_TYPE_LITERAL, LITERAL_TZ)) {
-               _unimplemented ("TZ");
+               sparql->current_state.convert_to_string = TRUE;
+               _expect (sparql, RULE_TYPE_LITERAL, LITERAL_OPEN_PARENS);
+               _append_string (sparql, "SparqlTimezoneString (");
+               _call_rule (sparql, NAMED_RULE_Expression, error);
+               _expect (sparql, RULE_TYPE_LITERAL, LITERAL_CLOSE_PARENS);
+               _append_string (sparql, ") ");
+               sparql->current_state.expression_type = TRACKER_PROPERTY_TYPE_STRING;
        } else if (_accept (sparql, RULE_TYPE_LITERAL, LITERAL_MD5)) {
                sparql->current_state.convert_to_string = TRUE;
                _expect (sparql, RULE_TYPE_LITERAL, LITERAL_OPEN_PARENS);


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