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 "utils/sqlite/database.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/optional.ipp"
43 #include "utils/sanity.hpp"
44 #include "utils/sqlite/exceptions.hpp"
45 #include "utils/sqlite/statement.ipp"
46 #include "utils/sqlite/transaction.hpp"
48 namespace fs = utils::fs;
49 namespace sqlite = utils::sqlite;
52 using utils::optional;
55 /// Internal implementation for sqlite::database.
56 struct utils::sqlite::database::impl : utils::noncopyable {
57 /// Path to the database as seen at construction time.
58 optional< fs::path > db_filename;
60 /// The SQLite 3 internal database.
63 /// Whether we own the database or not (to decide if we close it).
68 /// \param db_filename_ The path to the database as seen at construction
69 /// time, if any, or none for in-memory databases. We should use
70 /// sqlite3_db_filename instead, but this function appeared in 3.7.10
71 /// and Ubuntu 12.04 LTS (which we support for Travis CI builds as of
72 /// 2015-07-07) ships with 3.7.9.
73 /// \param db_ The SQLite internal database.
74 /// \param owned_ Whether this object owns the db_ object or not. If it
75 /// does, the internal db_ will be released during destruction.
76 impl(optional< fs::path > db_filename_, ::sqlite3* db_, const bool owned_) :
77 db_filename(db_filename_), db(db_), owned(owned_)
83 /// It is important to keep this as part of the 'impl' class instead of the
84 /// container class. The 'impl' class is destroyed exactly once (because it
85 /// is managed by a shared_ptr) and thus releasing the resources here is
86 /// OK. However, the container class is potentially released many times,
87 /// which means that we would be double-freeing the internal object and
88 /// reusing invalid data.
91 if (owned && db != NULL)
95 /// Exception-safe version of sqlite3_open_v2.
97 /// \param file The path to the database file to be opened.
98 /// \param flags The flags to be passed to the open routine.
100 /// \return The opened database.
102 /// \throw std::bad_alloc If there is not enough memory to open the
104 /// \throw api_error If there is any problem opening the database.
106 safe_open(const char* file, const int flags)
109 const int error = ::sqlite3_open_v2(file, &db, flags, NULL);
110 if (error != SQLITE_OK) {
112 throw std::bad_alloc();
114 sqlite::database error_db(utils::make_optional(fs::path(file)),
116 throw sqlite::api_error::from_database(error_db,
124 /// Shared code for the public close() method.
129 int error = ::sqlite3_close(db);
130 // For now, let's consider a return of SQLITE_BUSY an error. We should
131 // not be trying to close a busy database in our code. Maybe revisit
132 // this later to raise busy errors as exceptions.
133 PRE(error == SQLITE_OK);
139 /// Initializes the SQLite database.
141 /// You must share the same database object alongside the lifetime of your
142 /// SQLite session. As soon as the object is destroyed, the session is
145 /// \param db_filename_ The path to the database as seen at construction
146 /// time, if any, or none for in-memory databases.
147 /// \param db_ Raw pointer to the C SQLite 3 object.
148 /// \param owned_ Whether this instance will own the pointer or not.
149 sqlite::database::database(
150 const utils::optional< utils::fs::path >& db_filename_, void* db_,
152 _pimpl(new impl(db_filename_, static_cast< ::sqlite3* >(db_), owned_))
157 /// Destructor for the SQLite 3 database.
159 /// Closes the session unless it has already been closed by calling the
160 /// close() method. It is recommended to explicitly close the session in the
162 sqlite::database::~database(void)
167 /// Opens a memory-based temporary SQLite database.
169 /// \return An in-memory database instance.
171 /// \throw std::bad_alloc If there is not enough memory to open the database.
172 /// \throw api_error If there is any problem opening the database.
174 sqlite::database::in_memory(void)
176 return database(none, impl::safe_open(":memory:", SQLITE_OPEN_READWRITE),
181 /// Opens a named on-disk SQLite database.
183 /// \param file The path to the database file to be opened. This does not
184 /// accept the values "" and ":memory:"; use temporary() and in_memory()
186 /// \param open_flags The flags to be passed to the open routine.
188 /// \return A file-backed database instance.
190 /// \throw std::bad_alloc If there is not enough memory to open the database.
191 /// \throw api_error If there is any problem opening the database.
193 sqlite::database::open(const fs::path& file, int open_flags)
195 PRE_MSG(!file.str().empty(), "Use database::temporary() instead");
196 PRE_MSG(file.str() != ":memory:", "Use database::in_memory() instead");
199 if (open_flags & open_readonly) {
200 flags |= SQLITE_OPEN_READONLY;
201 open_flags &= ~open_readonly;
203 if (open_flags & open_readwrite) {
204 flags |= SQLITE_OPEN_READWRITE;
205 open_flags &= ~open_readwrite;
207 if (open_flags & open_create) {
208 flags |= SQLITE_OPEN_CREATE;
209 open_flags &= ~open_create;
211 PRE(open_flags == 0);
213 return database(utils::make_optional(file),
214 impl::safe_open(file.c_str(), flags), true);
218 /// Opens an unnamed on-disk SQLite database.
220 /// \return A file-backed database instance.
222 /// \throw std::bad_alloc If there is not enough memory to open the database.
223 /// \throw api_error If there is any problem opening the database.
225 sqlite::database::temporary(void)
227 return database(none, impl::safe_open("", SQLITE_OPEN_READWRITE), true);
231 /// Gets the internal sqlite3 object.
233 /// \return The raw SQLite 3 database. This is returned as a void pointer to
234 /// prevent including the sqlite3.h header file from our public interface. The
235 /// only way to call this method is by using the c_gate module, and c_gate takes
236 /// care of casting this object to the appropriate type.
238 sqlite::database::raw_database(void)
244 /// Terminates the connection to the database.
246 /// It is recommended to call this instead of relying on the destructor to do
247 /// the cleanup, but it is not a requirement to use close().
249 /// \pre close() has not yet been called.
251 sqlite::database::close(void)
257 /// Returns the path to the connected database.
259 /// It is OK to call this function on a live database object, even after close()
260 /// has been called. The returned value is consistent at all times.
262 /// \return The path to the file that matches the connected database or none if
263 /// the connection points to a transient database.
264 const optional< fs::path >&
265 sqlite::database::db_filename(void) const
267 return _pimpl->db_filename;
271 /// Executes an arbitrary SQL string.
273 /// As the documentation explains, this is unsafe. The code should really be
274 /// preparing statements and executing them step by step. However, it is
275 /// perfectly fine to use this function for, e.g. the initial creation of
276 /// tables in a database and in tests.
278 /// \param sql The SQL commands to be executed.
280 /// \throw api_error If there is any problem while processing the SQL.
282 sqlite::database::exec(const std::string& sql)
284 const int error = ::sqlite3_exec(_pimpl->db, sql.c_str(), NULL, NULL, NULL);
285 if (error != SQLITE_OK)
286 throw api_error::from_database(*this, "sqlite3_exec");
290 /// Opens a new transaction.
292 /// \return An object representing the state of the transaction.
294 /// \throw api_error If there is any problem while opening the transaction.
296 sqlite::database::begin_transaction(void)
298 exec("BEGIN TRANSACTION");
299 return transaction(*this);
303 /// Prepares a new statement.
305 /// \param sql The SQL statement to prepare.
307 /// \return The prepared statement.
309 sqlite::database::create_statement(const std::string& sql)
311 LD(F("Creating statement: %s") % sql);
313 const int error = ::sqlite3_prepare_v2(_pimpl->db, sql.c_str(),
314 sql.length() + 1, &stmt, NULL);
315 if (error != SQLITE_OK)
316 throw api_error::from_database(*this, "sqlite3_prepare_v2");
317 return statement(*this, static_cast< void* >(stmt));
321 /// Returns the row identifier of the last insert.
323 /// \return A row identifier.
325 sqlite::database::last_insert_rowid(void)
327 return ::sqlite3_last_insert_rowid(_pimpl->db);