db_gateway.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. #include "db_gateway.hpp"
  2. namespace svapi {
  3. std::string qualify(const std::string& project, const std::string& collection) {
  4. if (project.empty() || project == "default") return collection;
  5. return project + ":" + collection;
  6. }
  7. namespace {
  8. smartbotic::database::Client::Config makeConfig(std::string address, uint32_t timeoutMs) {
  9. smartbotic::database::Client::Config cfg;
  10. cfg.address = std::move(address);
  11. cfg.timeoutMs = timeoutMs;
  12. return cfg; // leave cfg.project = "default"; we qualify per-call
  13. }
  14. } // namespace
  15. DbGateway::DbGateway(std::string address, uint32_t timeoutMs)
  16. : client_(makeConfig(std::move(address), timeoutMs)) {}
  17. bool DbGateway::connect() { return client_.connect(); }
  18. bool DbGateway::healthy() { return client_.healthCheck(); }
  19. smartbotic::database::Client& DbGateway::client() { return client_; }
  20. std::vector<std::string> DbGateway::listProjects() { return client_.listProjects(); }
  21. bool DbGateway::ensureProject(const std::string& name) {
  22. try { client_.createProject(name); return true; } // idempotent server-side
  23. catch (const std::exception&) { return false; }
  24. }
  25. bool DbGateway::dropProject(const std::string& name) {
  26. if (name == "default") return false;
  27. try { client_.dropProject(name); return true; }
  28. catch (const std::exception&) { return false; }
  29. }
  30. } // namespace svapi