From 3b6dd303323cfc5b7bebe5b1d88170f1030f2de2 Mon Sep 17 00:00:00 2001
From: Michael Gratton <mike@vee.net>
Date: Sat, 5 Sep 2020 14:13:01 +1000
Subject: [PATCH 001/124] Geary.Db.Context: Update access to
 DatabaseConnections

Ensure internal code can access a DatabaseConnection from context
objects to get access to connection-specific code, but make the
polymorphic context accessors internal so transactions can't access
them.
---
 src/engine/db/db-connection.vala             |  5 +++--
 src/engine/db/db-context.vala                |  8 ++++----
 src/engine/db/db-database-connection.vala    |  8 ++++----
 src/engine/db/db-database.vala               |  8 ++++----
 src/engine/db/db-result.vala                 |  8 ++++----
 src/engine/db/db-statement.vala              | 19 ++++++++++++-------
 src/engine/db/db-transaction-connection.vala | 11 +----------
 src/engine/imap-db/imap-db-attachment.vala   |  2 +-
 8 files changed, 33 insertions(+), 36 deletions(-)

diff --git a/src/engine/db/db-connection.vala b/src/engine/db/db-connection.vala
index 4f0859e1..ebce27dc 100644
--- a/src/engine/db/db-connection.vala
+++ b/src/engine/db/db-connection.vala
@@ -18,7 +18,7 @@
  * A connection will be automatically closed when its last reference
  * is dropped.
  */
-public interface Geary.Db.Connection : Context {
+public interface Geary.Db.Connection : BaseObject {
 
     private const string PRAGMA_FOREIGN_KEYS = "foreign_keys";
     private const string PRAGMA_RECURSIVE_TRIGGERS = "recursive_triggers";
@@ -278,7 +278,8 @@ public interface Geary.Db.Connection : Context {
      *
      * @see exec
      */
-    public abstract Result query(string sql, GLib.Cancellable? cancellable = null)
+    public abstract Result query(string sql,
+                                 GLib.Cancellable? cancellable = null)
         throws GLib.Error;
 
     /**
diff --git a/src/engine/db/db-context.vala b/src/engine/db/db-context.vala
index 9bbb8503..a59f6c4c 100644
--- a/src/engine/db/db-context.vala
+++ b/src/engine/db/db-context.vala
@@ -37,19 +37,19 @@ public abstract class Geary.Db.Context : BaseObject, Logging.Source {
     private weak Logging.Source? _logging_parent = null;
 
 
-    public virtual Database? get_database() {
+    internal virtual Database? get_database() {
         return get_connection() != null ? get_connection().database : null;
     }
 
-    public virtual Connection? get_connection() {
+    internal virtual DatabaseConnection? get_connection() {
         return get_statement() != null ? get_statement().connection : null;
     }
 
-    public virtual Statement? get_statement() {
+    internal virtual Statement? get_statement() {
         return get_result() != null ? get_result().statement : null;
     }
 
-    public virtual Result? get_result() {
+    internal virtual Result? get_result() {
         return null;
     }
 
diff --git a/src/engine/db/db-database-connection.vala b/src/engine/db/db-database-connection.vala
index 4e7ceb78..dd311bea 100644
--- a/src/engine/db/db-database-connection.vala
+++ b/src/engine/db/db-database-connection.vala
@@ -255,13 +255,13 @@ public class Geary.Db.DatabaseConnection : Context, Connection {
         return yield job.wait_for_completion_async();
     }
 
-    public override Connection? get_connection() {
-        return this;
-    }
-
     /** {@inheritDoc} */
     public override Logging.State to_logging_state() {
         return new Logging.State(this, "%u", this.cx_number);
     }
 
+    internal override DatabaseConnection? get_connection() {
+        return this;
+    }
+
 }
diff --git a/src/engine/db/db-database.vala b/src/engine/db/db-database.vala
index 592bd306..a807e7ba 100644
--- a/src/engine/db/db-database.vala
+++ b/src/engine/db/db-database.vala
@@ -358,10 +358,6 @@ public class Geary.Db.Database : Context {
     }
 
 
-    public override Database? get_database() {
-        return this;
-    }
-
     /** {@inheritDoc} */
     public override Logging.State to_logging_state() {
         return new Logging.State(
@@ -386,6 +382,10 @@ public class Geary.Db.Database : Context {
         this.thread_pool.add(new_job);
     }
 
+    internal override Database? get_database() {
+        return this;
+    }
+
     /**
      * Hook for subclasses to modify a new SQLite connection before use.
      *
diff --git a/src/engine/db/db-result.vala b/src/engine/db/db-result.vala
index 1ec3ed55..64c78756 100644
--- a/src/engine/db/db-result.vala
+++ b/src/engine/db/db-result.vala
@@ -294,15 +294,15 @@ public class Geary.Db.Result : Geary.Db.Context {
         return column;
     }
 
-    public override Result? get_result() {
-        return this;
-    }
-
     /** {@inheritDoc} */
     public override Logging.State to_logging_state() {
         return new Logging.State(this, this.finished ? "finished" : "not finished");
     }
 
+    internal override Result? get_result() {
+        return this;
+    }
+
     [PrintfFormat]
     private void log_result(string fmt, ...) {
         if (Db.Context.enable_sql_logging) {
diff --git a/src/engine/db/db-statement.vala b/src/engine/db/db-statement.vala
index 0a36dfb1..088b882b 100644
--- a/src/engine/db/db-statement.vala
+++ b/src/engine/db/db-statement.vala
@@ -13,7 +13,7 @@ public class Geary.Db.Statement : Context {
         get { return this.stmt.sql(); }
     }
 
-    public Connection connection { get; private set; }
+    internal DatabaseConnection connection { get; private set; }
 
     internal Sqlite.Statement stmt;
 
@@ -36,9 +36,14 @@ public class Geary.Db.Statement : Context {
 
     private Gee.HashSet<Memory.Buffer> held_buffers = new Gee.HashSet<Memory.Buffer>();
 
-    internal Statement(Connection connection, string sql) throws DatabaseError {
+    internal Statement(DatabaseConnection connection, string sql)
+        throws DatabaseError {
         this.connection = connection;
-        throw_on_error("Statement.ctor", connection.db.prepare_v2(sql, -1, out stmt, null), sql);
+        throw_on_error(
+            "Statement.ctor",
+            connection.db.prepare_v2(sql, -1, out stmt, null),
+            sql
+        );
     }
 
     /** Returns SQL for the statement with bound parameters expanded. */
@@ -271,13 +276,13 @@ public class Geary.Db.Statement : Context {
         return this;
     }
 
-    public override Statement? get_statement() {
-        return this;
-    }
-
     /** {@inheritDoc} */
     public override Logging.State to_logging_state() {
         return new Logging.State(this, this.sql);
     }
 
+    internal override Statement? get_statement() {
+        return this;
+    }
+
 }
diff --git a/src/engine/db/db-transaction-connection.vala b/src/engine/db/db-transaction-connection.vala
index 48244dbc..ebdd18b4 100644
--- a/src/engine/db/db-transaction-connection.vala
+++ b/src/engine/db/db-transaction-connection.vala
@@ -9,7 +9,7 @@
 /**
  * A connection to the database for transactions.
  */
-internal class Geary.Db.TransactionConnection : Context, Connection {
+internal class Geary.Db.TransactionConnection : BaseObject, Connection {
 
 
     /** {@inheritDoc} */
@@ -54,13 +54,4 @@ internal class Geary.Db.TransactionConnection : Context, Connection {
         this.db_cx.exec_file(file, cancellable);
     }
 
-    public override Connection? get_connection() {
-        return this;
-    }
-
-    /** {@inheritDoc} */
-    public override Logging.State to_logging_state() {
-        return new Logging.State(this, "");
-    }
-
 }
diff --git a/src/engine/imap-db/imap-db-attachment.vala b/src/engine/imap-db/imap-db-attachment.vala
index d8e8f9db..fa94b630 100644
--- a/src/engine/imap-db/imap-db-attachment.vala
+++ b/src/engine/imap-db/imap-db-attachment.vala
@@ -245,7 +245,7 @@ private class Geary.ImapDB.Attachment : Geary.Attachment {
         }
 
         // Ensure they're dead, Jim.
-        Db.Statement stmt = new Db.Statement(cx, """
+        Db.Statement stmt = cx.prepare("""
             DELETE FROM MessageAttachmentTable WHERE message_id = ?
         """);
         stmt.bind_rowid(0, message_id);
-- 
2.29.2

