1 // Copyright 2011 The Kyua Authors.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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.
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.
29 #include "store/write_backend.hpp"
33 #include "store/exceptions.hpp"
34 #include "store/metadata.hpp"
35 #include "store/read_backend.hpp"
36 #include "store/write_transaction.hpp"
37 #include "utils/env.hpp"
38 #include "utils/format/macros.hpp"
39 #include "utils/fs/path.hpp"
40 #include "utils/logging/macros.hpp"
41 #include "utils/noncopyable.hpp"
42 #include "utils/sanity.hpp"
43 #include "utils/stream.hpp"
44 #include "utils/sqlite/database.hpp"
45 #include "utils/sqlite/exceptions.hpp"
46 #include "utils/sqlite/statement.ipp"
48 namespace fs = utils::fs;
49 namespace sqlite = utils::sqlite;
52 /// The current schema version.
54 /// Any new database gets this schema version. Existing databases with an older
55 /// schema version must be first migrated to the current schema with
56 /// migrate_schema() before they can be used.
58 /// This must be kept in sync with the value in the corresponding schema_vX.sql
59 /// file, where X matches this version number.
61 /// This variable is not const to allow tests to modify it. No other code
62 /// should change its value.
63 int store::detail::current_schema_version = 3;
69 /// Checks if a database is empty (i.e. if it is new).
71 /// \param db The database to check.
73 /// \return True if the database is empty.
75 empty_database(sqlite::database& db)
77 sqlite::statement stmt = db.create_statement("SELECT * FROM sqlite_master");
82 } // anonymous namespace
85 /// Calculates the path to the schema file for the database.
87 /// \return The path to the installed schema_vX.sql file that matches the
88 /// current_schema_version.
90 store::detail::schema_file(void)
92 return fs::path(utils::getenv_with_default("KYUA_STOREDIR", KYUA_STOREDIR))
93 / (F("schema_v%s.sql") % current_schema_version);
97 /// Initializes an empty database.
99 /// \param db The database to initialize.
101 /// \return The metadata record written into the new database.
103 /// \throw store::error If there is a problem initializing the database.
105 store::detail::initialize(sqlite::database& db)
107 PRE(empty_database(db));
109 const fs::path schema = schema_file();
111 LI(F("Populating new database with schema from %s") % schema);
113 db.exec(utils::read_file(schema));
115 const metadata metadata = metadata::fetch_latest(db);
116 LI(F("New metadata entry %s") % metadata.timestamp());
117 if (metadata.schema_version() != detail::current_schema_version) {
118 UNREACHABLE_MSG(F("current_schema_version is out of sync with "
122 } catch (const store::integrity_error& e) {
123 // Could be raised by metadata::fetch_latest.
124 UNREACHABLE_MSG("Inconsistent code while creating a database");
125 } catch (const sqlite::error& e) {
126 throw error(F("Failed to initialize database: %s") % e.what());
127 } catch (const std::runtime_error& e) {
128 throw error(F("Cannot read database schema '%s'") % schema);
133 /// Internal implementation for the backend.
134 struct store::write_backend::impl : utils::noncopyable {
135 /// The SQLite database this backend talks to.
136 sqlite::database database;
140 /// \param database_ The SQLite database instance.
141 impl(sqlite::database& database_) : database(database_)
147 /// Constructs a new backend.
149 /// \param pimpl_ The internal data.
150 store::write_backend::write_backend(impl* pimpl_) :
157 store::write_backend::~write_backend(void)
162 /// Opens a database in read-write mode and creates it if necessary.
164 /// \param file The database file to be opened.
166 /// \return The backend representation.
168 /// \throw store::error If there is any problem opening or creating
171 store::write_backend::open_rw(const fs::path& file)
173 sqlite::database db = detail::open_and_setup(
174 file, sqlite::open_readwrite | sqlite::open_create);
175 if (!empty_database(db))
176 throw error(F("%s already exists and is not empty; cannot open "
177 "for write") % file);
178 detail::initialize(db);
179 return write_backend(new impl(db));
183 /// Closes the SQLite database.
185 store::write_backend::close(void)
187 _pimpl->database.close();
191 /// Gets the connection to the SQLite database.
193 /// \return A database connection.
195 store::write_backend::database(void)
197 return _pimpl->database;
201 /// Opens a write-only transaction.
203 /// \return A new transaction.
204 store::write_transaction
205 store::write_backend::start_write(void)
207 return write_transaction(*this);