[tracker/tracker-0.8] SPARQL: Refactor use of contexts



commit 7e0104b4a1686caf1d393210f09c5be57fe5fd93
Author: Jürg Billeter <j bitron ch>
Date:   Mon May 17 11:46:16 2010 +0200

    SPARQL: Refactor use of contexts
    
    Create contexts within translate methods as preparation for
    improvements in variable handling.

 src/libtracker-data/tracker-sparql-expression.vala |    9 +---
 src/libtracker-data/tracker-sparql-pattern.vala    |   46 +++++++++++++------
 src/libtracker-data/tracker-sparql-query.vala      |   20 +++++++--
 3 files changed, 49 insertions(+), 26 deletions(-)
---
diff --git a/src/libtracker-data/tracker-sparql-expression.vala b/src/libtracker-data/tracker-sparql-expression.vala
index 653321f..94adfda 100644
--- a/src/libtracker-data/tracker-sparql-expression.vala
+++ b/src/libtracker-data/tracker-sparql-expression.vala
@@ -31,7 +31,6 @@ class Tracker.Sparql.Expression : Object {
 
 	Context context {
 		get { return query.context; }
-		set { query.context = value; }
 	}
 
 	Pattern pattern {
@@ -1136,16 +1135,12 @@ class Tracker.Sparql.Expression : Object {
 		if (current () == SparqlTokenType.SELECT) {
 			// scalar subquery
 
-			context = new Context.subquery (context);
-
 			sql.append ("(");
-			var type = pattern.translate_select (sql, true);
+			var select_context = pattern.translate_select (sql, true, true);
 			sql.append (")");
 
-			context = context.parent_context;
-
 			expect (SparqlTokenType.CLOSE_PARENS);
-			return type;
+			return select_context.type;
 		}
 
 		var optype = translate_expression (sql);
diff --git a/src/libtracker-data/tracker-sparql-pattern.vala b/src/libtracker-data/tracker-sparql-pattern.vala
index 7cea75b..aac144f 100644
--- a/src/libtracker-data/tracker-sparql-pattern.vala
+++ b/src/libtracker-data/tracker-sparql-pattern.vala
@@ -221,7 +221,14 @@ class Tracker.Sparql.Pattern : Object {
 
 	TripleContext? triple_context;
 
-	internal PropertyType translate_select (StringBuilder sql, bool subquery = false) throws SparqlError {
+	internal SelectContext translate_select (StringBuilder sql, bool subquery = false, bool scalar_subquery = false) throws SparqlError {
+		SelectContext result;
+		if (scalar_subquery) {
+			result = new SelectContext.subquery (context);
+		} else {
+			result = new SelectContext (context);
+		}
+		context = result;
 		var type = PropertyType.UNKNOWN;
 
 		var pattern_sql = new StringBuilder ();
@@ -247,7 +254,10 @@ class Tracker.Sparql.Pattern : Object {
 
 		accept (SparqlTokenType.WHERE);
 
-		translate_group_graph_pattern (pattern_sql);
+		var pattern = translate_group_graph_pattern (pattern_sql);
+		foreach (var key in pattern.var_set.get_keys ()) {
+			context.var_set.insert (key, VariableState.BOUND);
+		}
 
 		// process select variables
 		var after_where = get_location ();
@@ -390,7 +400,11 @@ class Tracker.Sparql.Pattern : Object {
 			query.bindings.append (binding);
 		}
 
-		return type;
+		context = context.parent_context;
+
+		result.type = type;
+
+		return result;
 	}
 
 	internal string parse_var_or_term (StringBuilder? sql, out bool is_var) throws SparqlError {
@@ -788,20 +802,26 @@ class Tracker.Sparql.Pattern : Object {
 		}
 	}
 
-	internal void translate_group_graph_pattern (StringBuilder sql) throws SparqlError {
+	internal Context translate_group_graph_pattern (StringBuilder sql) throws SparqlError {
 		expect (SparqlTokenType.OPEN_BRACE);
 
 		if (current () == SparqlTokenType.SELECT) {
-			translate_select (sql, true);
+			var result = translate_select (sql, true);
+			context = result;
 
 			// only export selected variables
 			context.var_set = context.select_var_set;
 			context.select_var_set = new HashTable<Variable,int>.full (direct_hash, direct_equal, g_object_unref, null);
 
 			expect (SparqlTokenType.CLOSE_BRACE);
-			return;
+
+			context = context.parent_context;
+			return result;
 		}
 
+		var result = new Context (context);
+		context = result;
+
 		SourceLocation[] filters = { };
 
 		bool in_triples_block = false;
@@ -846,9 +866,7 @@ class Tracker.Sparql.Pattern : Object {
 
 					sql.append_printf (") AS t%d_g LEFT JOIN (", left_index);
 
-					context = new Context (context);
-
-					translate_group_graph_pattern (sql);
+					context = translate_group_graph_pattern (sql);
 
 					sql.append_printf (") AS t%d_g", right_index);
 
@@ -1000,6 +1018,9 @@ class Tracker.Sparql.Pattern : Object {
 
 			set_location (end);
 		}
+
+		context = context.parent_context;
+		return result;
 	}
 
 	void translate_group_or_union_graph_pattern (StringBuilder sql) throws SparqlError {
@@ -1010,13 +1031,8 @@ class Tracker.Sparql.Pattern : Object {
 		long[] offsets = { };
 
 		do {
-			context = new Context (context);
-
-			contexts += context;
 			offsets += sql.len;
-			translate_group_graph_pattern (sql);
-
-			context = context.parent_context;
+			contexts += translate_group_graph_pattern (sql);
 		} while (accept (SparqlTokenType.UNION));
 
 		if (contexts.length > 1) {
diff --git a/src/libtracker-data/tracker-sparql-query.vala b/src/libtracker-data/tracker-sparql-query.vala
index ef688aa..3659ae6 100644
--- a/src/libtracker-data/tracker-sparql-query.vala
+++ b/src/libtracker-data/tracker-sparql-query.vala
@@ -165,6 +165,18 @@ namespace Tracker.Sparql {
 			return result;
 		}
 	}
+
+	class SelectContext : Context {
+		public PropertyType type;
+
+		public SelectContext (Context? parent_context = null) {
+			base (parent_context);
+		}
+
+		public SelectContext.subquery (Context parent_context) {
+			base.subquery (parent_context);
+		}
+	}
 }
 
 public class Tracker.Sparql.Query : Object {
@@ -490,7 +502,6 @@ public class Tracker.Sparql.Query : Object {
 		// ASK query
 
 		var pattern_sql = new StringBuilder ();
-		context = new Context ();
 
 		// build SQL
 		var sql = new StringBuilder ();
@@ -502,7 +513,7 @@ public class Tracker.Sparql.Query : Object {
 
 		accept (SparqlTokenType.WHERE);
 
-		pattern.translate_group_graph_pattern (pattern_sql);
+		context = pattern.translate_group_graph_pattern (pattern_sql);
 
 		// select from results of WHERE clause
 		sql.append (" FROM (");
@@ -555,7 +566,6 @@ public class Tracker.Sparql.Query : Object {
 		}
 
 		var pattern_sql = new StringBuilder ();
-		context = new Context ();
 
 		var sql = new StringBuilder ();
 
@@ -567,9 +577,11 @@ public class Tracker.Sparql.Query : Object {
 			var old_graph = current_graph;
 			current_graph = null;
 
-			pattern.translate_group_graph_pattern (pattern_sql);
+			context = pattern.translate_group_graph_pattern (pattern_sql);
 
 			current_graph = old_graph;
+		} else {
+			context = new Context ();
 		}
 
 		var after_where = get_location ();



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