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



commit 6e23ca5f220e1653de79c3dc868ffdafa1f5cd33
Author: Alexander Mikhaylenko <exalm7659 gmail com>
Date:   Mon Sep 3 23:48:37 2018 +0500

    database: Cache description

 src/database/database-description.vala | 14 +++++++++
 src/database/database-game.vala        |  4 ++-
 src/database/database-metadata.vala    | 52 ++++++++++++++++++++++++++++++++--
 src/database/database.vala             |  1 +
 src/meson.build                        |  1 +
 5 files changed, 69 insertions(+), 3 deletions(-)
---
diff --git a/src/database/database-description.vala b/src/database/database-description.vala
new file mode 100644
index 00000000..72e5258d
--- /dev/null
+++ b/src/database/database-description.vala
@@ -0,0 +1,14 @@
+// This file is part of GNOME Games. License: GPL-3.0+.
+
+private class Games.DatabaseDescription : Object, Description {
+       private DatabaseMetadata metadata;
+
+       public DatabaseDescription (DatabaseMetadata metadata) {
+               this.metadata = metadata;
+               metadata.description_changed.connect (() => changed ());
+       }
+
+       public string get_description () {
+               return metadata.get_description ();
+       }
+}
diff --git a/src/database/database-game.vala b/src/database/database-game.vala
index 20c7ce51..e64355c0 100644
--- a/src/database/database-game.vala
+++ b/src/database/database-game.vala
@@ -9,6 +9,7 @@ private class Games.DatabaseGame : Object, Game {
        private Game game;
        private Uid uid;
        private Developer developer;
+       private Description description;
 
        public DatabaseGame (Database database, Game game) {
                this.database = database;
@@ -18,6 +19,7 @@ private class Games.DatabaseGame : Object, Game {
 
                uid = new DatabaseUid (metadata);
                developer = new DatabaseDeveloper (metadata);
+               description = new DatabaseDescription (metadata);
        }
 
        public Uid get_uid () {
@@ -57,7 +59,7 @@ private class Games.DatabaseGame : Object, Game {
        }
 
        public Description get_description () {
-               return game.get_description ();
+               return description;
        }
 
        public Rating get_rating () {
diff --git a/src/database/database-metadata.vala b/src/database/database-metadata.vala
index b3ba3b4e..08ccd38e 100644
--- a/src/database/database-metadata.vala
+++ b/src/database/database-metadata.vala
@@ -2,26 +2,35 @@
 
 private class Games.DatabaseMetadata : Object {
        private const string LOAD_QUERY = """
-               SELECT developer FROM game_metadata WHERE uid=$UID;
+               SELECT description, developer FROM game_metadata WHERE uid=$UID;
        """;
 
        private const string ADD_GAME_QUERY = """
                INSERT OR IGNORE INTO game_metadata (uid) VALUES ($UID);
        """;
 
+       private const string SAVE_DESCRIPTION_QUERY = """
+               UPDATE game_metadata SET description=$DESCRIPTION WHERE uid=$UID;
+       """;
+
        private const string SAVE_DEVELOPER_QUERY = """
                UPDATE game_metadata SET developer=$DEVELOPER WHERE uid=$UID;
        """;
 
+       public signal void description_changed ();
        public signal void developer_changed ();
 
        private Game game;
        private string uid;
+       private string description;
        private string developer;
+
+       private bool description_loaded;
        private bool developer_loaded;
 
        private Sqlite.Statement add_game_statement;
        private Sqlite.Statement load_statement;
+       private Sqlite.Statement save_description_statement;
        private Sqlite.Statement save_developer_statement;
 
        public DatabaseMetadata (Sqlite.Database database, Game game) {
@@ -30,6 +39,7 @@ private class Games.DatabaseMetadata : Object {
                try {
                        add_game_statement = Database.prepare (database, ADD_GAME_QUERY);
                        load_statement = Database.prepare (database, LOAD_QUERY);
+                       save_description_statement = Database.prepare (database, SAVE_DESCRIPTION_QUERY);
                        save_developer_statement = Database.prepare (database, SAVE_DEVELOPER_QUERY);
 
                        uid = game.get_uid ().get_uid ();
@@ -46,6 +56,15 @@ private class Games.DatabaseMetadata : Object {
                return uid;
        }
 
+       public string get_description () {
+               if (!description_loaded) {
+                       save_description ();
+                       game.get_description ().changed.connect (on_description_changed);
+               }
+
+               return description;
+       }
+
        public string get_developer () {
                if (!developer_loaded) {
                        save_developer ();
@@ -55,12 +74,39 @@ private class Games.DatabaseMetadata : Object {
                return developer;
        }
 
+       private void on_description_changed () {
+               save_description ();
+
+               description_changed ();
+       }
+
        private void on_developer_changed () {
                save_developer ();
 
                developer_changed ();
        }
 
+       private void save_description () {
+               try {
+                       description = game.get_description ().get_description ();
+
+                       description_loaded = true;
+
+                       if (description == "")
+                               return;
+
+                       save_description_statement.reset ();
+                       Database.bind_text (save_description_statement, "$UID", uid);
+                       Database.bind_text (save_description_statement, "$DESCRIPTION", description);
+
+                       if (save_description_statement.step () != Sqlite.DONE)
+                               warning ("Execution failed.");
+               }
+               catch (Error e) {
+                       warning (e.message);
+               }
+       }
+
        private void save_developer () {
                try {
                        developer = game.get_developer ().get_developer ();
@@ -93,8 +139,10 @@ private class Games.DatabaseMetadata : Object {
                Database.bind_text (load_statement, "$UID", uid);
 
                if (load_statement.step () == Sqlite.ROW) {
-                       developer = load_statement.column_text (0);
+                       description = load_statement.column_text (0);
+                       description_loaded = description != null;
 
+                       developer = load_statement.column_text (1);
                        developer_loaded = developer != null;
                }
        }
diff --git a/src/database/database.vala b/src/database/database.vala
index 352b0caa..8f300d3a 100644
--- a/src/database/database.vala
+++ b/src/database/database.vala
@@ -21,6 +21,7 @@ private class Games.Database : Object {
        private const string CREATE_METADATA_TABLE_QUERY = """
                CREATE TABLE IF NOT EXISTS game_metadata (
                        uid TEXT PRIMARY KEY NOT NULL,
+                       description TEXT,
                        developer TEXT
                ) WITHOUT ROWID;
        """;
diff --git a/src/meson.build b/src/meson.build
index 0bca2627..33ab1670 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -46,6 +46,7 @@ vala_sources = [
   'core/uri-source.vala',
 
   'database/database.vala',
+  'database/database-description.vala',
   'database/database-developer.vala',
   'database/database-error.vala',
   'database/database-game.vala',


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]