[gnome-games] database: Add DatabaseDeveloper
- From: Alexander Mikhaylenko <alexm src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-games] database: Add DatabaseDeveloper
- Date: Sun, 26 Aug 2018 19:27:14 +0000 (UTC)
commit 1254a936116c6a48494e9677b88ac85bbd090f69
Author: theawless <theawless gmail com>
Date: Sun Aug 26 07:04:40 2018 +0530
database: Add DatabaseDeveloper
This class will be responsible to cache Developer in the database.
src/database/database-developer.vala | 75 ++++++++++++++++++++++++++++++++++++
src/meson.build | 1 +
2 files changed, 76 insertions(+)
---
diff --git a/src/database/database-developer.vala b/src/database/database-developer.vala
new file mode 100644
index 00000000..ef17dd87
--- /dev/null
+++ b/src/database/database-developer.vala
@@ -0,0 +1,75 @@
+// This file is part of GNOME Games. License: GPL-3.0+.
+
+private class Games.DatabaseDeveloper : Object, Developer {
+ private const string LOAD_DEVELOPER_QUERY = """
+ SELECT developer FROM game_metadata WHERE uid=$UID;
+ """;
+
+ private const string SAVE_DEVELOPER_QUERY = """
+ UPDATE game_metadata SET developer=$DEVELOPER WHERE uid=$UID;
+ """;
+
+ private Developer developer;
+ private Uid uid;
+ private Sqlite.Statement load_statement;
+ private Sqlite.Statement save_statement;
+ private string loaded;
+
+ public DatabaseDeveloper (Sqlite.Database database, Developer developer, Uid uid) {
+ this.developer = developer;
+ this.uid = uid;
+
+ developer.changed.connect (() => changed ());
+
+ try {
+ load_statement = Database.prepare (database, LOAD_DEVELOPER_QUERY);
+ save_statement = Database.prepare (database, SAVE_DEVELOPER_QUERY);
+ }
+ catch (Error e) {
+ warning (e.message);
+ }
+ }
+
+ public string get_developer () {
+ try {
+ if (loaded == null)
+ load_developer ();
+ if (loaded != "")
+ return loaded;
+ else
+ save_developer ();
+ }
+ catch (Error e) {
+ warning (e.message);
+ }
+
+ return developer.get_developer ();
+ }
+
+ private void load_developer () throws Error {
+ if (load_statement == null)
+ return;
+
+ Database.bind_text (load_statement, "$UID", uid.get_uid ());
+
+ if (load_statement.step () == Sqlite.ROW)
+ loaded = load_statement.column_text (0) ?? "";
+ else
+ warning ("Execution failed.");
+ }
+
+ private void save_developer () throws Error {
+ if (save_statement == null)
+ return;
+
+ loaded = developer.get_developer ();
+ if (loaded == "")
+ return;
+
+ Database.bind_text (save_statement, "$UID", uid.get_uid ());
+ Database.bind_text (save_statement, "$DEVELOPER", loaded);
+
+ if (save_statement.step () != Sqlite.DONE)
+ warning ("Execution failed.");
+ }
+}
diff --git a/src/meson.build b/src/meson.build
index 5bb851a8..c28123ff 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -46,6 +46,7 @@ vala_sources = [
'core/uri-source.vala',
'database/database.vala',
+ 'database/database-developer.vala',
'database/database-error.vala',
'database/database-uid.vala',
'database/database-uri-iterator.vala',
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]