| 42 | } |
| 43 | |
| 44 | SQLitePtr openSQLiteDB(const String & path, ContextPtr context, bool throw_on_error) |
| 45 | { |
| 46 | // If run in Local mode, no need for path checking. |
| 47 | bool need_check = context->getApplicationType() != Context::ApplicationType::LOCAL; |
| 48 | |
| 49 | auto user_files_path = context->getUserFilesPath(); |
| 50 | auto database_path = validateSQLiteDatabasePath(path, user_files_path, need_check, throw_on_error); |
| 51 | |
| 52 | /// For attach database there is no throw mode. |
| 53 | if (database_path.empty()) |
| 54 | return nullptr; |
| 55 | |
| 56 | if (!fs::exists(database_path)) |
| 57 | LOG_DEBUG(getLogger("SQLite"), "SQLite database path {} does not exist, will create an empty SQLite database", database_path); |
| 58 | |
| 59 | sqlite3 * tmp_sqlite_db = nullptr; |
| 60 | int status = 0; |
| 61 | { |
| 62 | std::lock_guard lock(init_sqlite_db_mutex); |
| 63 | status = sqlite3_open(database_path.c_str(), &tmp_sqlite_db); |
| 64 | } |
| 65 | |
| 66 | if (status != SQLITE_OK) |
| 67 | { |
| 68 | processSQLiteError(fmt::format("Cannot access sqlite database. Error status: {}. Message: {}", |
| 69 | status, sqlite3_errstr(status)), throw_on_error); |
| 70 | return nullptr; |
| 71 | } |
| 72 | |
| 73 | return std::shared_ptr<sqlite3>(tmp_sqlite_db, sqlite3_close); |
| 74 | } |
| 75 | |
| 76 | } |
| 77 |
no test coverage detected