ladybird/Libraries/LibDatabase/Database.h
Timothy Flynn 187d02c45d LibDatabase+LibWebView: Extract our SQLite wrapper to its own library
It currently lives in LibWebView as it was only used for cookies and
local storage, both of which are managed in the UI process. Let's move
it to its own library now to allow other processes to use it, without
having to depend on LibWebView (and therefore LibWeb).
2025-10-14 13:40:33 +02:00

61 lines
1.7 KiB
C++

/*
* Copyright (c) 2022-2025, Tim Flynn <trflynn89@ladybird.org>
* Copyright (c) 2023, Jelle Raaijmakers <jelle@ladybird.org>
*
* 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>
#include <LibDatabase/Forward.h>
struct sqlite3;
struct sqlite3_stmt;
namespace Database {
class DATABASE_API Database : public RefCounted<Database> {
public:
static ErrorOr<NonnullRefPtr<Database>> create(ByteString const& directory, StringView name);
~Database();
using OnResult = Function<void(StatementID)>;
ErrorOr<StatementID> prepare_statement(StringView statement);
void execute_statement(StatementID, OnResult on_result);
template<typename... PlaceholderValues>
void execute_statement(StatementID statement_id, OnResult on_result, PlaceholderValues&&... placeholder_values)
{
int index = 1;
(apply_placeholder(statement_id, index++, forward<PlaceholderValues>(placeholder_values)), ...);
execute_statement(statement_id, move(on_result));
}
template<typename ValueType>
ValueType result_column(StatementID, int column);
private:
explicit Database(sqlite3*);
template<typename ValueType>
void apply_placeholder(StatementID statement_id, int index, ValueType const& value);
ALWAYS_INLINE sqlite3_stmt* prepared_statement(StatementID statement_id)
{
VERIFY(statement_id < m_prepared_statements.size());
return m_prepared_statements[statement_id];
}
sqlite3* m_database { nullptr };
Vector<sqlite3_stmt*> m_prepared_statements;
};
}