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



commit c303e3d08a24245647f56259a74ff94110d39c5e
Author: Alexander Mikhaylenko <exalm7659 gmail com>
Date:   Sat Oct 6 13:25:52 2018 +0500

    database: Cache Genre
    
    Handle serialization and deserialization of genre lists via Variant.

 src/database/database-game.vala     |  4 ++-
 src/database/database-genre.vala    | 26 +++++++++++++++
 src/database/database-metadata.vala | 65 ++++++++++++++++++++++++++++++++++++-
 src/database/database.vala          |  3 +-
 src/meson.build                     |  1 +
 5 files changed, 96 insertions(+), 3 deletions(-)
---
diff --git a/src/database/database-game.vala b/src/database/database-game.vala
index fffdb776..1e612e92 100644
--- a/src/database/database-game.vala
+++ b/src/database/database-game.vala
@@ -11,6 +11,7 @@ private class Games.DatabaseGame : Object, Game {
        private Cooperative cooperative;
        private Description description;
        private Developer developer;
+       private Genre genre;
 
        public DatabaseGame (Database database, Game game) {
                this.database = database;
@@ -21,6 +22,7 @@ private class Games.DatabaseGame : Object, Game {
                cooperative = new DatabaseCooperative (metadata);
                description = new DatabaseDescription (metadata);
                developer = new DatabaseDeveloper (metadata);
+               genre = new DatabaseGenre (metadata);
        }
 
        public Uid get_uid () {
@@ -44,7 +46,7 @@ private class Games.DatabaseGame : Object, Game {
        }
 
        public Genre get_genre () {
-               return game.get_genre ();
+               return genre;
        }
 
        public Players get_players () {
diff --git a/src/database/database-genre.vala b/src/database/database-genre.vala
new file mode 100644
index 00000000..88864103
--- /dev/null
+++ b/src/database/database-genre.vala
@@ -0,0 +1,26 @@
+// This file is part of GNOME Games. License: GPL-3.0+.
+
+private class Games.DatabaseGenre : Object, Genre {
+       private DatabaseMetadata metadata;
+
+       public bool has_loaded { get; protected set; }
+
+       public DatabaseGenre (DatabaseMetadata metadata) {
+               this.metadata = metadata;
+
+               if (metadata.genre_loaded)
+                       has_loaded = true;
+               else {
+                       metadata.notify["genre-loaded"].connect (on_genre_loaded);
+                       metadata.get_genre ();
+               }
+       }
+
+       private void on_genre_loaded (Object object, ParamSpec param) {
+               has_loaded = metadata.genre_loaded;
+       }
+
+       public unowned List<string> get_genre () {
+               return metadata.get_genre ();
+       }
+}
diff --git a/src/database/database-metadata.vala b/src/database/database-metadata.vala
index aefc1f83..c63ed785 100644
--- a/src/database/database-metadata.vala
+++ b/src/database/database-metadata.vala
@@ -5,7 +5,8 @@ private class Games.DatabaseMetadata : Object {
                SELECT
                        cooperative,
                        description,
-                       developer
+                       developer,
+                       genre
                FROM game_metadata WHERE uid=$UID;
        """;
 
@@ -25,26 +26,34 @@ private class Games.DatabaseMetadata : Object {
                UPDATE game_metadata SET developer=$DEVELOPER WHERE uid=$UID;
        """;
 
+       private const string SAVE_GENRE_QUERY = """
+               UPDATE game_metadata SET genre=$GENRE WHERE uid=$UID;
+       """;
+
        private Game game;
        private Uid uid;
        private Cooperative cooperative;
        private Description description;
        private Developer developer;
+       private Genre genre;
 
        private string uid_value;
        private bool cooperative_value;
        private string description_value;
        private string developer_value;
+       private List<string> genre_value;
 
        private Sqlite.Statement add_game_statement;
        private Sqlite.Statement load_statement;
        private Sqlite.Statement save_cooperative_statement;
        private Sqlite.Statement save_description_statement;
        private Sqlite.Statement save_developer_statement;
+       private Sqlite.Statement save_genre_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 DatabaseMetadata (Sqlite.Database database, Game game) {
                this.game = game;
@@ -53,6 +62,7 @@ private class Games.DatabaseMetadata : Object {
                cooperative = game.get_cooperative ();
                description = game.get_description ();
                developer = game.get_developer ();
+               genre = game.get_genre ();
 
                try {
                        uid_value = game.get_uid ().get_uid ();
@@ -62,6 +72,7 @@ private class Games.DatabaseMetadata : Object {
                        save_cooperative_statement = Database.prepare (database, SAVE_COOPERATIVE_QUERY);
                        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);
 
                        try_add_game ();
 
@@ -102,6 +113,16 @@ private class Games.DatabaseMetadata : Object {
                return developer_value;
        }
 
+       public unowned List<string> get_genre () {
+               if (!genre_loaded) {
+                       on_genre_loaded ();
+                       genre.notify.connect (on_genre_loaded);
+                       return genre.get_genre ();
+               }
+
+               return genre_value;
+       }
+
        private void on_cooperative_loaded () {
                if (!cooperative.has_loaded)
                        return;
@@ -132,6 +153,16 @@ private class Games.DatabaseMetadata : Object {
                save_developer ();
        }
 
+       private void on_genre_loaded () {
+               if (!genre.has_loaded)
+                       return;
+
+               genre_value = genre.get_genre ().copy ();
+               genre_loaded = true;
+
+               save_genre ();
+       }
+
        private void save_cooperative () {
                try {
                        save_cooperative_statement.reset ();
@@ -174,6 +205,26 @@ private class Games.DatabaseMetadata : Object {
                }
        }
 
+       private void save_genre () {
+               try {
+                       string[] genre_array = {};
+                       foreach (var genre in genre_value)
+                               genre_array += genre;
+                       var variant = new Variant.strv (genre_array);
+                       var string_value = variant.print (false);
+
+                       save_genre_statement.reset ();
+                       Database.bind_text (save_genre_statement, "$UID", uid_value);
+                       Database.bind_text (save_genre_statement, "$GENRE", string_value);
+
+                       if (save_genre_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);
 
@@ -199,6 +250,18 @@ private class Games.DatabaseMetadata : Object {
                                developer_value = load_statement.column_text (2);
                                developer_loaded = true;
                        }
+
+                       if (load_statement.column_type (3) != Sqlite.NULL) {
+                               var string_value = load_statement.column_text (3);
+                               var variant = Variant.parse (VariantType.STRING_ARRAY, string_value);
+                               var genre_array = variant.get_strv ();
+
+                               genre_value = new List<string> ();
+                               foreach (var genre in genre_array)
+                                       genre_value.append (genre);
+
+                               genre_loaded = true;
+                       }
                }
        }
 }
diff --git a/src/database/database.vala b/src/database/database.vala
index 431f0096..bf4cb54b 100644
--- a/src/database/database.vala
+++ b/src/database/database.vala
@@ -23,7 +23,8 @@ private class Games.Database : Object {
                        uid TEXT PRIMARY KEY NOT NULL,
                        cooperative INTEGER,
                        description TEXT,
-                       developer TEXT
+                       developer TEXT,
+                       genre TEXT
                ) WITHOUT ROWID;
        """;
 
diff --git a/src/meson.build b/src/meson.build
index cf195219..1203d2e5 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -51,6 +51,7 @@ vala_sources = [
   'database/database-developer.vala',
   'database/database-error.vala',
   'database/database-game.vala',
+  'database/database-genre.vala',
   'database/database-metadata.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]