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



commit 583a5ce61b0760377b880de76d1705462646f6e3
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 | 52 ++++++++++++++++++++++++++++++++++++-
 src/database/database-players.vala  | 26 +++++++++++++++++++
 src/database/database.vala          |  3 ++-
 src/meson.build                     |  1 +
 5 files changed, 83 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 c63ed785..120614ea 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; }
 
        public DatabaseMetadata (Sqlite.Database database, Game game) {
                this.game = game;
@@ -63,6 +72,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 ();
@@ -73,6 +83,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);
 
                        try_add_game ();
 
@@ -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;
@@ -163,6 +184,16 @@ 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;
+
+               save_players ();
+       }
+
        private void save_cooperative () {
                try {
                        save_cooperative_statement.reset ();
@@ -225,6 +256,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 try_add_game () throws Error {
                Database.bind_text (add_game_statement, "$UID", uid_value);
 
@@ -262,6 +307,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]