2022-10-27 12:56:22 -04:00
|
|
|
/*
|
2025-10-07 14:35:22 -04:00
|
|
|
* Copyright (c) 2022-2025, Tim Flynn <trflynn89@ladybird.org>
|
2024-11-01 12:14:53 +01:00
|
|
|
* Copyright (c) 2023, Jelle Raaijmakers <jelle@ladybird.org>
|
2022-10-27 12:56:22 -04:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Error.h>
|
|
|
|
#include <AK/Function.h>
|
|
|
|
#include <AK/NonnullRefPtr.h>
|
|
|
|
#include <AK/RefCounted.h>
|
|
|
|
#include <AK/StringView.h>
|
|
|
|
#include <AK/Vector.h>
|
2025-10-07 14:35:22 -04:00
|
|
|
#include <LibDatabase/Forward.h>
|
2024-06-04 16:34:32 -04:00
|
|
|
|
|
|
|
struct sqlite3;
|
|
|
|
struct sqlite3_stmt;
|
2022-10-27 12:56:22 -04:00
|
|
|
|
2025-10-07 14:35:22 -04:00
|
|
|
namespace Database {
|
2022-10-27 12:56:22 -04:00
|
|
|
|
2025-10-07 14:35:22 -04:00
|
|
|
class DATABASE_API Database : public RefCounted<Database> {
|
2022-10-27 12:56:22 -04:00
|
|
|
public:
|
2025-10-07 14:35:22 -04:00
|
|
|
static ErrorOr<NonnullRefPtr<Database>> create(ByteString const& directory, StringView name);
|
2024-06-04 16:34:32 -04:00
|
|
|
~Database();
|
|
|
|
|
|
|
|
using OnResult = Function<void(StatementID)>;
|
2022-10-27 12:56:22 -04:00
|
|
|
|
2024-06-04 16:34:32 -04:00
|
|
|
ErrorOr<StatementID> prepare_statement(StringView statement);
|
|
|
|
void execute_statement(StatementID, OnResult on_result);
|
2022-10-27 12:56:22 -04:00
|
|
|
|
|
|
|
template<typename... PlaceholderValues>
|
2024-06-04 16:34:32 -04:00
|
|
|
void execute_statement(StatementID statement_id, OnResult on_result, PlaceholderValues&&... placeholder_values)
|
2022-10-27 12:56:22 -04:00
|
|
|
{
|
2024-06-04 16:34:32 -04:00
|
|
|
int index = 1;
|
|
|
|
(apply_placeholder(statement_id, index++, forward<PlaceholderValues>(placeholder_values)), ...);
|
2023-08-07 23:33:01 +02:00
|
|
|
|
2024-06-04 16:34:32 -04:00
|
|
|
execute_statement(statement_id, move(on_result));
|
2022-10-27 12:56:22 -04:00
|
|
|
}
|
|
|
|
|
2024-06-04 16:34:32 -04:00
|
|
|
template<typename ValueType>
|
|
|
|
ValueType result_column(StatementID, int column);
|
2022-10-27 12:56:22 -04:00
|
|
|
|
2024-06-04 16:34:32 -04:00
|
|
|
private:
|
|
|
|
explicit Database(sqlite3*);
|
2022-10-27 12:56:22 -04:00
|
|
|
|
2024-06-04 16:34:32 -04:00
|
|
|
template<typename ValueType>
|
|
|
|
void apply_placeholder(StatementID statement_id, int index, ValueType const& value);
|
2022-10-27 12:56:22 -04:00
|
|
|
|
2024-06-04 16:34:32 -04:00
|
|
|
ALWAYS_INLINE sqlite3_stmt* prepared_statement(StatementID statement_id)
|
2023-02-03 10:33:10 -05:00
|
|
|
{
|
2024-06-04 16:34:32 -04:00
|
|
|
VERIFY(statement_id < m_prepared_statements.size());
|
|
|
|
return m_prepared_statements[statement_id];
|
2023-02-03 10:33:10 -05:00
|
|
|
}
|
|
|
|
|
2024-06-04 16:34:32 -04:00
|
|
|
sqlite3* m_database { nullptr };
|
|
|
|
Vector<sqlite3_stmt*> m_prepared_statements;
|
2022-10-27 12:56:22 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|