[gnome-games/wip/exalm/cache2: 19/20] database: Cache ReleaseDate



commit 93f2bd21f795a5b5c4a884af280c927297910901
Author: Alexander Mikhaylenko <exalm7659 gmail com>
Date:   Sat Oct 6 14:21:07 2018 +0500

    database: Cache ReleaseDate
    
    Represent DateTime as ISO 8601 strings.

 src/database/database-game.vala         |  4 ++-
 src/database/database-metadata.vala     | 64 ++++++++++++++++++++++++++++++++-
 src/database/database-release-date.vala | 26 ++++++++++++++
 src/database/database.vala              |  3 +-
 src/meson.build                         |  1 +
 5 files changed, 95 insertions(+), 3 deletions(-)
---
diff --git a/src/database/database-game.vala b/src/database/database-game.vala
index 816f70df..af2526e2 100644
--- a/src/database/database-game.vala
+++ b/src/database/database-game.vala
@@ -15,6 +15,7 @@ private class Games.DatabaseGame : Object, Game {
        private Players players;
        private Publisher publisher;
        private Rating rating;
+       private ReleaseDate release_date;
 
        public DatabaseGame (Database database, Game game) {
                this.database = database;
@@ -29,6 +30,7 @@ private class Games.DatabaseGame : Object, Game {
                players = new DatabasePlayers (metadata);
                publisher = new DatabasePublisher (metadata);
                rating = new DatabaseRating (metadata);
+               release_date = new DatabaseReleaseDate (metadata);
        }
 
        public Uid get_uid () {
@@ -44,7 +46,7 @@ private class Games.DatabaseGame : Object, Game {
        }
 
        public ReleaseDate get_release_date () {
-               return game.get_release_date ();
+               return release_date;
        }
 
        public Cooperative get_cooperative () {
diff --git a/src/database/database-metadata.vala b/src/database/database-metadata.vala
index 38375bc1..2ac01bcc 100644
--- a/src/database/database-metadata.vala
+++ b/src/database/database-metadata.vala
@@ -9,7 +9,8 @@ private class Games.DatabaseMetadata : Object {
                        genre,
                        players,
                        publisher,
-                       rating
+                       rating,
+                       release_date
                FROM game_metadata WHERE uid=$UID;
        """;
 
@@ -45,6 +46,10 @@ private class Games.DatabaseMetadata : Object {
                UPDATE game_metadata SET rating=$RATING WHERE uid=$UID;
        """;
 
+       private const string SAVE_RELEASE_DATE_QUERY = """
+               UPDATE game_metadata SET release_date=$RELEASE_DATE WHERE uid=$UID;
+       """;
+
        private Game game;
        private Uid uid;
        private Cooperative cooperative;
@@ -54,6 +59,7 @@ private class Games.DatabaseMetadata : Object {
        private Players players;
        private Publisher publisher;
        private Rating rating;
+       private ReleaseDate release_date;
 
        private string uid_value;
        private bool cooperative_value;
@@ -63,6 +69,7 @@ private class Games.DatabaseMetadata : Object {
        private string players_value;
        private string publisher_value;
        private float rating_value;
+       private DateTime release_date_value;
 
        private Sqlite.Statement add_game_statement;
        private Sqlite.Statement load_statement;
@@ -73,6 +80,7 @@ private class Games.DatabaseMetadata : Object {
        private Sqlite.Statement save_players_statement;
        private Sqlite.Statement save_publisher_statement;
        private Sqlite.Statement save_rating_statement;
+       private Sqlite.Statement save_release_date_statement;
 
        public bool cooperative_loaded { get; set; }
        public bool description_loaded { get; set; }
@@ -81,6 +89,7 @@ private class Games.DatabaseMetadata : Object {
        public bool players_loaded { get; set; }
        public bool publisher_loaded { get; set; }
        public bool rating_loaded { get; set; }
+       public bool release_date_loaded { get; set; }
 
        private bool game_added;
 
@@ -95,6 +104,7 @@ private class Games.DatabaseMetadata : Object {
                players = game.get_players ();
                publisher = game.get_publisher ();
                rating = game.get_rating ();
+               release_date = game.get_release_date ();
 
                try {
                        uid_value = game.get_uid ().get_uid ();
@@ -108,6 +118,7 @@ private class Games.DatabaseMetadata : Object {
                        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);
+                       save_release_date_statement = Database.prepare (database, SAVE_RELEASE_DATE_QUERY);
 
                        load_metadata ();
                }
@@ -186,6 +197,16 @@ private class Games.DatabaseMetadata : Object {
                return rating_value;
        }
 
+       public DateTime get_release_date () {
+               if (!release_date_loaded) {
+                       on_release_date_loaded ();
+                       release_date.notify.connect (on_release_date_loaded);
+                       return release_date.get_release_date ();
+               }
+
+               return release_date_value;
+       }
+
        private void on_cooperative_loaded () {
                if (!cooperative.has_loaded)
                        return;
@@ -263,6 +284,17 @@ private class Games.DatabaseMetadata : Object {
                save_rating ();
        }
 
+       private void on_release_date_loaded () {
+               if (!release_date.has_loaded)
+                       return;
+
+               release_date_value = release_date.get_release_date ();
+               release_date_loaded = true;
+
+               add_game ();
+               save_release_date ();
+       }
+
        private void save_cooperative () {
                try {
                        save_cooperative_statement.reset ();
@@ -367,6 +399,24 @@ private class Games.DatabaseMetadata : Object {
                }
        }
 
+       private void save_release_date () {
+               try {
+                       var string_value = "";
+                       if (release_date_value != null)
+                               string_value = release_date_value.format ("%F");
+
+                       save_release_date_statement.reset ();
+                       Database.bind_text (save_release_date_statement, "$UID", uid_value);
+                       Database.bind_text (save_release_date_statement, "$RELEASE_DATE", string_value);
+
+                       if (save_release_date_statement.step () != Sqlite.DONE)
+                               warning ("Execution failed.");
+               }
+               catch (Error e) {
+                       warning (e.message);
+               }
+       }
+
        private void add_game () {
                if (game_added)
                        return;
@@ -433,6 +483,18 @@ private class Games.DatabaseMetadata : Object {
                                rating_value = (float) load_statement.column_double (6);
                                rating_loaded = true;
                        }
+
+                       if (load_statement.column_type (7) != Sqlite.NULL) {
+                               var string_value = load_statement.column_text (7);
+
+                               if (string_value != "") {
+                                       var timezone = new TimeZone.utc ();
+
+                                       release_date_value = new DateTime.from_iso8601 (string_value, 
timezone);
+                               }
+
+                               release_date_loaded = true;
+                       }
                }
        }
 }
diff --git a/src/database/database-release-date.vala b/src/database/database-release-date.vala
new file mode 100644
index 00000000..97ce9ed6
--- /dev/null
+++ b/src/database/database-release-date.vala
@@ -0,0 +1,26 @@
+// This file is part of GNOME Games. License: GPL-3.0+.
+
+private class Games.DatabaseReleaseDate : Object, ReleaseDate {
+       private DatabaseMetadata metadata;
+
+       public bool has_loaded { get; protected set; }
+
+       public DatabaseReleaseDate (DatabaseMetadata metadata) {
+               this.metadata = metadata;
+
+               if (metadata.release_date_loaded)
+                       has_loaded = true;
+               else {
+                       metadata.notify["release-date-loaded"].connect (on_release_date_loaded);
+                       metadata.get_release_date ();
+               }
+       }
+
+       private void on_release_date_loaded (Object object, ParamSpec param) {
+               has_loaded = metadata.release_date_loaded;
+       }
+
+       public DateTime get_release_date () {
+               return metadata.get_release_date ();
+       }
+}
diff --git a/src/database/database.vala b/src/database/database.vala
index 8256aed1..5ce1545a 100644
--- a/src/database/database.vala
+++ b/src/database/database.vala
@@ -27,7 +27,8 @@ private class Games.Database : Object {
                        genre TEXT,
                        players TEXT,
                        publisher TEXT,
-                       rating REAL
+                       rating REAL,
+                       release_date TEXT
                ) WITHOUT ROWID;
        """;
 
diff --git a/src/meson.build b/src/meson.build
index 47752e2b..2bb73e11 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -56,6 +56,7 @@ vala_sources = [
   'database/database-players.vala',
   'database/database-publisher.vala',
   'database/database-rating.vala',
+  'database/database-release-date.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]