LibDatabase: Ensure the exact number of placeholder values are provided

By default, sqlite will fill missing placeholder values with NULL. There
isn't a situation where we want this, so let's assert that the correct
number of placeholders were provided by the caller.
This commit is contained in:
Timothy Flynn 2026-02-06 08:41:21 -05:00 committed by Jelle Raaijmakers
parent 918f6a4c9f
commit 5bb084aa3a
2 changed files with 19 additions and 3 deletions

View file

@ -96,7 +96,7 @@ ErrorOr<StatementID> Database::prepare_statement(StringView statement)
return statement_id;
}
void Database::execute_statement(StatementID statement_id, OnResult on_result)
void Database::execute_statement_internal(StatementID statement_id, OnResult on_result)
{
auto* statement = prepared_statement(statement_id);
@ -120,6 +120,12 @@ void Database::execute_statement(StatementID statement_id, OnResult on_result)
}
}
int Database::bound_parameter_count(StatementID statement_id)
{
auto* statement = prepared_statement(statement_id);
return sqlite3_bind_parameter_count(statement);
}
template<typename ValueType>
void Database::apply_placeholder(StatementID statement_id, int index, ValueType const& value)
{

View file

@ -28,7 +28,12 @@ public:
using OnResult = Function<void(StatementID)>;
ErrorOr<StatementID> prepare_statement(StringView statement);
void execute_statement(StatementID, OnResult on_result);
void execute_statement(StatementID statement_id, OnResult on_result)
{
VERIFY(bound_parameter_count(statement_id) == 0);
execute_statement_internal(statement_id, move(on_result));
}
template<typename... PlaceholderValues>
void execute_statement(StatementID statement_id, OnResult on_result, PlaceholderValues&&... placeholder_values)
@ -36,7 +41,8 @@ public:
int index = 1;
(apply_placeholder(statement_id, index++, forward<PlaceholderValues>(placeholder_values)), ...);
execute_statement(statement_id, move(on_result));
VERIFY(bound_parameter_count(statement_id) == index - 1);
execute_statement_internal(statement_id, move(on_result));
}
template<typename ValueType>
@ -65,6 +71,10 @@ public:
private:
explicit Database(sqlite3*);
void execute_statement_internal(StatementID, OnResult);
int bound_parameter_count(StatementID);
template<typename ValueType>
void apply_placeholder(StatementID statement_id, int index, ValueType const& value);