[gnome-games/wip/exalm/cache2: 21/25] database: Cache Publisher



commit ac9785a5e858b2885bf3d4d6188e9797c96ed852
Author: Alexander Mikhaylenko <exalm7659 gmail com>
Date:   Sat Oct 6 13:39:02 2018 +0500

    database: Cache Publisher

 src/database/database-game.vala      |  4 ++-
 src/database/database-metadata.vala  | 53 +++++++++++++++++++++++++++++++++++-
 src/database/database-publisher.vala | 26 ++++++++++++++++++
 src/database/database.vala           |  3 +-
 src/meson.build                      |  1 +
 5 files changed, 84 insertions(+), 3 deletions(-)
---
diff --git a/src/database/database-game.vala b/src/database/database-game.vala
index 1f0ea147..e238c31f 100644
--- a/src/database/database-game.vala
+++ b/src/database/database-game.vala
@@ -13,6 +13,7 @@ private class Games.DatabaseGame : Object, Game {
        private Developer developer;
        private Genre genre;
        private Players players;
+       private Publisher publisher;
 
        public DatabaseGame (Database database, Game game) {
                this.database = database;
@@ -25,6 +26,7 @@ private class Games.DatabaseGame : Object, Game {
                developer = new DatabaseDeveloper (metadata);
                genre = new DatabaseGenre (metadata);
                players = new DatabasePlayers (metadata);
+               publisher = new DatabasePublisher (metadata);
        }
 
        public Uid get_uid () {
@@ -60,7 +62,7 @@ private class Games.DatabaseGame : Object, Game {
        }
 
        public Publisher get_publisher () {
-               return game.get_publisher ();
+               return publisher;
        }
 
        public Description get_description () {
diff --git a/src/database/database-metadata.vala b/src/database/database-metadata.vala
index 0c58b551..43974601 100644
--- a/src/database/database-metadata.vala
+++ b/src/database/database-metadata.vala
@@ -7,7 +7,8 @@ private class Games.DatabaseMetadata : Object {
                        description,
                        developer,
                        genre,
-                       players
+                       players,
+                       publisher
                FROM game_metadata WHERE uid=$UID;
        """;
 
@@ -35,6 +36,10 @@ private class Games.DatabaseMetadata : Object {
                UPDATE game_metadata SET players=$PLAYERS WHERE uid=$UID;
        """;
 
+       private const string SAVE_PUBLISHER_QUERY = """
+               UPDATE game_metadata SET publisher=$PUBLISHER WHERE uid=$UID;
+       """;
+
        private Game game;
        private Uid uid;
        private Cooperative cooperative;
@@ -42,6 +47,7 @@ private class Games.DatabaseMetadata : Object {
        private Developer developer;
        private Genre genre;
        private Players players;
+       private Publisher publisher;
 
        private string uid_value;
        private bool cooperative_value;
@@ -49,6 +55,7 @@ private class Games.DatabaseMetadata : Object {
        private string developer_value;
        private List<string> genre_value;
        private string players_value;
+       private string publisher_value;
 
        private Sqlite.Statement add_game_statement;
        private Sqlite.Statement load_statement;
@@ -57,12 +64,14 @@ private class Games.DatabaseMetadata : Object {
        private Sqlite.Statement save_developer_statement;
        private Sqlite.Statement save_genre_statement;
        private Sqlite.Statement save_players_statement;
+       private Sqlite.Statement save_publisher_statement;
 
        public bool cooperative_loaded { get; set; }
        public bool description_loaded { get; set; }
        public bool developer_loaded { get; set; }
        public bool genre_loaded { get; set; }
        public bool players_loaded { get; set; }
+       public bool publisher_loaded { get; set; }
 
        private bool game_added;
 
@@ -75,6 +84,7 @@ private class Games.DatabaseMetadata : Object {
                developer = game.get_developer ();
                genre = game.get_genre ();
                players = game.get_players ();
+               publisher = game.get_publisher ();
 
                try {
                        uid_value = game.get_uid ().get_uid ();
@@ -86,6 +96,7 @@ private class Games.DatabaseMetadata : Object {
                        save_developer_statement = Database.prepare (database, SAVE_DEVELOPER_QUERY);
                        save_genre_statement = Database.prepare (database, SAVE_GENRE_QUERY);
                        save_players_statement = Database.prepare (database, SAVE_PLAYERS_QUERY);
+                       save_publisher_statement = Database.prepare (database, SAVE_PUBLISHER_QUERY);
 
                        load_metadata ();
                }
@@ -144,6 +155,16 @@ private class Games.DatabaseMetadata : Object {
                return players_value;
        }
 
+       public string get_publisher () {
+               if (!publisher_loaded) {
+                       on_publisher_loaded ();
+                       publisher.notify.connect (on_publisher_loaded);
+                       return publisher.get_publisher ();
+               }
+
+               return publisher_value;
+       }
+
        private void on_cooperative_loaded () {
                if (!cooperative.has_loaded)
                        return;
@@ -199,6 +220,17 @@ private class Games.DatabaseMetadata : Object {
                save_players ();
        }
 
+       private void on_publisher_loaded () {
+               if (!publisher.has_loaded)
+                       return;
+
+               publisher_value = publisher.get_publisher ();
+               publisher_loaded = true;
+
+               add_game ();
+               save_publisher ();
+       }
+
        private void save_cooperative () {
                try {
                        save_cooperative_statement.reset ();
@@ -275,6 +307,20 @@ private class Games.DatabaseMetadata : Object {
                }
        }
 
+       private void save_publisher () {
+               try {
+                       save_publisher_statement.reset ();
+                       Database.bind_text (save_publisher_statement, "$UID", uid_value);
+                       Database.bind_text (save_publisher_statement, "$PUBLISHER", publisher_value);
+
+                       if (save_publisher_statement.step () != Sqlite.DONE)
+                               warning ("Execution failed.");
+               }
+               catch (Error e) {
+                       warning (e.message);
+               }
+       }
+
        private void add_game () {
                if (game_added)
                        return;
@@ -331,6 +377,11 @@ private class Games.DatabaseMetadata : Object {
                                players_value = load_statement.column_text (4);
                                players_loaded = true;
                        }
+
+                       if (load_statement.column_type (5) != Sqlite.NULL) {
+                               publisher_value = load_statement.column_text (5);
+                               publisher_loaded = true;
+                       }
                }
        }
 }
diff --git a/src/database/database-publisher.vala b/src/database/database-publisher.vala
new file mode 100644
index 00000000..c19bc48a
--- /dev/null
+++ b/src/database/database-publisher.vala
@@ -0,0 +1,26 @@
+// This file is part of GNOME Games. License: GPL-3.0+.
+
+private class Games.DatabasePublisher : Object, Publisher {
+       private DatabaseMetadata metadata;
+
+       public bool has_loaded { get; protected set; }
+
+       public DatabasePublisher (DatabaseMetadata metadata) {
+               this.metadata = metadata;
+
+               if (metadata.publisher_loaded)
+                       has_loaded = true;
+               else {
+                       metadata.notify["publisher-loaded"].connect (on_publisher_loaded);
+                       metadata.get_publisher ();
+               }
+       }
+
+       private void on_publisher_loaded (Object object, ParamSpec param) {
+               has_loaded = metadata.publisher_loaded;
+       }
+
+       public string get_publisher () {
+               return metadata.get_publisher ();
+       }
+}
diff --git a/src/database/database.vala b/src/database/database.vala
index 96a7fbae..94ddfee5 100644
--- a/src/database/database.vala
+++ b/src/database/database.vala
@@ -25,7 +25,8 @@ private class Games.Database : Object {
                        description TEXT,
                        developer TEXT,
                        genre TEXT,
-                       players TEXT
+                       players TEXT,
+                       publisher TEXT
                ) WITHOUT ROWID;
        """;
 
diff --git a/src/meson.build b/src/meson.build
index 734474d9..e3c2c38e 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -54,6 +54,7 @@ vala_sources = [
   'database/database-genre.vala',
   'database/database-metadata.vala',
   'database/database-players.vala',
+  'database/database-publisher.vala',
   'database/database-uri-iterator.vala',
   'database/database-uri-source.vala',
 


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