[epiphany/wip/google-safe-browsing: 56/57] sqlite-connection: Add function to enable foreign keys



commit 2e686fba88016d0265bf3fa1252f21b01bb0408a
Author: Gabriel Ivascu <gabrielivascu gnome org>
Date:   Tue Oct 3 16:25:15 2017 +0200

    sqlite-connection: Add function to enable foreign keys
    
    This removes the duplicate code in EphyHistoryService and EphyGSBStorage.

 lib/ephy-sqlite-connection.c         |   14 ++++++++++++++
 lib/ephy-sqlite-connection.h         |    1 +
 lib/history/ephy-history-service.c   |   21 ++-------------------
 lib/safe-browsing/ephy-gsb-storage.c |    7 +------
 4 files changed, 18 insertions(+), 25 deletions(-)
---
diff --git a/lib/ephy-sqlite-connection.c b/lib/ephy-sqlite-connection.c
index 3d34921..8f91e6b 100644
--- a/lib/ephy-sqlite-connection.c
+++ b/lib/ephy-sqlite-connection.c
@@ -191,6 +191,20 @@ ephy_sqlite_connection_get_last_insert_id (EphySQLiteConnection *self)
   return sqlite3_last_insert_rowid (self->database);
 }
 
+void
+ephy_sqlite_connection_enable_foreign_keys (EphySQLiteConnection *self)
+{
+  GError *error = NULL;
+
+  g_assert (EPHY_IS_SQLITE_CONNECTION (self));
+
+  ephy_sqlite_connection_execute (self, "PRAGMA foreign_keys=ON", &error);
+  if (error) {
+    g_warning ("Failed to enable foreign keys pragma: %s", error->message);
+    g_error_free (error);
+  }
+}
+
 gboolean
 ephy_sqlite_connection_begin_transaction (EphySQLiteConnection *self, GError **error)
 {
diff --git a/lib/ephy-sqlite-connection.h b/lib/ephy-sqlite-connection.h
index 2c19b65..b332c27 100644
--- a/lib/ephy-sqlite-connection.h
+++ b/lib/ephy-sqlite-connection.h
@@ -46,6 +46,7 @@ void                    ephy_sqlite_connection_get_error               (EphySQLi
 gboolean                ephy_sqlite_connection_execute                 (EphySQLiteConnection *self, const 
char *sql, GError **error);
 EphySQLiteStatement *   ephy_sqlite_connection_create_statement        (EphySQLiteConnection *self, const 
char *sql, GError **error);
 gint64                  ephy_sqlite_connection_get_last_insert_id      (EphySQLiteConnection *self);
+void                    ephy_sqlite_connection_enable_foreign_keys     (EphySQLiteConnection *self);
 
 gboolean                ephy_sqlite_connection_begin_transaction       (EphySQLiteConnection *self, GError 
**error);
 gboolean                ephy_sqlite_connection_commit_transaction      (EphySQLiteConnection *self, GError 
**error);
diff --git a/lib/history/ephy-history-service.c b/lib/history/ephy-history-service.c
index be43f26..448c4eb 100644
--- a/lib/history/ephy-history-service.c
+++ b/lib/history/ephy-history-service.c
@@ -391,23 +391,6 @@ ephy_history_service_commit_transaction (EphyHistoryService *self)
   }
 }
 
-static void
-ephy_history_service_enable_foreign_keys (EphyHistoryService *self)
-{
-  GError *error = NULL;
-
-  if (self->history_database == NULL)
-    return;
-
-  ephy_sqlite_connection_execute (self->history_database,
-                                  "PRAGMA foreign_keys = ON", &error);
-
-  if (error) {
-    g_warning ("Could not enable foreign keys pragma: %s", error->message);
-    g_error_free (error);
-  }
-}
-
 static gboolean
 ephy_history_service_open_database_connections (EphyHistoryService *self)
 {
@@ -434,10 +417,10 @@ ephy_history_service_open_database_connections (EphyHistoryService *self)
     }
     g_error_free (error);
     return FALSE;
+  } else {
+    ephy_sqlite_connection_enable_foreign_keys (self->history_database);
   }
 
-  ephy_history_service_enable_foreign_keys (self);
-
   return self->read_only ||
           (ephy_history_service_initialize_hosts_table (self) &&
            ephy_history_service_initialize_urls_table (self) &&
diff --git a/lib/safe-browsing/ephy-gsb-storage.c b/lib/safe-browsing/ephy-gsb-storage.c
index bff3b4f..18950ee 100644
--- a/lib/safe-browsing/ephy-gsb-storage.c
+++ b/lib/safe-browsing/ephy-gsb-storage.c
@@ -314,12 +314,7 @@ ephy_gsb_storage_open_db (EphyGSBStorage *self)
     return FALSE;
   }
 
-  /* Enable foreign keys. */
-  ephy_sqlite_connection_execute (self->db, "PRAGMA foreign_keys=ON", &error);
-  if (error) {
-    g_warning ("Failed to enable foreign keys pragma: %s", error->message);
-    g_clear_pointer (&error, (GDestroyNotify)g_error_free);
-  }
+  ephy_sqlite_connection_enable_foreign_keys (self->db);
 
   ephy_sqlite_connection_execute (self->db, "PRAGMA synchronous=OFF", &error);
   if (error) {


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