2021-10-16 11:03:15 +00:00
|
|
|
#include "db.hpp"
|
|
|
|
|
|
|
|
#include <sqlite3.h>
|
|
|
|
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <string>
|
|
|
|
#include <string_view>
|
|
|
|
|
|
|
|
namespace Sqlite3
|
|
|
|
{
|
|
|
|
void CloseSqlite3::operator()(sqlite3* handle) const noexcept
|
|
|
|
{
|
2022-03-10 17:34:35 +00:00
|
|
|
sqlite3_close_v2(handle);
|
2021-10-16 11:03:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Db makeDb(std::string_view path, const char* schema)
|
|
|
|
{
|
|
|
|
sqlite3* handle = nullptr;
|
2022-01-27 20:17:56 +00:00
|
|
|
// All uses of NavMeshDb are protected by a mutex (navmeshtool) or serialized in a single thread (DbWorker)
|
|
|
|
// so additional synchronization between threads is not required and SQLITE_OPEN_NOMUTEX can be used.
|
|
|
|
// This is unsafe to use NavMeshDb without external synchronization because of internal state.
|
|
|
|
const int flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_NOMUTEX;
|
2021-10-16 11:03:15 +00:00
|
|
|
if (const int ec = sqlite3_open_v2(std::string(path).c_str(), &handle, flags, nullptr); ec != SQLITE_OK)
|
|
|
|
{
|
|
|
|
const std::string message(sqlite3_errmsg(handle));
|
|
|
|
sqlite3_close(handle);
|
|
|
|
throw std::runtime_error("Failed to open database: " + message);
|
|
|
|
}
|
|
|
|
Db result(handle);
|
|
|
|
if (const int ec = sqlite3_exec(result.get(), schema, nullptr, nullptr, nullptr); ec != SQLITE_OK)
|
|
|
|
throw std::runtime_error("Failed create database schema: " + std::string(sqlite3_errmsg(handle)));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|