xref: /src/contrib/kyua/utils/sqlite/database.cpp (revision b0d29bc47dba79f6f38e67eabadfb4b32ffd9390)
108334c51SBrooks Davis // Copyright 2011 The Kyua Authors.
208334c51SBrooks Davis // All rights reserved.
308334c51SBrooks Davis //
408334c51SBrooks Davis // Redistribution and use in source and binary forms, with or without
508334c51SBrooks Davis // modification, are permitted provided that the following conditions are
608334c51SBrooks Davis // met:
708334c51SBrooks Davis //
808334c51SBrooks Davis // * Redistributions of source code must retain the above copyright
908334c51SBrooks Davis //   notice, this list of conditions and the following disclaimer.
1008334c51SBrooks Davis // * Redistributions in binary form must reproduce the above copyright
1108334c51SBrooks Davis //   notice, this list of conditions and the following disclaimer in the
1208334c51SBrooks Davis //   documentation and/or other materials provided with the distribution.
1308334c51SBrooks Davis // * Neither the name of Google Inc. nor the names of its contributors
1408334c51SBrooks Davis //   may be used to endorse or promote products derived from this software
1508334c51SBrooks Davis //   without specific prior written permission.
1608334c51SBrooks Davis //
1708334c51SBrooks Davis // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1808334c51SBrooks Davis // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1908334c51SBrooks Davis // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2008334c51SBrooks Davis // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2108334c51SBrooks Davis // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2208334c51SBrooks Davis // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2308334c51SBrooks Davis // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2408334c51SBrooks Davis // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2508334c51SBrooks Davis // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2608334c51SBrooks Davis // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2708334c51SBrooks Davis // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2808334c51SBrooks Davis 
2908334c51SBrooks Davis #include "utils/sqlite/database.hpp"
3008334c51SBrooks Davis 
3108334c51SBrooks Davis extern "C" {
3208334c51SBrooks Davis #include <sqlite3.h>
3308334c51SBrooks Davis }
3408334c51SBrooks Davis 
3508334c51SBrooks Davis #include <cstring>
3608334c51SBrooks Davis #include <stdexcept>
3708334c51SBrooks Davis 
3808334c51SBrooks Davis #include "utils/format/macros.hpp"
3908334c51SBrooks Davis #include "utils/fs/path.hpp"
4008334c51SBrooks Davis #include "utils/logging/macros.hpp"
4108334c51SBrooks Davis #include "utils/noncopyable.hpp"
4208334c51SBrooks Davis #include "utils/optional.ipp"
4308334c51SBrooks Davis #include "utils/sanity.hpp"
4408334c51SBrooks Davis #include "utils/sqlite/exceptions.hpp"
4508334c51SBrooks Davis #include "utils/sqlite/statement.ipp"
4608334c51SBrooks Davis #include "utils/sqlite/transaction.hpp"
4708334c51SBrooks Davis 
4808334c51SBrooks Davis namespace fs = utils::fs;
4908334c51SBrooks Davis namespace sqlite = utils::sqlite;
5008334c51SBrooks Davis 
5108334c51SBrooks Davis using utils::none;
5208334c51SBrooks Davis using utils::optional;
5308334c51SBrooks Davis 
5408334c51SBrooks Davis 
5508334c51SBrooks Davis /// Internal implementation for sqlite::database.
5608334c51SBrooks Davis struct utils::sqlite::database::impl : utils::noncopyable {
5708334c51SBrooks Davis     /// Path to the database as seen at construction time.
5808334c51SBrooks Davis     optional< fs::path > db_filename;
5908334c51SBrooks Davis 
6008334c51SBrooks Davis     /// The SQLite 3 internal database.
6108334c51SBrooks Davis     ::sqlite3* db;
6208334c51SBrooks Davis 
6308334c51SBrooks Davis     /// Whether we own the database or not (to decide if we close it).
6408334c51SBrooks Davis     bool owned;
6508334c51SBrooks Davis 
6608334c51SBrooks Davis     /// Constructor.
6708334c51SBrooks Davis     ///
6808334c51SBrooks Davis     /// \param db_filename_ The path to the database as seen at construction
6908334c51SBrooks Davis     ///     time, if any, or none for in-memory databases.  We should use
7008334c51SBrooks Davis     ///     sqlite3_db_filename instead, but this function appeared in 3.7.10
7108334c51SBrooks Davis     ///     and Ubuntu 12.04 LTS (which we support for Travis CI builds as of
7208334c51SBrooks Davis     ///     2015-07-07) ships with 3.7.9.
7308334c51SBrooks Davis     /// \param db_ The SQLite internal database.
7408334c51SBrooks Davis     /// \param owned_ Whether this object owns the db_ object or not.  If it
7508334c51SBrooks Davis     ///     does, the internal db_ will be released during destruction.
implutils::sqlite::database::impl7608334c51SBrooks Davis     impl(optional< fs::path > db_filename_, ::sqlite3* db_, const bool owned_) :
7708334c51SBrooks Davis         db_filename(db_filename_), db(db_), owned(owned_)
7808334c51SBrooks Davis     {
7908334c51SBrooks Davis     }
8008334c51SBrooks Davis 
8108334c51SBrooks Davis     /// Destructor.
8208334c51SBrooks Davis     ///
8308334c51SBrooks Davis     /// It is important to keep this as part of the 'impl' class instead of the
8408334c51SBrooks Davis     /// container class.  The 'impl' class is destroyed exactly once (because it
8508334c51SBrooks Davis     /// is managed by a shared_ptr) and thus releasing the resources here is
8608334c51SBrooks Davis     /// OK.  However, the container class is potentially released many times,
8708334c51SBrooks Davis     /// which means that we would be double-freeing the internal object and
8808334c51SBrooks Davis     /// reusing invalid data.
~implutils::sqlite::database::impl8908334c51SBrooks Davis     ~impl(void)
9008334c51SBrooks Davis     {
9108334c51SBrooks Davis         if (owned && db != NULL)
9208334c51SBrooks Davis             close();
9308334c51SBrooks Davis     }
9408334c51SBrooks Davis 
9508334c51SBrooks Davis     /// Exception-safe version of sqlite3_open_v2.
9608334c51SBrooks Davis     ///
9708334c51SBrooks Davis     /// \param file The path to the database file to be opened.
9808334c51SBrooks Davis     /// \param flags The flags to be passed to the open routine.
9908334c51SBrooks Davis     ///
10008334c51SBrooks Davis     /// \return The opened database.
10108334c51SBrooks Davis     ///
10208334c51SBrooks Davis     /// \throw std::bad_alloc If there is not enough memory to open the
10308334c51SBrooks Davis     ///     database.
10408334c51SBrooks Davis     /// \throw api_error If there is any problem opening the database.
10508334c51SBrooks Davis     static ::sqlite3*
safe_openutils::sqlite::database::impl10608334c51SBrooks Davis     safe_open(const char* file, const int flags)
10708334c51SBrooks Davis     {
10808334c51SBrooks Davis         ::sqlite3* db;
10908334c51SBrooks Davis         const int error = ::sqlite3_open_v2(file, &db, flags, NULL);
11008334c51SBrooks Davis         if (error != SQLITE_OK) {
11108334c51SBrooks Davis             if (db == NULL)
11208334c51SBrooks Davis                 throw std::bad_alloc();
11308334c51SBrooks Davis             else {
11408334c51SBrooks Davis                 sqlite::database error_db(utils::make_optional(fs::path(file)),
11508334c51SBrooks Davis                                           db, true);
11608334c51SBrooks Davis                 throw sqlite::api_error::from_database(error_db,
11708334c51SBrooks Davis                                                        "sqlite3_open_v2");
11808334c51SBrooks Davis             }
11908334c51SBrooks Davis         }
12008334c51SBrooks Davis         INV(db != NULL);
12108334c51SBrooks Davis         return db;
12208334c51SBrooks Davis     }
12308334c51SBrooks Davis 
12408334c51SBrooks Davis     /// Shared code for the public close() method.
12508334c51SBrooks Davis     void
closeutils::sqlite::database::impl12608334c51SBrooks Davis     close(void)
12708334c51SBrooks Davis     {
12808334c51SBrooks Davis         PRE(db != NULL);
12908334c51SBrooks Davis         int error = ::sqlite3_close(db);
13008334c51SBrooks Davis         // For now, let's consider a return of SQLITE_BUSY an error.  We should
13108334c51SBrooks Davis         // not be trying to close a busy database in our code.  Maybe revisit
13208334c51SBrooks Davis         // this later to raise busy errors as exceptions.
13308334c51SBrooks Davis         PRE(error == SQLITE_OK);
13408334c51SBrooks Davis         db = NULL;
13508334c51SBrooks Davis     }
13608334c51SBrooks Davis };
13708334c51SBrooks Davis 
13808334c51SBrooks Davis 
13908334c51SBrooks Davis /// Initializes the SQLite database.
14008334c51SBrooks Davis ///
14108334c51SBrooks Davis /// You must share the same database object alongside the lifetime of your
14208334c51SBrooks Davis /// SQLite session.  As soon as the object is destroyed, the session is
14308334c51SBrooks Davis /// terminated.
14408334c51SBrooks Davis ///
14508334c51SBrooks Davis /// \param db_filename_ The path to the database as seen at construction
14608334c51SBrooks Davis ///     time, if any, or none for in-memory databases.
14708334c51SBrooks Davis /// \param db_ Raw pointer to the C SQLite 3 object.
14808334c51SBrooks Davis /// \param owned_ Whether this instance will own the pointer or not.
database(const utils::optional<utils::fs::path> & db_filename_,void * db_,const bool owned_)14908334c51SBrooks Davis sqlite::database::database(
15008334c51SBrooks Davis     const utils::optional< utils::fs::path >& db_filename_, void* db_,
15108334c51SBrooks Davis     const bool owned_) :
15208334c51SBrooks Davis     _pimpl(new impl(db_filename_, static_cast< ::sqlite3* >(db_), owned_))
15308334c51SBrooks Davis {
15408334c51SBrooks Davis }
15508334c51SBrooks Davis 
15608334c51SBrooks Davis 
15708334c51SBrooks Davis /// Destructor for the SQLite 3 database.
15808334c51SBrooks Davis ///
15908334c51SBrooks Davis /// Closes the session unless it has already been closed by calling the
16008334c51SBrooks Davis /// close() method.  It is recommended to explicitly close the session in the
16108334c51SBrooks Davis /// code.
~database(void)16208334c51SBrooks Davis sqlite::database::~database(void)
16308334c51SBrooks Davis {
16408334c51SBrooks Davis }
16508334c51SBrooks Davis 
16608334c51SBrooks Davis 
16708334c51SBrooks Davis /// Opens a memory-based temporary SQLite database.
16808334c51SBrooks Davis ///
16908334c51SBrooks Davis /// \return An in-memory database instance.
17008334c51SBrooks Davis ///
17108334c51SBrooks Davis /// \throw std::bad_alloc If there is not enough memory to open the database.
17208334c51SBrooks Davis /// \throw api_error If there is any problem opening the database.
17308334c51SBrooks Davis sqlite::database
in_memory(void)17408334c51SBrooks Davis sqlite::database::in_memory(void)
17508334c51SBrooks Davis {
17608334c51SBrooks Davis     return database(none, impl::safe_open(":memory:", SQLITE_OPEN_READWRITE),
17708334c51SBrooks Davis                     true);
17808334c51SBrooks Davis }
17908334c51SBrooks Davis 
18008334c51SBrooks Davis 
18108334c51SBrooks Davis /// Opens a named on-disk SQLite database.
18208334c51SBrooks Davis ///
18308334c51SBrooks Davis /// \param file The path to the database file to be opened.  This does not
18408334c51SBrooks Davis ///     accept the values "" and ":memory:"; use temporary() and in_memory()
18508334c51SBrooks Davis ///     instead.
18608334c51SBrooks Davis /// \param open_flags The flags to be passed to the open routine.
18708334c51SBrooks Davis ///
18808334c51SBrooks Davis /// \return A file-backed database instance.
18908334c51SBrooks Davis ///
19008334c51SBrooks Davis /// \throw std::bad_alloc If there is not enough memory to open the database.
19108334c51SBrooks Davis /// \throw api_error If there is any problem opening the database.
19208334c51SBrooks Davis sqlite::database
open(const fs::path & file,int open_flags)19308334c51SBrooks Davis sqlite::database::open(const fs::path& file, int open_flags)
19408334c51SBrooks Davis {
19508334c51SBrooks Davis     PRE_MSG(!file.str().empty(), "Use database::temporary() instead");
19608334c51SBrooks Davis     PRE_MSG(file.str() != ":memory:", "Use database::in_memory() instead");
19708334c51SBrooks Davis 
19808334c51SBrooks Davis     int flags = 0;
19908334c51SBrooks Davis     if (open_flags & open_readonly) {
20008334c51SBrooks Davis         flags |= SQLITE_OPEN_READONLY;
20108334c51SBrooks Davis         open_flags &= ~open_readonly;
20208334c51SBrooks Davis     }
20308334c51SBrooks Davis     if (open_flags & open_readwrite) {
20408334c51SBrooks Davis         flags |= SQLITE_OPEN_READWRITE;
20508334c51SBrooks Davis         open_flags &= ~open_readwrite;
20608334c51SBrooks Davis     }
20708334c51SBrooks Davis     if (open_flags & open_create) {
20808334c51SBrooks Davis         flags |= SQLITE_OPEN_CREATE;
20908334c51SBrooks Davis         open_flags &= ~open_create;
21008334c51SBrooks Davis     }
21108334c51SBrooks Davis     PRE(open_flags == 0);
21208334c51SBrooks Davis 
21308334c51SBrooks Davis     return database(utils::make_optional(file),
21408334c51SBrooks Davis                     impl::safe_open(file.c_str(), flags), true);
21508334c51SBrooks Davis }
21608334c51SBrooks Davis 
21708334c51SBrooks Davis 
21808334c51SBrooks Davis /// Opens an unnamed on-disk SQLite database.
21908334c51SBrooks Davis ///
22008334c51SBrooks Davis /// \return A file-backed database instance.
22108334c51SBrooks Davis ///
22208334c51SBrooks Davis /// \throw std::bad_alloc If there is not enough memory to open the database.
22308334c51SBrooks Davis /// \throw api_error If there is any problem opening the database.
22408334c51SBrooks Davis sqlite::database
temporary(void)22508334c51SBrooks Davis sqlite::database::temporary(void)
22608334c51SBrooks Davis {
22708334c51SBrooks Davis     return database(none, impl::safe_open("", SQLITE_OPEN_READWRITE), true);
22808334c51SBrooks Davis }
22908334c51SBrooks Davis 
23008334c51SBrooks Davis 
23108334c51SBrooks Davis /// Gets the internal sqlite3 object.
23208334c51SBrooks Davis ///
23308334c51SBrooks Davis /// \return The raw SQLite 3 database.  This is returned as a void pointer to
23408334c51SBrooks Davis /// prevent including the sqlite3.h header file from our public interface.  The
23508334c51SBrooks Davis /// only way to call this method is by using the c_gate module, and c_gate takes
23608334c51SBrooks Davis /// care of casting this object to the appropriate type.
23708334c51SBrooks Davis void*
raw_database(void)23808334c51SBrooks Davis sqlite::database::raw_database(void)
23908334c51SBrooks Davis {
24008334c51SBrooks Davis     return _pimpl->db;
24108334c51SBrooks Davis }
24208334c51SBrooks Davis 
24308334c51SBrooks Davis 
24408334c51SBrooks Davis /// Terminates the connection to the database.
24508334c51SBrooks Davis ///
24608334c51SBrooks Davis /// It is recommended to call this instead of relying on the destructor to do
24708334c51SBrooks Davis /// the cleanup, but it is not a requirement to use close().
24808334c51SBrooks Davis ///
24908334c51SBrooks Davis /// \pre close() has not yet been called.
25008334c51SBrooks Davis void
close(void)25108334c51SBrooks Davis sqlite::database::close(void)
25208334c51SBrooks Davis {
25308334c51SBrooks Davis     _pimpl->close();
25408334c51SBrooks Davis }
25508334c51SBrooks Davis 
25608334c51SBrooks Davis 
25708334c51SBrooks Davis /// Returns the path to the connected database.
25808334c51SBrooks Davis ///
25908334c51SBrooks Davis /// It is OK to call this function on a live database object, even after close()
26008334c51SBrooks Davis /// has been called.  The returned value is consistent at all times.
26108334c51SBrooks Davis ///
26208334c51SBrooks Davis /// \return The path to the file that matches the connected database or none if
26308334c51SBrooks Davis /// the connection points to a transient database.
26408334c51SBrooks Davis const optional< fs::path >&
db_filename(void) const26508334c51SBrooks Davis sqlite::database::db_filename(void) const
26608334c51SBrooks Davis {
26708334c51SBrooks Davis     return _pimpl->db_filename;
26808334c51SBrooks Davis }
26908334c51SBrooks Davis 
27008334c51SBrooks Davis 
27108334c51SBrooks Davis /// Executes an arbitrary SQL string.
27208334c51SBrooks Davis ///
27308334c51SBrooks Davis /// As the documentation explains, this is unsafe.  The code should really be
27408334c51SBrooks Davis /// preparing statements and executing them step by step.  However, it is
27508334c51SBrooks Davis /// perfectly fine to use this function for, e.g. the initial creation of
27608334c51SBrooks Davis /// tables in a database and in tests.
27708334c51SBrooks Davis ///
27808334c51SBrooks Davis /// \param sql The SQL commands to be executed.
27908334c51SBrooks Davis ///
28008334c51SBrooks Davis /// \throw api_error If there is any problem while processing the SQL.
28108334c51SBrooks Davis void
exec(const std::string & sql)28208334c51SBrooks Davis sqlite::database::exec(const std::string& sql)
28308334c51SBrooks Davis {
28408334c51SBrooks Davis     const int error = ::sqlite3_exec(_pimpl->db, sql.c_str(), NULL, NULL, NULL);
28508334c51SBrooks Davis     if (error != SQLITE_OK)
28608334c51SBrooks Davis         throw api_error::from_database(*this, "sqlite3_exec");
28708334c51SBrooks Davis }
28808334c51SBrooks Davis 
28908334c51SBrooks Davis 
29008334c51SBrooks Davis /// Opens a new transaction.
29108334c51SBrooks Davis ///
29208334c51SBrooks Davis /// \return An object representing the state of the transaction.
29308334c51SBrooks Davis ///
29408334c51SBrooks Davis /// \throw api_error If there is any problem while opening the transaction.
29508334c51SBrooks Davis sqlite::transaction
begin_transaction(void)29608334c51SBrooks Davis sqlite::database::begin_transaction(void)
29708334c51SBrooks Davis {
29808334c51SBrooks Davis     exec("BEGIN TRANSACTION");
29908334c51SBrooks Davis     return transaction(*this);
30008334c51SBrooks Davis }
30108334c51SBrooks Davis 
30208334c51SBrooks Davis 
30308334c51SBrooks Davis /// Prepares a new statement.
30408334c51SBrooks Davis ///
30508334c51SBrooks Davis /// \param sql The SQL statement to prepare.
30608334c51SBrooks Davis ///
30708334c51SBrooks Davis /// \return The prepared statement.
30808334c51SBrooks Davis sqlite::statement
create_statement(const std::string & sql)30908334c51SBrooks Davis sqlite::database::create_statement(const std::string& sql)
31008334c51SBrooks Davis {
31108334c51SBrooks Davis     LD(F("Creating statement: %s") % sql);
31208334c51SBrooks Davis     sqlite3_stmt* stmt;
31308334c51SBrooks Davis     const int error = ::sqlite3_prepare_v2(_pimpl->db, sql.c_str(),
31408334c51SBrooks Davis                                            sql.length() + 1, &stmt, NULL);
31508334c51SBrooks Davis     if (error != SQLITE_OK)
31608334c51SBrooks Davis         throw api_error::from_database(*this, "sqlite3_prepare_v2");
31708334c51SBrooks Davis     return statement(*this, static_cast< void* >(stmt));
31808334c51SBrooks Davis }
31908334c51SBrooks Davis 
32008334c51SBrooks Davis 
32108334c51SBrooks Davis /// Returns the row identifier of the last insert.
32208334c51SBrooks Davis ///
32308334c51SBrooks Davis /// \return A row identifier.
32408334c51SBrooks Davis int64_t
last_insert_rowid(void)32508334c51SBrooks Davis sqlite::database::last_insert_rowid(void)
32608334c51SBrooks Davis {
32708334c51SBrooks Davis     return ::sqlite3_last_insert_rowid(_pimpl->db);
32808334c51SBrooks Davis }
329