-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb_handler.cpp
More file actions
151 lines (138 loc) · 4.11 KB
/
db_handler.cpp
File metadata and controls
151 lines (138 loc) · 4.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include "db_handler.h"
#include <iostream>
DBHandler::DBHandler(const std::string &dbPath) : db(dbPath, SQLite::OPEN_READWRITE | SQLite::OPEN_CREATE)
{
try
{
db.exec("PRAGMA foreign_keys = ON;");
db.exec("CREATE TABLE IF NOT EXISTS workspaces ("
"id INTEGER PRIMARY KEY, "
"name TEXT NOT NULL UNIQUE)");
db.exec("CREATE TABLE IF NOT EXISTS dlls ("
"id INTEGER PRIMARY KEY, "
"path TEXT NOT NULL, "
"workspace_id INTEGER NOT NULL, "
"FOREIGN KEY(workspace_id) REFERENCES workspaces(id) ON DELETE CASCADE, "
"UNIQUE(path, workspace_id))");
if (getWorkspaces().empty())
{
addWorkspace("Default");
}
}
catch (const std::exception &e)
{
std::cerr << "sqlite exception during initialization " << e.what() << '\n';
throw;
}
}
std::vector<Workspace> DBHandler::getWorkspaces()
{
std::vector<Workspace> workspaces;
try
{
SQLite::Statement query(db, "SELECT id, name FROM workspaces ORDER BY name");
while (query.executeStep())
{
workspaces.push_back({query.getColumn(0).getInt(), query.getColumn(1).getString()});
}
}
catch (const std::exception &e)
{
std::cerr << "sqlite exception in getworkspaces " << e.what() << '\n';
}
return workspaces;
}
long long DBHandler::addWorkspace(const std::string &name)
{
try
{
SQLite::Statement query(db, "INSERT OR IGNORE INTO workspaces (name) VALUES (?)");
query.bind(1, name);
int changes = query.exec();
if (changes > 0)
{
return db.getLastInsertRowid();
}
}
catch (const std::exception &e)
{
std::cerr << "sqlite exception in addworkspace " << e.what() << '\n';
}
return 0;
}
void DBHandler::renameWorkspace(int id, const std::string &newName)
{
try
{
SQLite::Statement query(db, "UPDATE workspaces SET name = ? WHERE id = ?");
query.bind(1, newName);
query.bind(2, id);
query.exec();
}
catch (const std::exception &e)
{
std::cerr << "sqlite exception in renameworkspace " << e.what() << '\n';
}
}
void DBHandler::deleteWorkspace(int id)
{
try
{
SQLite::Statement query(db, "DELETE FROM workspaces WHERE id = ?");
query.bind(1, id);
query.exec();
}
catch (const std::exception &e)
{
std::cerr << "sqlite exception in deleteworkspace " << e.what() << '\n';
}
}
std::vector<DllInfo> DBHandler::getDlls(int workspaceId)
{
std::vector<DllInfo> dlls;
try
{
SQLite::Statement query(db, "SELECT path FROM dlls WHERE workspace_id = ?");
query.bind(1, workspaceId);
while (query.executeStep())
{
dlls.push_back({query.getColumn(0).getString()});
}
}
catch (const std::exception &e)
{
std::cerr << "sqlite exception in getdlls " << e.what() << '\n';
}
return dlls;
}
void DBHandler::syncDlls(int workspaceId, const std::vector<std::string> &paths)
{
try
{
db.exec("BEGIN");
SQLite::Statement del_query(db, "DELETE FROM dlls WHERE workspace_id = ?");
del_query.bind(1, workspaceId);
del_query.exec();
SQLite::Statement ins_query(db, "INSERT OR IGNORE INTO dlls (path, workspace_id) VALUES (?, ?)");
for (const auto &path : paths)
{
ins_query.bind(1, path);
ins_query.bind(2, workspaceId);
ins_query.exec();
ins_query.reset();
}
db.exec("COMMIT");
}
catch (const std::exception &e)
{
std::cerr << "sqlite exception in syncdlls " << e.what() << '\n';
try
{
db.exec("ROLLBACK");
}
catch (const std::exception &e2)
{
std::cerr << "sqlite exception during rollback " << e2.what() << '\n';
}
}
}