[tracker/tracker-0.8] SPARQL: Allow aggregate expressions as subexpressions



commit d4c0a0738caf769980cc6371b4d6724053182917
Author: Jürg Billeter <j bitron ch>
Date:   Tue May 11 15:08:30 2010 +0200

    SPARQL: Allow aggregate expressions as subexpressions
    
    Fixes GB#617515.

 src/libtracker-data/tracker-sparql-expression.vala |   78 +++++++++++---------
 1 files changed, 44 insertions(+), 34 deletions(-)
---
diff --git a/src/libtracker-data/tracker-sparql-expression.vala b/src/libtracker-data/tracker-sparql-expression.vala
index 853babc..653321f 100644
--- a/src/libtracker-data/tracker-sparql-expression.vala
+++ b/src/libtracker-data/tracker-sparql-expression.vala
@@ -122,38 +122,7 @@ class Tracker.Sparql.Expression : Object {
 
 		long begin = sql.len;
 		var type = PropertyType.UNKNOWN;
-		if (accept (SparqlTokenType.COUNT)) {
-			sql.append ("COUNT(");
-			translate_aggregate_expression (sql);
-			sql.append (")");
-			type = PropertyType.INTEGER;
-		} else if (accept (SparqlTokenType.SUM)) {
-			sql.append ("SUM(");
-			type = translate_aggregate_expression (sql);
-			sql.append (")");
-		} else if (accept (SparqlTokenType.AVG)) {
-			sql.append ("AVG(");
-			type = translate_aggregate_expression (sql);
-			sql.append (")");
-		} else if (accept (SparqlTokenType.MIN)) {
-			sql.append ("MIN(");
-			type = translate_aggregate_expression (sql);
-			sql.append (")");
-		} else if (accept (SparqlTokenType.MAX)) {
-			sql.append ("MAX(");
-			type = translate_aggregate_expression (sql);
-			sql.append (")");
-		} else if (accept (SparqlTokenType.GROUP_CONCAT)) {
-			sql.append ("GROUP_CONCAT(");
-			expect (SparqlTokenType.OPEN_PARENS);
-			translate_expression_as_string (sql);
-			sql.append (", ");
-			expect (SparqlTokenType.COMMA);
-			sql.append (escape_sql_string_literal (parse_string_literal ()));
-			sql.append (")");
-			expect (SparqlTokenType.CLOSE_PARENS);
-			type = PropertyType.STRING;
-		} else if (current () == SparqlTokenType.VAR) {
+		if (current () == SparqlTokenType.VAR) {
 			type = translate_expression (sql);
 			// we need variable name in case of compositional subqueries
 			variable = context.get_variable (get_last_string ().substring (1));
@@ -812,6 +781,8 @@ class Tracker.Sparql.Expression : Object {
 	}
 
 	PropertyType translate_primary_expression (StringBuilder sql) throws SparqlError {
+		PropertyType type;
+
 		switch (current ()) {
 		case SparqlTokenType.OPEN_PARENS:
 			return translate_bracketted_expression (sql);
@@ -855,8 +826,6 @@ class Tracker.Sparql.Expression : Object {
 		case SparqlTokenType.STRING_LITERAL2:
 		case SparqlTokenType.STRING_LITERAL_LONG1:
 		case SparqlTokenType.STRING_LITERAL_LONG2:
-			PropertyType type;
-
 			var binding = new LiteralBinding ();
 			binding.literal = parse_string_literal (out type);
 			query.bindings.append (binding);
@@ -938,6 +907,47 @@ class Tracker.Sparql.Expression : Object {
 		case SparqlTokenType.REGEX:
 			translate_regex (sql);
 			return PropertyType.BOOLEAN;
+		case SparqlTokenType.COUNT:
+			next ();
+			sql.append ("COUNT(");
+			translate_aggregate_expression (sql);
+			sql.append (")");
+			return PropertyType.INTEGER;
+		case SparqlTokenType.SUM:
+			next ();
+			sql.append ("SUM(");
+			type = translate_aggregate_expression (sql);
+			sql.append (")");
+			return type;
+		case SparqlTokenType.AVG:
+			next ();
+			sql.append ("AVG(");
+			type = translate_aggregate_expression (sql);
+			sql.append (")");
+			return type;
+		case SparqlTokenType.MIN:
+			next ();
+			sql.append ("MIN(");
+			type = translate_aggregate_expression (sql);
+			sql.append (")");
+			return type;
+		case SparqlTokenType.MAX:
+			next ();
+			sql.append ("MAX(");
+			type = translate_aggregate_expression (sql);
+			sql.append (")");
+			return type;
+		case SparqlTokenType.GROUP_CONCAT:
+			next ();
+			sql.append ("GROUP_CONCAT(");
+			expect (SparqlTokenType.OPEN_PARENS);
+			translate_expression_as_string (sql);
+			sql.append (", ");
+			expect (SparqlTokenType.COMMA);
+			sql.append (escape_sql_string_literal (parse_string_literal ()));
+			sql.append (")");
+			expect (SparqlTokenType.CLOSE_PARENS);
+			return PropertyType.STRING;
 		case SparqlTokenType.PN_PREFIX:
 			next ();
 			string ns = get_last_string ();



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