[tracker/property-functions: 2/2] SPARQL: Support graph retrieval using property functions



commit 834413f8fe44e04b4c8f9b3b63e38d56e6329dc5
Author: Jürg Billeter <j bitron ch>
Date:   Mon Dec 20 17:17:24 2010 +0100

    SPARQL: Support graph retrieval using property functions
    
    Property functions accept two extra optional arguments. The first
    optional argument specifies the delimiter between multiple property
    values. It is ignored for single-valued properties and it defaults to
    comma.
    
    If the second optional argument is present, the graph is returned in
    addition to the property value. The graph is separated from the
    property value by the delimiter specified in the argument.

 src/libtracker-data/tracker-sparql-expression.vala |   42 ++++++++++++++++---
 1 files changed, 35 insertions(+), 7 deletions(-)
---
diff --git a/src/libtracker-data/tracker-sparql-expression.vala b/src/libtracker-data/tracker-sparql-expression.vala
index f082d6e..49b49e5 100644
--- a/src/libtracker-data/tracker-sparql-expression.vala
+++ b/src/libtracker-data/tracker-sparql-expression.vala
@@ -668,22 +668,50 @@ class Tracker.Sparql.Expression : Object {
 			var expr = new StringBuilder ();
 			translate_expression (expr);
 
+			string value_separator = ",";
+			string? graph_separator = null;
+
+			if (accept (SparqlTokenType.COMMA)) {
+				value_separator = parse_string_literal ();
+
+				if (accept (SparqlTokenType.COMMA)) {
+					graph_separator = parse_string_literal ();
+				}
+			}
+
 			if (prop.multiple_values) {
+				// multi-valued property
 				sql.append ("(SELECT GROUP_CONCAT(");
 				long begin = sql.len;
 				sql.append_printf ("\"%s\"", prop.name);
 				convert_expression_to_string (sql, prop.data_type, begin);
-				sql.append_printf (",',') FROM \"%s\" WHERE ID = %s)", prop.table_name, expr.str);
+				if (graph_separator != null) {
+					sql.append_printf (" || %s || COALESCE((SELECT Uri FROM Resource WHERE ID = \"%s:graph\"), '')", escape_sql_string_literal (graph_separator), prop.name);
+				}
+				sql.append_printf (",%s)", escape_sql_string_literal (value_separator));
+				sql.append_printf (" FROM \"%s\" WHERE ID = %s)", prop.table_name, expr.str);
 
 				return PropertyType.STRING;
 			} else {
-				sql.append_printf ("(SELECT \"%s\" FROM \"%s\" WHERE ID = %s)", prop.name, prop.table_name, expr.str);
+				// single-valued property
+				if (graph_separator == null) {
+					sql.append_printf ("(SELECT \"%s\" FROM \"%s\" WHERE ID = %s)", prop.name, prop.table_name, expr.str);
 
-				if (prop.data_type == PropertyType.STRING) {
-					append_collate (sql);
-				}
+					if (prop.data_type == PropertyType.STRING) {
+						append_collate (sql);
+					}
 
-				return prop.data_type;
+					return prop.data_type;
+				} else {
+					sql.append ("(SELECT ");
+					long begin = sql.len;
+					sql.append_printf ("\"%s\"", prop.name);
+					convert_expression_to_string (sql, prop.data_type, begin);
+					sql.append_printf (" || %s || COALESCE((SELECT Uri FROM Resource WHERE ID = \"%s:graph\"), '')", escape_sql_string_literal (graph_separator), prop.name);
+					sql.append_printf (" FROM \"%s\" WHERE ID = %s)", prop.table_name, expr.str);
+
+					return PropertyType.STRING;
+				}
 			}
 		}
 	}



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