1 // Copyright 2011 Google Inc. 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 extern "C" { 39 #include <stdint.h> 40 } 41 42 #include <cstddef> 43 44 #include "utils/fs/path.hpp" 45 #include "utils/shared_ptr.hpp" 46 47 namespace utils { 48 namespace sqlite { 49 50 51 class database_c_gate; 52 class statement; 53 class transaction; 54 55 56 /// Constant for the database::open flags: open in read-only mode. 57 static const int open_readonly = 1 << 0; 58 /// Constant for the database::open flags: open in read-write mode. 59 static const int open_readwrite = 1 << 1; 60 /// Constant for the database::open flags: create on open. 61 static const int open_create = 1 << 2; 62 63 64 /// A RAII model for the SQLite 3 database. 65 /// 66 /// This class holds the database of the SQLite 3 interface during its existence 67 /// and provides wrappers around several SQLite 3 library functions that operate 68 /// on such database. 69 /// 70 /// These wrapper functions differ from the C versions in that they use the 71 /// implicit database hold by the class, they use C++ types where appropriate 72 /// and they use exceptions to report errors. 73 /// 74 /// The wrappers intend to be as lightweight as possible but, in some 75 /// situations, they are pretty complex because of the workarounds needed to 76 /// make the SQLite 3 more C++ friendly. We prefer a clean C++ interface over 77 /// optimal efficiency, so this is OK. 78 class database { 79 struct impl; 80 81 /// Pointer to the shared internal implementation. 82 std::shared_ptr< impl > _pimpl; 83 84 friend class database_c_gate; 85 database(void*, const bool); 86 void* raw_database(void); 87 88 public: 89 ~database(void); 90 91 static database in_memory(void); 92 static database open(const fs::path&, int); 93 static database temporary(void); 94 void close(void); 95 96 void exec(const std::string&); 97 98 transaction begin_transaction(void); 99 statement create_statement(const std::string&); 100 101 int64_t last_insert_rowid(void); 102 }; 103 104 105 } // namespace sqlite 106 } // namespace utils 107 108 #endif // !defined(UTILS_SQLITE_DATABASE_HPP) 109