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