[gnome-games/wip/exalm/cache2: 23/26] database: Cache Rating



commit 3d68a4b707166e3626fab5eeb560f09cd342d258
Author: Alexander Mikhaylenko <exalm7659 gmail com>
Date:   Sat Oct 6 13:59:45 2018 +0500

    database: Cache Rating

 src/database/database-game.vala     |  4 ++-
 src/database/database-metadata.vala | 53 ++++++++++++++++++++++++++++++++++++-
 src/database/database-rating.vala   | 26 ++++++++++++++++++
 src/database/database.vala          | 11 +++++++-
 src/meson.build                     |  1 +
 5 files changed, 92 insertions(+), 3 deletions(-)
---
diff --git a/src/database/database-game.vala b/src/database/database-game.vala
index e238c31f..816f70df 100644
--- a/src/database/database-game.vala
+++ b/src/database/database-game.vala
@@ -14,6 +14,7 @@ private class Games.DatabaseGame : Object, Game {
        private Genre genre;
        private Players players;
        private Publisher publisher;
+       private Rating rating;
 
        public DatabaseGame (Database database, Game game) {
                this.database = database;
@@ -27,6 +28,7 @@ private class Games.DatabaseGame : Object, Game {
                genre = new DatabaseGenre (metadata);
                players = new DatabasePlayers (metadata);
                publisher = new DatabasePublisher (metadata);
+               rating = new DatabaseRating (metadata);
        }
 
        public Uid get_uid () {
@@ -70,7 +72,7 @@ private class Games.DatabaseGame : Object, Game {
        }
 
        public Rating get_rating () {
-               return game.get_rating ();
+               return rating;
        }
 
        public Platform get_platform () {
diff --git a/src/database/database-metadata.vala b/src/database/database-metadata.vala
index 43974601..38375bc1 100644
--- a/src/database/database-metadata.vala
+++ b/src/database/database-metadata.vala
@@ -8,7 +8,8 @@ private class Games.DatabaseMetadata : Object {
                        developer,
                        genre,
                        players,
-                       publisher
+                       publisher,
+                       rating
                FROM game_metadata WHERE uid=$UID;
        """;
 
@@ -40,6 +41,10 @@ private class Games.DatabaseMetadata : Object {
                UPDATE game_metadata SET publisher=$PUBLISHER WHERE uid=$UID;
        """;
 
+       private const string SAVE_RATING_QUERY = """
+               UPDATE game_metadata SET rating=$RATING WHERE uid=$UID;
+       """;
+
        private Game game;
        private Uid uid;
        private Cooperative cooperative;
@@ -48,6 +53,7 @@ private class Games.DatabaseMetadata : Object {
        private Genre genre;
        private Players players;
        private Publisher publisher;
+       private Rating rating;
 
        private string uid_value;
        private bool cooperative_value;
@@ -56,6 +62,7 @@ private class Games.DatabaseMetadata : Object {
        private List<string> genre_value;
        private string players_value;
        private string publisher_value;
+       private float rating_value;
 
        private Sqlite.Statement add_game_statement;
        private Sqlite.Statement load_statement;
@@ -65,6 +72,7 @@ private class Games.DatabaseMetadata : Object {
        private Sqlite.Statement save_genre_statement;
        private Sqlite.Statement save_players_statement;
        private Sqlite.Statement save_publisher_statement;
+       private Sqlite.Statement save_rating_statement;
 
        public bool cooperative_loaded { get; set; }
        public bool description_loaded { get; set; }
@@ -72,6 +80,7 @@ private class Games.DatabaseMetadata : Object {
        public bool genre_loaded { get; set; }
        public bool players_loaded { get; set; }
        public bool publisher_loaded { get; set; }
+       public bool rating_loaded { get; set; }
 
        private bool game_added;
 
@@ -85,6 +94,7 @@ private class Games.DatabaseMetadata : Object {
                genre = game.get_genre ();
                players = game.get_players ();
                publisher = game.get_publisher ();
+               rating = game.get_rating ();
 
                try {
                        uid_value = game.get_uid ().get_uid ();
@@ -97,6 +107,7 @@ private class Games.DatabaseMetadata : Object {
                        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);
+                       save_rating_statement = Database.prepare (database, SAVE_RATING_QUERY);
 
                        load_metadata ();
                }
@@ -165,6 +176,16 @@ private class Games.DatabaseMetadata : Object {
                return publisher_value;
        }
 
+       public float get_rating () {
+               if (!rating_loaded) {
+                       on_rating_loaded ();
+                       rating.notify.connect (on_rating_loaded);
+                       return rating.get_rating ();
+               }
+
+               return rating_value;
+       }
+
        private void on_cooperative_loaded () {
                if (!cooperative.has_loaded)
                        return;
@@ -231,6 +252,17 @@ private class Games.DatabaseMetadata : Object {
                save_publisher ();
        }
 
+       private void on_rating_loaded () {
+               if (!rating.has_loaded)
+                       return;
+
+               rating_value = rating.get_rating ();
+               rating_loaded = true;
+
+               add_game ();
+               save_rating ();
+       }
+
        private void save_cooperative () {
                try {
                        save_cooperative_statement.reset ();
@@ -321,6 +353,20 @@ private class Games.DatabaseMetadata : Object {
                }
        }
 
+       private void save_rating () {
+               try {
+                       save_rating_statement.reset ();
+                       Database.bind_text (save_rating_statement, "$UID", uid_value);
+                       Database.bind_double (save_rating_statement, "$RATING", rating_value);
+
+                       if (save_rating_statement.step () != Sqlite.DONE)
+                               warning ("Execution failed.");
+               }
+               catch (Error e) {
+                       warning (e.message);
+               }
+       }
+
        private void add_game () {
                if (game_added)
                        return;
@@ -382,6 +428,11 @@ private class Games.DatabaseMetadata : Object {
                                publisher_value = load_statement.column_text (5);
                                publisher_loaded = true;
                        }
+
+                       if (load_statement.column_type (6) != Sqlite.NULL) {
+                               rating_value = (float) load_statement.column_double (6);
+                               rating_loaded = true;
+                       }
                }
        }
 }
diff --git a/src/database/database-rating.vala b/src/database/database-rating.vala
new file mode 100644
index 00000000..a4e9e9c0
--- /dev/null
+++ b/src/database/database-rating.vala
@@ -0,0 +1,26 @@
+// This file is part of GNOME Games. License: GPL-3.0+.
+
+private class Games.DatabaseRating : Object, Rating {
+       private DatabaseMetadata metadata;
+
+       public bool has_loaded { get; protected set; }
+
+       public DatabaseRating (DatabaseMetadata metadata) {
+               this.metadata = metadata;
+
+               if (metadata.rating_loaded)
+                       has_loaded = true;
+               else {
+                       metadata.notify["rating-loaded"].connect (on_rating_loaded);
+                       metadata.get_rating ();
+               }
+       }
+
+       private void on_rating_loaded (Object object, ParamSpec param) {
+               has_loaded = metadata.rating_loaded;
+       }
+
+       public float get_rating () {
+               return metadata.get_rating ();
+       }
+}
diff --git a/src/database/database.vala b/src/database/database.vala
index 94ddfee5..8256aed1 100644
--- a/src/database/database.vala
+++ b/src/database/database.vala
@@ -26,7 +26,8 @@ private class Games.Database : Object {
                        developer TEXT,
                        genre TEXT,
                        players TEXT,
-                       publisher TEXT
+                       publisher TEXT,
+                       rating REAL
                ) WITHOUT ROWID;
        """;
 
@@ -92,6 +93,14 @@ private class Games.Database : Object {
                return statement;
        }
 
+       internal static void bind_double (Sqlite.Statement statement, string parameter, double val) throws 
Error {
+               var position = statement.bind_parameter_index (parameter);
+               if (position <= 0)
+                       throw new DatabaseError.BINDING_FAILED ("Couldn't bind double to the parameter ā€œ%sā€, 
unexpected position: %d.", parameter, position);
+
+               statement.bind_double (position, val);
+       }
+
        internal static void bind_int (Sqlite.Statement statement, string parameter, int val) throws Error {
                var position = statement.bind_parameter_index (parameter);
                if (position <= 0)
diff --git a/src/meson.build b/src/meson.build
index e3c2c38e..47752e2b 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -55,6 +55,7 @@ vala_sources = [
   'database/database-metadata.vala',
   'database/database-players.vala',
   'database/database-publisher.vala',
+  'database/database-rating.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]