[epiphany/history-rewrite] ephy-history-service: Add ephy_history_service_find_url_rows()



commit b7c29e067fa00e29f713885de7d5703bb20e6457
Author: Claudio Saavedra <csaavedra igalia com>
Date:   Mon Jul 18 15:30:00 2011 +0300

    ephy-history-service: Add ephy_history_service_find_url_rows()
    
    This finds all URLs that match a given EphyHistoryQuery. In order to
    find out URLs that have been visited in a given time, a join between
    the urls and visits tables is performed.

 lib/history/ephy-history-service-private.h    |    1 +
 lib/history/ephy-history-service-urls-table.c |  103 +++++++++++++++++++++++++
 2 files changed, 104 insertions(+), 0 deletions(-)
---
diff --git a/lib/history/ephy-history-service-private.h b/lib/history/ephy-history-service-private.h
index f362126..c86b16b 100644
--- a/lib/history/ephy-history-service-private.h
+++ b/lib/history/ephy-history-service-private.h
@@ -36,6 +36,7 @@ gboolean                 ephy_history_service_initialize_urls_table   (EphyHisto
 EphyHistoryURL *         ephy_history_service_get_url_row             (EphyHistoryService *self, const char *url_string, EphyHistoryURL *url);
 void                     ephy_history_service_add_url_row             (EphyHistoryService *self, EphyHistoryURL *url);
 void                     ephy_history_service_update_url_row          (EphyHistoryService *self, EphyHistoryURL *url);
+GList*                   ephy_history_service_find_url_rows           (EphyHistoryService *self, EphyHistoryQuery *query);
 
 gboolean                 ephy_history_service_initialize_visits_table (EphyHistoryService *self);
 void                     ephy_history_service_add_visit_row           (EphyHistoryService *self, EphyHistoryPageVisit *visit);
diff --git a/lib/history/ephy-history-service-urls-table.c b/lib/history/ephy-history-service-urls-table.c
index e9a7994..22d7d77 100644
--- a/lib/history/ephy-history-service-urls-table.c
+++ b/lib/history/ephy-history-service-urls-table.c
@@ -214,3 +214,106 @@ create_url_from_statement (EphySQLiteStatement *statement)
 
   return url;
 }
+
+GList *
+ephy_history_service_find_url_rows (EphyHistoryService *self, EphyHistoryQuery *query)
+{
+  EphyHistoryServicePrivate *priv = EPHY_HISTORY_SERVICE (self)->priv;
+  EphySQLiteStatement *statement = NULL;
+  GList *substring;
+  GString *statement_str;
+  GList *urls = NULL;
+  GError *error = NULL;
+  const char *base_statement = ""
+    "SELECT "
+      "DISTINCT urls.id, "
+      "urls.url, "
+      "urls.title, "
+      "urls.visit_count, "
+      "urls.typed_count, "
+      "urls.last_visit_time, "
+      "urls.zoom_level "
+    "FROM "
+      "urls JOIN visits ON visits.url = urls.id "
+    "WHERE ";
+
+  int i = 0;
+
+  g_assert (priv->history_thread == g_thread_self ());
+  g_assert (priv->history_database != NULL);
+
+  statement_str = g_string_new (base_statement);
+
+  if (query->from >= 0)
+    statement_str = g_string_append (statement_str, "visits.visit_time >= ? AND ");
+  if (query->to >= 0)
+    statement_str = g_string_append (statement_str, "visits.visit_time <= ? AND ");
+
+  for (substring = query->substring_list; substring != NULL; substring = substring->next) {
+    statement_str = g_string_append (statement_str, "(urls.url LIKE ? OR urls.title LIKE ?) AND ");
+  }
+
+  statement_str = g_string_append (statement_str, "1");
+
+  statement = ephy_sqlite_connection_create_statement (priv->history_database,
+						       statement_str->str, &error);
+  g_string_free (statement_str, TRUE);
+
+  if (error) {
+    g_error ("Could not build urls table query statement: %s", error->message);
+    g_error_free (error);
+    g_object_unref (statement);
+    return NULL;
+  }
+
+  if (query->from >= 0) {
+    if (ephy_sqlite_statement_bind_int (statement, i++, (int)query->from, &error) == FALSE) {
+      g_error ("Could not build urls table query statement: %s", error->message);
+      g_error_free (error);
+      g_object_unref (statement);
+      return NULL;
+    }
+  }
+  if (query->to >= 0) {
+    if (ephy_sqlite_statement_bind_int (statement, i++, (int)query->to, &error) == FALSE) {
+      g_error ("Could not build urls table query statement: %s", error->message);
+      g_error_free (error);
+      g_object_unref (statement);
+      return NULL;
+    }
+  }
+  for (substring = query->substring_list; substring != NULL; substring = substring->next) {
+    char *string = g_strdup_printf ("%%%s%%", (char*)substring->data);
+    if (ephy_sqlite_statement_bind_string (statement, i++, string, &error) == FALSE) {
+      g_error ("Could not build urls table query statement: %s", error->message);
+      g_error_free (error);
+      g_object_unref (statement);
+      g_free (string);
+      return NULL;
+    }
+    if (ephy_sqlite_statement_bind_string (statement, i++, string, &error) == FALSE) {
+      g_error ("Could not build urls table query statement: %s", error->message);
+      g_error_free (error);
+      g_object_unref (statement);
+      g_free (string);
+      return NULL;
+    }
+    g_free (string);
+  }
+
+  while (ephy_sqlite_statement_step (statement, &error)) {
+    urls = g_list_prepend (urls, create_url_from_statement (statement));
+  }
+  urls = g_list_reverse (urls);
+
+  if (error) {
+    g_error ("Could not execute urls table query statement: %s", error->message);
+    g_error_free (error);
+    g_object_unref (statement);
+    g_list_free_full (urls, (GDestroyNotify)ephy_history_url_free);
+    return NULL;
+  }
+
+  g_object_unref (statement);
+  return urls;
+}



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