[gnome-games/wip/exalm/cache2: 18/26] database: Cache Cooperative



commit f8d736a859afbc63064398481a3c1e85024acf20
Author: Alexander Mikhaylenko <exalm7659 gmail com>
Date:   Tue Oct 2 12:32:22 2018 +0500

    database: Cache Cooperative

 src/database/database-cooperative.vala | 26 ++++++++++++++++
 src/database/database-game.vala        |  4 ++-
 src/database/database-metadata.vala    | 56 ++++++++++++++++++++++++++++++++--
 src/database/database.vala             |  9 ++++++
 src/meson.build                        |  1 +
 5 files changed, 92 insertions(+), 4 deletions(-)
---
diff --git a/src/database/database-cooperative.vala b/src/database/database-cooperative.vala
new file mode 100644
index 00000000..b513159e
--- /dev/null
+++ b/src/database/database-cooperative.vala
@@ -0,0 +1,26 @@
+// This file is part of GNOME Games. License: GPL-3.0+.
+
+private class Games.DatabaseCooperative : Object, Cooperative {
+       private DatabaseMetadata metadata;
+
+       public bool has_loaded { get; protected set; }
+
+       public DatabaseCooperative (DatabaseMetadata metadata) {
+               this.metadata = metadata;
+
+               if (metadata.cooperative_loaded)
+                       has_loaded = true;
+               else {
+                       metadata.notify["cooperative-loaded"].connect (on_cooperative_loaded);
+                       metadata.get_cooperative ();
+               }
+       }
+
+       private void on_cooperative_loaded (Object object, ParamSpec param) {
+               has_loaded = metadata.cooperative_loaded;
+       }
+
+       public bool get_cooperative () {
+               return metadata.get_cooperative ();
+       }
+}
diff --git a/src/database/database-game.vala b/src/database/database-game.vala
index aa86ff62..a98893f1 100644
--- a/src/database/database-game.vala
+++ b/src/database/database-game.vala
@@ -8,6 +8,7 @@ private class Games.DatabaseGame : Object, Game {
        private Database database;
        private Game game;
 
+       private Cooperative cooperative;
        private Developer developer;
 
        public DatabaseGame (Database database, Game game) {
@@ -16,6 +17,7 @@ private class Games.DatabaseGame : Object, Game {
 
                var metadata = database.get_metadata (game);
 
+               cooperative = new DatabaseCooperative (metadata);
                developer = new DatabaseDeveloper (metadata);
        }
 
@@ -36,7 +38,7 @@ private class Games.DatabaseGame : Object, Game {
        }
 
        public Cooperative get_cooperative () {
-               return game.get_cooperative ();
+               return cooperative;
        }
 
        public Genre get_genre () {
diff --git a/src/database/database-metadata.vala b/src/database/database-metadata.vala
index e7af16c2..5d62a2e2 100644
--- a/src/database/database-metadata.vala
+++ b/src/database/database-metadata.vala
@@ -2,28 +2,36 @@
 
 private class Games.DatabaseMetadata : Object {
        private const string LOAD_QUERY = """
-               SELECT developer FROM game_metadata WHERE uid=$UID;
+               SELECT cooperative, 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_COOPERATIVE_QUERY = """
+               UPDATE game_metadata SET cooperative=$COOPERATIVE WHERE uid=$UID;
+       """;
+
        private const string SAVE_DEVELOPER_QUERY = """
                UPDATE game_metadata SET developer=$DEVELOPER WHERE uid=$UID;
        """;
 
        private Game game;
        private Uid uid;
+       private Cooperative cooperative;
        private Developer developer;
 
        private string uid_value;
+       private bool cooperative_value;
        private string developer_value;
 
        private Sqlite.Statement add_game_statement;
        private Sqlite.Statement load_statement;
+       private Sqlite.Statement save_cooperative_statement;
        private Sqlite.Statement save_developer_statement;
 
+       public bool cooperative_loaded { get; set; }
        public bool developer_loaded { get; set; }
 
        private bool game_added;
@@ -32,6 +40,7 @@ private class Games.DatabaseMetadata : Object {
                this.game = game;
 
                uid = game.get_uid ();
+               cooperative = game.get_cooperative ();
                developer = game.get_developer ();
 
                try {
@@ -39,6 +48,7 @@ private class Games.DatabaseMetadata : Object {
 
                        add_game_statement = Database.prepare (database, ADD_GAME_QUERY);
                        load_statement = Database.prepare (database, LOAD_QUERY);
+                       save_cooperative_statement = Database.prepare (database, SAVE_COOPERATIVE_QUERY);
                        save_developer_statement = Database.prepare (database, SAVE_DEVELOPER_QUERY);
 
                        load_metadata ();
@@ -48,6 +58,16 @@ private class Games.DatabaseMetadata : Object {
                }
        }
 
+       public bool get_cooperative () {
+               if (!cooperative_loaded) {
+                       on_cooperative_loaded ();
+                       cooperative.notify.connect (on_cooperative_loaded);
+                       return cooperative.get_cooperative ();
+               }
+
+               return cooperative_value;
+       }
+
        public string get_developer () {
                if (!developer_loaded) {
                        on_developer_loaded ();
@@ -58,6 +78,17 @@ private class Games.DatabaseMetadata : Object {
                return developer_value;
        }
 
+       private void on_cooperative_loaded () {
+               if (!cooperative.has_loaded)
+                       return;
+
+               cooperative_value = cooperative.get_cooperative ();
+               cooperative_loaded = true;
+
+               add_game ();
+               save_cooperative ();
+       }
+
        private void on_developer_loaded () {
                if (!developer.has_loaded)
                        return;
@@ -69,6 +100,20 @@ private class Games.DatabaseMetadata : Object {
                save_developer ();
        }
 
+       private void save_cooperative () {
+               try {
+                       save_cooperative_statement.reset ();
+                       Database.bind_text (save_cooperative_statement, "$UID", uid_value);
+                       Database.bind_int (save_cooperative_statement, "$COOPERATIVE", cooperative_value ? 1 
: 0);
+
+                       if (save_cooperative_statement.step () != Sqlite.DONE)
+                               warning ("Execution failed.");
+               }
+               catch (Error e) {
+                       warning (e.message);
+               }
+       }
+
        private void save_developer () {
                try {
                        save_developer_statement.reset ();
@@ -108,10 +153,15 @@ private class Games.DatabaseMetadata : Object {
                Database.bind_text (load_statement, "$UID", uid_value);
 
                if (load_statement.step () == Sqlite.ROW) {
-                       developer_value = load_statement.column_text (0);
+                       if (load_statement.column_type (0) != Sqlite.NULL) {
+                               cooperative_value = load_statement.column_int (0) == 1;
+                               cooperative_loaded = true;
+                       }
 
-                       if (developer_value != null)
+                       if (load_statement.column_type (1) != Sqlite.NULL) {
+                               developer_value = load_statement.column_text (1);
                                developer_loaded = true;
+                       }
                }
        }
 }
diff --git a/src/database/database.vala b/src/database/database.vala
index 352b0caa..57c3c8a2 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,
+                       cooperative INTEGER,
                        developer TEXT
                ) WITHOUT ROWID;
        """;
@@ -87,6 +88,14 @@ private class Games.Database : Object {
                return statement;
        }
 
+       internal static void bind_int (Sqlite.Statement statement, string parameter, int val) throws Error {
+               var position = statement.bind_parameter_index (parameter);
+               if (position <= 0)
+                       throw new DatabaseError.BINDING_FAILED ("Couldn't bind int to the parameter ā€œ%sā€, 
unexpected position: %d.", parameter, position);
+
+               statement.bind_int (position, val);
+       }
+
        internal static void bind_text (Sqlite.Statement statement, string parameter, string text) throws 
Error {
                var position = statement.bind_parameter_index (parameter);
                if (position <= 0)
diff --git a/src/meson.build b/src/meson.build
index e77705d9..a2c72691 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -46,6 +46,7 @@ vala_sources = [
   'core/uri-source.vala',
 
   'database/database.vala',
+  'database/database-cooperative.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]