1 // Copyright 2011 The Kyua Authors. 2 // All rights reserved. 3 // 4 // Redistribution and use in source and binary forms, with or without 5 // modification, are permitted provided that the following conditions are 6 // met: 7 // 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. 16 // 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. 28 29 /// \file utils/sqlite/database.hpp 30 /// Wrapper classes and utilities for the SQLite database state. 31 /// 32 /// This module contains thin RAII wrappers around the SQLite 3 structures 33 /// representing the database, and lightweight. 34 35 #if !defined(UTILS_SQLITE_DATABASE_HPP) 36 #define UTILS_SQLITE_DATABASE_HPP 37 38 #include "utils/sqlite/database_fwd.hpp" 39 40 extern "C" { 41 #include <stdint.h> 42 } 43 44 #include <cstddef> 45 #include <memory> 46 #include <string> 47 48 #include "utils/fs/path_fwd.hpp" 49 #include "utils/optional_fwd.hpp" 50 #include "utils/sqlite/c_gate_fwd.hpp" 51 #include "utils/sqlite/statement_fwd.hpp" 52 #include "utils/sqlite/transaction_fwd.hpp" 53 54 namespace utils { 55 namespace sqlite { 56 57 58 /// Constant for the database::open flags: open in read-only mode. 59 static const int open_readonly = 1 << 0; 60 /// Constant for the database::open flags: open in read-write mode. 61 static const int open_readwrite = 1 << 1; 62 /// Constant for the database::open flags: create on open. 63 static const int open_create = 1 << 2; 64 65 66 /// A RAII model for the SQLite 3 database. 67 /// 68 /// This class holds the database of the SQLite 3 interface during its existence 69 /// and provides wrappers around several SQLite 3 library functions that operate 70 /// on such database. 71 /// 72 /// These wrapper functions differ from the C versions in that they use the 73 /// implicit database hold by the class, they use C++ types where appropriate 74 /// and they use exceptions to report errors. 75 /// 76 /// The wrappers intend to be as lightweight as possible but, in some 77 /// situations, they are pretty complex because of the workarounds needed to 78 /// make the SQLite 3 more C++ friendly. We prefer a clean C++ interface over 79 /// optimal efficiency, so this is OK. 80 class database { 81 struct impl; 82 83 /// Pointer to the shared internal implementation. 84 std::shared_ptr< impl > _pimpl; 85 86 friend class database_c_gate; 87 database(const utils::optional< utils::fs::path >&, void*, const bool); 88 void* raw_database(void); 89 90 public: 91 ~database(void); 92 93 static database in_memory(void); 94 static database open(const fs::path&, int); 95 static database temporary(void); 96 void close(void); 97 98 const utils::optional< utils::fs::path >& db_filename(void) const; 99 100 void exec(const std::string&); 101 102 transaction begin_transaction(void); 103 statement create_statement(const std::string&); 104 105 int64_t last_insert_rowid(void); 106 }; 107 108 109 } // namespace sqlite 110 } // namespace utils 111 112 #endif // !defined(UTILS_SQLITE_DATABASE_HPP) 113