[gnome-games/wip/exalm/cache2: 50/52] database: Add DatabaseMetadata



commit 34bfaf35b22b714f92fda9b7fbe3668d2d7a5fd2
Author: Alexander Mikhaylenko <exalm7659 gmail com>
Date:   Mon Sep 3 22:40:17 2018 +0500

    database: Add DatabaseMetadata
    
    This will be used in the following commits to represent full metadata of a
    game, and a full row from 'game_metadata' database table.
    
    This allows loading all metadata of a given game via a single SQL query.

 src/database/database-metadata.vala | 102 ++++++++++++++++++++++++++++++++++++
 src/database/database.vala          |   4 ++
 src/meson.build                     |   1 +
 3 files changed, 107 insertions(+)
---
diff --git a/src/database/database-metadata.vala b/src/database/database-metadata.vala
new file mode 100644
index 00000000..520734fa
--- /dev/null
+++ b/src/database/database-metadata.vala
@@ -0,0 +1,102 @@
+// This file is part of GNOME Games. License: GPL-3.0+.
+
+private class Games.DatabaseMetadata : Object {
+       private const string LOAD_QUERY = """
+               SELECT 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_DEVELOPER_QUERY = """
+               UPDATE game_metadata SET developer=$DEVELOPER WHERE uid=$UID;
+       """;
+
+       private Game game;
+       private Uid uid;
+       private Developer developer;
+
+       private string uid_value;
+       private string developer_value;
+
+       private Sqlite.Statement add_game_statement;
+       private Sqlite.Statement load_statement;
+       private Sqlite.Statement save_developer_statement;
+
+       public bool developer_loaded { get; set; }
+
+       public DatabaseMetadata (Sqlite.Database database, Game game) {
+               this.game = game;
+
+               uid = game.get_uid ();
+               developer = game.get_developer ();
+
+               try {
+                       uid_value = game.get_uid ().get_uid ();
+
+                       add_game_statement = Database.prepare (database, ADD_GAME_QUERY);
+                       load_statement = Database.prepare (database, LOAD_QUERY);
+                       save_developer_statement = Database.prepare (database, SAVE_DEVELOPER_QUERY);
+
+                       try_add_game ();
+
+                       load_metadata ();
+               }
+               catch (Error e) {
+                       warning (e.message);
+               }
+       }
+
+       public string get_developer () {
+               if (!developer_loaded) {
+                       on_developer_loaded ();
+                       developer.notify.connect (on_developer_loaded);
+                       return developer.get_developer ();
+               }
+
+               return developer_value;
+       }
+
+       private void on_developer_loaded () {
+               if (!developer.has_loaded)
+                       return;
+
+               developer_value = developer.get_developer ();
+               developer_loaded = true;
+
+               save_developer ();
+       }
+
+       private void save_developer () {
+               try {
+                       save_developer_statement.reset ();
+                       Database.bind_text (save_developer_statement, "$UID", uid_value);
+                       Database.bind_text (save_developer_statement, "$DEVELOPER", developer_value);
+
+                       if (save_developer_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);
+
+               if (add_game_statement.step () != Sqlite.DONE)
+                       warning ("Execution failed.");
+       }
+
+       private void load_metadata () throws Error {
+               Database.bind_text (load_statement, "$UID", uid_value);
+
+               if (load_statement.step () == Sqlite.ROW) {
+                       developer_value = load_statement.column_text (0);
+
+                       if (developer_value != null)
+                               developer_loaded = true;
+               }
+       }
+}
diff --git a/src/database/database.vala b/src/database/database.vala
index b513df88..da7904ed 100644
--- a/src/database/database.vala
+++ b/src/database/database.vala
@@ -71,6 +71,10 @@ private class Games.Database : Object {
                return new DatabaseDeveloper (database, developer, uid);
        }
 
+       public DatabaseMetadata get_metadata (Game game) {
+               return new DatabaseMetadata (database, game);
+       }
+
        private void create_tables () throws Error {
                exec (CREATE_RESOURCES_TABLE_QUERY, null);
                exec (CREATE_METADATA_TABLE_QUERY, null);
diff --git a/src/meson.build b/src/meson.build
index 9a886051..fab72bb9 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -49,6 +49,7 @@ vala_sources = [
   'database/database-developer.vala',
   'database/database-error.vala',
   'database/database-game.vala',
+  'database/database-metadata.vala',
   'database/database-uid.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]