Path: blob/main/contrib/kyua/store/read_backend.cpp
39478 views
// Copyright 2011 The Kyua Authors.1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions are5// met:6//7// * Redistributions of source code must retain the above copyright8// notice, this list of conditions and the following disclaimer.9// * Redistributions in binary form must reproduce the above copyright10// notice, this list of conditions and the following disclaimer in the11// documentation and/or other materials provided with the distribution.12// * Neither the name of Google Inc. nor the names of its contributors13// may be used to endorse or promote products derived from this software14// without specific prior written permission.15//16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2728#include "store/read_backend.hpp"2930#include "store/exceptions.hpp"31#include "store/metadata.hpp"32#include "store/read_transaction.hpp"33#include "store/write_backend.hpp"34#include "utils/format/macros.hpp"35#include "utils/fs/path.hpp"36#include "utils/noncopyable.hpp"37#include "utils/sqlite/database.hpp"38#include "utils/sqlite/exceptions.hpp"3940namespace fs = utils::fs;41namespace sqlite = utils::sqlite;424344/// Opens a database and defines session pragmas.45///46/// This auxiliary function ensures that, every time we open a SQLite database,47/// we define the same set of pragmas for it.48///49/// \param file The database file to be opened.50/// \param flags The flags for the open; see sqlite::database::open.51///52/// \return The opened database.53///54/// \throw store::error If there is a problem opening or creating the database.55sqlite::database56store::detail::open_and_setup(const fs::path& file, const int flags)57{58try {59sqlite::database database = sqlite::database::open(file, flags);60database.exec("PRAGMA foreign_keys = ON");61return database;62} catch (const sqlite::error& e) {63throw store::error(F("Cannot open '%s': %s") % file % e.what());64}65}666768/// Internal implementation for the backend.69struct store::read_backend::impl : utils::noncopyable {70/// The SQLite database this backend talks to.71sqlite::database database;7273/// Constructor.74///75/// \param database_ The SQLite database instance.76/// \param metadata_ The metadata for the loaded database. This must match77/// the schema version we implement in this module; otherwise, a78/// migration is necessary.79///80/// \throw integrity_error If the schema in the database is too modern,81/// which might indicate some form of corruption or an old binary.82/// \throw old_schema_error If the schema in the database is older than our83/// currently-implemented version and needs an upgrade. The caller can84/// use migrate_schema() to fix this problem.85impl(sqlite::database& database_, const metadata& metadata_) :86database(database_)87{88const int database_version = metadata_.schema_version();8990if (database_version == detail::current_schema_version) {91// OK.92} else if (database_version < detail::current_schema_version) {93throw old_schema_error(database_version);94} else if (database_version > detail::current_schema_version) {95throw integrity_error(96F("Database at schema version %s, which is newer than the "97"supported version %s")98% database_version % detail::current_schema_version);99}100}101};102103104/// Constructs a new backend.105///106/// \param pimpl_ The internal data.107store::read_backend::read_backend(impl* pimpl_) :108_pimpl(pimpl_)109{110}111112113/// Destructor.114store::read_backend::~read_backend(void)115{116}117118119/// Opens a database in read-only mode.120///121/// \param file The database file to be opened.122///123/// \return The backend representation.124///125/// \throw store::error If there is any problem opening the database.126store::read_backend127store::read_backend::open_ro(const fs::path& file)128{129sqlite::database db = detail::open_and_setup(file, sqlite::open_readonly);130return read_backend(new impl(db, metadata::fetch_latest(db)));131}132133134/// Closes the SQLite database.135void136store::read_backend::close(void)137{138_pimpl->database.close();139}140141142/// Gets the connection to the SQLite database.143///144/// \return A database connection.145sqlite::database&146store::read_backend::database(void)147{148return _pimpl->database;149}150151152/// Opens a read-only transaction.153///154/// \return A new transaction.155store::read_transaction156store::read_backend::start_read(void)157{158return read_transaction(*this);159}160161162