111be35a1SLionel Sambuc // Copyright 2011 Google Inc.
211be35a1SLionel Sambuc // All rights reserved.
311be35a1SLionel Sambuc //
411be35a1SLionel Sambuc // Redistribution and use in source and binary forms, with or without
511be35a1SLionel Sambuc // modification, are permitted provided that the following conditions are
611be35a1SLionel Sambuc // met:
711be35a1SLionel Sambuc //
811be35a1SLionel Sambuc // * Redistributions of source code must retain the above copyright
911be35a1SLionel Sambuc // notice, this list of conditions and the following disclaimer.
1011be35a1SLionel Sambuc // * Redistributions in binary form must reproduce the above copyright
1111be35a1SLionel Sambuc // notice, this list of conditions and the following disclaimer in the
1211be35a1SLionel Sambuc // documentation and/or other materials provided with the distribution.
1311be35a1SLionel Sambuc // * Neither the name of Google Inc. nor the names of its contributors
1411be35a1SLionel Sambuc // may be used to endorse or promote products derived from this software
1511be35a1SLionel Sambuc // without specific prior written permission.
1611be35a1SLionel Sambuc //
1711be35a1SLionel Sambuc // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1811be35a1SLionel Sambuc // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1911be35a1SLionel Sambuc // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2011be35a1SLionel Sambuc // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2111be35a1SLionel Sambuc // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2211be35a1SLionel Sambuc // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2311be35a1SLionel Sambuc // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2411be35a1SLionel Sambuc // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2511be35a1SLionel Sambuc // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2611be35a1SLionel Sambuc // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2711be35a1SLionel Sambuc // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2811be35a1SLionel Sambuc
2911be35a1SLionel Sambuc #include "utils/sqlite/database.hpp"
3011be35a1SLionel Sambuc
3111be35a1SLionel Sambuc extern "C" {
3211be35a1SLionel Sambuc #include <sqlite3.h>
3311be35a1SLionel Sambuc }
3411be35a1SLionel Sambuc
3511be35a1SLionel Sambuc #include <stdexcept>
3611be35a1SLionel Sambuc
3711be35a1SLionel Sambuc #include "utils/format/macros.hpp"
3811be35a1SLionel Sambuc #include "utils/logging/macros.hpp"
3911be35a1SLionel Sambuc #include "utils/sanity.hpp"
4011be35a1SLionel Sambuc #include "utils/sqlite/exceptions.hpp"
4111be35a1SLionel Sambuc #include "utils/sqlite/statement.ipp"
4211be35a1SLionel Sambuc #include "utils/sqlite/transaction.hpp"
4311be35a1SLionel Sambuc
4411be35a1SLionel Sambuc namespace sqlite = utils::sqlite;
4511be35a1SLionel Sambuc
4611be35a1SLionel Sambuc
4711be35a1SLionel Sambuc /// Internal implementation for sqlite::database.
4811be35a1SLionel Sambuc struct utils::sqlite::database::impl {
4911be35a1SLionel Sambuc /// The SQLite 3 internal database.
5011be35a1SLionel Sambuc ::sqlite3* db;
5111be35a1SLionel Sambuc
5211be35a1SLionel Sambuc /// Whether we own the database or not (to decide if we close it).
5311be35a1SLionel Sambuc bool owned;
5411be35a1SLionel Sambuc
5511be35a1SLionel Sambuc /// Constructor.
5611be35a1SLionel Sambuc ///
5711be35a1SLionel Sambuc /// \param db_ The SQLite internal database.
5811be35a1SLionel Sambuc /// \param owned_ Whether this object owns the db_ object or not. If it
5911be35a1SLionel Sambuc /// does, the internal db_ will be released during destruction.
implutils::sqlite::database::impl6011be35a1SLionel Sambuc impl(::sqlite3* db_, const bool owned_) :
6111be35a1SLionel Sambuc db(db_),
6211be35a1SLionel Sambuc owned(owned_)
6311be35a1SLionel Sambuc {
6411be35a1SLionel Sambuc }
6511be35a1SLionel Sambuc
6611be35a1SLionel Sambuc /// Destructor.
6711be35a1SLionel Sambuc ///
6811be35a1SLionel Sambuc /// It is important to keep this as part of the 'impl' class instead of the
6911be35a1SLionel Sambuc /// container class. The 'impl' class is destroyed exactly once (because it
7011be35a1SLionel Sambuc /// is managed by a shared_ptr) and thus releasing the resources here is
7111be35a1SLionel Sambuc /// OK. However, the container class is potentially released many times,
7211be35a1SLionel Sambuc /// which means that we would be double-freeing the internal object and
7311be35a1SLionel Sambuc /// reusing invalid data.
~implutils::sqlite::database::impl7411be35a1SLionel Sambuc ~impl(void)
7511be35a1SLionel Sambuc {
7611be35a1SLionel Sambuc if (owned && db != NULL)
7711be35a1SLionel Sambuc close();
7811be35a1SLionel Sambuc }
7911be35a1SLionel Sambuc
8011be35a1SLionel Sambuc /// Exception-safe version of sqlite3_open_v2.
8111be35a1SLionel Sambuc ///
8211be35a1SLionel Sambuc /// \param file The path to the database file to be opened.
8311be35a1SLionel Sambuc /// \param flags The flags to be passed to the open routine.
8411be35a1SLionel Sambuc ///
8511be35a1SLionel Sambuc /// \return The opened database.
8611be35a1SLionel Sambuc ///
8711be35a1SLionel Sambuc /// \throw std::bad_alloc If there is not enough memory to open the
8811be35a1SLionel Sambuc /// database.
8911be35a1SLionel Sambuc /// \throw api_error If there is any problem opening the database.
9011be35a1SLionel Sambuc static ::sqlite3*
safe_openutils::sqlite::database::impl9111be35a1SLionel Sambuc safe_open(const char* file, const int flags)
9211be35a1SLionel Sambuc {
9311be35a1SLionel Sambuc ::sqlite3* db;
9411be35a1SLionel Sambuc const int error = ::sqlite3_open_v2(file, &db, flags, NULL);
9511be35a1SLionel Sambuc if (error != SQLITE_OK) {
9611be35a1SLionel Sambuc if (db == NULL)
9711be35a1SLionel Sambuc throw std::bad_alloc();
9811be35a1SLionel Sambuc else {
9911be35a1SLionel Sambuc sqlite::database error_db(db, true);
10011be35a1SLionel Sambuc throw sqlite::api_error::from_database(error_db,
10111be35a1SLionel Sambuc "sqlite3_open_v2");
10211be35a1SLionel Sambuc }
10311be35a1SLionel Sambuc }
10411be35a1SLionel Sambuc INV(db != NULL);
10511be35a1SLionel Sambuc return db;
10611be35a1SLionel Sambuc }
10711be35a1SLionel Sambuc
10811be35a1SLionel Sambuc /// Shared code for the public close() method.
10911be35a1SLionel Sambuc void
closeutils::sqlite::database::impl11011be35a1SLionel Sambuc close(void)
11111be35a1SLionel Sambuc {
11211be35a1SLionel Sambuc PRE(db != NULL);
113*3260d16fSLionel Sambuc #if defined(__minix) && !defined(NDEBUG)
114*3260d16fSLionel Sambuc int error =
115*3260d16fSLionel Sambuc #endif /* defined(__minix) && !defined(NDEBUG) */
116*3260d16fSLionel Sambuc ::sqlite3_close(db);
11711be35a1SLionel Sambuc // For now, let's consider a return of SQLITE_BUSY an error. We should
11811be35a1SLionel Sambuc // not be trying to close a busy database in our code. Maybe revisit
11911be35a1SLionel Sambuc // this later to raise busy errors as exceptions.
12011be35a1SLionel Sambuc PRE(error == SQLITE_OK);
12111be35a1SLionel Sambuc db = NULL;
12211be35a1SLionel Sambuc }
12311be35a1SLionel Sambuc };
12411be35a1SLionel Sambuc
12511be35a1SLionel Sambuc
12611be35a1SLionel Sambuc /// Initializes the SQLite database.
12711be35a1SLionel Sambuc ///
12811be35a1SLionel Sambuc /// You must share the same database object alongside the lifetime of your
12911be35a1SLionel Sambuc /// SQLite session. As soon as the object is destroyed, the session is
13011be35a1SLionel Sambuc /// terminated.
13111be35a1SLionel Sambuc ///
13211be35a1SLionel Sambuc /// \param db_ Raw pointer to the C SQLite 3 object.
13311be35a1SLionel Sambuc /// \param owned_ Whether this instance will own the pointer or not.
database(void * db_,const bool owned_)13411be35a1SLionel Sambuc sqlite::database::database(void* db_, const bool owned_) :
13511be35a1SLionel Sambuc _pimpl(new impl(static_cast< ::sqlite3* >(db_), owned_))
13611be35a1SLionel Sambuc {
13711be35a1SLionel Sambuc }
13811be35a1SLionel Sambuc
13911be35a1SLionel Sambuc
14011be35a1SLionel Sambuc /// Destructor for the SQLite 3 database.
14111be35a1SLionel Sambuc ///
14211be35a1SLionel Sambuc /// Closes the session unless it has already been closed by calling the
14311be35a1SLionel Sambuc /// close() method. It is recommended to explicitly close the session in the
14411be35a1SLionel Sambuc /// code.
~database(void)14511be35a1SLionel Sambuc sqlite::database::~database(void)
14611be35a1SLionel Sambuc {
14711be35a1SLionel Sambuc }
14811be35a1SLionel Sambuc
14911be35a1SLionel Sambuc
15011be35a1SLionel Sambuc /// Opens a memory-based temporary SQLite database.
15111be35a1SLionel Sambuc ///
15211be35a1SLionel Sambuc /// \return An in-memory database instance.
15311be35a1SLionel Sambuc ///
15411be35a1SLionel Sambuc /// \throw std::bad_alloc If there is not enough memory to open the database.
15511be35a1SLionel Sambuc /// \throw api_error If there is any problem opening the database.
15611be35a1SLionel Sambuc sqlite::database
in_memory(void)15711be35a1SLionel Sambuc sqlite::database::in_memory(void)
15811be35a1SLionel Sambuc {
15911be35a1SLionel Sambuc return database(impl::safe_open(":memory:", SQLITE_OPEN_READWRITE), true);
16011be35a1SLionel Sambuc }
16111be35a1SLionel Sambuc
16211be35a1SLionel Sambuc
16311be35a1SLionel Sambuc /// Opens a named on-disk SQLite database.
16411be35a1SLionel Sambuc ///
16511be35a1SLionel Sambuc /// \param file The path to the database file to be opened. This does not
16611be35a1SLionel Sambuc /// accept the values "" and ":memory:"; use temporary() and in_memory()
16711be35a1SLionel Sambuc /// instead.
16811be35a1SLionel Sambuc /// \param open_flags The flags to be passed to the open routine.
16911be35a1SLionel Sambuc ///
17011be35a1SLionel Sambuc /// \return A file-backed database instance.
17111be35a1SLionel Sambuc ///
17211be35a1SLionel Sambuc /// \throw std::bad_alloc If there is not enough memory to open the database.
17311be35a1SLionel Sambuc /// \throw api_error If there is any problem opening the database.
17411be35a1SLionel Sambuc sqlite::database
open(const fs::path & file,int open_flags)17511be35a1SLionel Sambuc sqlite::database::open(const fs::path& file, int open_flags)
17611be35a1SLionel Sambuc {
17711be35a1SLionel Sambuc PRE_MSG(!file.str().empty(), "Use database::temporary() instead");
17811be35a1SLionel Sambuc PRE_MSG(file.str() != ":memory:", "Use database::in_memory() instead");
17911be35a1SLionel Sambuc
18011be35a1SLionel Sambuc int flags = 0;
18111be35a1SLionel Sambuc if (open_flags & open_readonly) {
18211be35a1SLionel Sambuc flags |= SQLITE_OPEN_READONLY;
18311be35a1SLionel Sambuc open_flags &= ~open_readonly;
18411be35a1SLionel Sambuc }
18511be35a1SLionel Sambuc if (open_flags & open_readwrite) {
18611be35a1SLionel Sambuc flags |= SQLITE_OPEN_READWRITE;
18711be35a1SLionel Sambuc open_flags &= ~open_readwrite;
18811be35a1SLionel Sambuc }
18911be35a1SLionel Sambuc if (open_flags & open_create) {
19011be35a1SLionel Sambuc flags |= SQLITE_OPEN_CREATE;
19111be35a1SLionel Sambuc open_flags &= ~open_create;
19211be35a1SLionel Sambuc }
19311be35a1SLionel Sambuc PRE(open_flags == 0);
19411be35a1SLionel Sambuc
19511be35a1SLionel Sambuc return database(impl::safe_open(file.c_str(), flags), true);
19611be35a1SLionel Sambuc }
19711be35a1SLionel Sambuc
19811be35a1SLionel Sambuc
19911be35a1SLionel Sambuc /// Opens an unnamed on-disk SQLite database.
20011be35a1SLionel Sambuc ///
20111be35a1SLionel Sambuc /// \return A file-backed database instance.
20211be35a1SLionel Sambuc ///
20311be35a1SLionel Sambuc /// \throw std::bad_alloc If there is not enough memory to open the database.
20411be35a1SLionel Sambuc /// \throw api_error If there is any problem opening the database.
20511be35a1SLionel Sambuc sqlite::database
temporary(void)20611be35a1SLionel Sambuc sqlite::database::temporary(void)
20711be35a1SLionel Sambuc {
20811be35a1SLionel Sambuc return database(impl::safe_open("", SQLITE_OPEN_READWRITE), true);
20911be35a1SLionel Sambuc }
21011be35a1SLionel Sambuc
21111be35a1SLionel Sambuc
21211be35a1SLionel Sambuc /// Gets the internal sqlite3 object.
21311be35a1SLionel Sambuc ///
21411be35a1SLionel Sambuc /// \return The raw SQLite 3 database. This is returned as a void pointer to
21511be35a1SLionel Sambuc /// prevent including the sqlite3.h header file from our public interface. The
21611be35a1SLionel Sambuc /// only way to call this method is by using the c_gate module, and c_gate takes
21711be35a1SLionel Sambuc /// care of casting this object to the appropriate type.
21811be35a1SLionel Sambuc void*
raw_database(void)21911be35a1SLionel Sambuc sqlite::database::raw_database(void)
22011be35a1SLionel Sambuc {
22111be35a1SLionel Sambuc return _pimpl->db;
22211be35a1SLionel Sambuc }
22311be35a1SLionel Sambuc
22411be35a1SLionel Sambuc
22511be35a1SLionel Sambuc /// Terminates the connection to the database.
22611be35a1SLionel Sambuc ///
22711be35a1SLionel Sambuc /// It is recommended to call this instead of relying on the destructor to do
22811be35a1SLionel Sambuc /// the cleanup, but it is not a requirement to use close().
22911be35a1SLionel Sambuc ///
23011be35a1SLionel Sambuc /// \pre close() has not yet been called.
23111be35a1SLionel Sambuc void
close(void)23211be35a1SLionel Sambuc sqlite::database::close(void)
23311be35a1SLionel Sambuc {
23411be35a1SLionel Sambuc _pimpl->close();
23511be35a1SLionel Sambuc }
23611be35a1SLionel Sambuc
23711be35a1SLionel Sambuc
23811be35a1SLionel Sambuc /// Executes an arbitrary SQL string.
23911be35a1SLionel Sambuc ///
24011be35a1SLionel Sambuc /// As the documentation explains, this is unsafe. The code should really be
24111be35a1SLionel Sambuc /// preparing statements and executing them step by step. However, it is
24211be35a1SLionel Sambuc /// perfectly fine to use this function for, e.g. the initial creation of
24311be35a1SLionel Sambuc /// tables in a database and in tests.
24411be35a1SLionel Sambuc ///
24511be35a1SLionel Sambuc /// \param sql The SQL commands to be executed.
24611be35a1SLionel Sambuc ///
24711be35a1SLionel Sambuc /// \throw api_error If there is any problem while processing the SQL.
24811be35a1SLionel Sambuc void
exec(const std::string & sql)24911be35a1SLionel Sambuc sqlite::database::exec(const std::string& sql)
25011be35a1SLionel Sambuc {
25111be35a1SLionel Sambuc const int error = ::sqlite3_exec(_pimpl->db, sql.c_str(), NULL, NULL, NULL);
25211be35a1SLionel Sambuc if (error != SQLITE_OK)
25311be35a1SLionel Sambuc throw api_error::from_database(*this, "sqlite3_exec");
25411be35a1SLionel Sambuc }
25511be35a1SLionel Sambuc
25611be35a1SLionel Sambuc
25711be35a1SLionel Sambuc /// Opens a new transaction.
25811be35a1SLionel Sambuc ///
25911be35a1SLionel Sambuc /// \return An object representing the state of the transaction.
26011be35a1SLionel Sambuc ///
26111be35a1SLionel Sambuc /// \throw api_error If there is any problem while opening the transaction.
26211be35a1SLionel Sambuc sqlite::transaction
begin_transaction(void)26311be35a1SLionel Sambuc sqlite::database::begin_transaction(void)
26411be35a1SLionel Sambuc {
26511be35a1SLionel Sambuc exec("BEGIN TRANSACTION");
26611be35a1SLionel Sambuc return transaction(*this);
26711be35a1SLionel Sambuc }
26811be35a1SLionel Sambuc
26911be35a1SLionel Sambuc
27011be35a1SLionel Sambuc /// Prepares a new statement.
27111be35a1SLionel Sambuc ///
27211be35a1SLionel Sambuc /// \param sql The SQL statement to prepare.
27311be35a1SLionel Sambuc ///
27411be35a1SLionel Sambuc /// \return The prepared statement.
27511be35a1SLionel Sambuc sqlite::statement
create_statement(const std::string & sql)27611be35a1SLionel Sambuc sqlite::database::create_statement(const std::string& sql)
27711be35a1SLionel Sambuc {
27811be35a1SLionel Sambuc LD(F("Creating statement: %s") % sql);
27911be35a1SLionel Sambuc sqlite3_stmt* stmt;
28011be35a1SLionel Sambuc const int error = ::sqlite3_prepare_v2(_pimpl->db, sql.c_str(),
28111be35a1SLionel Sambuc sql.length() + 1, &stmt, NULL);
28211be35a1SLionel Sambuc if (error != SQLITE_OK)
28311be35a1SLionel Sambuc throw api_error::from_database(*this, "sqlite3_prepare_v2");
28411be35a1SLionel Sambuc return statement(*this, static_cast< void* >(stmt));
28511be35a1SLionel Sambuc }
28611be35a1SLionel Sambuc
28711be35a1SLionel Sambuc
28811be35a1SLionel Sambuc /// Returns the row identifier of the last insert.
28911be35a1SLionel Sambuc ///
29011be35a1SLionel Sambuc /// \return A row identifier.
29111be35a1SLionel Sambuc int64_t
last_insert_rowid(void)29211be35a1SLionel Sambuc sqlite::database::last_insert_rowid(void)
29311be35a1SLionel Sambuc {
29411be35a1SLionel Sambuc return ::sqlite3_last_insert_rowid(_pimpl->db);
29511be35a1SLionel Sambuc }
296