|
| 1 | +#include "file.h" |
| 2 | +#include "utils/logging_utils.h" |
| 3 | +#include "utils/scope_exit.h" |
| 4 | + |
| 5 | +namespace cortex::db { |
| 6 | + |
| 7 | +cpp::result<std::vector<OpenAi::File>, std::string> File::GetFileList() const { |
| 8 | + try { |
| 9 | + db_.exec("BEGIN TRANSACTION;"); |
| 10 | + cortex::utils::ScopeExit se([this] { db_.exec("COMMIT;"); }); |
| 11 | + std::vector<OpenAi::File> entries; |
| 12 | + SQLite::Statement query(db_, |
| 13 | + "SELECT id, object, " |
| 14 | + "purpose, filename, created_at, bytes FROM files"); |
| 15 | + |
| 16 | + while (query.executeStep()) { |
| 17 | + OpenAi::File entry; |
| 18 | + entry.id = query.getColumn(0).getString(); |
| 19 | + entry.object = query.getColumn(1).getString(); |
| 20 | + entry.purpose = query.getColumn(2).getString(); |
| 21 | + entry.filename = query.getColumn(3).getString(); |
| 22 | + entry.created_at = query.getColumn(4).getInt(); |
| 23 | + entry.bytes = query.getColumn(5).getInt(); |
| 24 | + entries.push_back(entry); |
| 25 | + } |
| 26 | + return entries; |
| 27 | + } catch (const std::exception& e) { |
| 28 | + CTL_WRN(e.what()); |
| 29 | + return cpp::fail(e.what()); |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +cpp::result<OpenAi::File, std::string> File::GetFileById( |
| 34 | + const std::string& file_id) const { |
| 35 | + try { |
| 36 | + SQLite::Statement query(db_, |
| 37 | + "SELECT id, object, " |
| 38 | + "purpose, filename, created_at, bytes FROM files " |
| 39 | + "WHERE id = ?"); |
| 40 | + |
| 41 | + query.bind(1, file_id); |
| 42 | + if (query.executeStep()) { |
| 43 | + OpenAi::File entry; |
| 44 | + entry.id = query.getColumn(0).getString(); |
| 45 | + entry.object = query.getColumn(1).getString(); |
| 46 | + entry.purpose = query.getColumn(2).getString(); |
| 47 | + entry.filename = query.getColumn(3).getString(); |
| 48 | + entry.created_at = query.getColumn(4).getInt(); |
| 49 | + entry.bytes = query.getColumn(5).getInt64(); |
| 50 | + return entry; |
| 51 | + } else { |
| 52 | + return cpp::fail("File not found: " + file_id); |
| 53 | + } |
| 54 | + } catch (const std::exception& e) { |
| 55 | + return cpp::fail(e.what()); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +cpp::result<void, std::string> File::AddFileEntry(OpenAi::File& file) { |
| 60 | + try { |
| 61 | + SQLite::Statement insert( |
| 62 | + db_, |
| 63 | + "INSERT INTO files (id, object, " |
| 64 | + "purpose, filename, created_at, bytes) VALUES (?, ?, " |
| 65 | + "?, ?, ?, ?)"); |
| 66 | + insert.bind(1, file.id); |
| 67 | + insert.bind(2, file.object); |
| 68 | + insert.bind(3, file.purpose); |
| 69 | + insert.bind(4, file.filename); |
| 70 | + insert.bind(5, std::to_string(file.created_at)); |
| 71 | + insert.bind(6, std::to_string(file.bytes)); |
| 72 | + insert.exec(); |
| 73 | + |
| 74 | + CTL_INF("Inserted: " << file.ToJson()->toStyledString()); |
| 75 | + return {}; |
| 76 | + } catch (const std::exception& e) { |
| 77 | + CTL_WRN(e.what()); |
| 78 | + return cpp::fail(e.what()); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +cpp::result<void, std::string> File::DeleteFileEntry( |
| 83 | + const std::string& file_id) { |
| 84 | + try { |
| 85 | + SQLite::Statement del(db_, "DELETE from files WHERE id = ?"); |
| 86 | + del.bind(1, file_id); |
| 87 | + if (del.exec() == 1) { |
| 88 | + CTL_INF("Deleted: " << file_id); |
| 89 | + return {}; |
| 90 | + } |
| 91 | + return {}; |
| 92 | + } catch (const std::exception& e) { |
| 93 | + return cpp::fail(e.what()); |
| 94 | + } |
| 95 | +} |
| 96 | +} // namespace cortex::db |
0 commit comments