29 LogPrintf(
"SQLite Error. Code: %d. Message: %s\n", code, msg);
46 throw std::runtime_error(
47 strprintf(
"SQLiteDatabase: Failed to setup error log: %s\n",
54 throw std::runtime_error(
55 strprintf(
"SQLiteDatabase: Failed to initialize SQLite: %s\n",
62 }
catch (
const std::runtime_error &) {
76 "SQLiteDatabase: Failed to setup SQL statements: %s\n",
82 "INSERT INTO main VALUES(?, ?)", -1,
85 "SQLiteDatabase: Failed to setup SQL statements: %s\n",
94 "SQLiteDatabase: Failed to setup SQL statements: %s\n",
100 "DELETE FROM main WHERE key = ?", -1,
103 "SQLiteDatabase: Failed to setup SQL statements: %s\n",
109 "SELECT key, value FROM main", -1,
112 "SQLiteDatabase: Failed to setup SQL statements : %s\n",
129 LogPrintf(
"SQLiteDatabase: Failed to shutdown SQLite: %s\n",
144 "verify database: %s"),
155 "to verify database: %s"),
162 "verification error: %s"),
176 return error.empty();
187 if (
m_db ==
nullptr) {
191 throw std::runtime_error(
192 strprintf(
"SQLiteDatabase: Failed to open database: %s\n",
198 throw std::runtime_error(
"SQLiteDatabase: Database opened in readonly "
199 "mode but read-write permissions are needed");
207 throw std::runtime_error(
208 strprintf(
"SQLiteDatabase: Unable to change database locking mode "
209 "to exclusive: %s\n",
217 throw std::runtime_error(
218 "SQLiteDatabase: Unable to obtain an exclusive lock on the "
219 "database, is it being used by another bitcoind?\n");
224 "SQLiteDatabase: Unable to end exclusive lock transaction: %s\n",
232 throw std::runtime_error(
233 strprintf(
"SQLiteDatabase: Failed to enable fullfsync: %s\n",
242 "SELECT name FROM sqlite_master WHERE type='table' AND name='main'", -1,
245 throw std::runtime_error(
246 strprintf(
"SQLiteDatabase: Failed to prepare statement to check "
247 "table existence: %s\n",
252 throw std::runtime_error(
253 strprintf(
"SQLiteDatabase: Failed to finalize statement checking "
254 "table existence: %s\n",
263 throw std::runtime_error(
264 strprintf(
"SQLiteDatabase: Failed to execute statement to check "
265 "table existence: %s\n",
273 "CREATE TABLE main(key BLOB PRIMARY KEY NOT NULL, "
274 "value BLOB NOT NULL)",
275 nullptr,
nullptr,
nullptr);
277 throw std::runtime_error(
278 strprintf(
"SQLiteDatabase: Failed to create new database: %s\n",
321 throw std::runtime_error(
322 strprintf(
"SQLiteDatabase: Failed to close database: %s\n",
330 return std::make_unique<SQLiteBatch>(*
this);
345 LogPrintf(
"SQLiteBatch: Batch closed unexpectedly without the "
346 "transaction being explicitly committed or aborted\n");
349 "SQLiteBatch: Batch closed and failed to abort transaction\n");
356 LogPrintf(
"SQLiteBatch: Batch closed but could not finalize read "
362 LogPrintf(
"SQLiteBatch: Batch closed but could not finalize insert "
368 LogPrintf(
"SQLiteBatch: Batch closed but could not finalize overwrite "
374 LogPrintf(
"SQLiteBatch: Batch closed but could not finalize delete "
380 LogPrintf(
"SQLiteBatch: Batch closed but could not finalize cursor "
546 "SQLiteBatch::ReadAtCursor: Unable to execute cursor step: %s\n",
573 LogPrintf(
"SQLiteBatch: Failed to begin the transaction\n");
585 LogPrintf(
"SQLiteBatch: Failed to commit the transaction\n");
597 LogPrintf(
"SQLiteBatch: Failed to abort the transaction\n");
606std::unique_ptr<SQLiteDatabase>
611 auto db = std::make_unique<SQLiteDatabase>(path, file);
617 }
catch (
const std::runtime_error &
e) {
619 error.original =
e.what();
Double ended buffer combining vector and stream-like interfaces.
void write(Span< const value_type > src)
Different type to mark Mutex at global scope.
bool WriteKey(CDataStream &&key, CDataStream &&value, bool overwrite=true) override
sqlite3_stmt * m_read_stmt
sqlite3_stmt * m_cursor_stmt
void SetupSQLStatements()
bool HasKey(CDataStream &&key) override
sqlite3_stmt * m_insert_stmt
sqlite3_stmt * m_overwrite_stmt
bool StartCursor() override
bool EraseKey(CDataStream &&key) override
bool ReadAtCursor(CDataStream &key, CDataStream &value, bool &complete) override
sqlite3_stmt * m_delete_stmt
bool ReadKey(CDataStream &&key, CDataStream &value) override
bool TxnCommit() override
SQLiteDatabase & m_database
void CloseCursor() override
SQLiteBatch(SQLiteDatabase &database)
An instance of this class represents one SQLite3 database.
void Close() override
Close the database.
void Open() override
Open the database if it is not already opened.
std::unique_ptr< DatabaseBatch > MakeBatch(bool flush_on_close=true) override
Make a SQLiteBatch connected to this database.
const std::string m_dir_path
bool Verify(bilingual_str &error)
const std::string m_file_path
bool Backup(const std::string &dest) const override
Back up the entire database to a file.
bool Rewrite(const char *skip=nullptr) override
Rewrite the entire database on disk.
An instance of this class represents one database.
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
bool TryCreateDirectories(const fs::path &p)
Ignores exceptions thrown by create_directories if the requested directory exists.
bool error(const char *fmt, const Args &...args)
Filesystem operations and types.
static path PathFromString(const std::string &string)
Convert byte string to path object.
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
const std::byte * BytePtr(const void *data)
Convert a data pointer to a std::byte data pointer.
static void ErrorLogCallback(void *arg, int code, const char *msg)
std::string SQLiteDatabaseVersion()
static GlobalMutex g_sqlite_mutex
std::unique_ptr< SQLiteDatabase > MakeSQLiteDatabase(const fs::path &path, const DatabaseOptions &options, DatabaseStatus &status, bilingual_str &error)
static const char *const DATABASE_FILENAME
bool ExistsSQLiteDatabase(const fs::path &path)
std::string SQLiteDatabaseVersion()
bilingual_str _(const char *psz)
Translation function.
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.