1// Copyright 2011 The Kyua Authors.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8// * Redistributions of source code must retain the above copyright
9//   notice, this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above copyright
11//   notice, this list of conditions and the following disclaimer in the
12//   documentation and/or other materials provided with the distribution.
13// * Neither the name of Google Inc. nor the names of its contributors
14//   may be used to endorse or promote products derived from this software
15//   without specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29#include "store/read_backend.hpp"
30
31#include "store/exceptions.hpp"
32#include "store/metadata.hpp"
33#include "store/read_transaction.hpp"
34#include "store/write_backend.hpp"
35#include "utils/format/macros.hpp"
36#include "utils/fs/path.hpp"
37#include "utils/noncopyable.hpp"
38#include "utils/sqlite/database.hpp"
39#include "utils/sqlite/exceptions.hpp"
40
41namespace fs = utils::fs;
42namespace sqlite = utils::sqlite;
43
44
45/// Opens a database and defines session pragmas.
46///
47/// This auxiliary function ensures that, every time we open a SQLite database,
48/// we define the same set of pragmas for it.
49///
50/// \param file The database file to be opened.
51/// \param flags The flags for the open; see sqlite::database::open.
52///
53/// \return The opened database.
54///
55/// \throw store::error If there is a problem opening or creating the database.
56sqlite::database
57store::detail::open_and_setup(const fs::path& file, const int flags)
58{
59    try {
60        sqlite::database database = sqlite::database::open(file, flags);
61        database.exec("PRAGMA foreign_keys = ON");
62        return database;
63    } catch (const sqlite::error& e) {
64        throw store::error(F("Cannot open '%s': %s") % file % e.what());
65    }
66}
67
68
69/// Internal implementation for the backend.
70struct store::read_backend::impl : utils::noncopyable {
71    /// The SQLite database this backend talks to.
72    sqlite::database database;
73
74    /// Constructor.
75    ///
76    /// \param database_ The SQLite database instance.
77    /// \param metadata_ The metadata for the loaded database.  This must match
78    ///     the schema version we implement in this module; otherwise, a
79    ///     migration is necessary.
80    ///
81    /// \throw integrity_error If the schema in the database is too modern,
82    ///     which might indicate some form of corruption or an old binary.
83    /// \throw old_schema_error If the schema in the database is older than our
84    ///     currently-implemented version and needs an upgrade.  The caller can
85    ///     use migrate_schema() to fix this problem.
86    impl(sqlite::database& database_, const metadata& metadata_) :
87        database(database_)
88    {
89        const int database_version = metadata_.schema_version();
90
91        if (database_version == detail::current_schema_version) {
92            // OK.
93        } else if (database_version < detail::current_schema_version) {
94            throw old_schema_error(database_version);
95        } else if (database_version > detail::current_schema_version) {
96            throw integrity_error(
97                F("Database at schema version %s, which is newer than the "
98                  "supported version %s")
99                % database_version % detail::current_schema_version);
100        }
101    }
102};
103
104
105/// Constructs a new backend.
106///
107/// \param pimpl_ The internal data.
108store::read_backend::read_backend(impl* pimpl_) :
109    _pimpl(pimpl_)
110{
111}
112
113
114/// Destructor.
115store::read_backend::~read_backend(void)
116{
117}
118
119
120/// Opens a database in read-only mode.
121///
122/// \param file The database file to be opened.
123///
124/// \return The backend representation.
125///
126/// \throw store::error If there is any problem opening the database.
127store::read_backend
128store::read_backend::open_ro(const fs::path& file)
129{
130    sqlite::database db = detail::open_and_setup(file, sqlite::open_readonly);
131    return read_backend(new impl(db, metadata::fetch_latest(db)));
132}
133
134
135/// Closes the SQLite database.
136void
137store::read_backend::close(void)
138{
139    _pimpl->database.close();
140}
141
142
143/// Gets the connection to the SQLite database.
144///
145/// \return A database connection.
146sqlite::database&
147store::read_backend::database(void)
148{
149    return _pimpl->database;
150}
151
152
153/// Opens a read-only transaction.
154///
155/// \return A new transaction.
156store::read_transaction
157store::read_backend::start_read(void)
158{
159    return read_transaction(*this);
160}
161