SQLite: Move integrity_check to pragma table

This commit is contained in:
Tim Wojtulewicz 2025-05-20 13:05:56 -07:00
parent 78dffb1d6f
commit a58128a45c
3 changed files with 10 additions and 12 deletions

View file

@ -25,7 +25,8 @@ export {
## sqlite. The ``integrity_check`` pragma is run automatically and does ## sqlite. The ``integrity_check`` pragma is run automatically and does
## not need to be included here. For pragmas without a second argument, ## not need to be included here. For pragmas without a second argument,
## set the value to an empty string. ## set the value to an empty string.
pragma_commands: table[string] of string &default=table( pragma_commands: table[string] of string &ordered &default=table(
["integrity_check"] = "",
["busy_timeout"] = "5000", ["busy_timeout"] = "5000",
["journal_mode"] = "WAL", ["journal_mode"] = "WAL",
["synchronous"] = "normal", ["synchronous"] = "normal",

View file

@ -5,6 +5,7 @@
#include <thread> #include <thread>
#include "zeek/3rdparty/sqlite3.h" #include "zeek/3rdparty/sqlite3.h"
#include "zeek/DebugLogger.h"
#include "zeek/Func.h" #include "zeek/Func.h"
#include "zeek/Val.h" #include "zeek/Val.h"
#include "zeek/storage/ReturnCode.h" #include "zeek/storage/ReturnCode.h"
@ -21,6 +22,8 @@ OperationResult SQLite::RunPragma(std::string_view name, std::optional<std::stri
if ( value && ! value->empty() ) if ( value && ! value->empty() )
cmd += util::fmt(" = %.*s", static_cast<int>(value->size()), value->data()); cmd += util::fmt(" = %.*s", static_cast<int>(value->size()), value->data());
DBG_LOG(DBG_STORAGE, "Executing pragma %s on %s", cmd.c_str(), full_path.c_str());
while ( pragma_timeout == 0ms || time_spent < pragma_timeout ) { while ( pragma_timeout == 0ms || time_spent < pragma_timeout ) {
int res = sqlite3_exec(db, cmd.c_str(), NULL, NULL, &errorMsg); int res = sqlite3_exec(db, cmd.c_str(), NULL, NULL, &errorMsg);
if ( res == SQLITE_OK ) { if ( res == SQLITE_OK ) {
@ -89,14 +92,6 @@ OperationResult SQLite::DoOpen(OpenResultCallback* cb, RecordValPtr options) {
return open_res; return open_res;
} }
char* errorMsg = nullptr;
OperationResult pragma_res = RunPragma("integrity_check");
if ( pragma_res.code != ReturnCode::SUCCESS ) {
Error(pragma_res.err_str.c_str());
return pragma_res;
}
auto pragmas = backend_options->GetField<TableVal>("pragma_commands")->ToMap(); auto pragmas = backend_options->GetField<TableVal>("pragma_commands")->ToMap();
for ( const auto& [k, v] : pragmas ) { for ( const auto& [k, v] : pragmas ) {
auto ks = k->AsListVal()->Idx(0)->AsStringVal(); auto ks = k->AsListVal()->Idx(0)->AsStringVal();
@ -104,7 +99,7 @@ OperationResult SQLite::DoOpen(OpenResultCallback* cb, RecordValPtr options) {
auto vs = v->AsStringVal(); auto vs = v->AsStringVal();
auto vs_sv = vs->ToStdStringView(); auto vs_sv = vs->ToStdStringView();
pragma_res = RunPragma(ks_sv, vs_sv); auto pragma_res = RunPragma(ks_sv, vs_sv);
if ( pragma_res.code != ReturnCode::SUCCESS ) { if ( pragma_res.code != ReturnCode::SUCCESS ) {
Error(pragma_res.err_str.c_str()); Error(pragma_res.err_str.c_str());
return pragma_res; return pragma_res;
@ -114,6 +109,7 @@ OperationResult SQLite::DoOpen(OpenResultCallback* cb, RecordValPtr options) {
std::string create = "create table if not exists " + table_name + " ("; std::string create = "create table if not exists " + table_name + " (";
create.append("key_str blob primary key, value_str blob not null, expire_time real);"); create.append("key_str blob primary key, value_str blob not null, expire_time real);");
char* errorMsg = nullptr;
if ( int res = sqlite3_exec(db, create.c_str(), NULL, NULL, &errorMsg); res != SQLITE_OK ) { if ( int res = sqlite3_exec(db, create.c_str(), NULL, NULL, &errorMsg); res != SQLITE_OK ) {
std::string err = util::fmt("Error executing table creation statement: (%d) %s", res, errorMsg); std::string err = util::fmt("Error executing table creation statement: (%d) %s", res, errorMsg);
Error(err.c_str()); Error(err.c_str());

View file

@ -2,8 +2,9 @@
Storage::backend_opened, Storage::STORAGE_BACKEND_SQLITE, [serializer=Storage::STORAGE_SERIALIZER_JSON, sqlite=[database_path=test.sqlite, table_name=testing, pragma_commands={ Storage::backend_opened, Storage::STORAGE_BACKEND_SQLITE, [serializer=Storage::STORAGE_SERIALIZER_JSON, sqlite=[database_path=test.sqlite, table_name=testing, pragma_commands={
[synchronous] = normal, [synchronous] = normal,
[journal_mode] = WAL, [journal_mode] = WAL,
[temp_store] = memory, [busy_timeout] = 5000,
[busy_timeout] = 5000 [integrity_check] = ,
[temp_store] = memory
}, pragma_timeout=500.0 msecs, pragma_wait_on_failure=5.0 msecs]] }, pragma_timeout=500.0 msecs, pragma_wait_on_failure=5.0 msecs]]
open result, [code=Storage::SUCCESS, error_str=<uninitialized>, value=<opaque of BackendHandleVal>] open result, [code=Storage::SUCCESS, error_str=<uninitialized>, value=<opaque of BackendHandleVal>]
put result, [code=Storage::SUCCESS, error_str=<uninitialized>, value=<uninitialized>] put result, [code=Storage::SUCCESS, error_str=<uninitialized>, value=<uninitialized>]