[gnome-games/wip/exalm/cache2: 7/22] database-developer: Reimplement using DatabaseMetadata



commit 2a5bcffa668e8b2ad25f11aa241f0834881aee60
Author: Alexander Mikhaylenko <exalm7659 gmail com>
Date:   Mon Oct 1 21:33:13 2018 +0500

    database-developer: Reimplement using DatabaseMetadata
    
    Since DatabaseMetadata can fetch developers now, turn DatabaseDeveloper
    into an adapter object.
    
    Also remove now unused Database.get_developer().

 src/database/database-developer.vala | 91 +++++-------------------------------
 src/database/database-game.vala      |  4 +-
 src/database/database.vala           |  4 --
 3 files changed, 14 insertions(+), 85 deletions(-)
---
diff --git a/src/database/database-developer.vala b/src/database/database-developer.vala
index 92868cea..8293ba4b 100644
--- a/src/database/database-developer.vala
+++ b/src/database/database-developer.vala
@@ -1,95 +1,26 @@
 // This file is part of GNOME Games. License: GPL-3.0+.
 
 private class Games.DatabaseDeveloper : Object, Developer {
-       private const string LOAD_DEVELOPER_QUERY = """
-               SELECT developer FROM game_metadata WHERE uid=$UID;
-       """;
-
-       private const string SAVE_DEVELOPER_QUERY = """
-               UPDATE game_metadata SET developer=$DEVELOPER WHERE uid=$UID;
-       """;
-
-       private Developer developer;
-       private Uid uid;
-       private Sqlite.Statement load_statement;
-       private Sqlite.Statement save_statement;
-       private string loaded;
+       private DatabaseMetadata metadata;
 
        public bool has_loaded { get; protected set; }
 
-       public DatabaseDeveloper (Sqlite.Database database, Developer developer, Uid uid) {
-               this.developer = developer;
-               this.uid = uid;
+       public DatabaseDeveloper (DatabaseMetadata metadata) {
+               this.metadata = metadata;
 
-               try {
-                       load_statement = Database.prepare (database, LOAD_DEVELOPER_QUERY);
-                       save_statement = Database.prepare (database, SAVE_DEVELOPER_QUERY);
-               }
-               catch (Error e) {
-                       warning (e.message);
+               if (metadata.developer_loaded)
+                       has_loaded = true;
+               else {
+                       metadata.notify["developer-loaded"].connect (on_developer_loaded);
+                       metadata.get_developer ();
                }
        }
 
-       private void on_developer_loaded () {
-               if (!developer.has_loaded)
-                       return;
-
-               has_loaded = true;
-
-               try {
-                       save_developer ();
-               }
-               catch (Error e) {
-                       warning (e.message);
-               }
+       private void on_developer_loaded (Object object, ParamSpec param) {
+               has_loaded = metadata.developer_loaded;
        }
 
        public string get_developer () {
-               if (!has_loaded) {
-                       on_developer_loaded ();
-                       developer.notify["has-loaded"].connect (on_developer_loaded);
-               }
-
-               try {
-                       if (loaded == null)
-                               load_developer ();
-                       if (loaded != "" && loaded != null)
-                               return loaded;
-               }
-               catch (Error e) {
-                       warning (e.message);
-               }
-
-               return developer.get_developer ();
-       }
-
-       private void load_developer () throws Error {
-               if (load_statement == null)
-                       return;
-
-               Database.bind_text (load_statement, "$UID", uid.get_uid ());
-
-               if (load_statement.step () == Sqlite.ROW)
-                       loaded = load_statement.column_text (0) ?? "";
-               else
-                       warning ("Execution failed.");
-
-               if (loaded != "" && loaded != null)
-                       has_loaded = true;
-       }
-
-       private void save_developer () throws Error {
-               if (save_statement == null)
-                       return;
-
-               loaded = developer.get_developer ();
-               if (loaded == "")
-                       return;
-
-               Database.bind_text (save_statement, "$UID", uid.get_uid ());
-               Database.bind_text (save_statement, "$DEVELOPER", loaded);
-
-               if (save_statement.step () != Sqlite.DONE)
-                       warning ("Execution failed.");
+               return metadata.get_developer ();
        }
 }
diff --git a/src/database/database-game.vala b/src/database/database-game.vala
index f9175a72..71d3137f 100644
--- a/src/database/database-game.vala
+++ b/src/database/database-game.vala
@@ -14,7 +14,9 @@ private class Games.DatabaseGame : Object, Game {
                this.database = database;
                this.game = game;
 
-               developer = database.get_developer (game.get_developer (), get_uid ());
+               var metadata = database.get_metadata (game);
+
+               developer = new DatabaseDeveloper (metadata);
        }
 
        public Uid get_uid () {
diff --git a/src/database/database.vala b/src/database/database.vala
index da7904ed..ebd54f45 100644
--- a/src/database/database.vala
+++ b/src/database/database.vala
@@ -67,10 +67,6 @@ private class Games.Database : Object {
                return new DatabaseUid (database, uid);
        }
 
-       public DatabaseDeveloper get_developer (Developer developer, Uid uid) {
-               return new DatabaseDeveloper (database, developer, uid);
-       }
-
        public DatabaseMetadata get_metadata (Game game) {
                return new DatabaseMetadata (database, game);
        }


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