xref: /minix3/external/bsd/kyua-cli/dist/store/metadata.cpp (revision 11be35a165022172ed3cea20f2b5df0307540b0e)
1*11be35a1SLionel Sambuc // Copyright 2011 Google Inc.
2*11be35a1SLionel Sambuc // All rights reserved.
3*11be35a1SLionel Sambuc //
4*11be35a1SLionel Sambuc // Redistribution and use in source and binary forms, with or without
5*11be35a1SLionel Sambuc // modification, are permitted provided that the following conditions are
6*11be35a1SLionel Sambuc // met:
7*11be35a1SLionel Sambuc //
8*11be35a1SLionel Sambuc // * Redistributions of source code must retain the above copyright
9*11be35a1SLionel Sambuc //   notice, this list of conditions and the following disclaimer.
10*11be35a1SLionel Sambuc // * Redistributions in binary form must reproduce the above copyright
11*11be35a1SLionel Sambuc //   notice, this list of conditions and the following disclaimer in the
12*11be35a1SLionel Sambuc //   documentation and/or other materials provided with the distribution.
13*11be35a1SLionel Sambuc // * Neither the name of Google Inc. nor the names of its contributors
14*11be35a1SLionel Sambuc //   may be used to endorse or promote products derived from this software
15*11be35a1SLionel Sambuc //   without specific prior written permission.
16*11be35a1SLionel Sambuc //
17*11be35a1SLionel Sambuc // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18*11be35a1SLionel Sambuc // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*11be35a1SLionel Sambuc // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20*11be35a1SLionel Sambuc // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21*11be35a1SLionel Sambuc // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22*11be35a1SLionel Sambuc // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23*11be35a1SLionel Sambuc // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*11be35a1SLionel Sambuc // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*11be35a1SLionel Sambuc // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*11be35a1SLionel Sambuc // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27*11be35a1SLionel Sambuc // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*11be35a1SLionel Sambuc 
29*11be35a1SLionel Sambuc #include "store/metadata.hpp"
30*11be35a1SLionel Sambuc 
31*11be35a1SLionel Sambuc #include "store/exceptions.hpp"
32*11be35a1SLionel Sambuc #include "utils/format/macros.hpp"
33*11be35a1SLionel Sambuc #include "utils/sanity.hpp"
34*11be35a1SLionel Sambuc #include "utils/sqlite/database.hpp"
35*11be35a1SLionel Sambuc #include "utils/sqlite/exceptions.hpp"
36*11be35a1SLionel Sambuc #include "utils/sqlite/statement.ipp"
37*11be35a1SLionel Sambuc 
38*11be35a1SLionel Sambuc namespace sqlite = utils::sqlite;
39*11be35a1SLionel Sambuc 
40*11be35a1SLionel Sambuc 
41*11be35a1SLionel Sambuc namespace {
42*11be35a1SLionel Sambuc 
43*11be35a1SLionel Sambuc 
44*11be35a1SLionel Sambuc /// Fetches an integer column from a statement of the 'metadata' table.
45*11be35a1SLionel Sambuc ///
46*11be35a1SLionel Sambuc /// \param stmt The statement from which to get the column value.
47*11be35a1SLionel Sambuc /// \param column The name of the column to retrieve.
48*11be35a1SLionel Sambuc ///
49*11be35a1SLionel Sambuc /// \return The value of the column.
50*11be35a1SLionel Sambuc ///
51*11be35a1SLionel Sambuc /// \throw store::integrity_error If there is a problem fetching the value
52*11be35a1SLionel Sambuc ///     caused by an invalid schema or data.
53*11be35a1SLionel Sambuc static int64_t
int64_column(sqlite::statement & stmt,const char * column)54*11be35a1SLionel Sambuc int64_column(sqlite::statement& stmt, const char* column)
55*11be35a1SLionel Sambuc {
56*11be35a1SLionel Sambuc     int index;
57*11be35a1SLionel Sambuc     try {
58*11be35a1SLionel Sambuc         index = stmt.column_id(column);
59*11be35a1SLionel Sambuc     } catch (const sqlite::invalid_column_error& e) {
60*11be35a1SLionel Sambuc         UNREACHABLE_MSG("Invalid column specification; the SELECT statement "
61*11be35a1SLionel Sambuc                         "should have caught this");
62*11be35a1SLionel Sambuc     }
63*11be35a1SLionel Sambuc     if (stmt.column_type(index) != sqlite::type_integer)
64*11be35a1SLionel Sambuc         throw store::integrity_error(F("The '%s' column in 'metadata' table "
65*11be35a1SLionel Sambuc                                        "has an invalid type") % column);
66*11be35a1SLionel Sambuc     return stmt.column_int64(index);
67*11be35a1SLionel Sambuc }
68*11be35a1SLionel Sambuc 
69*11be35a1SLionel Sambuc 
70*11be35a1SLionel Sambuc }  // anonymous namespace
71*11be35a1SLionel Sambuc 
72*11be35a1SLionel Sambuc 
73*11be35a1SLionel Sambuc /// Constructs a new metadata object.
74*11be35a1SLionel Sambuc ///
75*11be35a1SLionel Sambuc /// \param schema_version_ The schema version.
76*11be35a1SLionel Sambuc /// \param timestamp_ The time at which this version was created.
metadata(const int schema_version_,const int64_t timestamp_)77*11be35a1SLionel Sambuc store::metadata::metadata(const int schema_version_, const int64_t timestamp_) :
78*11be35a1SLionel Sambuc     _schema_version(schema_version_),
79*11be35a1SLionel Sambuc     _timestamp(timestamp_)
80*11be35a1SLionel Sambuc {
81*11be35a1SLionel Sambuc }
82*11be35a1SLionel Sambuc 
83*11be35a1SLionel Sambuc 
84*11be35a1SLionel Sambuc /// Returns the timestamp of this entry.
85*11be35a1SLionel Sambuc ///
86*11be35a1SLionel Sambuc /// \return The timestamp in this metadata entry.
87*11be35a1SLionel Sambuc int64_t
timestamp(void) const88*11be35a1SLionel Sambuc store::metadata::timestamp(void) const
89*11be35a1SLionel Sambuc {
90*11be35a1SLionel Sambuc     return _timestamp;
91*11be35a1SLionel Sambuc }
92*11be35a1SLionel Sambuc 
93*11be35a1SLionel Sambuc 
94*11be35a1SLionel Sambuc /// Returns the schema version.
95*11be35a1SLionel Sambuc ///
96*11be35a1SLionel Sambuc /// \return The schema version in this metadata entry.
97*11be35a1SLionel Sambuc int
schema_version(void) const98*11be35a1SLionel Sambuc store::metadata::schema_version(void) const
99*11be35a1SLionel Sambuc {
100*11be35a1SLionel Sambuc     return _schema_version;
101*11be35a1SLionel Sambuc }
102*11be35a1SLionel Sambuc 
103*11be35a1SLionel Sambuc 
104*11be35a1SLionel Sambuc /// Reads the latest metadata entry from the database.
105*11be35a1SLionel Sambuc ///
106*11be35a1SLionel Sambuc /// \param db The database from which to read the metadata from.
107*11be35a1SLionel Sambuc ///
108*11be35a1SLionel Sambuc /// \return The current metadata of the database.  It is not OK for the metadata
109*11be35a1SLionel Sambuc /// table to be empty, so this is guaranteed to return a value unless there is
110*11be35a1SLionel Sambuc /// an error.
111*11be35a1SLionel Sambuc ///
112*11be35a1SLionel Sambuc /// \throw store::integrity_error If the metadata in the database is empty,
113*11be35a1SLionel Sambuc ///     has an invalid schema or its contents are bogus.
114*11be35a1SLionel Sambuc store::metadata
fetch_latest(sqlite::database & db)115*11be35a1SLionel Sambuc store::metadata::fetch_latest(sqlite::database& db)
116*11be35a1SLionel Sambuc {
117*11be35a1SLionel Sambuc     try {
118*11be35a1SLionel Sambuc         sqlite::statement stmt = db.create_statement(
119*11be35a1SLionel Sambuc             "SELECT schema_version, timestamp FROM metadata "
120*11be35a1SLionel Sambuc             "ORDER BY schema_version DESC LIMIT 1");
121*11be35a1SLionel Sambuc         if (!stmt.step())
122*11be35a1SLionel Sambuc             throw store::integrity_error("The 'metadata' table is empty");
123*11be35a1SLionel Sambuc 
124*11be35a1SLionel Sambuc         const int schema_version_ =
125*11be35a1SLionel Sambuc             static_cast< int >(int64_column(stmt, "schema_version"));
126*11be35a1SLionel Sambuc         const int64_t timestamp_ = int64_column(stmt, "timestamp");
127*11be35a1SLionel Sambuc 
128*11be35a1SLionel Sambuc         if (stmt.step())
129*11be35a1SLionel Sambuc             UNREACHABLE_MSG("Got more than one result from a query that "
130*11be35a1SLionel Sambuc                             "does not permit this; any pragmas defined?");
131*11be35a1SLionel Sambuc 
132*11be35a1SLionel Sambuc         return metadata(schema_version_, timestamp_);
133*11be35a1SLionel Sambuc     } catch (const sqlite::error& e) {
134*11be35a1SLionel Sambuc         throw store::integrity_error(F("Invalid metadata schema: %s") %
135*11be35a1SLionel Sambuc                                      e.what());
136*11be35a1SLionel Sambuc     }
137*11be35a1SLionel Sambuc }
138