-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdatabase_manager.cpp
180 lines (148 loc) · 5.71 KB
/
database_manager.cpp
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <iostream>
#include "ansi_color.h"
#include "database_manager.h"
#include "util.h"
CDatabaseManager::CDatabaseManager() {
// 데이터베이스 파일 오픈, 파일 없으면 새로 생성
if (sqlite3_open(DATABASE_NAME, &m_pDb)) {
HandleError(ERROR_DATABASE_GENERAL, "Can't open database: " + std::string(sqlite3_errmsg(m_pDb)));
} else {
std::cout << COLOR_GREEN <<"Opened database successfully" << COLOR_RESET << "\n";;
}
InitializeDatabase();
}
CDatabaseManager::~CDatabaseManager() {
if (m_pDb) {
sqlite3_close(m_pDb);
}
}
// 데이터베이스 초기화 + 필요한 테이블 생성
void CDatabaseManager::InitializeDatabase() {
const char* chSql = R"(
CREATE TABLE IF NOT EXISTS files (
id INTEGER PRIMARY KEY AUTOINCREMENT,
file_path TEXT UNIQUE,
creation_time TEXT,
last_modified_time TEXT,
hash TEXT,
file_size INTEGER
);
CREATE TABLE IF NOT EXISTS file_events (
id INTEGER PRIMARY KEY AUTOINCREMENT,
file_path TEXT,
event_time TEXT,
hash TEXT,
event_type TEXT,
file_size INTEGER,
user TEXT,
process_id INTEGER
);
)";
ExecuteSQL(chSql);
}
// SQL 쿼리 준비
bool CDatabaseManager::PrepareSQL(const std::string& sql, sqlite3_stmt** stmt) {
if (sqlite3_prepare_v2(m_pDb, sql.c_str(), -1, stmt, nullptr) != SQLITE_OK) {
PrintErrorMessage(ERROR_DATABASE_GENERAL, "Failed to prepare statement: " + std::string(sqlite3_errmsg(m_pDb)));
return false;
}
return true;
}
// SQL 쿼리 실행 및 리소스 해제
void CDatabaseManager::FinalizeAndExecuteSQL(sqlite3_stmt* stmt) {
if (sqlite3_step(stmt) != SQLITE_DONE) {
PrintErrorMessage(ERROR_DATABASE_GENERAL, "Failed to execute statement: " + std::string(sqlite3_errmsg(m_pDb)));
}
sqlite3_finalize(stmt);
}
// 파일 이벤트 발생마다 데이터 추가 및 업데이트
void CDatabaseManager::LogEventToDatabase(const ST_MonitorData& data) {
sqlite3_stmt* stmt;
// file_events 테이블에 삽입
std::string insertEventSql = R"(
INSERT INTO file_events (file_path, event_time, hash, event_type, file_size, user, process_id)
VALUES (?, ?, ?, ?, ?, ?, ?);
)";
// SQL 쿼리 준비
if (!PrepareSQL(insertEventSql, &stmt)) {
return;
}
// SQL 쿼리에 매개변수 바인딩
sqlite3_bind_text(stmt, 1, data.filePath.c_str(), -1, SQLITE_STATIC);
sqlite3_bind_text(stmt, 2, data.timestamp.c_str(), -1, SQLITE_STATIC);
sqlite3_bind_text(stmt, 3, data.newHash.c_str(), -1, SQLITE_STATIC);
sqlite3_bind_text(stmt, 4, data.eventType.c_str(), -1, SQLITE_STATIC);
sqlite3_bind_int64(stmt, 5, data.fileSize);
sqlite3_bind_text(stmt, 6, data.user.c_str(), -1, SQLITE_STATIC);
sqlite3_bind_int64(stmt, 7, data.processId);
// SQL 쿼리 실행
FinalizeAndExecuteSQL(stmt);
// files 테이블 업데이트
if (data.eventType != "File moved from" && data.eventType != "File deleted") {
std::string updateMainSql = R"(
INSERT INTO files (file_path, creation_time, last_modified_time, hash, file_size)
VALUES (?, ?, ?, ?, ?)
ON CONFLICT(file_path) DO UPDATE SET
last_modified_time=excluded.last_modified_time,
hash=excluded.hash,
file_size=excluded.file_size;
)";
if (!PrepareSQL(updateMainSql, &stmt)) {
return;
}
sqlite3_bind_text(stmt, 1, data.filePath.c_str(), -1, SQLITE_STATIC);
sqlite3_bind_text(stmt, 2, data.timestamp.c_str(), -1, SQLITE_STATIC);
sqlite3_bind_text(stmt, 3, data.timestamp.c_str(), -1, SQLITE_STATIC);
sqlite3_bind_text(stmt, 4, data.newHash.c_str(), -1, SQLITE_STATIC);
sqlite3_bind_int64(stmt, 5, data.fileSize);
FinalizeAndExecuteSQL(stmt);
}
}
// SQL 쿼리 실행
void CDatabaseManager::ExecuteSQL(const std::string& sql) {
char* errMsg = nullptr;
if (sqlite3_exec(m_pDb, sql.c_str(), nullptr, nullptr, &errMsg) != SQLITE_OK) {
PrintErrorMessage(ERROR_DATABASE_GENERAL, "Failed to execute SQL: " + std::string(errMsg));
sqlite3_free(errMsg);
}
}
// 파일 경로에 대한 해시 값을 files 테이블에서 가져옴
std::string CDatabaseManager::GetFileHash(const std::string& filePath) {
sqlite3_stmt* stmt;
std::string sql = "SELECT hash FROM files WHERE file_path = ?";
if (!PrepareSQL(sql, &stmt)) {
return "";
}
sqlite3_bind_text(stmt, 1, filePath.c_str(), -1, SQLITE_STATIC);
std::string hash;
if (sqlite3_step(stmt) == SQLITE_ROW) {
hash = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0));
}
sqlite3_finalize(stmt);
return hash;
}
// 특정 파일 경로의 파일 크기 가져옴
int64_t CDatabaseManager::GetFileSize(const std::string& filePath) {
sqlite3_stmt* stmt;
std::string sql = "SELECT file_size FROM files WHERE file_path = ?";
if (!PrepareSQL(sql, &stmt)) {
return -1;
}
sqlite3_bind_text(stmt, 1, filePath.c_str(), -1, SQLITE_STATIC);
int64_t fileSize = -1;
if (sqlite3_step(stmt) == SQLITE_ROW) {
fileSize = sqlite3_column_int64(stmt, 0);
}
sqlite3_finalize(stmt);
return fileSize;
}
// 파일 경로에 대한 데이터를 files 테이블에서 삭제
void CDatabaseManager::RemoveFileFromDatabase(const std::string& filePath) {
sqlite3_stmt* stmt;
std::string sql = "DELETE FROM files WHERE file_path = ?";
if (!PrepareSQL(sql, &stmt)) {
return;
}
sqlite3_bind_text(stmt, 1, filePath.c_str(), -1, SQLITE_STATIC);
FinalizeAndExecuteSQL(stmt);
}