20std::vector<std::string> loadStatementDraftIds(sqlite3* db,
21 const std::string& importLogId) {
22 std::vector<std::string> ids;
23 if (!db || importLogId.empty()) {
28 db,
"SELECT statement_draft_id FROM import_log_statement_drafts WHERE "
29 "import_log_id = ? ORDER BY position, statement_draft_id;");
34 stmt.bindText(1, importLogId);
35 while (stmt.step() == SQLITE_ROW) {
36 ids.push_back(stmt.columnText(0));
41void saveStatementDraftIds(sqlite3* db,
const std::string& importLogId,
42 const std::vector<std::string>& statementDraftIds) {
43 if (!db || importLogId.empty()) {
48 db,
"DELETE FROM import_log_statement_drafts WHERE import_log_id = ?;");
52 deleteStmt.bindText(1, importLogId);
56 db,
"INSERT OR IGNORE INTO import_log_statement_drafts (import_log_id, "
57 "statement_draft_id, position) VALUES (?, ?, ?);");
62 for (std::size_t i = 0; i < statementDraftIds.size(); ++i) {
63 const auto& statementDraftId = statementDraftIds[i];
64 if (statementDraftId.empty()) {
68 insertStmt.bindText(1, importLogId);
69 insertStmt.bindText(2, statementDraftId);
70 insertStmt.bindInt(3,
static_cast<int>(i));
77 const std::shared_ptr<core::application::importing::ImportLog>& log,
83 if (log->id.empty()) {
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);
107 explicit Impl(std::shared_ptr<SqliteDb>
db) :
db(std::move(
db)) {}
109 std::shared_ptr<SqliteDb>
db;
116 std::shared_ptr<SqliteDb> db)
117 : pimpl_(std::make_unique<
Impl>(std::move(db))) {
119 throw std::runtime_error(
"db is null");
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 "?, ?, ?, ?, ?, ?, ?, ?);");
133 saveStatementDraftIds(db, log->id, log->statementDraftIds);
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();
143 "SELECT id, time, type, file, status, message, draft_attached, draft_id, "
144 "statement_id FROM import_logs ORDER BY time DESC, id DESC;");
149 while (stmt.
step() == SQLITE_ROW) {
150 auto log = std::make_shared<core::application::importing::ImportLog>();
157 log->draftAttached = stmt.
columnInt(6) != 0;
160 log->statementDraftIds =
161 loadStatementDraftIds(pimpl_->db->handle(), log->id);
162 logs.push_back(std::move(log));
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;");
179 if (stmt.
step() != SQLITE_ROW) {
183 auto log = std::make_shared<core::application::importing::ImportLog>();
190 log->draftAttached = stmt.
columnInt(6) != 0;
193 log->statementDraftIds = loadStatementDraftIds(pimpl_->db->handle(), log->id);
199 "DELETE FROM import_logs WHERE id = ?;");
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;");
214 saveStatementDraftIds(pimpl_->db->handle(), log->id,
215 log->statementDraftIds);
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 (?, ?, ?, ?, ?, ?, ?, ?, ?);");
226 saveStatementDraftIds(pimpl_->db->handle(), log->id,
227 log->statementDraftIds);
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
void bindText(int col, const std::string &v) noexcept
int columnInt(int col) const noexcept
std::string generateUuid()
Impl(std::shared_ptr< SqliteDb > db)
std::shared_ptr< SqliteDb > db