[gnome-games/wip/exalm/cache2: 16/20] database: Cache Players



commit 3ee8f433188546165d07e1bf3143e7ba516147d9
Author: Alexander Mikhaylenko <exalm7659 gmail com>
Date:   Sat Oct 6 13:32:04 2018 +0500

    database: Cache Players

 src/database/database-game.vala     |  4 ++-
 src/database/database-metadata.vala | 53 ++++++++++++++++++++++++++++++++++++-
 src/database/database-players.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 1e612e92..1f0ea147 100644
--- a/src/database/database-game.vala
+++ b/src/database/database-game.vala
@@ -12,6 +12,7 @@ private class Games.DatabaseGame : Object, Game {
        private Description description;
        private Developer developer;
        private Genre genre;
+       private Players players;
 
        public DatabaseGame (Database database, Game game) {
                this.database = database;
@@ -23,6 +24,7 @@ private class Games.DatabaseGame : Object, Game {
                description = new DatabaseDescription (metadata);
                developer = new DatabaseDeveloper (metadata);
                genre = new DatabaseGenre (metadata);
+               players = new DatabasePlayers (metadata);
        }
 
        public Uid get_uid () {
@@ -50,7 +52,7 @@ private class Games.DatabaseGame : Object, Game {
        }
 
        public Players get_players () {
-               return game.get_players ();
+               return players;
        }
 
        public Developer get_developer () {
diff --git a/src/database/database-metadata.vala b/src/database/database-metadata.vala
index a6060ccd..0c58b551 100644
--- a/src/database/database-metadata.vala
+++ b/src/database/database-metadata.vala
@@ -6,7 +6,8 @@ private class Games.DatabaseMetadata : Object {
                        cooperative,
                        description,
                        developer,
-                       genre
+                       genre,
+                       players
                FROM game_metadata WHERE uid=$UID;
        """;
 
@@ -30,18 +31,24 @@ private class Games.DatabaseMetadata : Object {
                UPDATE game_metadata SET genre=$GENRE WHERE uid=$UID;
        """;
 
+       private const string SAVE_PLAYERS_QUERY = """
+               UPDATE game_metadata SET players=$PLAYERS WHERE uid=$UID;
+       """;
+
        private Game game;
        private Uid uid;
        private Cooperative cooperative;
        private Description description;
        private Developer developer;
        private Genre genre;
+       private Players players;
 
        private string uid_value;
        private bool cooperative_value;
        private string description_value;
        private string developer_value;
        private List<string> genre_value;
+       private string players_value;
 
        private Sqlite.Statement add_game_statement;
        private Sqlite.Statement load_statement;
@@ -49,11 +56,13 @@ private class Games.DatabaseMetadata : Object {
        private Sqlite.Statement save_description_statement;
        private Sqlite.Statement save_developer_statement;
        private Sqlite.Statement save_genre_statement;
+       private Sqlite.Statement save_players_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; }
 
        private bool game_added;
 
@@ -65,6 +74,7 @@ private class Games.DatabaseMetadata : Object {
                description = game.get_description ();
                developer = game.get_developer ();
                genre = game.get_genre ();
+               players = game.get_players ();
 
                try {
                        uid_value = game.get_uid ().get_uid ();
@@ -75,6 +85,7 @@ private class Games.DatabaseMetadata : Object {
                        save_description_statement = Database.prepare (database, SAVE_DESCRIPTION_QUERY);
                        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);
 
                        load_metadata ();
                }
@@ -123,6 +134,16 @@ private class Games.DatabaseMetadata : Object {
                return genre_value;
        }
 
+       public string get_players () {
+               if (!players_loaded) {
+                       on_players_loaded ();
+                       players.notify.connect (on_players_loaded);
+                       return players.get_players ();
+               }
+
+               return players_value;
+       }
+
        private void on_cooperative_loaded () {
                if (!cooperative.has_loaded)
                        return;
@@ -167,6 +188,17 @@ private class Games.DatabaseMetadata : Object {
                save_genre ();
        }
 
+       private void on_players_loaded () {
+               if (!players.has_loaded)
+                       return;
+
+               players_value = players.get_players ();
+               players_loaded = true;
+
+               add_game ();
+               save_players ();
+       }
+
        private void save_cooperative () {
                try {
                        save_cooperative_statement.reset ();
@@ -229,6 +261,20 @@ private class Games.DatabaseMetadata : Object {
                }
        }
 
+       private void save_players () {
+               try {
+                       save_players_statement.reset ();
+                       Database.bind_text (save_players_statement, "$UID", uid_value);
+                       Database.bind_text (save_players_statement, "$PLAYERS", players_value);
+
+                       if (save_players_statement.step () != Sqlite.DONE)
+                               warning ("Execution failed.");
+               }
+               catch (Error e) {
+                       warning (e.message);
+               }
+       }
+
        private void add_game () {
                if (game_added)
                        return;
@@ -280,6 +326,11 @@ private class Games.DatabaseMetadata : Object {
 
                                genre_loaded = true;
                        }
+
+                       if (load_statement.column_type (4) != Sqlite.NULL) {
+                               players_value = load_statement.column_text (4);
+                               players_loaded = true;
+                       }
                }
        }
 }
diff --git a/src/database/database-players.vala b/src/database/database-players.vala
new file mode 100644
index 00000000..be79af76
--- /dev/null
+++ b/src/database/database-players.vala
@@ -0,0 +1,26 @@
+// This file is part of GNOME Games. License: GPL-3.0+.
+
+private class Games.DatabasePlayers : Object, Players {
+       private DatabaseMetadata metadata;
+
+       public bool has_loaded { get; protected set; }
+
+       public DatabasePlayers (DatabaseMetadata metadata) {
+               this.metadata = metadata;
+
+               if (metadata.players_loaded)
+                       has_loaded = true;
+               else {
+                       metadata.notify["players-loaded"].connect (on_players_loaded);
+                       metadata.get_players ();
+               }
+       }
+
+       private void on_players_loaded (Object object, ParamSpec param) {
+               has_loaded = metadata.players_loaded;
+       }
+
+       public string get_players () {
+               return metadata.get_players ();
+       }
+}
diff --git a/src/database/database.vala b/src/database/database.vala
index bf4cb54b..96a7fbae 100644
--- a/src/database/database.vala
+++ b/src/database/database.vala
@@ -24,7 +24,8 @@ private class Games.Database : Object {
                        cooperative INTEGER,
                        description TEXT,
                        developer TEXT,
-                       genre TEXT
+                       genre TEXT,
+                       players TEXT
                ) WITHOUT ROWID;
        """;
 
diff --git a/src/meson.build b/src/meson.build
index 1203d2e5..734474d9 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -53,6 +53,7 @@ vala_sources = [
   'database/database-game.vala',
   'database/database-genre.vala',
   'database/database-metadata.vala',
+  'database/database-players.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]