[tracker/harmattan-pre-signal-updates: 1379/1380] SPARQL: Rework variable name support to fix regression



commit 9269397179e17ad25943f51df9cfb100a41788a4
Author: Jürg Billeter <j bitron ch>
Date:   Wed Sep 22 16:44:04 2010 +0200

    SPARQL: Rework variable name support to fix regression

 src/libtracker-data/libtracker-data.vapi           |    2 +-
 src/libtracker-data/tracker-db-interface-sqlite.c  |   38 +++++++++++++++++---
 src/libtracker-data/tracker-db-interface.h         |    2 +
 src/libtracker-data/tracker-sparql-expression.vala |   11 ++----
 src/libtracker-data/tracker-sparql-pattern.vala    |    1 +
 src/libtracker-data/tracker-sparql-query.vala      |    9 +++--
 6 files changed, 45 insertions(+), 18 deletions(-)
---
diff --git a/src/libtracker-data/libtracker-data.vapi b/src/libtracker-data/libtracker-data.vapi
index 4bdef4f..388142b 100644
--- a/src/libtracker-data/libtracker-data.vapi
+++ b/src/libtracker-data/libtracker-data.vapi
@@ -72,7 +72,7 @@ namespace Tracker {
 		public abstract void bind_int (int index, int value);
 		public abstract void bind_text (int index, string value);
 		public abstract DBResultSet execute () throws DBInterfaceError;
-		public abstract DBCursor start_sparql_cursor (PropertyType[] types) throws DBInterfaceError;
+		public abstract DBCursor start_sparql_cursor (PropertyType[] types, string[] variable_names) throws DBInterfaceError;
 	}
 
 	[CCode (cheader_filename = "libtracker-data/tracker-class.h")]
diff --git a/src/libtracker-data/tracker-db-interface-sqlite.c b/src/libtracker-data/tracker-db-interface-sqlite.c
index 5e45fa8..2009aa5 100644
--- a/src/libtracker-data/tracker-db-interface-sqlite.c
+++ b/src/libtracker-data/tracker-db-interface-sqlite.c
@@ -72,6 +72,8 @@ struct TrackerDBCursor {
 	gboolean finished;
 	TrackerPropertyType *types;
 	gint n_types;
+	gchar **variable_names;
+	gint n_variable_names;
 };
 
 struct TrackerDBCursorClass {
@@ -95,7 +97,9 @@ static void                tracker_db_statement_sqlite_reset (TrackerDBStatement
 static TrackerDBCursor    *tracker_db_cursor_sqlite_new      (sqlite3_stmt         *sqlite_stmt,
                                                               TrackerDBStatement   *ref_stmt,
                                                               TrackerPropertyType  *types,
-                                                              gint                  n_types);
+                                                              gint                  n_types,
+                                                              const gchar         **variable_names,
+                                                              gint                  n_variable_names);
 static gboolean            tracker_db_cursor_get_boolean     (TrackerSparqlCursor  *cursor,
                                                               guint                 column);
 static gboolean            db_cursor_iter_next               (TrackerDBCursor      *cursor,
@@ -1092,11 +1096,17 @@ static void
 tracker_db_cursor_finalize (GObject *object)
 {
 	TrackerDBCursor *cursor;
+	int i;
 
 	cursor = TRACKER_DB_CURSOR (object);
 
 	g_free (cursor->types);
 
+	for (i = 0; i < cursor->n_variable_names; i++) {
+		g_free (cursor->variable_names[i]);
+	}
+	g_free (cursor->variable_names);
+
 	if (cursor->ref_stmt) {
 		cursor->ref_stmt->stmt_is_sunk = FALSE;
 		tracker_db_statement_sqlite_reset (cursor->ref_stmt);
@@ -1198,7 +1208,9 @@ static TrackerDBCursor *
 tracker_db_cursor_sqlite_new (sqlite3_stmt        *sqlite_stmt,
                               TrackerDBStatement  *ref_stmt,
                               TrackerPropertyType *types,
-                              gint                 n_types)
+                              gint                 n_types,
+                              const gchar        **variable_names,
+                              gint                 n_variable_names)
 {
 	TrackerDBCursor *cursor;
 
@@ -1224,6 +1236,16 @@ tracker_db_cursor_sqlite_new (sqlite3_stmt        *sqlite_stmt,
 		}
 	}
 
+	if (variable_names) {
+		gint i;
+
+		cursor->variable_names = g_new (gchar *, n_variable_names);
+		cursor->n_variable_names = n_variable_names;
+		for (i = 0; i < n_variable_names; i++) {
+			cursor->variable_names[i] = g_strdup (variable_names[i]);
+		}
+	}
+
 	return cursor;
 }
 
@@ -1428,7 +1450,11 @@ const gchar*
 tracker_db_cursor_get_variable_name (TrackerDBCursor *cursor,
                                      guint            column)
 {
-	return sqlite3_column_name (cursor->stmt, column);
+	if (column < cursor->n_variable_names) {
+		return cursor->variable_names[column];
+	} else {
+		return sqlite3_column_name (cursor->stmt, column);
+	}
 }
 
 const gchar*
@@ -1461,18 +1487,20 @@ tracker_db_statement_start_cursor (TrackerDBStatement  *stmt,
 {
 	g_return_val_if_fail (!stmt->stmt_is_sunk, NULL);
 
-	return tracker_db_cursor_sqlite_new (stmt->stmt, stmt, NULL, 0);
+	return tracker_db_cursor_sqlite_new (stmt->stmt, stmt, NULL, 0, NULL, 0);
 }
 
 TrackerDBCursor *
 tracker_db_statement_start_sparql_cursor (TrackerDBStatement   *stmt,
                                           TrackerPropertyType  *types,
                                           gint                  n_types,
+                                          const gchar         **variable_names,
+                                          gint                  n_variable_names,
                                           GError              **error)
 {
 	g_return_val_if_fail (!stmt->stmt_is_sunk, NULL);
 
-	return tracker_db_cursor_sqlite_new (stmt->stmt, stmt, types, n_types);
+	return tracker_db_cursor_sqlite_new (stmt->stmt, stmt, types, n_types, variable_names, n_variable_names);
 }
 
 static void
diff --git a/src/libtracker-data/tracker-db-interface.h b/src/libtracker-data/tracker-db-interface.h
index d63e1b1..2b160de 100644
--- a/src/libtracker-data/tracker-db-interface.h
+++ b/src/libtracker-data/tracker-db-interface.h
@@ -120,6 +120,8 @@ TrackerDBCursor *       tracker_db_statement_start_cursor       (TrackerDBStatem
 TrackerDBCursor *       tracker_db_statement_start_sparql_cursor(TrackerDBStatement  *stmt,
                                                                  TrackerPropertyType *types,
                                                                  gint                 n_types,
+                                                                 const gchar        **variable_names,
+                                                                 gint                 n_variable_names,
                                                                  GError             **error);
 
 /* Semi private TrackerDBResultSet functions */
diff --git a/src/libtracker-data/tracker-sparql-expression.vala b/src/libtracker-data/tracker-sparql-expression.vala
index b75b0d7..60387b7 100644
--- a/src/libtracker-data/tracker-sparql-expression.vala
+++ b/src/libtracker-data/tracker-sparql-expression.vala
@@ -148,10 +148,9 @@ class Tracker.Sparql.Expression : Object {
 				expect (SparqlTokenType.VAR);
 				variable = context.get_variable (get_last_string ().substring (1));
 			}
+			sql.append_printf (" AS %s", variable.sql_expression);
 
 			if (subquery) {
-				sql.append_printf (" AS %s", variable.sql_expression);
-
 				var binding = new VariableBinding ();
 				binding.data_type = type;
 				binding.variable = variable;
@@ -167,13 +166,9 @@ class Tracker.Sparql.Expression : Object {
 			}
 			context.select_var_set.insert (variable, state);
 
-			if (!subquery) {
-				sql.append_printf (" AS \"%s\"", variable.name);
-			}
+			((SelectContext) context).variable_names += variable.name;
 		} else {
-			if (!subquery) {
-				sql.append_printf (" AS \"var%d\"", variable_index + 1);
-			}
+			((SelectContext) context).variable_names += "var%d".printf (variable_index + 1);
 		}
 
 		return type;
diff --git a/src/libtracker-data/tracker-sparql-pattern.vala b/src/libtracker-data/tracker-sparql-pattern.vala
index 394dfac..3496f6b 100644
--- a/src/libtracker-data/tracker-sparql-pattern.vala
+++ b/src/libtracker-data/tracker-sparql-pattern.vala
@@ -298,6 +298,7 @@ class Tracker.Sparql.Pattern : Object {
 					sql.append_printf (" AS \"%s\"", variable.name);
 				}
 				result.types += variable.binding.data_type;
+				result.variable_names += variable.name;
 			}
 		} else {
 			for (int i = 0; ; i++) {
diff --git a/src/libtracker-data/tracker-sparql-query.vala b/src/libtracker-data/tracker-sparql-query.vala
index 4238a10..baa4d33 100644
--- a/src/libtracker-data/tracker-sparql-query.vala
+++ b/src/libtracker-data/tracker-sparql-query.vala
@@ -159,6 +159,7 @@ namespace Tracker.Sparql {
 	class SelectContext : Context {
 		public PropertyType type;
 		public PropertyType[] types = {};
+		public string[] variable_names = {};
 
 		public SelectContext (Context? parent_context = null) {
 			base (parent_context);
@@ -518,10 +519,10 @@ public class Tracker.Sparql.Query : Object {
 		return stmt.execute ();
 	}
 
-	DBCursor? exec_sql_cursor (string sql, PropertyType[] types) throws DBInterfaceError, Sparql.Error, DateError {
+	DBCursor? exec_sql_cursor (string sql, PropertyType[] types, string[] variable_names) throws DBInterfaceError, Sparql.Error, DateError {
 		var stmt = prepare_for_exec (sql);
 
-		return stmt.start_sparql_cursor (types);
+		return stmt.start_sparql_cursor (types, variable_names);
 	}
 
 	string get_select_query (out SelectContext context) throws DBInterfaceError, Sparql.Error, DateError {
@@ -545,7 +546,7 @@ public class Tracker.Sparql.Query : Object {
 		SelectContext context;
 		string sql = get_select_query (out context);
 
-		return exec_sql_cursor (sql, context.types);
+		return exec_sql_cursor (sql, context.types, context.variable_names);
 	}
 
 	string get_ask_query () throws DBInterfaceError, Sparql.Error, DateError {
@@ -580,7 +581,7 @@ public class Tracker.Sparql.Query : Object {
 	}
 
 	DBCursor? execute_ask_cursor () throws DBInterfaceError, Sparql.Error, DateError {
-		return exec_sql_cursor (get_ask_query (), new PropertyType[] { PropertyType.BOOLEAN });
+		return exec_sql_cursor (get_ask_query (), new PropertyType[] { PropertyType.BOOLEAN }, new string[] { "result" });
 	}
 
 	private void parse_from_or_into_param () throws Sparql.Error {



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