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



commit 4024c5633aea05900f2f8efacf53f1218ca9ff0c
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 | 59 ++++++++++++++++++++++++++++++++++++-
 src/database/database.vala          |  3 +-
 src/meson.build                     |  1 +
 5 files changed, 90 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..4ab21334
--- /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 string[] get_genre () {
+               return metadata.get_genre ();
+       }
+}
diff --git a/src/database/database-metadata.vala b/src/database/database-metadata.vala
index d15ef0e8..4058bdbc 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 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; }
 
        private bool game_added;
 
@@ -55,6 +64,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 ();
@@ -64,6 +74,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);
 
                        load_metadata ();
                }
@@ -102,6 +113,16 @@ private class Games.DatabaseMetadata : Object {
                return developer_value;
        }
 
+       public unowned 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;
@@ -135,6 +156,17 @@ 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;
+
+               add_game ();
+               save_genre ();
+       }
+
        private void save_cooperative () {
                try {
                        save_cooperative_statement.reset ();
@@ -177,6 +209,23 @@ private class Games.DatabaseMetadata : Object {
                }
        }
 
+       private void save_genre () {
+               try {
+                       var variant = new Variant.strv (genre_value);
+                       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 add_game () {
                if (game_added)
                        return;
@@ -216,6 +265,14 @@ 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);
+                               genre_value = variant.get_strv ();
+
+                               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]