[tracker/table-name] libtracker-data: Add Property.table_name property and use it



commit 79528ef64f2d817a2f160a372077c72be70bc341
Author: Jürg Billeter <j bitron ch>
Date:   Mon Mar 15 14:45:59 2010 +0100

    libtracker-data: Add Property.table_name property and use it

 src/libtracker-data/libtracker-data.vapi      |    3 +-
 src/libtracker-data/tracker-data-update.c     |   55 +++++--------------------
 src/libtracker-data/tracker-property.c        |   35 ++++++++++++++++
 src/libtracker-data/tracker-property.h        |    1 +
 src/libtracker-data/tracker-sparql-query.vala |   26 +++---------
 5 files changed, 54 insertions(+), 66 deletions(-)
---
diff --git a/src/libtracker-data/libtracker-data.vapi b/src/libtracker-data/libtracker-data.vapi
index 467c2de..1857bfd 100644
--- a/src/libtracker-data/libtracker-data.vapi
+++ b/src/libtracker-data/libtracker-data.vapi
@@ -34,7 +34,8 @@ namespace Tracker {
 
 	[CCode (cheader_filename = "libtracker-data/tracker-property.h")]
 	public class Property : GLib.Object {
-		public string name { get; set; }
+		public string name { get; }
+		public string table_name { get; }
 		public string uri { get; set; }
 		public PropertyType data_type { get; set; }
 		public Class domain { get; set; }
diff --git a/src/libtracker-data/tracker-data-update.c b/src/libtracker-data/tracker-data-update.c
index 77ecbc7..d00ebec 100644
--- a/src/libtracker-data/tracker-data-update.c
+++ b/src/libtracker-data/tracker-data-update.c
@@ -993,16 +993,10 @@ get_property_values (TrackerProperty *property)
 		TrackerDBInterface *iface;
 		TrackerDBStatement *stmt;
 		TrackerDBResultSet *result_set;
-		gchar              *table_name;
+		const gchar        *table_name;
 		const gchar        *field_name;
 
-		if (multiple_values) {
-			table_name = g_strdup_printf ("%s_%s",
-				                      tracker_class_get_name (tracker_property_get_domain (property)),
-				                      tracker_property_get_name (property));
-		} else {
-			table_name = g_strdup (tracker_class_get_name (tracker_property_get_domain (property)));
-		}
+		table_name = tracker_property_get_table_name (property);
 		field_name = tracker_property_get_name (property);
 
 		iface = tracker_db_manager_get_db_interface ();
@@ -1041,8 +1035,6 @@ get_property_values (TrackerProperty *property)
 			g_object_unref (result_set);
 		}
 		g_object_unref (stmt);
-
-		g_free (table_name);
 	}
 
 	return old_values;
@@ -1163,7 +1155,7 @@ cache_set_metadata_decomposed (TrackerProperty  *property,
                                GError          **error)
 {
 	gboolean            multiple_values, fts;
-	gchar              *table_name;
+	const gchar        *table_name;
 	const gchar        *field_name;
 	TrackerProperty   **super_properties;
 	GValue              gvalue = { 0 };
@@ -1183,13 +1175,7 @@ cache_set_metadata_decomposed (TrackerProperty  *property,
 	}
 
 	multiple_values = tracker_property_get_multiple_values (property);
-	if (multiple_values) {
-		table_name = g_strdup_printf ("%s_%s",
-		                              tracker_class_get_name (tracker_property_get_domain (property)),
-		                              tracker_property_get_name (property));
-	} else {
-		table_name = g_strdup (tracker_class_get_name (tracker_property_get_domain (property)));
-	}
+	table_name = tracker_property_get_table_name (property);
 	field_name = tracker_property_get_name (property);
 
 	fts = tracker_property_get_fulltext_indexed (property);
@@ -1230,8 +1216,6 @@ cache_set_metadata_decomposed (TrackerProperty  *property,
 		change = TRUE;
 	}
 
-	g_free (table_name);
-
 	return change;
 }
 
@@ -1241,7 +1225,7 @@ delete_metadata_decomposed (TrackerProperty  *property,
                             GError          **error)
 {
 	gboolean            multiple_values, fts;
-	gchar              *table_name;
+	const gchar        *table_name;
 	const gchar        *field_name;
 	TrackerProperty   **super_properties;
 	GValue gvalue = { 0 };
@@ -1250,13 +1234,7 @@ delete_metadata_decomposed (TrackerProperty  *property,
 	gboolean            change = FALSE;
 
 	multiple_values = tracker_property_get_multiple_values (property);
-	if (multiple_values) {
-		table_name = g_strdup_printf ("%s_%s",
-		                              tracker_class_get_name (tracker_property_get_domain (property)),
-		                              tracker_property_get_name (property));
-	} else {
-		table_name = g_strdup (tracker_class_get_name (tracker_property_get_domain (property)));
-	}
+	table_name = tracker_property_get_table_name (property);
 	field_name = tracker_property_get_name (property);
 
 	fts = tracker_property_get_fulltext_indexed (property);
@@ -1264,7 +1242,6 @@ delete_metadata_decomposed (TrackerProperty  *property,
 	/* read existing property values */
 	old_values = get_old_property_values (property, &new_error);
 	if (new_error) {
-		g_free (table_name);
 		g_propagate_error (error, new_error);
 		return FALSE;
 	}
@@ -1272,7 +1249,6 @@ delete_metadata_decomposed (TrackerProperty  *property,
 	string_to_gvalue (value, tracker_property_get_data_type (property), &gvalue, &new_error);
 
 	if (new_error) {
-		g_free (table_name);
 		g_propagate_error (error, new_error);
 		return FALSE;
 	}
@@ -1287,8 +1263,6 @@ delete_metadata_decomposed (TrackerProperty  *property,
 		change = TRUE;
 	}
 
-	g_free (table_name);
-
 	/* also delete super property values */
 	super_properties = tracker_property_get_super_properties (property);
 	while (*super_properties) {
@@ -1354,7 +1328,7 @@ cache_delete_resource_type (TrackerClass *class,
 
 	for (p = 0; p < n_props; p++) {
 		gboolean            multiple_values, fts;
-		gchar              *table_name;
+		const gchar        *table_name;
 		const gchar        *field_name;
 		GValueArray        *old_values;
 		gint                i;
@@ -1366,13 +1340,7 @@ cache_delete_resource_type (TrackerClass *class,
 		}
 
 		multiple_values = tracker_property_get_multiple_values (prop);
-		if (multiple_values) {
-			table_name = g_strdup_printf ("%s_%s",
-			                              tracker_class_get_name (class),
-			                              tracker_property_get_name (prop));
-		} else {
-			table_name = g_strdup (tracker_class_get_name (class));
-		}
+		table_name = tracker_property_get_table_name (prop);
 		field_name = tracker_property_get_name (prop);
 
 		fts = tracker_property_get_fulltext_indexed (prop);
@@ -1391,8 +1359,6 @@ cache_delete_resource_type (TrackerClass *class,
 			cache_delete_value (table_name, field_name, &gvalue, multiple_values, fts,
 			                    tracker_property_get_data_type (prop) == TRACKER_PROPERTY_TYPE_DATETIME);
 		}
-
-		g_free (table_name);
 	}
 
 	cache_delete_row (class);
@@ -2279,9 +2245,8 @@ tracker_data_delete_resource_description (const gchar *graph,
 					format_sql_value_as_string (sql, property);
 
 					g_string_append_printf (sql,
-					                        " FROM \"%s_%s\" WHERE ID = ?",
-					                        tracker_class_get_name (tracker_property_get_domain (property)),
-					                        tracker_property_get_name (property));
+					                        " FROM \"%s\" WHERE ID = ?",
+					                        tracker_property_get_table_name (property));
 
 					stmt = tracker_db_interface_create_statement (iface, "%s", sql->str);
 					tracker_db_statement_bind_int (stmt, 0, resource_id);
diff --git a/src/libtracker-data/tracker-property.c b/src/libtracker-data/tracker-property.c
index 497649f..03ec40c 100644
--- a/src/libtracker-data/tracker-property.c
+++ b/src/libtracker-data/tracker-property.c
@@ -44,6 +44,7 @@ typedef struct _TrackerPropertyPriv TrackerPropertyPriv;
 struct _TrackerPropertyPriv {
 	gchar         *uri;
 	gchar         *name;
+	gchar         *table_name;
 
 	TrackerPropertyType  data_type;
 	TrackerClass   *domain;
@@ -77,6 +78,7 @@ enum {
 	PROP_0,
 	PROP_URI,
 	PROP_NAME,
+	PROP_TABLE_NAME,
 	PROP_DATA_TYPE,
 	PROP_DOMAIN,
 	PROP_RANGE,
@@ -177,6 +179,13 @@ tracker_property_class_init (TrackerPropertyClass *klass)
 	                                                      NULL,
 	                                                      G_PARAM_READABLE));
 	g_object_class_install_property (object_class,
+	                                 PROP_TABLE_NAME,
+	                                 g_param_spec_string ("table-name",
+	                                                      "table-name",
+	                                                      "Table name",
+	                                                      NULL,
+	                                                      G_PARAM_READABLE));
+	g_object_class_install_property (object_class,
 	                                 PROP_DATA_TYPE,
 	                                 g_param_spec_enum ("data-type",
 	                                                    "data-type",
@@ -309,6 +318,7 @@ property_finalize (GObject *object)
 
 	g_free (priv->uri);
 	g_free (priv->name);
+	g_free (priv->table_name);
 
 	if (priv->domain) {
 		g_object_unref (priv->domain);
@@ -340,6 +350,9 @@ property_get_property (GObject    *object,
 	case PROP_NAME:
 		g_value_set_string (value, priv->name);
 		break;
+	case PROP_TABLE_NAME:
+		g_value_set_string (value, tracker_property_get_table_name ((TrackerProperty *) object));
+		break;
 	case PROP_DATA_TYPE:
 		g_value_set_enum (value, priv->data_type);
 		break;
@@ -539,6 +552,28 @@ tracker_property_get_name (TrackerProperty *field)
 	return priv->name;
 }
 
+const gchar *
+tracker_property_get_table_name (TrackerProperty *field)
+{
+	TrackerPropertyPriv *priv;
+
+	g_return_val_if_fail (TRACKER_IS_PROPERTY (field), NULL);
+
+	priv = GET_PRIV (field);
+
+	if (!priv->table_name) {
+		if (priv->multiple_values) {
+			priv->table_name = g_strdup_printf ("%s_%s",
+				tracker_class_get_name (priv->domain),
+				priv->name);
+		} else {
+			priv->table_name = g_strdup (tracker_class_get_name (priv->domain));
+		}
+	}
+
+	return priv->table_name;
+}
+
 TrackerPropertyType
 tracker_property_get_data_type (TrackerProperty *field)
 {
diff --git a/src/libtracker-data/tracker-property.h b/src/libtracker-data/tracker-property.h
index 5dcb2fc..189d339 100644
--- a/src/libtracker-data/tracker-property.h
+++ b/src/libtracker-data/tracker-property.h
@@ -75,6 +75,7 @@ GType               tracker_property_get_type             (void) G_GNUC_CONST;
 TrackerProperty *   tracker_property_new                  (void);
 const gchar *       tracker_property_get_uri              (TrackerProperty      *property);
 const gchar *       tracker_property_get_name             (TrackerProperty      *property);
+const gchar *       tracker_property_get_table_name       (TrackerProperty      *property);
 TrackerPropertyType tracker_property_get_data_type        (TrackerProperty      *property);
 TrackerClass *      tracker_property_get_domain           (TrackerProperty      *property);
 TrackerClass *      tracker_property_get_range            (TrackerProperty      *property);
diff --git a/src/libtracker-data/tracker-sparql-query.vala b/src/libtracker-data/tracker-sparql-query.vala
index b9783bc..229eb6d 100644
--- a/src/libtracker-data/tracker-sparql-query.vala
+++ b/src/libtracker-data/tracker-sparql-query.vala
@@ -143,11 +143,7 @@ public class Tracker.SparqlQuery : Object {
 									append_expression_as_string (sql, "\"%s\"".printf (prop.name), prop.data_type);
 
 									sql.append (" AS \"object\" FROM ");
-									if (prop.multiple_values) {
-										sql.append_printf ("\"%s_%s\"", prop.domain.name, prop.name);
-									} else {
-										sql.append_printf ("\"%s\"", prop.domain.name);
-									}
+									sql.append_printf ("\"%s\"", prop.table_name);
 
 									sql.append (" WHERE ID = ?");
 
@@ -190,11 +186,7 @@ public class Tracker.SparqlQuery : Object {
 									append_expression_as_string (sql, "\"%s\"".printf (prop.name), prop.data_type);
 
 									sql.append (" AS \"object\" FROM ");
-									if (prop.multiple_values) {
-										sql.append_printf ("\"%s_%s\"", prop.domain.name, prop.name);
-									} else {
-										sql.append_printf ("\"%s\"", prop.domain.name);
-									}
+									sql.append_printf ("\"%s\"", prop.table_name);
 								}
 							}
 						} while (result_set.iter_next ());
@@ -217,11 +209,7 @@ public class Tracker.SparqlQuery : Object {
 							append_expression_as_string (sql, "\"%s\"".printf (prop.name), prop.data_type);
 
 							sql.append (" AS \"object\" FROM ");
-							if (prop.multiple_values) {
-								sql.append_printf ("\"%s_%s\"", prop.domain.name, prop.name);
-							} else {
-								sql.append_printf ("\"%s\"", prop.domain.name);
-							}
+							sql.append_printf ("\"%s\"", prop.table_name);
 						}
 					}
 				} else {
@@ -1634,13 +1622,13 @@ public class Tracker.SparqlQuery : Object {
 				long begin = sql.len;
 				sql.append_printf ("\"%s\"", prop.name);
 				convert_expression_to_string (sql, prop.data_type, begin);
-				sql.append_printf (",',') FROM \"%s_%s\" WHERE ID = ", prop.domain.name, prop.name);
+				sql.append_printf (",',') FROM \"%s\" WHERE ID = ", prop.table_name);
 				translate_expression (sql);
 				sql.append (")");
 
 				return PropertyType.STRING;
 			} else {
-				sql.append_printf ("(SELECT \"%s\" FROM \"%s\" WHERE ID = ", prop.name, prop.domain.name);
+				sql.append_printf ("(SELECT \"%s\" FROM \"%s\" WHERE ID = ", prop.name, prop.table_name);
 				translate_expression (sql);
 				sql.append (")");
 
@@ -2956,13 +2944,11 @@ public class Tracker.SparqlQuery : Object {
 					pv.domain = domain;
 				}
 
+				db_table = prop.table_name;
 				if (prop.multiple_values) {
-					db_table = "%s_%s".printf (prop.domain.name, prop.name);
 					// we can never share the table with multiple triples
 					// for multi value properties as a property may consist of multiple rows
 					share_table = false;
-				} else {
-					db_table = prop.domain.name;
 				}
 				subject_type = prop.domain;
 



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