| 16 | } |
| 17 | |
| 18 | void sqlite::Dbi::open_check_create(OpenMode open_mode, const std::string &fp, bool *created) { |
| 19 | full_path = fp; |
| 20 | sqlite::check(sqlite3_open_v2(platform::expand_path(full_path).c_str(), |
| 21 | &handle, |
| 22 | open_mode == OpenMode::O_READ_EXISTING |
| 23 | ? SQLITE_OPEN_READONLY |
| 24 | : open_mode == OpenMode::O_OPEN_EXISTING ? SQLITE_OPEN_READWRITE |
| 25 | : (SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE), |
| 26 | nullptr), |
| 27 | "sqlite3_open "); |
| 28 | invariant(open_mode != OpenMode::O_CREATE_ALWAYS, "sqlite database does not support clearing existing data"); |
| 29 | if (open_mode == OpenMode::O_READ_EXISTING) |
| 30 | exec("BEGIN TRANSACTION", |
| 31 | "modifying database impossible. Disk read-only or database used by other running instance?"); |
| 32 | else |
| 33 | exec("BEGIN IMMEDIATE TRANSACTION", |
| 34 | "modifying database impossible. Disk read-only or database used by other running instance?"); |
| 35 | sqlite::Stmt stmt_get_tables; |
| 36 | stmt_get_tables.prepare(*this, "SELECT name FROM sqlite_master WHERE type = 'table'"); |
| 37 | *created = !stmt_get_tables.step(); |
| 38 | if (open_mode == OpenMode::O_CREATE_NEW && !*created) |
| 39 | throw ErrorDBExists("sqlite database " + full_path + " already exists and will not be overwritten"); |
| 40 | sqlite::check(sqlite3_busy_timeout(handle, 5000), "sqlite3_busy_timeout"); // ms |
| 41 | } |
| 42 | |
| 43 | void sqlite::Dbi::exec(const char *statement, const char *err_msg) { |
| 44 | auto rc = sqlite3_exec(handle, statement, nullptr, nullptr, nullptr); |
no test coverage detected