[tracker/IorR-null-support: 2/3] libtracker-data: Support for insert or replace null objects



commit a53e62fe3813a0f9a42aadc75744d28acf60febe
Author: Philip Van Hoof <philip codeminded be>
Date:   Tue Aug 9 14:59:29 2011 +0200

    libtracker-data: Support for insert or replace null objects

 src/libtracker-data/libtracker-data.vapi        |    2 +-
 src/libtracker-data/tracker-data-update.c       |   13 ++++++-----
 src/libtracker-data/tracker-sparql-query.vala   |   26 +++++++++++++++++-----
 src/libtracker-data/tracker-sparql-scanner.vala |    5 ++++
 4 files changed, 33 insertions(+), 13 deletions(-)
---
diff --git a/src/libtracker-data/libtracker-data.vapi b/src/libtracker-data/libtracker-data.vapi
index 491cf9e..07feac1 100644
--- a/src/libtracker-data/libtracker-data.vapi
+++ b/src/libtracker-data/libtracker-data.vapi
@@ -200,7 +200,7 @@ namespace Tracker {
 		public void load_turtle_file (GLib.File file) throws Sparql.Error;
 		public void notify_transaction (CommitType commit_type);
 		public void delete_statement (string? graph, string subject, string predicate, string object) throws Sparql.Error, DateError;
-		public void update_statement (string? graph, string subject, string predicate, string object) throws Sparql.Error, DateError;
+		public void update_statement (string? graph, string subject, string predicate, string? object) throws Sparql.Error, DateError;
 		public void insert_statement (string? graph, string subject, string predicate, string object) throws Sparql.Error, DateError;
 		public void insert_statement_with_uri (string? graph, string subject, string predicate, string object) throws Sparql.Error;
 		public void insert_statement_with_string (string? graph, string subject, string predicate, string object) throws Sparql.Error, DateError;
diff --git a/src/libtracker-data/tracker-data-update.c b/src/libtracker-data/tracker-data-update.c
index 3bc7ad9..c252455 100644
--- a/src/libtracker-data/tracker-data-update.c
+++ b/src/libtracker-data/tracker-data-update.c
@@ -2729,7 +2729,6 @@ tracker_data_update_statement_with_uri (const gchar            *graph,
 
 	g_return_if_fail (subject != NULL);
 	g_return_if_fail (predicate != NULL);
-	g_return_if_fail (object != NULL);
 	g_return_if_fail (in_transaction);
 
 	property = tracker_ontologies_get_property_by_uri (predicate);
@@ -2935,7 +2934,6 @@ tracker_data_update_statement_with_string (const gchar            *graph,
 
 	g_return_if_fail (subject != NULL);
 	g_return_if_fail (predicate != NULL);
-	g_return_if_fail (object != NULL);
 	g_return_if_fail (in_transaction);
 
 	property = tracker_ontologies_get_property_by_uri (predicate);
@@ -3068,15 +3066,18 @@ tracker_data_update_statement (const gchar            *graph,
 
 	g_return_if_fail (subject != NULL);
 	g_return_if_fail (predicate != NULL);
-	g_return_if_fail (object != NULL);
 	g_return_if_fail (in_transaction);
 
 	property = tracker_ontologies_get_property_by_uri (predicate);
 	if (property != NULL) {
-		if (tracker_property_get_data_type (property) == TRACKER_PROPERTY_TYPE_RESOURCE) {
-			tracker_data_update_statement_with_uri (graph, subject, predicate, object, error);
+		if (object == NULL) {
+			delete_all_objects (graph, subject, predicate, error);
 		} else {
-			tracker_data_update_statement_with_string (graph, subject, predicate, object, error);
+			if (tracker_property_get_data_type (property) == TRACKER_PROPERTY_TYPE_RESOURCE) {
+				tracker_data_update_statement_with_uri (graph, subject, predicate, object, error);
+			} else {
+				tracker_data_update_statement_with_string (graph, subject, predicate, object, error);
+			}
 		}
 	} else {
 		g_set_error (error, TRACKER_SPARQL_ERROR, TRACKER_SPARQL_ERROR_UNKNOWN_PROPERTY,
diff --git a/src/libtracker-data/tracker-sparql-query.vala b/src/libtracker-data/tracker-sparql-query.vala
index 0c9f453..3040b13 100644
--- a/src/libtracker-data/tracker-sparql-query.vala
+++ b/src/libtracker-data/tracker-sparql-query.vala
@@ -755,14 +755,16 @@ public class Tracker.Sparql.Query : Object {
 		expect (SparqlTokenType.OPEN_BRACE);
 
 		while (current () != SparqlTokenType.CLOSE_BRACE) {
+			bool is_null; // ignored
+
 			if (accept (SparqlTokenType.GRAPH)) {
 				var old_graph = current_graph;
-				current_graph = parse_construct_var_or_term (var_value_map);
+				current_graph = parse_construct_var_or_term (var_value_map, out is_null);
 
 				expect (SparqlTokenType.OPEN_BRACE);
 
 				while (current () != SparqlTokenType.CLOSE_BRACE) {
-					current_subject = parse_construct_var_or_term (var_value_map);
+					current_subject = parse_construct_var_or_term (var_value_map, out is_null);
 					parse_construct_property_list_not_empty (var_value_map);
 					if (!accept (SparqlTokenType.DOT)) {
 						// no triples following
@@ -776,7 +778,7 @@ public class Tracker.Sparql.Query : Object {
 
 				accept (SparqlTokenType.DOT);
 			} else {
-				current_subject = parse_construct_var_or_term (var_value_map);
+				current_subject = parse_construct_var_or_term (var_value_map, out is_null);
 				parse_construct_property_list_not_empty (var_value_map);
 				if (!accept (SparqlTokenType.DOT) && current () != SparqlTokenType.GRAPH) {
 					// neither GRAPH nor triples following
@@ -790,8 +792,9 @@ public class Tracker.Sparql.Query : Object {
 
 	bool anon_blank_node_open = false;
 
-	string? parse_construct_var_or_term (HashTable<string,string> var_value_map) throws Sparql.Error, DateError {
+	string? parse_construct_var_or_term (HashTable<string,string> var_value_map, out bool is_null) throws Sparql.Error, DateError {
 		string result = "";
+		is_null = false;
 		if (current () == SparqlTokenType.VAR) {
 			next ();
 			result = var_value_map.lookup (get_last_string ().substring (1));
@@ -825,6 +828,10 @@ public class Tracker.Sparql.Query : Object {
 		} else if (current () == SparqlTokenType.INTEGER) {
 			next ();
 			result = get_last_string ();
+		} else if (current () == SparqlTokenType.NULL) {
+			next ();
+			result = "null";
+			is_null = true;
 		} else if (current () == SparqlTokenType.DECIMAL) {
 			next ();
 			result = get_last_string ();
@@ -920,7 +927,8 @@ public class Tracker.Sparql.Query : Object {
 	}
 
 	void parse_construct_object (HashTable<string,string> var_value_map) throws Sparql.Error, DateError {
-		string object = parse_construct_var_or_term (var_value_map);
+		bool is_null = false;
+		string object = parse_construct_var_or_term (var_value_map, out is_null);
 		if (current_subject == null || current_predicate == null || object == null) {
 			// the SPARQL specification says that triples containing unbound variables
 			// should be excluded from the output RDF graph of CONSTRUCT
@@ -929,12 +937,18 @@ public class Tracker.Sparql.Query : Object {
 		try {
 			if (update_statements) {
 				// update triple in database
-				Data.update_statement (current_graph, current_subject, current_predicate, object);
+				Data.update_statement (current_graph, current_subject, current_predicate, is_null ? null : object);
 			} else if (delete_statements) {
 				// delete triple from database
+				if (is_null) {
+					throw get_error ("'null' not supported in this mode");
+				}
 				Data.delete_statement (current_graph, current_subject, current_predicate, object);
 			} else {
 				// insert triple into database
+				if (is_null) {
+					throw get_error ("'null' not supported in this mode");
+				}
 				Data.insert_statement (current_graph, current_subject, current_predicate, object);
 			}
 		} catch (Sparql.Error e) {
diff --git a/src/libtracker-data/tracker-sparql-scanner.vala b/src/libtracker-data/tracker-sparql-scanner.vala
index dda1343..aed826d 100644
--- a/src/libtracker-data/tracker-sparql-scanner.vala
+++ b/src/libtracker-data/tracker-sparql-scanner.vala
@@ -169,6 +169,10 @@ public class Tracker.SparqlScanner : Object {
 			case 'l':
 				if (matches (begin, "LANG")) return SparqlTokenType.LANG;
 				break;
+			case 'N':
+			case 'n':
+				if (matches (begin, "NULL")) return SparqlTokenType.NULL;
+				break;
 			case 'T':
 			case 't':
 				if (matches (begin, "TRUE")) return SparqlTokenType.TRUE;
@@ -907,6 +911,7 @@ public enum Tracker.SparqlTokenType {
 	MINUS,
 	NAMED,
 	NOT,
+	NULL, /* Non-standard in SPARQL spec */
 	OFFSET,
 	OP_AND,
 	OP_EQ,



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