[gnome-games/wip/exalm/cache2: 1/3] database: Add DatabaseMetadata



commit 9e34ff2a398d02fe627a8d87cfabfc5c18af5244
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..20c0f372
--- /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_UID_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_uid_statement;
+       private Sqlite.Statement load_statement;
+       private Sqlite.Statement save_developer_statement;
+
+       public DatabaseMetadata (Sqlite.Database database, Game game) {
+               this.game = game;
+
+               try {
+                       add_uid_statement = Database.prepare (database, ADD_UID_QUERY);
+                       load_statement = Database.prepare (database, LOAD_QUERY);
+                       save_developer_statement = Database.prepare (database, SAVE_DEVELOPER_QUERY);
+
+                       uid = game.get_uid ().get_uid ();
+                       add_uid ();
+
+                       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 add_uid () throws Error {
+               Database.bind_text (add_uid_statement, "$UID", uid);
+
+               if (add_uid_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]