[gnome-games/wip/exalm/cache2: 5/7] database: Switch DatabaseUid to DatabaseMetadata



commit be328ce5542f5b6ec05cd29e6a87a5db198dc7ab
Author: Alexander Mikhaylenko <exalm7659 gmail com>
Date:   Mon Sep 3 22:46:32 2018 +0500

    database: Switch DatabaseUid to DatabaseMetadata
    
    Uids are now managed via DatabaseMetadata, so make DatabaseUid a simple
    adapter for it.

 src/database/database-game.vala |  7 ++++-
 src/database/database-uid.vala  | 63 ++++-------------------------------------
 src/database/database.vala      |  4 ---
 3 files changed, 11 insertions(+), 63 deletions(-)
---
diff --git a/src/database/database-game.vala b/src/database/database-game.vala
index fe322f5b..7b82d7ec 100644
--- a/src/database/database-game.vala
+++ b/src/database/database-game.vala
@@ -7,14 +7,19 @@ private class Games.DatabaseGame : Object, Game {
 
        private Database database;
        private Game game;
+       private Uid uid;
 
        public DatabaseGame (Database database, Game game) {
                this.database = database;
                this.game = game;
+
+               var metadata = database.get_metadata (game);
+
+               uid = new DatabaseUid (metadata);
        }
 
        public Uid get_uid () {
-               return database.get_uid (game.get_uid ());
+               return uid;
        }
 
        public Icon get_icon () {
diff --git a/src/database/database-uid.vala b/src/database/database-uid.vala
index b5700e18..14c33853 100644
--- a/src/database/database-uid.vala
+++ b/src/database/database-uid.vala
@@ -1,66 +1,13 @@
 // This file is part of GNOME Games. License: GPL-3.0+.
 
 private class Games.DatabaseUid : Object, Uid {
-       private const string HAS_UID_QUERY = """
-               SELECT EXISTS (SELECT 1 FROM game_metadata WHERE uid=$UID LIMIT 1);
-       """;
+       private DatabaseMetadata metadata;
 
-       private const string ADD_UID_QUERY = """
-               INSERT INTO game_metadata (uid) VALUES ($UID);
-       """;
-
-       private Uid uid;
-       private Sqlite.Statement has_statement;
-       private Sqlite.Statement add_statement;
-       private bool has;
-
-       internal DatabaseUid (Sqlite.Database database, Uid uid) {
-               this.uid = uid;
-
-               try {
-                       has_statement = Database.prepare (database, HAS_UID_QUERY);
-                       add_statement = Database.prepare (database, ADD_UID_QUERY);
-               }
-               catch (Error e) {
-                       warning (e.message);
-               }
+       public DatabaseUid (DatabaseMetadata metadata) {
+               this.metadata = metadata;
        }
 
-       public string get_uid () throws Error {
-               try {
-                       if (!has)
-                               has_uid ();
-                       if (!has)
-                               add_uid ();
-               }
-               catch (Error e) {
-                       warning (e.message);
-               }
-
-               return uid.get_uid ();
-       }
-
-       private void has_uid () throws Error {
-               if (has_statement == null)
-                       return;
-
-               Database.bind_text (has_statement, "$UID", uid.get_uid ());
-
-               if (has_statement.step () == Sqlite.ROW)
-                       has = has_statement.column_text (0) == "1";
-               else
-                       warning ("Execution failed.");
-       }
-
-       private void add_uid () throws Error {
-               if (add_statement == null)
-                       return;
-
-               has = true;
-
-               Database.bind_text (add_statement, "$UID", uid.get_uid ());
-
-               if (add_statement.step () != Sqlite.DONE)
-                       warning ("Execution failed.");
+       public string get_uid () {
+               return metadata.get_uid ();
        }
 }
diff --git a/src/database/database.vala b/src/database/database.vala
index da7904ed..e5ddc5ec 100644
--- a/src/database/database.vala
+++ b/src/database/database.vala
@@ -63,10 +63,6 @@ private class Games.Database : Object {
                return new DatabaseUriSource (database);
        }
 
-       public DatabaseUid get_uid (Uid uid) {
-               return new DatabaseUid (database, uid);
-       }
-
        public DatabaseDeveloper get_developer (Developer developer, Uid uid) {
                return new DatabaseDeveloper (database, developer, uid);
        }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]