[gnome-games/wip/exalm/cache2: 10/13] database: Add DatabaseMetadata



commit 45ed2a7b554b93a253f6bafb57f8973801b3fea2
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 | 101 ++++++++++++++++++++++++++++++++++++
 src/database/database.vala          |   4 ++
 src/meson.build                     |   1 +
 3 files changed, 106 insertions(+)
---
diff --git a/src/database/database-metadata.vala b/src/database/database-metadata.vala
new file mode 100644
index 00000000..b3ba3b4e
--- /dev/null
+++ b/src/database/database-metadata.vala
@@ -0,0 +1,101 @@
+// 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;
+       """;
+
+       public signal void developer_changed ();
+
+       private Game game;
+       private string uid;
+       private string developer;
+       private bool developer_loaded;
+
+       private Sqlite.Statement add_game_statement;
+       private Sqlite.Statement load_statement;
+       private Sqlite.Statement save_developer_statement;
+
+       public DatabaseMetadata (Sqlite.Database database, Game game) {
+               this.game = game;
+
+               try {
+                       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);
+
+                       uid = game.get_uid ().get_uid ();
+                       try_add_game ();
+
+                       load_metadata ();
+               }
+               catch (Error e) {
+                       warning (e.message);
+               }
+       }
+
+       public string get_uid () {
+               return uid;
+       }
+
+       public string get_developer () {
+               if (!developer_loaded) {
+                       save_developer ();
+                       game.get_developer ().changed.connect (on_developer_changed);
+               }
+
+               return developer;
+       }
+
+       private void on_developer_changed () {
+               save_developer ();
+
+               developer_changed ();
+       }
+
+       private void save_developer () {
+               try {
+                       developer = game.get_developer ().get_developer ();
+
+                       developer_loaded = true;
+
+                       if (developer == "")
+                               return;
+
+                       save_developer_statement.reset ();
+                       Database.bind_text (save_developer_statement, "$UID", uid);
+                       Database.bind_text (save_developer_statement, "$DEVELOPER", developer);
+
+                       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);
+
+               if (add_game_statement.step () != Sqlite.DONE)
+                       warning ("Execution failed.");
+       }
+
+       private void load_metadata () throws Error {
+               Database.bind_text (load_statement, "$UID", uid);
+
+               if (load_statement.step () == Sqlite.ROW) {
+                       developer = load_statement.column_text (0);
+
+                       developer_loaded = developer != null;
+               }
+       }
+}
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 6c556d47..0bca2627 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]