1 | //============================================================================ |
2 | // |
3 | // SSSS tt lll lll |
4 | // SS SS tt ll ll |
5 | // SS tttttt eeee ll ll aaaa |
6 | // SSSS tt ee ee ll ll aa |
7 | // SS tt eeeeee ll ll aaaaa -- "An Atari 2600 VCS Emulator" |
8 | // SS SS tt ee ll ll aa aa |
9 | // SSSS ttt eeeee llll llll aaaaa |
10 | // |
11 | // Copyright (c) 1995-2019 by Bradford W. Mott, Stephen Anthony |
12 | // and the Stella Team |
13 | // |
14 | // See the file "License.txt" for information on usage and redistribution of |
15 | // this file, and for a DISCLAIMER OF ALL WARRANTIES. |
16 | //============================================================================ |
17 | |
18 | #include "KeyValueRepositorySqlite.hxx" |
19 | #include "Logger.hxx" |
20 | #include "SqliteError.hxx" |
21 | #include "SqliteTransaction.hxx" |
22 | |
23 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
24 | KeyValueRepositorySqlite::KeyValueRepositorySqlite( |
25 | SqliteDatabase& db, |
26 | const string& tableName |
27 | ) : myTableName(tableName), |
28 | myDb(db) |
29 | { |
30 | } |
31 | |
32 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
33 | std::map<string, Variant> KeyValueRepositorySqlite::load() |
34 | { |
35 | std::map<string, Variant> values; |
36 | |
37 | try { |
38 | myStmtSelect->reset(); |
39 | |
40 | while (myStmtSelect->step()) |
41 | values[myStmtSelect->columnText(0)] = myStmtSelect->columnText(1); |
42 | |
43 | myStmtSelect->reset(); |
44 | } |
45 | catch (SqliteError err) { |
46 | Logger::info(err.message); |
47 | } |
48 | |
49 | return values; |
50 | } |
51 | |
52 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
53 | void KeyValueRepositorySqlite::save(const std::map<string, Variant>& values) |
54 | { |
55 | try { |
56 | SqliteTransaction tx(myDb); |
57 | |
58 | myStmtInsert->reset(); |
59 | |
60 | for (const auto& pair: values) { |
61 | (*myStmtInsert) |
62 | .bind(1, pair.first.c_str()) |
63 | .bind(2, pair.second.toCString()) |
64 | .step(); |
65 | |
66 | myStmtInsert->reset(); |
67 | } |
68 | |
69 | tx.commit(); |
70 | } |
71 | catch (SqliteError err) { |
72 | Logger::info(err.message); |
73 | } |
74 | } |
75 | |
76 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
77 | void KeyValueRepositorySqlite::save(const string& key, const Variant& value) |
78 | { |
79 | try { |
80 | myStmtInsert->reset(); |
81 | |
82 | (*myStmtInsert) |
83 | .bind(1, key.c_str()) |
84 | .bind(2, value.toCString()) |
85 | .step(); |
86 | |
87 | myStmtInsert->reset(); |
88 | } |
89 | catch (SqliteError err) { |
90 | Logger::info(err.message); |
91 | } |
92 | } |
93 | |
94 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
95 | void KeyValueRepositorySqlite::initialize() |
96 | { |
97 | myDb.exec( |
98 | "CREATE TABLE IF NOT EXISTS `" + myTableName + "` (`key` TEXT PRIMARY KEY, `value` TEXT) WITHOUT ROWID" |
99 | ); |
100 | |
101 | myStmtInsert = make_unique<SqliteStatement>(myDb, "INSERT OR REPLACE INTO `" + myTableName + "` VALUES (?, ?)" ); |
102 | myStmtSelect = make_unique<SqliteStatement>(myDb, "SELECT `key`, `VALUE` FROM `" + myTableName + "`" ); |
103 | } |
104 | |