#ifdef UNSAFE_BUFFERS_BUILD
#pragma allow_unsafe_buffers
#endif
#include "sql/database.h"
#include <limits.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <cinttypes>
#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include <tuple>
#include <utility>
#include <vector>
#include "base/check.h"
#include "base/check_op.h"
#include "base/dcheck_is_on.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/format_macros.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "base/no_destructor.h"
#include "base/notimplemented.h"
#include "base/notreached.h"
#include "base/ranges/algorithm.h"
#include "base/sequence_checker.h"
#include "base/strings/cstring_view.h"
#include "base/strings/strcat.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/synchronization/lock.h"
#include "base/threading/scoped_blocking_call.h"
#include "base/time/time.h"
#include "base/trace_event/memory_dump_manager.h"
#include "base/trace_event/trace_event.h"
#include "base/tracing/protos/chrome_track_event.pbzero.h"
#include "base/types/pass_key.h"
#include "build/build_config.h"
#include "sql/database_memory_dump_provider.h"
#include "sql/initialization.h"
#include "sql/internal_api_token.h"
#include "sql/meta_table.h"
#include "sql/sqlite_result_code.h"
#include "sql/sqlite_result_code_values.h"
#include "sql/statement.h"
#include "sql/statement_id.h"
#include "sql/transaction.h"
#include "third_party/perfetto/include/perfetto/tracing/traced_proto.h"
#include "third_party/sqlite/sqlite3.h"
#if BUILDFLAG(IS_WIN)
#include "base/containers/contains.h"
#endif
namespace sql {
namespace {
bool enable_mmap_by_default_ = …;
static constexpr char kSqliteMainDatabaseName[] = …;
static constexpr char kSqliteOpenInMemoryPath[] = …;
const int kBusyTimeoutSeconds = …;
class ScopedWritableSchema { … };
SqliteResultCode BackupDatabaseForRaze(sqlite3* source_db,
sqlite3* destination_db) { … }
bool ValidAttachmentPoint(std::string_view attachment_point) { … }
std::string AsUTF8ForSQL(const base::FilePath& path) { … }
}
Database::ScopedErrorExpecterCallback* Database::current_expecter_cb_ = …;
bool Database::IsExpectedSqliteError(int sqlite_error_code) { … }
void Database::SetScopedErrorExpecter(
Database::ScopedErrorExpecterCallback* cb,
base::PassKey<test::ScopedErrorExpecter>) { … }
void Database::ResetScopedErrorExpecter(
base::PassKey<test::ScopedErrorExpecter>) { … }
base::FilePath Database::JournalPath(const base::FilePath& db_path) { … }
base::FilePath Database::WriteAheadLogPath(const base::FilePath& db_path) { … }
base::FilePath Database::SharedMemoryFilePath(const base::FilePath& db_path) { … }
base::WeakPtr<Database> Database::GetWeakPtr(InternalApiToken) { … }
Database::StatementRef::StatementRef(Database* database,
sqlite3_stmt* stmt,
bool was_valid)
: … { … }
Database::StatementRef::~StatementRef() { … }
void Database::StatementRef::Close(bool forced) { … }
static_assert …;
DatabaseDiagnostics::DatabaseDiagnostics() = default;
DatabaseDiagnostics::~DatabaseDiagnostics() = default;
void DatabaseDiagnostics::WriteIntoTrace(
perfetto::TracedProto<TraceProto> context) const { … }
Database::Database() : … { … }
Database::Database(DatabaseOptions options)
: … { … }
Database::~Database() { … }
void Database::DisableMmapByDefault() { … }
bool Database::Open(const base::FilePath& path) { … }
bool Database::OpenInMemory() { … }
void Database::DetachFromSequence() { … }
void Database::CloseInternal(bool forced) { … }
bool Database::is_open() const { … }
void Database::Close() { … }
void Database::Preload() { … }
void Database::ReleaseCacheMemoryIfNeeded(bool implicit_change_performed) { … }
base::FilePath Database::DbPath() const { … }
std::string Database::CollectErrorInfo(int sqlite_error_code,
Statement* stmt,
DatabaseDiagnostics* diagnostics) const { … }
std::string Database::CollectCorruptionInfo() { … }
bool Database::GetMmapAltStatus(int64_t* status) { … }
bool Database::SetMmapAltStatus(int64_t status) { … }
size_t Database::ComputeMmapSizeForOpen() { … }
int Database::SqlitePrepareFlags() const { … }
sqlite3_file* Database::GetSqliteVfsFile() { … }
void Database::TrimMemory() { … }
bool Database::Raze() { … }
bool Database::RazeAndPoison() { … }
void Database::Poison() { … }
bool Database::Delete(const base::FilePath& path) { … }
bool Database::BeginTransaction(InternalApiToken) { … }
void Database::RollbackTransaction(InternalApiToken) { … }
bool Database::CommitTransaction(InternalApiToken) { … }
bool Database::BeginTransactionDeprecated() { … }
bool Database::CommitTransactionDeprecated() { … }
void Database::RollbackTransactionDeprecated() { … }
void Database::RollbackAllTransactions() { … }
bool Database::AttachDatabase(const base::FilePath& other_db_path,
std::string_view attachment_point) { … }
bool Database::DetachDatabase(std::string_view attachment_point) { … }
SqliteResultCode Database::ExecuteAndReturnResultCode(
base::cstring_view initial_sql) { … }
bool Database::Execute(base::cstring_view sql) { … }
bool Database::ExecuteWithTimeout(base::cstring_view sql,
base::TimeDelta timeout) { … }
bool Database::ExecuteScriptForTesting(base::cstring_view sql_script) { … }
scoped_refptr<Database::StatementRef> Database::GetCachedStatement(
StatementID id,
base::cstring_view sql) { … }
scoped_refptr<Database::StatementRef> Database::GetUniqueStatement(
base::cstring_view sql) { … }
scoped_refptr<Database::StatementRef> Database::GetReadonlyStatement(
base::cstring_view sql) { … }
scoped_refptr<Database::StatementRef> Database::GetStatementImpl(
base::cstring_view sql,
bool is_readonly) { … }
std::string Database::GetSchema() { … }
bool Database::IsSQLValid(base::cstring_view sql) { … }
bool Database::DoesIndexExist(std::string_view index_name) { … }
bool Database::DoesTableExist(std::string_view table_name) { … }
bool Database::DoesViewExist(std::string_view view_name) { … }
bool Database::DoesSchemaItemExist(std::string_view name,
std::string_view type) { … }
bool Database::DoesColumnExist(base::cstring_view table_name,
base::cstring_view column_name) { … }
int64_t Database::GetLastInsertRowId() const { … }
int64_t Database::GetLastChangeCount() { … }
int Database::GetMemoryUsage() { … }
int Database::GetErrorCode() const { … }
int Database::GetLastErrno() const { … }
const char* Database::GetErrorMessage() const { … }
bool Database::OpenInternal(const std::string& db_file_path) { … }
void Database::ConfigureSqliteDatabaseObject() { … }
void Database::DoRollback() { … }
void Database::StatementRefCreated(StatementRef* ref) { … }
void Database::StatementRefDeleted(StatementRef* ref) { … }
void Database::OnSqliteError(SqliteErrorCode sqlite_error_code,
sql::Statement* statement,
const char* sql_statement) { … }
std::string Database::GetDiagnosticInfo(int sqlite_error_code,
Statement* statement,
DatabaseDiagnostics* diagnostics) { … }
bool Database::FullIntegrityCheck(std::vector<std::string>* messages) { … }
bool Database::ReportMemoryUsage(base::trace_event::ProcessMemoryDump* pmd,
const std::string& dump_name) { … }
bool Database::UseWALMode() const { … }
bool Database::CheckpointDatabase() { … }
}