6
6
#include " ../include/sqlite_orm.h"
7
7
#include < unistd.h>
8
8
#include < iostream>
9
+ #include < utility>
9
10
#include < vector>
10
- #include < string.h>
11
11
12
12
using namespace sqlite_orm ;
13
13
14
- std::vector<Item> Database::read (std::string file_name) {
15
- Storage storage = initStorage (file_name);
14
+ std::vector<Item> Database::read (std::string database_path) {
15
+ /*
16
+ * Read items from sqlite database.
17
+ * The Item struct is defined in Database.h
18
+ *
19
+ * @param: file_name String denoting file name of sqlite file.
20
+ * @return: vector of Items.
21
+ */
22
+
23
+ Storage storage = initStorage (std::move (database_path));
16
24
std::vector<Item> allItems = storage.get_all <Item>();
17
25
18
26
return allItems;
19
27
}
20
28
21
- Storage Database::write (Item item) {
22
- Storage storage = initStorage (" ownly.db" );
29
+ Storage Database::write (Item item, std::string database_path) {
30
+ /*
31
+ * Write an Item to the sqlite database.
32
+ *
33
+ * @param: item an Item containing attributes of a household item
34
+ * @return: Storage instance.
35
+ */
36
+
37
+
38
+ Storage storage = initStorage (std::move (database_path));
23
39
24
40
auto insertedId = storage.insert (item);
25
41
std::cout << " insertedId = " << insertedId << std::endl;
@@ -30,38 +46,94 @@ Storage Database::write(Item item) {
30
46
return storage;
31
47
}
32
48
33
- int Database::writeDbToDisk (Storage storage) {
49
+ void Database::writeDbToDisk (Storage storage) {
50
+ /*
51
+ * Write in-memory items to sqlite file.
52
+ * @param storage: A storage instance.
53
+ */
34
54
std::map<std::string, sqlite_orm::sync_schema_result> schema_sync_result = storage.sync_schema (false );
35
- return 0 ;
36
55
}
37
56
38
57
void Database::truncate (Storage storage) {
58
+ /*
59
+ * Remove all rows from the sqlite database.
60
+ * @param storage: A storage instance.
61
+ */
62
+
39
63
storage.remove_all <Item>();
40
64
writeDbToDisk (storage);
41
65
}
42
66
43
67
void Database::deleteRow (Storage storage, int row_number) {
68
+ /*
69
+ * Delete a specific row from the sqlite database.
70
+ * @param storage: A storage instance.
71
+ * @param row_number: Row number to delete. This is the primary key in the sqlite file.
72
+ */
73
+
44
74
storage.remove <Item>(row_number);
75
+ writeDbToDisk (storage);
45
76
}
46
77
47
78
Item Database::read_row (Storage storage, int row) {
79
+ /*
80
+ * Read a specific row from the sqlite database.
81
+ * @param storage: A storage instance.
82
+ * @param row: The primary key id denoting the row to read.
83
+ */
84
+
48
85
Item item = storage.get <Item>(row);
49
86
return item;
50
87
}
51
88
52
- void Database::update (const Item& item) {
53
- Storage storage = initStorage (" ownly.db" );
89
+ void Database::update (const Item& item, std::string database_path) {
90
+ /*
91
+ * Update an Item that exists in the database.
92
+ * @param item: The Item to update.
93
+ */
94
+
95
+ Storage storage = initStorage (std::move (database_path));
54
96
storage.update (item);
55
97
}
56
98
57
- std::vector<Item> Database::filter (std::string category, std::string file_name) {
58
- Storage storage = initStorage (file_name);
99
+ std::vector<Item> Database::filter (const std::string& category, const std::string& database_path) {
100
+ /*
101
+ * Filter database by a category.
102
+ * @param category: Category to filter database on.
103
+ * @param file_name: file name of sqlite file.
104
+ */
105
+
106
+ Storage storage = initStorage (database_path);
59
107
std::vector<Item> items_by_category;
60
108
if (category == " All Items" ) {
61
- items_by_category = read (file_name );
109
+ items_by_category = read (database_path );
62
110
} else {
63
111
items_by_category = storage.get_all <Item>(sqlite_orm::where (sqlite_orm::c (&Item::category) == category));
64
112
}
65
113
66
114
return items_by_category;
67
115
}
116
+
117
+ std::string Database::set_db_path () {
118
+ /*
119
+ * Get the path to the users APPDATA directory for database storage.
120
+ * @return database_path: Path where database will be stored.
121
+ */
122
+
123
+ std::string database_path;
124
+ PWSTR localAppData = nullptr ;
125
+ if (SHGetKnownFolderPath (FOLDERID_RoamingAppData, 0 , nullptr , &localAppData) == S_OK) {
126
+ std::wstring ws_path (localAppData);
127
+ std::string database_directory;
128
+ using convert_type = std::codecvt_utf8<wchar_t >;
129
+ std::wstring_convert<convert_type, wchar_t > converter;
130
+ database_directory = converter.to_bytes (ws_path) + " \\ Ownly" ;
131
+ database_path = database_directory + " \\ ownly_data.db" ;
132
+ CoTaskMemFree (static_cast <void *>(localAppData));
133
+
134
+ CreateDirectory (database_directory.c_str (), nullptr );
135
+ std::cout << " DB path: " << database_path << std::endl;
136
+ }
137
+
138
+ return database_path;
139
+ }
0 commit comments