FOSSredder 0.5.0
Loading...
Searching...
No Matches
SqliteImportLogRepository.cpp
Go to the documentation of this file.
1
5
7
11#include "persistence/Uuid.h"
12
13#include <sqlite3.h>
14
15#include <stdexcept>
16#include <utility>
17
18namespace {
19
20std::vector<std::string> loadStatementDraftIds(sqlite3* db,
21 const std::string& importLogId) {
22 std::vector<std::string> ids;
23 if (!db || importLogId.empty()) {
24 return ids;
25 }
26
28 db, "SELECT statement_draft_id FROM import_log_statement_drafts WHERE "
29 "import_log_id = ? ORDER BY position, statement_draft_id;");
30 if (!stmt) {
31 return ids;
32 }
33
34 stmt.bindText(1, importLogId);
35 while (stmt.step() == SQLITE_ROW) {
36 ids.push_back(stmt.columnText(0));
37 }
38 return ids;
39}
40
41void saveStatementDraftIds(sqlite3* db, const std::string& importLogId,
42 const std::vector<std::string>& statementDraftIds) {
43 if (!db || importLogId.empty()) {
44 return;
45 }
46
47 persistence::StmtGuard deleteStmt(
48 db, "DELETE FROM import_log_statement_drafts WHERE import_log_id = ?;");
49 if (!deleteStmt) {
50 return;
51 }
52 deleteStmt.bindText(1, importLogId);
53 deleteStmt.step();
54
55 persistence::StmtGuard insertStmt(
56 db, "INSERT OR IGNORE INTO import_log_statement_drafts (import_log_id, "
57 "statement_draft_id, position) VALUES (?, ?, ?);");
58 if (!insertStmt) {
59 return;
60 }
61
62 for (std::size_t i = 0; i < statementDraftIds.size(); ++i) {
63 const auto& statementDraftId = statementDraftIds[i];
64 if (statementDraftId.empty()) {
65 continue;
66 }
67 insertStmt.reset();
68 insertStmt.bindText(1, importLogId);
69 insertStmt.bindText(2, statementDraftId);
70 insertStmt.bindInt(3, static_cast<int>(i));
71 insertStmt.step();
72 }
73}
74
75void writeImportLog(
76 sqlite3* db,
77 const std::shared_ptr<core::application::importing::ImportLog>& log,
78 const char* sql) {
79 if (!db || !log) {
80 return;
81 }
82
83 if (log->id.empty()) {
84 log->id = persistence::generateUuid();
85 }
86
87 persistence::StmtGuard stmt(db, sql);
88 if (!stmt) {
89 return;
90 }
91
92 stmt.bindText(1, log->id);
93 stmt.bindText(2, log->time);
94 stmt.bindText(3, log->type);
95 stmt.bindText(4, log->file);
96 stmt.bindText(5, log->status);
97 stmt.bindText(6, log->message);
98 stmt.bindInt(7, log->draftAttached ? 1 : 0);
99 stmt.bindText(8, log->draftId);
100 stmt.bindText(9, log->statementId);
101 stmt.step();
102}
103
104} // namespace
105
107 explicit Impl(std::shared_ptr<SqliteDb> db) : db(std::move(db)) {}
108
109 std::shared_ptr<SqliteDb> db;
110};
111
113 : SqliteImportLogRepository(std::make_shared<SqliteDb>(dbPath)) {}
114
116 std::shared_ptr<SqliteDb> db)
117 : pimpl_(std::make_unique<Impl>(std::move(db))) {
118 if (!pimpl_->db) {
119 throw std::runtime_error("db is null");
120 }
121}
122
124
126 const std::shared_ptr<core::application::importing::ImportLog>& log) {
127 const auto db = pimpl_->db->handle();
128 writeImportLog(db, log,
129 "INSERT INTO import_logs (id, time, type, file, status, "
130 "message, draft_attached, draft_id, statement_id) VALUES (?, "
131 "?, ?, ?, ?, ?, ?, ?, ?);");
132 if (log) {
133 saveStatementDraftIds(db, log->id, log->statementDraftIds);
134 }
135}
136
137std::vector<std::shared_ptr<core::application::importing::ImportLog>>
139 std::vector<std::shared_ptr<core::application::importing::ImportLog>> logs;
140 const auto db = pimpl_->db->handle();
142 db,
143 "SELECT id, time, type, file, status, message, draft_attached, draft_id, "
144 "statement_id FROM import_logs ORDER BY time DESC, id DESC;");
145 if (!stmt) {
146 return logs;
147 }
148
149 while (stmt.step() == SQLITE_ROW) {
150 auto log = std::make_shared<core::application::importing::ImportLog>();
151 log->id = stmt.columnText(0);
152 log->time = stmt.columnText(1);
153 log->type = stmt.columnText(2);
154 log->file = stmt.columnText(3);
155 log->status = stmt.columnText(4);
156 log->message = stmt.columnText(5);
157 log->draftAttached = stmt.columnInt(6) != 0;
158 log->draftId = stmt.columnText(7);
159 log->statementId = stmt.columnText(8);
160 log->statementDraftIds =
161 loadStatementDraftIds(pimpl_->db->handle(), log->id);
162 logs.push_back(std::move(log));
163 }
164
165 return logs;
166}
167
168std::optional<std::shared_ptr<core::application::importing::ImportLog>>
170 const auto db = pimpl_->db->handle();
172 db, "SELECT id, time, type, file, status, message, draft_attached, "
173 "draft_id, statement_id FROM import_logs WHERE id = ? LIMIT 1;");
174 if (!stmt) {
175 return std::nullopt;
176 }
177
178 stmt.bindText(1, id);
179 if (stmt.step() != SQLITE_ROW) {
180 return std::nullopt;
181 }
182
183 auto log = std::make_shared<core::application::importing::ImportLog>();
184 log->id = stmt.columnText(0);
185 log->time = stmt.columnText(1);
186 log->type = stmt.columnText(2);
187 log->file = stmt.columnText(3);
188 log->status = stmt.columnText(4);
189 log->message = stmt.columnText(5);
190 log->draftAttached = stmt.columnInt(6) != 0;
191 log->draftId = stmt.columnText(7);
192 log->statementId = stmt.columnText(8);
193 log->statementDraftIds = loadStatementDraftIds(pimpl_->db->handle(), log->id);
194 return log;
195}
196
198 persistence::StmtGuard stmt(pimpl_->db->handle(),
199 "DELETE FROM import_logs WHERE id = ?;");
200 if (!stmt) {
201 return;
202 }
203 stmt.bindText(1, id);
204 stmt.step();
205}
206
208 const std::shared_ptr<core::application::importing::ImportLog>& log) {
209 writeImportLog(pimpl_->db->handle(), log,
210 "UPDATE import_logs SET time = ?2, type = ?3, file = ?4, "
211 "status = ?5, message = ?6, draft_attached = ?7, draft_id = "
212 "?8, statement_id = ?9 WHERE id = ?1;");
213 if (log) {
214 saveStatementDraftIds(pimpl_->db->handle(), log->id,
215 log->statementDraftIds);
216 }
217}
218
220 const std::shared_ptr<core::application::importing::ImportLog>& log) {
221 writeImportLog(pimpl_->db->handle(), log,
222 "INSERT OR REPLACE INTO import_logs (id, time, type, file, "
223 "status, message, draft_attached, draft_id, statement_id) "
224 "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);");
225 if (log) {
226 saveStatementDraftIds(pimpl_->db->handle(), log->id,
227 log->statementDraftIds);
228 }
229}
230
232 persistence::StmtGuard stmt(pimpl_->db->handle(), "DELETE FROM import_logs;");
233 if (!stmt) {
234 return;
235 }
236 stmt.step();
237}
Declares the persisted metadata record for an import run.
Declares the lightweight SQLite connection wrapper.
Declares the SQLite-backed import log repository.
RAII wrapper for sqlite3_stmt* that auto-finalizes on destruction.
void removeImportLog(const std::string &id) override
Remove an import log entry by identifier.
void addImportLog(const std::shared_ptr< core::application::importing::ImportLog > &log) override
Insert a new import log entry.
std::vector< std::shared_ptr< core::application::importing::ImportLog > > getImportLogs() const override
Retrieve all import log entries.
void upsertImportLog(const std::shared_ptr< core::application::importing::ImportLog > &log) override
Insert or update an import log entry.
void clearImportLogs() override
Remove all import log entries.
SqliteImportLogRepository(const std::string &dbPath)
Create an import log repository for the database at the given path.
~SqliteImportLogRepository() override
Destroy the repository.
void updateImportLog(const std::shared_ptr< core::application::importing::ImportLog > &log) override
Update an existing import log entry.
std::optional< std::shared_ptr< core::application::importing::ImportLog > > getImportLogById(const std::string &id) const override
Retrieve an import log entry by identifier.
std::string columnText(int col) const noexcept
Definition StmtGuard.h:57
int step() noexcept
Definition StmtGuard.h:37
void bindText(int col, const std::string &v) noexcept
Definition StmtGuard.h:44
int columnInt(int col) const noexcept
Definition StmtGuard.h:61
std::string generateUuid()
Definition Uuid.h:9
Impl(std::shared_ptr< SqliteDb > db)