[epiphany/history-rewrite: 31/45] ephy-history-service: Complete ephy_history_service_find_visit_rows()



commit 013db299af014504dd7c6607e8d271a76c45d2b1
Author: Claudio Saavedra <csaavedra igalia com>
Date:   Mon Jul 18 15:46:36 2011 +0300

    ephy-history-service: Complete ephy_history_service_find_visit_rows()
    
    The method was not taking into account the substrings in the given
    EphyQuery.  Update it to perform a join with the urls table when there
    are substrings present and match them.

 lib/history/ephy-history-service-visits-table.c |   81 ++++++++++++++++++++--
 1 files changed, 73 insertions(+), 8 deletions(-)
---
diff --git a/lib/history/ephy-history-service-visits-table.c b/lib/history/ephy-history-service-visits-table.c
index 7a182c4..04aef43 100644
--- a/lib/history/ephy-history-service-visits-table.c
+++ b/lib/history/ephy-history-service-visits-table.c
@@ -103,26 +103,91 @@ ephy_history_service_find_visit_rows (EphyHistoryService *self, EphyHistoryQuery
 {
   EphyHistoryServicePrivate *priv = EPHY_HISTORY_SERVICE (self)->priv;
   EphySQLiteStatement *statement = NULL;
+  GList *substring;
+  GString *statement_str;
   GList *visits = NULL;
   GError *error = NULL;
+  const char *base_statement = ""
+    "SELECT "
+      "visits.url, "
+      "visits.visit_time, "
+      "visits.visit_type ";
+  const char *from_join_statement = ""
+    "FROM "
+      "visits JOIN urls ON visits.url = urls.id ";
+  const char *from_visits_statement = ""
+    "FROM "
+      "visits ";
+
+  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->substring_list) {
+    statement_str = g_string_append (statement_str, from_join_statement);
+  } else {
+    statement_str = g_string_append (statement_str, from_visits_statement);
+  }
+
+  statement_str = g_string_append (statement_str, "WHERE ");
+
+  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,
-    "SELECT url, visit_time, visit_type FROM visits WHERE visit_time >= ? and visit_time <= ?", &error);
+						       statement_str->str, &error);
+  g_string_free (statement_str, TRUE);
+
   if (error) {
     g_error ("Could not build visits table query statement: %s", error->message);
     g_error_free (error);
-    return NULL;
+    g_object_unref (statement);
   }
 
-  if (ephy_sqlite_statement_bind_int (statement, 0, (int) query->from, &error) == FALSE ||
-      ephy_sqlite_statement_bind_int (statement, 1, (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;
+  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)) {



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