tracker r2478 - in branches/libtracker-data: . src/libtracker-data src/trackerd
- From: juergbi svn gnome org
- To: svn-commits-list gnome org
- Subject: tracker r2478 - in branches/libtracker-data: . src/libtracker-data src/trackerd
- Date: Mon, 10 Nov 2008 09:38:02 +0000 (UTC)
Author: juergbi
Date: Mon Nov 10 09:38:02 2008
New Revision: 2478
URL: http://svn.gnome.org/viewvc/tracker?rev=2478&view=rev
Log:
2008-11-10 JÃrg Billeter <j bitron ch>
* src/libtracker-data/Makefile.am:
* src/libtracker-data/tracker-data-search.c:
* src/libtracker-data/tracker-data-search.h:
* src/trackerd/tracker-metadata.c:
Move SQL queries from trackerd/tracker-metadata to
libtracker-data/tracker-data-search
Modified:
branches/libtracker-data/ChangeLog
branches/libtracker-data/src/libtracker-data/Makefile.am
branches/libtracker-data/src/libtracker-data/tracker-data-search.c
branches/libtracker-data/src/libtracker-data/tracker-data-search.h
branches/libtracker-data/src/trackerd/tracker-metadata.c
Modified: branches/libtracker-data/src/libtracker-data/Makefile.am
==============================================================================
--- branches/libtracker-data/src/libtracker-data/Makefile.am (original)
+++ branches/libtracker-data/src/libtracker-data/Makefile.am Mon Nov 10 09:38:02 2008
@@ -5,6 +5,7 @@
-DG_LOG_DOMAIN=\"Tracker\" \
-DTRACKER_COMPILATION \
-I$(top_srcdir)/src \
+ $(DBUS_CFLAGS) \
$(GLIB2_CFLAGS)
libtracker_datadir = $(libdir)/tracker
@@ -38,5 +39,6 @@
libtracker_data_la_LIBADD = \
$(top_builddir)/src/libtracker-common/libtracker-common.la \
$(top_builddir)/src/libtracker-db/libtracker-db.la \
+ $(DBUS_LIBS) \
$(GLIB2_LIBS) \
-lz
Modified: branches/libtracker-data/src/libtracker-data/tracker-data-search.c
==============================================================================
--- branches/libtracker-data/src/libtracker-data/tracker-data-search.c (original)
+++ branches/libtracker-data/src/libtracker-data/tracker-data-search.c Mon Nov 10 09:38:02 2008
@@ -26,7 +26,9 @@
#include <string.h>
#include <stdlib.h>
+#include <libtracker-common/tracker-dbus.h>
#include <libtracker-common/tracker-type-utils.h>
+#include <libtracker-common/tracker-utils.h>
#include <libtracker-db/tracker-db-index.h>
#include <libtracker-db/tracker-db-interface-sqlite.h>
@@ -37,6 +39,9 @@
#include "tracker-data-schema.h"
#include "tracker-data-search.h"
#include "tracker-query-tree.h"
+#include "tracker-rdf-query.h"
+
+#define DEFAULT_METADATA_MAX_HITS 1024
TrackerDBResultSet *
tracker_data_search_text (TrackerDBInterface *iface,
@@ -623,3 +628,742 @@
NULL);
}
+
+static gint
+metadata_sanity_check_max_hits (gint max_hits)
+{
+ if (max_hits < 1) {
+ return DEFAULT_METADATA_MAX_HITS;
+ }
+
+ return max_hits;
+}
+
+static gboolean
+is_data_type_numeric (TrackerFieldType type) {
+ return (type == TRACKER_FIELD_TYPE_INTEGER
+ || type == TRACKER_FIELD_TYPE_DOUBLE);
+}
+
+static TrackerFieldData *
+tracker_metadata_add_metadata_field (TrackerDBInterface *iface,
+ const gchar *service,
+ GSList **fields,
+ const gchar *field_name,
+ gboolean is_select,
+ gboolean is_condition)
+{
+ TrackerFieldData *field_data;
+ gboolean field_exists;
+ GSList *l;
+
+ field_exists = FALSE;
+ field_data = NULL;
+
+ /* Check if field is already in list */
+ for (l = *fields; l; l = l->next) {
+ const gchar *this_field_name;
+
+ this_field_name = tracker_field_data_get_field_name (l->data);
+ if (!this_field_name) {
+ continue;
+ }
+
+ if (strcasecmp (this_field_name, field_name) == 0) {
+ field_data = l->data;
+ field_exists = TRUE;
+
+ if (is_condition) {
+ tracker_field_data_set_is_condition (field_data, TRUE);
+ }
+
+ break;
+ }
+ }
+
+ if (!field_exists) {
+ field_data = tracker_data_schema_get_metadata_field (iface,
+ service,
+ field_name,
+ g_slist_length (*fields),
+ is_select,
+ is_condition);
+ if (field_data) {
+ *fields = g_slist_prepend (*fields, field_data);
+ }
+ }
+
+ return field_data;
+}
+
+TrackerDBResultSet *
+tracker_data_search_get_unique_values (const gchar *service_type,
+ gchar **fields,
+ const gchar *query_condition,
+ gboolean order_desc,
+ gint offset,
+ gint max_hits,
+ GError **error)
+{
+ TrackerDBInterface *iface;
+ TrackerDBResultSet *result_set = NULL;
+
+ GSList *field_list = NULL;
+ gchar *str_offset, *str_limit;
+
+ GString *sql_select;
+ GString *sql_from;
+ GString *sql_where;
+ GString *sql_order;
+ gchar *sql;
+
+ char *rdf_where;
+ char *rdf_from;
+ GError *actual_error = NULL;
+
+ guint i;
+
+ g_return_val_if_fail (service_type != NULL, NULL);
+ g_return_val_if_fail (fields != NULL, NULL);
+ g_return_val_if_fail (query_condition != NULL, NULL);
+
+ if (!tracker_ontology_service_is_valid (service_type)) {
+ g_set_error (error, TRACKER_DBUS_ERROR, 0,
+ "Service_Type '%s' is invalid or has not been implemented yet",
+ service_type);
+ return NULL;
+ }
+
+ iface = tracker_db_manager_get_db_interface_by_service (service_type);
+
+ sql_select = g_string_new ("SELECT DISTINCT ");
+ sql_from = g_string_new ("\nFROM Services S ");
+ sql_where = g_string_new ("\nWHERE ");
+ sql_order = g_string_new ("\nORDER BY ");
+
+ for (i=0;i<g_strv_length(fields);i++) {
+ TrackerFieldData *def = NULL;
+ def = tracker_metadata_add_metadata_field (iface, service_type, &field_list, fields[i], FALSE, TRUE);
+
+ if (!def) {
+ g_string_free (sql_select, TRUE);
+ g_string_free (sql_from, TRUE);
+ g_string_free (sql_where, TRUE);
+ g_string_free (sql_order, TRUE);
+
+ g_set_error (error, TRACKER_DBUS_ERROR, 0,
+ "Invalid or non-existant metadata type '%s' specified",
+ fields[i]);
+ return NULL;
+ }
+
+ if (i) {
+ g_string_append_printf (sql_select, ",");
+ g_string_append_printf (sql_order, ",");
+ }
+
+ g_string_append_printf (sql_select, "%s", tracker_field_data_get_select_field (def));
+ g_string_append_printf (sql_order, " %s %s",
+ tracker_field_data_get_select_field (def),
+ order_desc ? "DESC" : "ASC" );
+
+ }
+
+ tracker_rdf_filter_to_sql (iface, query_condition, service_type,
+ &field_list, &rdf_from, &rdf_where, &actual_error);
+
+ if (actual_error) {
+
+ g_string_free (sql_select, TRUE);
+ g_string_free (sql_from, TRUE);
+ g_string_free (sql_where, TRUE);
+ g_string_free (sql_order, TRUE);
+
+ g_propagate_error (error, actual_error);
+ return NULL;
+ }
+
+ g_string_append_printf (sql_from, " %s ", rdf_from);
+ g_string_append_printf (sql_where, " %s ", rdf_where);
+
+ g_free (rdf_from);
+ g_free (rdf_where);
+
+ str_offset = tracker_gint_to_string (offset);
+ str_limit = tracker_gint_to_string (metadata_sanity_check_max_hits (max_hits));
+
+ g_string_append_printf (sql_order, " LIMIT %s,%s", str_offset, str_limit);
+
+ sql = g_strconcat (sql_select->str, " ", sql_from->str, " ", sql_where->str, " ", sql_order->str, NULL);
+
+ g_free (str_offset);
+ g_free (str_limit);
+
+ g_string_free (sql_select, TRUE);
+ g_string_free (sql_from, TRUE);
+ g_string_free (sql_where, TRUE);
+ g_string_free (sql_order, TRUE);
+
+ g_slist_foreach (field_list, (GFunc) g_object_unref, NULL);
+ g_slist_free (field_list);
+
+ g_message ("Unique values query executed:\n%s", sql);
+
+ result_set = tracker_db_interface_execute_query (iface, NULL, sql);
+
+ g_free (sql);
+
+ return result_set;
+}
+
+TrackerDBResultSet *
+tracker_data_search_get_unique_values_with_count (const gchar *service_type,
+ gchar **fields,
+ const gchar *query_condition,
+ const gchar *count_field,
+ gboolean order_desc,
+ gint offset,
+ gint max_hits,
+ GError **error)
+{
+ TrackerDBInterface *iface;
+ TrackerDBResultSet *result_set = NULL;
+
+ GSList *field_list = NULL;
+ gchar *str_offset, *str_limit;
+
+ GString *sql_select;
+ GString *sql_from;
+ GString *sql_where;
+ GString *sql_order;
+ GString *sql_group;
+ gchar *sql;
+
+ char *rdf_where;
+ char *rdf_from;
+ GError *actual_error = NULL;
+
+ guint i;
+
+ g_return_val_if_fail (service_type != NULL, NULL);
+ g_return_val_if_fail (fields != NULL, NULL);
+ g_return_val_if_fail (query_condition != NULL, NULL);
+
+ if (!tracker_ontology_service_is_valid (service_type)) {
+ g_set_error (error, TRACKER_DBUS_ERROR, 0,
+ "Service_Type '%s' is invalid or has not been implemented yet",
+ service_type);
+ return NULL;
+ }
+
+ iface = tracker_db_manager_get_db_interface_by_service (service_type);
+
+ sql_select = g_string_new ("SELECT DISTINCT ");
+ sql_from = g_string_new ("\nFROM Services S ");
+ sql_where = g_string_new ("\nWHERE ");
+ sql_order = g_string_new ("\nORDER BY ");
+ sql_group = g_string_new ("\nGROUP BY ");
+
+
+ for (i=0;i<g_strv_length(fields);i++) {
+ TrackerFieldData *def = NULL;
+ def = tracker_metadata_add_metadata_field (iface, service_type, &field_list, fields[i], FALSE, TRUE);
+
+ if (!def) {
+ g_string_free (sql_select, TRUE);
+ g_string_free (sql_from, TRUE);
+ g_string_free (sql_where, TRUE);
+ g_string_free (sql_order, TRUE);
+ g_string_free (sql_group, TRUE);
+
+ g_set_error (error, TRACKER_DBUS_ERROR, 0,
+ "Invalid or non-existant metadata type '%s' specified",
+ fields[i]);
+ return NULL;
+ }
+
+ if (i) {
+ g_string_append_printf (sql_select, ",");
+ g_string_append_printf (sql_order, ",");
+ g_string_append_printf (sql_group, ",");
+ }
+
+ g_string_append_printf (sql_select, "%s", tracker_field_data_get_select_field (def));
+ g_string_append_printf (sql_order, " %s %s",
+ tracker_field_data_get_select_field (def),
+ order_desc ? "DESC" : "ASC" );
+ g_string_append_printf (sql_group, "%s", tracker_field_data_get_select_field (def));
+
+ }
+
+ if (count_field && !(tracker_is_empty_string (count_field))) {
+ TrackerFieldData *def = NULL;
+
+ def = tracker_metadata_add_metadata_field (iface, service_type, &field_list, count_field, FALSE, TRUE);
+
+ if (!def) {
+ g_string_free (sql_select, TRUE);
+ g_string_free (sql_from, TRUE);
+ g_string_free (sql_where, TRUE);
+ g_string_free (sql_order, TRUE);
+ g_string_free (sql_group, TRUE);
+
+ g_set_error (error, TRACKER_DBUS_ERROR, 0,
+ "Invalid or non-existant metadata type '%s' specified",
+ count_field);
+ return NULL;
+ }
+
+ g_string_append_printf (sql_select, ", COUNT (DISTINCT %s)", tracker_field_data_get_select_field (def));
+ }
+
+ tracker_rdf_filter_to_sql (iface, query_condition, service_type,
+ &field_list, &rdf_from, &rdf_where, &actual_error);
+
+ if (actual_error) {
+
+ g_string_free (sql_select, TRUE);
+ g_string_free (sql_from, TRUE);
+ g_string_free (sql_where, TRUE);
+ g_string_free (sql_order, TRUE);
+ g_string_free (sql_group, TRUE);
+
+ g_propagate_error (error, actual_error);
+ return NULL;
+ }
+
+ g_string_append_printf (sql_from, " %s ", rdf_from);
+ g_string_append_printf (sql_where, " %s ", rdf_where);
+
+ g_free (rdf_from);
+ g_free (rdf_where);
+
+ str_offset = tracker_gint_to_string (offset);
+ str_limit = tracker_gint_to_string (metadata_sanity_check_max_hits (max_hits));
+
+ g_string_append_printf (sql_order, " LIMIT %s,%s", str_offset, str_limit);
+
+ sql = g_strconcat (sql_select->str, " ",
+ sql_from->str, " ",
+ sql_where->str, " ",
+ sql_group->str, " ",
+ sql_order->str, NULL);
+
+ g_free (str_offset);
+ g_free (str_limit);
+
+ g_string_free (sql_select, TRUE);
+ g_string_free (sql_from, TRUE);
+ g_string_free (sql_where, TRUE);
+ g_string_free (sql_order, TRUE);
+ g_string_free (sql_group, TRUE);
+
+ g_slist_foreach (field_list, (GFunc) g_object_unref, NULL);
+ g_slist_free (field_list);
+
+ g_message ("Unique values query executed:\n%s", sql);
+
+ result_set = tracker_db_interface_execute_query (iface, NULL, sql);
+
+ g_free (sql);
+
+ return result_set;
+}
+
+TrackerDBResultSet *
+tracker_data_search_get_unique_values_with_count_and_sum (const gchar *service_type,
+ gchar **fields,
+ const gchar *query_condition,
+ const gchar *count_field,
+ const gchar *sum_field,
+ gboolean order_desc,
+ gint offset,
+ gint max_hits,
+ GError **error)
+{
+ TrackerDBInterface *iface;
+ TrackerDBResultSet *result_set = NULL;
+
+ GSList *field_list = NULL;
+ gchar *str_offset, *str_limit;
+
+ GString *sql_select;
+ GString *sql_from;
+ GString *sql_where;
+ GString *sql_order;
+ GString *sql_group;
+ gchar *sql;
+
+ char *rdf_where;
+ char *rdf_from;
+ GError *actual_error = NULL;
+
+ guint i;
+
+ g_return_val_if_fail (service_type != NULL, NULL);
+ g_return_val_if_fail (fields != NULL, NULL);
+ g_return_val_if_fail (query_condition != NULL, NULL);
+
+ if (!tracker_ontology_service_is_valid (service_type)) {
+ g_set_error (error, TRACKER_DBUS_ERROR, 0,
+ "Service_Type '%s' is invalid or has not been implemented yet",
+ service_type);
+ return NULL;
+ }
+
+ iface = tracker_db_manager_get_db_interface_by_service (service_type);
+
+ sql_select = g_string_new ("SELECT DISTINCT ");
+ sql_from = g_string_new ("\nFROM Services S ");
+ sql_where = g_string_new ("\nWHERE ");
+ sql_order = g_string_new ("\nORDER BY ");
+ sql_group = g_string_new ("\nGROUP BY ");
+
+
+ for (i=0;i<g_strv_length(fields);i++) {
+ TrackerFieldData *def = NULL;
+ def = tracker_metadata_add_metadata_field (iface, service_type, &field_list, fields[i], FALSE, TRUE);
+
+ if (!def) {
+ g_string_free (sql_select, TRUE);
+ g_string_free (sql_from, TRUE);
+ g_string_free (sql_where, TRUE);
+ g_string_free (sql_order, TRUE);
+ g_string_free (sql_group, TRUE);
+
+ g_set_error (error, TRACKER_DBUS_ERROR, 0,
+ "Invalid or non-existant metadata type '%s' specified",
+ fields[i]);
+ return NULL;
+ }
+
+ if (i) {
+ g_string_append_printf (sql_select, ",");
+ g_string_append_printf (sql_order, ",");
+ g_string_append_printf (sql_group, ",");
+ }
+
+ g_string_append_printf (sql_select, "%s", tracker_field_data_get_select_field (def));
+ g_string_append_printf (sql_order, " %s %s",
+ tracker_field_data_get_select_field (def),
+ order_desc ? "DESC" : "ASC" );
+ g_string_append_printf (sql_group, "%s", tracker_field_data_get_select_field (def));
+
+ }
+
+ if (count_field && !(tracker_is_empty_string (count_field))) {
+ TrackerFieldData *def = NULL;
+
+ def = tracker_metadata_add_metadata_field (iface, service_type, &field_list, count_field, FALSE, TRUE);
+
+ if (!def) {
+ g_string_free (sql_select, TRUE);
+ g_string_free (sql_from, TRUE);
+ g_string_free (sql_where, TRUE);
+ g_string_free (sql_order, TRUE);
+ g_string_free (sql_group, TRUE);
+
+ g_set_error (error, TRACKER_DBUS_ERROR, 0,
+ "Invalid or non-existant metadata type '%s' specified",
+ count_field);
+ return NULL;
+ }
+
+ g_string_append_printf (sql_select, ", COUNT (DISTINCT %s)", tracker_field_data_get_select_field (def));
+ }
+
+ if (sum_field && !(tracker_is_empty_string (sum_field))) {
+ TrackerFieldData *def = NULL;
+ TrackerFieldType data_type;
+
+ def = tracker_metadata_add_metadata_field (iface, service_type, &field_list, sum_field, FALSE, TRUE);
+
+ if (!def) {
+ g_string_free (sql_select, TRUE);
+ g_string_free (sql_from, TRUE);
+ g_string_free (sql_where, TRUE);
+ g_string_free (sql_order, TRUE);
+ g_string_free (sql_group, TRUE);
+
+ g_set_error (error, TRACKER_DBUS_ERROR, 0,
+ "Invalid or non-existant metadata type '%s' specified",
+ sum_field);
+ return NULL;
+ }
+
+ data_type = tracker_field_data_get_data_type (def);
+ if (!is_data_type_numeric (data_type)) {
+ g_string_free (sql_select, TRUE);
+ g_string_free (sql_from, TRUE);
+ g_string_free (sql_where, TRUE);
+ g_string_free (sql_order, TRUE);
+ g_string_free (sql_group, TRUE);
+
+ g_set_error (error, TRACKER_DBUS_ERROR, 0,
+ "Cannot sum '%s': this metadata type is not numeric",
+ sum_field);
+ return NULL;
+ }
+
+ g_string_append_printf (sql_select, ", SUM (%s)", tracker_field_data_get_select_field (def));
+ }
+
+ tracker_rdf_filter_to_sql (iface, query_condition, service_type,
+ &field_list, &rdf_from, &rdf_where, &actual_error);
+
+ if (actual_error) {
+
+ g_string_free (sql_select, TRUE);
+ g_string_free (sql_from, TRUE);
+ g_string_free (sql_where, TRUE);
+ g_string_free (sql_order, TRUE);
+ g_string_free (sql_group, TRUE);
+
+ g_propagate_error (error, actual_error);
+ return NULL;
+ }
+
+ g_string_append_printf (sql_from, " %s ", rdf_from);
+ g_string_append_printf (sql_where, " %s ", rdf_where);
+
+ g_free (rdf_from);
+ g_free (rdf_where);
+
+ str_offset = tracker_gint_to_string (offset);
+ str_limit = tracker_gint_to_string (metadata_sanity_check_max_hits (max_hits));
+
+ g_string_append_printf (sql_order, " LIMIT %s,%s", str_offset, str_limit);
+
+ sql = g_strconcat (sql_select->str, " ",
+ sql_from->str, " ",
+ sql_where->str, " ",
+ sql_group->str, " ",
+ sql_order->str, NULL);
+
+ g_free (str_offset);
+ g_free (str_limit);
+
+ g_string_free (sql_select, TRUE);
+ g_string_free (sql_from, TRUE);
+ g_string_free (sql_where, TRUE);
+ g_string_free (sql_order, TRUE);
+ g_string_free (sql_group, TRUE);
+
+ g_slist_foreach (field_list, (GFunc) g_object_unref, NULL);
+ g_slist_free (field_list);
+
+ g_message ("Unique values query executed:\n%s", sql);
+
+ result_set = tracker_db_interface_execute_query (iface, NULL, sql);
+
+ g_free (sql);
+
+ return result_set;
+}
+
+gint
+tracker_data_search_get_sum (const gchar *service_type,
+ const gchar *field,
+ const gchar *query_condition,
+ GError **error)
+{
+ TrackerDBInterface *iface;
+ TrackerDBResultSet *result_set = NULL;
+
+ gint sum;
+ GSList *fields = NULL;
+ TrackerFieldData *def = NULL;
+ TrackerFieldType data_type;
+ GString *sql_select;
+ GString *sql_from;
+ GString *sql_where;
+ gchar *sql;
+
+ char *rdf_where;
+ char *rdf_from;
+ GError *actual_error = NULL;
+
+ g_return_val_if_fail (service_type != NULL, 0);
+ g_return_val_if_fail (field != NULL, 0);
+ g_return_val_if_fail (query_condition != NULL, 0);
+
+ if (!tracker_ontology_service_is_valid (service_type)) {
+ g_set_error (error, TRACKER_DBUS_ERROR, 0,
+ "Service_Type '%s' is invalid or has not been implemented yet",
+ service_type);
+ return 0;
+ }
+
+ iface = tracker_db_manager_get_db_interface_by_service (service_type);
+
+ sql_select = g_string_new ("SELECT ");
+ sql_from = g_string_new ("\nFROM Services S ");
+ sql_where = g_string_new ("\nWHERE ");
+
+ def = tracker_metadata_add_metadata_field (iface, service_type, &fields, field, FALSE, TRUE);
+
+ if (!def) {
+ g_string_free (sql_select, TRUE);
+ g_string_free (sql_from, TRUE);
+ g_string_free (sql_where, TRUE);
+
+ g_set_error (error, TRACKER_DBUS_ERROR, 0,
+ "Invalid or non-existant metadata type '%s' specified",
+ field);
+ return 0;
+ }
+
+ data_type = tracker_field_data_get_data_type (def);
+ if (!is_data_type_numeric (data_type)) {
+ g_string_free (sql_select, TRUE);
+ g_string_free (sql_from, TRUE);
+ g_string_free (sql_where, TRUE);
+
+ g_set_error (error, TRACKER_DBUS_ERROR, 0,
+ "Cannot sum '%s': this metadata type is not numeric",
+ field);
+ return 0;
+ }
+
+ g_string_append_printf (sql_select, "SUM (%s)", tracker_field_data_get_select_field (def));
+
+ tracker_rdf_filter_to_sql (iface, query_condition, service_type,
+ &fields, &rdf_from, &rdf_where, &actual_error);
+
+ if (actual_error) {
+
+ g_string_free (sql_select, TRUE);
+ g_string_free (sql_from, TRUE);
+ g_string_free (sql_where, TRUE);
+
+ g_propagate_error (error, actual_error);
+ return 0;
+ }
+
+ g_string_append_printf (sql_from, " %s ", rdf_from);
+ g_string_append_printf (sql_where, " %s ", rdf_where);
+
+ g_free (rdf_from);
+ g_free (rdf_where);
+
+ sql = g_strconcat (sql_select->str, " ", sql_from->str, " ", sql_where->str, NULL);
+
+ g_string_free (sql_select, TRUE);
+ g_string_free (sql_from, TRUE);
+ g_string_free (sql_where, TRUE);
+
+ g_slist_foreach (fields, (GFunc) g_object_unref, NULL);
+ g_slist_free (fields);
+
+ g_debug ("Sum query executed:\n%s", sql);
+
+ result_set = tracker_db_interface_execute_query (iface, NULL, sql);
+
+ g_free (sql);
+
+ tracker_db_result_set_get (result_set, 0, &sum, -1);
+
+ if (result_set) {
+ g_object_unref (result_set);
+ }
+
+ return sum;
+}
+
+
+gint
+tracker_data_search_get_count (const gchar *service_type,
+ const gchar *field,
+ const gchar *query_condition,
+ GError **error)
+{
+ TrackerDBInterface *iface;
+ TrackerDBResultSet *result_set = NULL;
+ gint count;
+ GSList *fields = NULL;
+ TrackerFieldData *def = NULL;
+
+ GString *sql_select;
+ GString *sql_from;
+ GString *sql_where;
+ gchar *sql;
+
+ char *rdf_where;
+ char *rdf_from;
+ GError *actual_error = NULL;
+
+ g_return_val_if_fail (service_type != NULL, 0);
+ g_return_val_if_fail (field != NULL, 0);
+ g_return_val_if_fail (query_condition != NULL, 0);
+
+ if (!tracker_ontology_service_is_valid (service_type)) {
+ g_set_error (error, TRACKER_DBUS_ERROR, 0,
+ "Service_Type '%s' is invalid or has not been implemented yet",
+ service_type);
+ return 0;
+ }
+
+ iface = tracker_db_manager_get_db_interface_by_service (service_type);
+
+ sql_select = g_string_new ("SELECT ");
+ sql_from = g_string_new ("\nFROM Services S ");
+ sql_where = g_string_new ("\nWHERE ");
+
+ def = tracker_metadata_add_metadata_field (iface, service_type, &fields, field, FALSE, TRUE);
+
+ if (!def) {
+ g_string_free (sql_select, TRUE);
+ g_string_free (sql_from, TRUE);
+ g_string_free (sql_where, TRUE);
+
+ g_set_error (error, TRACKER_DBUS_ERROR, 0,
+ "Invalid or non-existant metadata type '%s' specified",
+ field);
+ return 0;
+ }
+
+ g_string_append_printf (sql_select, "COUNT (DISTINCT %s)", tracker_field_data_get_select_field (def));
+
+ tracker_rdf_filter_to_sql (iface, query_condition, service_type,
+ &fields, &rdf_from, &rdf_where, &actual_error);
+
+ if (actual_error) {
+
+ g_string_free (sql_select, TRUE);
+ g_string_free (sql_from, TRUE);
+ g_string_free (sql_where, TRUE);
+
+ g_propagate_error (error, actual_error);
+ return 0;
+ }
+
+ g_string_append_printf (sql_from, " %s ", rdf_from);
+ g_string_append_printf (sql_where, " %s ", rdf_where);
+
+ g_free (rdf_from);
+ g_free (rdf_where);
+
+ sql = g_strconcat (sql_select->str, " ", sql_from->str, " ", sql_where->str, NULL);
+
+ g_string_free (sql_select, TRUE);
+ g_string_free (sql_from, TRUE);
+ g_string_free (sql_where, TRUE);
+
+ g_slist_foreach (fields, (GFunc) g_object_unref, NULL);
+ g_slist_free (fields);
+
+ g_message ("Count query executed:\n%s", sql);
+
+ result_set = tracker_db_interface_execute_query (iface, NULL, sql);
+
+ g_free (sql);
+
+ tracker_db_result_set_get (result_set, 0, &count, -1);
+
+ if (result_set) {
+ g_object_unref (result_set);
+ }
+
+ return count;
+}
+
Modified: branches/libtracker-data/src/libtracker-data/tracker-data-search.h
==============================================================================
--- branches/libtracker-data/src/libtracker-data/tracker-data-search.h (original)
+++ branches/libtracker-data/src/libtracker-data/tracker-data-search.h Mon Nov 10 09:38:02 2008
@@ -74,6 +74,41 @@
TrackerDBResultSet *tracker_data_search_keywords_get_list (TrackerDBInterface *iface,
const gchar *service);
+TrackerDBResultSet *tracker_data_search_get_unique_values (const gchar *service_type,
+ gchar **fields,
+ const gchar *query_condition,
+ gboolean order_desc,
+ gint offset,
+ gint max_hits,
+ GError **error);
+gint tracker_data_search_get_sum (const gchar *service_type,
+ const gchar *field,
+ const gchar *query_condition,
+ GError **error);
+gint tracker_data_search_get_count (const gchar *service_type,
+ const gchar *field,
+ const gchar *query_condition,
+ GError **error);
+
+TrackerDBResultSet *tracker_data_search_get_unique_values_with_count (const gchar *service_type,
+ gchar **fields,
+ const gchar *query_condition,
+ const gchar *count,
+ gboolean order_desc,
+ gint offset,
+ gint max_hits,
+ GError **error);
+
+TrackerDBResultSet *tracker_data_search_get_unique_values_with_count_and_sum (const gchar *service_type,
+ gchar **fields,
+ const gchar *query_condition,
+ const gchar *count,
+ const gchar *sum,
+ gboolean order_desc,
+ gint offset,
+ gint max_hits,
+ GError **error);
+
G_END_DECLS
#endif /* __TRACKER_DATA_SEARCH_H__ */
Modified: branches/libtracker-data/src/trackerd/tracker-metadata.c
==============================================================================
--- branches/libtracker-data/src/trackerd/tracker-metadata.c (original)
+++ branches/libtracker-data/src/trackerd/tracker-metadata.c Mon Nov 10 09:38:02 2008
@@ -36,6 +36,7 @@
#include <libtracker-data/tracker-data-manager.h>
#include <libtracker-data/tracker-data-query.h>
#include <libtracker-data/tracker-data-schema.h>
+#include <libtracker-data/tracker-data-search.h>
#include <libtracker-data/tracker-rdf-query.h>
#include "tracker-indexer-client.h"
@@ -43,8 +44,6 @@
#include "tracker-metadata.h"
#include "tracker-marshal.h"
-#define DEFAULT_METADATA_MAX_HITS 1024
-
G_DEFINE_TYPE(TrackerMetadata, tracker_metadata, G_TYPE_OBJECT)
static void
@@ -67,67 +66,6 @@
* Functions
*/
-static gint
-metadata_sanity_check_max_hits (gint max_hits)
-{
- if (max_hits < 1) {
- return DEFAULT_METADATA_MAX_HITS;
- }
-
- return max_hits;
-}
-
-static TrackerFieldData *
-tracker_metadata_add_metadata_field (TrackerDBInterface *iface,
- const gchar *service,
- GSList **fields,
- const gchar *field_name,
- gboolean is_select,
- gboolean is_condition)
-{
- TrackerFieldData *field_data;
- gboolean field_exists;
- GSList *l;
-
- field_exists = FALSE;
- field_data = NULL;
-
- /* Check if field is already in list */
- for (l = *fields; l; l = l->next) {
- const gchar *this_field_name;
-
- this_field_name = tracker_field_data_get_field_name (l->data);
- if (!this_field_name) {
- continue;
- }
-
- if (strcasecmp (this_field_name, field_name) == 0) {
- field_data = l->data;
- field_exists = TRUE;
-
- if (is_condition) {
- tracker_field_data_set_is_condition (field_data, TRUE);
- }
-
- break;
- }
- }
-
- if (!field_exists) {
- field_data = tracker_data_schema_get_metadata_field (iface,
- service,
- field_name,
- g_slist_length (*fields),
- is_select,
- is_condition);
- if (field_data) {
- *fields = g_slist_prepend (*fields, field_data);
- }
- }
-
- return field_data;
-}
-
void
tracker_metadata_get (TrackerMetadata *object,
const gchar *service_type,
@@ -535,12 +473,6 @@
tracker_dbus_request_success (request_id);
}
-static gboolean
-is_data_type_numeric (TrackerFieldType type) {
- return (type == TRACKER_FIELD_TYPE_INTEGER
- || type == TRACKER_FIELD_TYPE_DOUBLE);
-}
-
void
tracker_metadata_get_unique_values (TrackerMetadata *object,
const gchar *service_type,
@@ -552,26 +484,13 @@
DBusGMethodInvocation *context,
GError **error)
{
- TrackerDBInterface *iface;
TrackerDBResultSet *result_set = NULL;
guint request_id;
GPtrArray *values = NULL;
- GSList *field_list = NULL;
- gchar *str_offset, *str_limit;
-
- GString *sql_select;
- GString *sql_from;
- GString *sql_where;
- GString *sql_order;
- gchar *sql;
- char *rdf_where;
- char *rdf_from;
GError *actual_error = NULL;
- guint i;
-
request_id = tracker_dbus_get_next_request_id ();
tracker_dbus_async_return_if_fail (service_type != NULL, context);
@@ -584,104 +503,18 @@
service_type,
query_condition);
- if (!tracker_ontology_service_is_valid (service_type)) {
- tracker_dbus_request_failed (request_id,
- &actual_error,
- "Service_Type '%s' is invalid or has not been implemented yet",
- service_type);
- dbus_g_method_return_error (context, actual_error);
- g_error_free (actual_error);
- return;
- }
-
- iface = tracker_db_manager_get_db_interface_by_service (service_type);
-
- sql_select = g_string_new ("SELECT DISTINCT ");
- sql_from = g_string_new ("\nFROM Services S ");
- sql_where = g_string_new ("\nWHERE ");
- sql_order = g_string_new ("\nORDER BY ");
-
- for (i=0;i<g_strv_length(fields);i++) {
- TrackerFieldData *def = NULL;
- def = tracker_metadata_add_metadata_field (iface, service_type, &field_list, fields[i], FALSE, TRUE);
-
- if (!def) {
- g_string_free (sql_select, TRUE);
- g_string_free (sql_from, TRUE);
- g_string_free (sql_where, TRUE);
- g_string_free (sql_order, TRUE);
-
- tracker_dbus_request_failed (request_id,
- &actual_error,
- "Invalid or non-existant metadata type '%s' specified",
- fields[i]);
- dbus_g_method_return_error (context, actual_error);
- g_error_free (actual_error);
- return;
- }
-
- if (i) {
- g_string_append_printf (sql_select, ",");
- g_string_append_printf (sql_order, ",");
- }
-
- g_string_append_printf (sql_select, "%s", tracker_field_data_get_select_field (def));
- g_string_append_printf (sql_order, " %s %s",
- tracker_field_data_get_select_field (def),
- order_desc ? "DESC" : "ASC" );
-
- }
-
- tracker_rdf_filter_to_sql (iface, query_condition, service_type,
- &field_list, &rdf_from, &rdf_where, &actual_error);
+ result_set = tracker_data_search_get_unique_values (service_type, fields,
+ query_condition,
+ order_desc,
+ offset,
+ max_hits,
+ &actual_error);
if (actual_error) {
-
- g_string_free (sql_select, TRUE);
- g_string_free (sql_from, TRUE);
- g_string_free (sql_where, TRUE);
- g_string_free (sql_order, TRUE);
-
-
- tracker_dbus_request_failed (request_id,
- &actual_error,
- NULL);
-
- dbus_g_method_return_error (context, actual_error);
- g_error_free (actual_error);
+ g_propagate_error (error, actual_error);
return;
}
- g_string_append_printf (sql_from, " %s ", rdf_from);
- g_string_append_printf (sql_where, " %s ", rdf_where);
-
- g_free (rdf_from);
- g_free (rdf_where);
-
- str_offset = tracker_gint_to_string (offset);
- str_limit = tracker_gint_to_string (metadata_sanity_check_max_hits (max_hits));
-
- g_string_append_printf (sql_order, " LIMIT %s,%s", str_offset, str_limit);
-
- sql = g_strconcat (sql_select->str, " ", sql_from->str, " ", sql_where->str, " ", sql_order->str, NULL);
-
- g_free (str_offset);
- g_free (str_limit);
-
- g_string_free (sql_select, TRUE);
- g_string_free (sql_from, TRUE);
- g_string_free (sql_where, TRUE);
- g_string_free (sql_order, TRUE);
-
- g_slist_foreach (field_list, (GFunc) g_object_unref, NULL);
- g_slist_free (field_list);
-
- g_message ("Unique values query executed:\n%s", sql);
-
- result_set = tracker_db_interface_execute_query (iface, NULL, sql);
-
- g_free (sql);
-
values = tracker_dbus_query_result_to_ptr_array (result_set);
dbus_g_method_return (context, values);
@@ -709,27 +542,13 @@
DBusGMethodInvocation *context,
GError **error)
{
- TrackerDBInterface *iface;
TrackerDBResultSet *result_set = NULL;
guint request_id;
GPtrArray *values = NULL;
- GSList *field_list = NULL;
- gchar *str_offset, *str_limit;
-
- GString *sql_select;
- GString *sql_from;
- GString *sql_where;
- GString *sql_order;
- GString *sql_group;
- gchar *sql;
- char *rdf_where;
- char *rdf_from;
GError *actual_error = NULL;
- guint i;
-
request_id = tracker_dbus_get_next_request_id ();
tracker_dbus_async_return_if_fail (service_type != NULL, context);
@@ -744,139 +563,20 @@
query_condition,
count_field);
- if (!tracker_ontology_service_is_valid (service_type)) {
- tracker_dbus_request_failed (request_id,
- &actual_error,
- "Service_Type '%s' is invalid or has not been implemented yet",
- service_type);
- dbus_g_method_return_error (context, actual_error);
- g_error_free (actual_error);
- return;
- }
-
- iface = tracker_db_manager_get_db_interface_by_service (service_type);
-
- sql_select = g_string_new ("SELECT DISTINCT ");
- sql_from = g_string_new ("\nFROM Services S ");
- sql_where = g_string_new ("\nWHERE ");
- sql_order = g_string_new ("\nORDER BY ");
- sql_group = g_string_new ("\nGROUP BY ");
-
-
- for (i=0;i<g_strv_length(fields);i++) {
- TrackerFieldData *def = NULL;
- def = tracker_metadata_add_metadata_field (iface, service_type, &field_list, fields[i], FALSE, TRUE);
-
- if (!def) {
- g_string_free (sql_select, TRUE);
- g_string_free (sql_from, TRUE);
- g_string_free (sql_where, TRUE);
- g_string_free (sql_order, TRUE);
- g_string_free (sql_group, TRUE);
-
- tracker_dbus_request_failed (request_id,
- &actual_error,
- "Invalid or non-existant metadata type '%s' specified",
- fields[i]);
- dbus_g_method_return_error (context, actual_error);
- g_error_free (actual_error);
- return;
- }
-
- if (i) {
- g_string_append_printf (sql_select, ",");
- g_string_append_printf (sql_order, ",");
- g_string_append_printf (sql_group, ",");
- }
-
- g_string_append_printf (sql_select, "%s", tracker_field_data_get_select_field (def));
- g_string_append_printf (sql_order, " %s %s",
- tracker_field_data_get_select_field (def),
- order_desc ? "DESC" : "ASC" );
- g_string_append_printf (sql_group, "%s", tracker_field_data_get_select_field (def));
-
- }
-
- if (count_field && !(tracker_is_empty_string (count_field))) {
- TrackerFieldData *def = NULL;
-
- def = tracker_metadata_add_metadata_field (iface, service_type, &field_list, count_field, FALSE, TRUE);
-
- if (!def) {
- g_string_free (sql_select, TRUE);
- g_string_free (sql_from, TRUE);
- g_string_free (sql_where, TRUE);
- g_string_free (sql_order, TRUE);
- g_string_free (sql_group, TRUE);
-
- tracker_dbus_request_failed (request_id,
- &actual_error,
- "Invalid or non-existant metadata type '%s' specified",
- count_field);
- dbus_g_method_return_error (context, actual_error);
- g_error_free (actual_error);
- return;
- }
-
- g_string_append_printf (sql_select, ", COUNT (DISTINCT %s)", tracker_field_data_get_select_field (def));
- }
-
- tracker_rdf_filter_to_sql (iface, query_condition, service_type,
- &field_list, &rdf_from, &rdf_where, &actual_error);
+ result_set = tracker_data_search_get_unique_values_with_count (service_type,
+ fields,
+ query_condition,
+ count_field,
+ order_desc,
+ offset,
+ max_hits,
+ &actual_error);
if (actual_error) {
-
- g_string_free (sql_select, TRUE);
- g_string_free (sql_from, TRUE);
- g_string_free (sql_where, TRUE);
- g_string_free (sql_order, TRUE);
- g_string_free (sql_group, TRUE);
-
-
- tracker_dbus_request_failed (request_id,
- &actual_error,
- NULL);
-
- dbus_g_method_return_error (context, actual_error);
- g_error_free (actual_error);
+ g_propagate_error (error, actual_error);
return;
}
- g_string_append_printf (sql_from, " %s ", rdf_from);
- g_string_append_printf (sql_where, " %s ", rdf_where);
-
- g_free (rdf_from);
- g_free (rdf_where);
-
- str_offset = tracker_gint_to_string (offset);
- str_limit = tracker_gint_to_string (metadata_sanity_check_max_hits (max_hits));
-
- g_string_append_printf (sql_order, " LIMIT %s,%s", str_offset, str_limit);
-
- sql = g_strconcat (sql_select->str, " ",
- sql_from->str, " ",
- sql_where->str, " ",
- sql_group->str, " ",
- sql_order->str, NULL);
-
- g_free (str_offset);
- g_free (str_limit);
-
- g_string_free (sql_select, TRUE);
- g_string_free (sql_from, TRUE);
- g_string_free (sql_where, TRUE);
- g_string_free (sql_order, TRUE);
- g_string_free (sql_group, TRUE);
-
- g_slist_foreach (field_list, (GFunc) g_object_unref, NULL);
- g_slist_free (field_list);
-
- g_message ("Unique values query executed:\n%s", sql);
-
- result_set = tracker_db_interface_execute_query (iface, NULL, sql);
-
- g_free (sql);
-
values = tracker_dbus_query_result_to_ptr_array (result_set);
dbus_g_method_return (context, values);
@@ -905,27 +605,13 @@
DBusGMethodInvocation *context,
GError **error)
{
- TrackerDBInterface *iface;
TrackerDBResultSet *result_set = NULL;
guint request_id;
GPtrArray *values = NULL;
- GSList *field_list = NULL;
- gchar *str_offset, *str_limit;
-
- GString *sql_select;
- GString *sql_from;
- GString *sql_where;
- GString *sql_order;
- GString *sql_group;
- gchar *sql;
- char *rdf_where;
- char *rdf_from;
GError *actual_error = NULL;
- guint i;
-
request_id = tracker_dbus_get_next_request_id ();
tracker_dbus_async_return_if_fail (service_type != NULL, context);
@@ -942,181 +628,21 @@
count_field,
sum_field);
- if (!tracker_ontology_service_is_valid (service_type)) {
- tracker_dbus_request_failed (request_id,
- &actual_error,
- "Service_Type '%s' is invalid or has not been implemented yet",
- service_type);
- dbus_g_method_return_error (context, actual_error);
- g_error_free (actual_error);
- return;
- }
-
- iface = tracker_db_manager_get_db_interface_by_service (service_type);
-
- sql_select = g_string_new ("SELECT DISTINCT ");
- sql_from = g_string_new ("\nFROM Services S ");
- sql_where = g_string_new ("\nWHERE ");
- sql_order = g_string_new ("\nORDER BY ");
- sql_group = g_string_new ("\nGROUP BY ");
-
-
- for (i=0;i<g_strv_length(fields);i++) {
- TrackerFieldData *def = NULL;
- def = tracker_metadata_add_metadata_field (iface, service_type, &field_list, fields[i], FALSE, TRUE);
-
- if (!def) {
- g_string_free (sql_select, TRUE);
- g_string_free (sql_from, TRUE);
- g_string_free (sql_where, TRUE);
- g_string_free (sql_order, TRUE);
- g_string_free (sql_group, TRUE);
-
- tracker_dbus_request_failed (request_id,
- &actual_error,
- "Invalid or non-existant metadata type '%s' specified",
- fields[i]);
- dbus_g_method_return_error (context, actual_error);
- g_error_free (actual_error);
- return;
- }
-
- if (i) {
- g_string_append_printf (sql_select, ",");
- g_string_append_printf (sql_order, ",");
- g_string_append_printf (sql_group, ",");
- }
-
- g_string_append_printf (sql_select, "%s", tracker_field_data_get_select_field (def));
- g_string_append_printf (sql_order, " %s %s",
- tracker_field_data_get_select_field (def),
- order_desc ? "DESC" : "ASC" );
- g_string_append_printf (sql_group, "%s", tracker_field_data_get_select_field (def));
-
- }
-
- if (count_field && !(tracker_is_empty_string (count_field))) {
- TrackerFieldData *def = NULL;
-
- def = tracker_metadata_add_metadata_field (iface, service_type, &field_list, count_field, FALSE, TRUE);
-
- if (!def) {
- g_string_free (sql_select, TRUE);
- g_string_free (sql_from, TRUE);
- g_string_free (sql_where, TRUE);
- g_string_free (sql_order, TRUE);
- g_string_free (sql_group, TRUE);
-
- tracker_dbus_request_failed (request_id,
- &actual_error,
- "Invalid or non-existant metadata type '%s' specified",
- count_field);
- dbus_g_method_return_error (context, actual_error);
- g_error_free (actual_error);
- return;
- }
-
- g_string_append_printf (sql_select, ", COUNT (DISTINCT %s)", tracker_field_data_get_select_field (def));
- }
-
- if (sum_field && !(tracker_is_empty_string (sum_field))) {
- TrackerFieldData *def = NULL;
- TrackerFieldType data_type;
-
- def = tracker_metadata_add_metadata_field (iface, service_type, &field_list, sum_field, FALSE, TRUE);
-
- if (!def) {
- g_string_free (sql_select, TRUE);
- g_string_free (sql_from, TRUE);
- g_string_free (sql_where, TRUE);
- g_string_free (sql_order, TRUE);
- g_string_free (sql_group, TRUE);
-
- tracker_dbus_request_failed (request_id,
- &actual_error,
- "Invalid or non-existant metadata type '%s' specified",
- sum_field);
- dbus_g_method_return_error (context, actual_error);
- g_error_free (actual_error);
- return;
- }
-
- data_type = tracker_field_data_get_data_type (def);
- if (!is_data_type_numeric (data_type)) {
- g_string_free (sql_select, TRUE);
- g_string_free (sql_from, TRUE);
- g_string_free (sql_where, TRUE);
- g_string_free (sql_order, TRUE);
- g_string_free (sql_group, TRUE);
-
- tracker_dbus_request_failed (request_id,
- &actual_error,
- "Cannot sum '%s': this metadata type is not numeric",
- sum_field);
- dbus_g_method_return_error (context, actual_error);
- g_error_free (actual_error);
- return;
- }
-
- g_string_append_printf (sql_select, ", SUM (%s)", tracker_field_data_get_select_field (def));
- }
-
- tracker_rdf_filter_to_sql (iface, query_condition, service_type,
- &field_list, &rdf_from, &rdf_where, &actual_error);
+ result_set = tracker_data_search_get_unique_values_with_count_and_sum (service_type,
+ fields,
+ query_condition,
+ count_field,
+ sum_field,
+ order_desc,
+ offset,
+ max_hits,
+ &actual_error);
if (actual_error) {
-
- g_string_free (sql_select, TRUE);
- g_string_free (sql_from, TRUE);
- g_string_free (sql_where, TRUE);
- g_string_free (sql_order, TRUE);
- g_string_free (sql_group, TRUE);
-
-
- tracker_dbus_request_failed (request_id,
- &actual_error,
- NULL);
-
- dbus_g_method_return_error (context, actual_error);
- g_error_free (actual_error);
+ g_propagate_error (error, actual_error);
return;
}
- g_string_append_printf (sql_from, " %s ", rdf_from);
- g_string_append_printf (sql_where, " %s ", rdf_where);
-
- g_free (rdf_from);
- g_free (rdf_where);
-
- str_offset = tracker_gint_to_string (offset);
- str_limit = tracker_gint_to_string (metadata_sanity_check_max_hits (max_hits));
-
- g_string_append_printf (sql_order, " LIMIT %s,%s", str_offset, str_limit);
-
- sql = g_strconcat (sql_select->str, " ",
- sql_from->str, " ",
- sql_where->str, " ",
- sql_group->str, " ",
- sql_order->str, NULL);
-
- g_free (str_offset);
- g_free (str_limit);
-
- g_string_free (sql_select, TRUE);
- g_string_free (sql_from, TRUE);
- g_string_free (sql_where, TRUE);
- g_string_free (sql_order, TRUE);
- g_string_free (sql_group, TRUE);
-
- g_slist_foreach (field_list, (GFunc) g_object_unref, NULL);
- g_slist_free (field_list);
-
- g_message ("Unique values query executed:\n%s", sql);
-
- result_set = tracker_db_interface_execute_query (iface, NULL, sql);
-
- g_free (sql);
-
values = tracker_dbus_query_result_to_ptr_array (result_set);
dbus_g_method_return (context, values);
@@ -1140,21 +666,10 @@
DBusGMethodInvocation *context,
GError **error)
{
- TrackerDBInterface *iface;
- TrackerDBResultSet *result_set = NULL;
guint request_id;
gint sum;
- GSList *fields = NULL;
- TrackerFieldData *def = NULL;
- TrackerFieldType data_type;
- GString *sql_select;
- GString *sql_from;
- GString *sql_where;
- gchar *sql;
- char *rdf_where;
- char *rdf_from;
GError *actual_error = NULL;
request_id = tracker_dbus_get_next_request_id ();
@@ -1170,95 +685,16 @@
field,
query_condition);
- if (!tracker_ontology_service_is_valid (service_type)) {
- tracker_dbus_request_failed (request_id,
- &actual_error,
- "Service '%s' is invalid or has not been implemented yet",
- service_type);
- dbus_g_method_return_error (context, actual_error);
- g_error_free (actual_error);
- return;
- }
-
- iface = tracker_db_manager_get_db_interface_by_service (service_type);
-
- sql_select = g_string_new ("SELECT ");
- sql_from = g_string_new ("\nFROM Services S ");
- sql_where = g_string_new ("\nWHERE ");
-
- def = tracker_metadata_add_metadata_field (iface, service_type, &fields, field, FALSE, TRUE);
-
- if (!def) {
- g_string_free (sql_select, TRUE);
- g_string_free (sql_from, TRUE);
- g_string_free (sql_where, TRUE);
- tracker_dbus_request_failed (request_id,
- &actual_error,
- "Invalid or non-existant metadata type '%s' specified",
- field);
- dbus_g_method_return_error (context, actual_error);
- g_error_free (actual_error);
- return;
- }
-
- data_type = tracker_field_data_get_data_type (def);
- if (!is_data_type_numeric (data_type)) {
- g_string_free (sql_select, TRUE);
- g_string_free (sql_from, TRUE);
- g_string_free (sql_where, TRUE);
- tracker_dbus_request_failed (request_id,
- &actual_error,
- "Cannot sum '%s': this metadata type is not numeric",
- field);
- dbus_g_method_return_error (context, actual_error);
- g_error_free (actual_error);
- return;
- }
-
- g_string_append_printf (sql_select, "SUM (%s)", tracker_field_data_get_select_field (def));
-
- tracker_rdf_filter_to_sql (iface, query_condition, service_type,
- &fields, &rdf_from, &rdf_where, &actual_error);
+ sum = tracker_data_search_get_sum (service_type,
+ field,
+ query_condition,
+ &actual_error);
if (actual_error) {
-
- g_string_free (sql_select, TRUE);
- g_string_free (sql_from, TRUE);
- g_string_free (sql_where, TRUE);
-
- tracker_dbus_request_failed (request_id, &actual_error, NULL);
- dbus_g_method_return_error (context, actual_error);
- g_error_free (actual_error);
+ g_propagate_error (error, actual_error);
return;
}
- g_string_append_printf (sql_from, " %s ", rdf_from);
- g_string_append_printf (sql_where, " %s ", rdf_where);
-
- g_free (rdf_from);
- g_free (rdf_where);
-
- sql = g_strconcat (sql_select->str, " ", sql_from->str, " ", sql_where->str, NULL);
-
- g_string_free (sql_select, TRUE);
- g_string_free (sql_from, TRUE);
- g_string_free (sql_where, TRUE);
-
- g_slist_foreach (fields, (GFunc) g_object_unref, NULL);
- g_slist_free (fields);
-
- g_debug ("Sum query executed:\n%s", sql);
-
- result_set = tracker_db_interface_execute_query (iface, NULL, sql);
-
- g_free (sql);
-
- tracker_db_result_set_get (result_set, 0, &sum, -1);
-
- if (result_set) {
- g_object_unref (result_set);
- }
-
dbus_g_method_return (context, sum);
tracker_dbus_request_success (request_id);
@@ -1275,20 +711,9 @@
DBusGMethodInvocation *context,
GError **error)
{
- TrackerDBInterface *iface;
- TrackerDBResultSet *result_set = NULL;
guint request_id;
gint count;
- GSList *fields = NULL;
- TrackerFieldData *def = NULL;
-
- GString *sql_select;
- GString *sql_from;
- GString *sql_where;
- gchar *sql;
- char *rdf_where;
- char *rdf_from;
GError *actual_error = NULL;
request_id = tracker_dbus_get_next_request_id ();
@@ -1304,84 +729,16 @@
field,
query_condition);
- if (!tracker_ontology_service_is_valid (service_type)) {
- tracker_dbus_request_failed (request_id,
- &actual_error,
- "Service '%s' is invalid or has not been implemented yet",
- service_type);
- dbus_g_method_return_error (context, actual_error);
- g_error_free (actual_error);
- return;
- }
-
- iface = tracker_db_manager_get_db_interface_by_service (service_type);
-
- sql_select = g_string_new ("SELECT ");
- sql_from = g_string_new ("\nFROM Services S ");
- sql_where = g_string_new ("\nWHERE ");
-
- def = tracker_metadata_add_metadata_field (iface, service_type, &fields, field, FALSE, TRUE);
-
- if (!def) {
- g_string_free (sql_select, TRUE);
- g_string_free (sql_from, TRUE);
- g_string_free (sql_where, TRUE);
-
- tracker_dbus_request_failed (request_id,
- &actual_error,
- "Invalid or non-existant metadata type '%s' specified",
- field);
- dbus_g_method_return_error (context, actual_error);
- g_error_free (actual_error);
- return;
- }
-
- g_string_append_printf (sql_select, "COUNT (DISTINCT %s)", tracker_field_data_get_select_field (def));
-
- tracker_rdf_filter_to_sql (iface, query_condition, service_type,
- &fields, &rdf_from, &rdf_where, &actual_error);
+ count = tracker_data_search_get_count (service_type,
+ field,
+ query_condition,
+ &actual_error);
if (actual_error) {
-
- g_string_free (sql_select, TRUE);
- g_string_free (sql_from, TRUE);
- g_string_free (sql_where, TRUE);
-
- tracker_dbus_request_failed (request_id,
- &actual_error,
- NULL);
- dbus_g_method_return_error (context, actual_error);
- g_error_free (actual_error);
+ g_propagate_error (error, actual_error);
return;
}
- g_string_append_printf (sql_from, " %s ", rdf_from);
- g_string_append_printf (sql_where, " %s ", rdf_where);
-
- g_free (rdf_from);
- g_free (rdf_where);
-
- sql = g_strconcat (sql_select->str, " ", sql_from->str, " ", sql_where->str, NULL);
-
- g_string_free (sql_select, TRUE);
- g_string_free (sql_from, TRUE);
- g_string_free (sql_where, TRUE);
-
- g_slist_foreach (fields, (GFunc) g_object_unref, NULL);
- g_slist_free (fields);
-
- g_message ("Count query executed:\n%s", sql);
-
- result_set = tracker_db_interface_execute_query (iface, NULL, sql);
-
- g_free (sql);
-
- tracker_db_result_set_get (result_set, 0, &count, -1);
-
- if (result_set) {
- g_object_unref (result_set);
- }
-
dbus_g_method_return (context, count);
tracker_dbus_request_success (request_id);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]