Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/contrib/kyua/utils/sqlite/database.cpp
48178 views
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
#include "utils/sqlite/database.hpp"
30
31
extern "C" {
32
#include <sqlite3.h>
33
}
34
35
#include <cstring>
36
#include <stdexcept>
37
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"
47
48
namespace fs = utils::fs;
49
namespace sqlite = utils::sqlite;
50
51
using utils::none;
52
using utils::optional;
53
54
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;
59
60
/// The SQLite 3 internal database.
61
::sqlite3* db;
62
63
/// Whether we own the database or not (to decide if we close it).
64
bool owned;
65
66
/// Constructor.
67
///
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_)
78
{
79
}
80
81
/// Destructor.
82
///
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.
89
~impl(void)
90
{
91
if (owned && db != NULL)
92
close();
93
}
94
95
/// Exception-safe version of sqlite3_open_v2.
96
///
97
/// \param file The path to the database file to be opened.
98
/// \param flags The flags to be passed to the open routine.
99
///
100
/// \return The opened database.
101
///
102
/// \throw std::bad_alloc If there is not enough memory to open the
103
/// database.
104
/// \throw api_error If there is any problem opening the database.
105
static ::sqlite3*
106
safe_open(const char* file, const int flags)
107
{
108
::sqlite3* db;
109
const int error = ::sqlite3_open_v2(file, &db, flags, NULL);
110
if (error != SQLITE_OK) {
111
if (db == NULL)
112
throw std::bad_alloc();
113
else {
114
sqlite::database error_db(utils::make_optional(fs::path(file)),
115
db, true);
116
throw sqlite::api_error::from_database(error_db,
117
"sqlite3_open_v2");
118
}
119
}
120
INV(db != NULL);
121
return db;
122
}
123
124
/// Shared code for the public close() method.
125
void
126
close(void)
127
{
128
PRE(db != NULL);
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);
134
db = NULL;
135
}
136
};
137
138
139
/// Initializes the SQLite database.
140
///
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
143
/// terminated.
144
///
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_,
151
const bool owned_) :
152
_pimpl(new impl(db_filename_, static_cast< ::sqlite3* >(db_), owned_))
153
{
154
}
155
156
157
/// Destructor for the SQLite 3 database.
158
///
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
161
/// code.
162
sqlite::database::~database(void)
163
{
164
}
165
166
167
/// Opens a memory-based temporary SQLite database.
168
///
169
/// \return An in-memory database instance.
170
///
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.
173
sqlite::database
174
sqlite::database::in_memory(void)
175
{
176
return database(none, impl::safe_open(":memory:", SQLITE_OPEN_READWRITE),
177
true);
178
}
179
180
181
/// Opens a named on-disk SQLite database.
182
///
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()
185
/// instead.
186
/// \param open_flags The flags to be passed to the open routine.
187
///
188
/// \return A file-backed database instance.
189
///
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.
192
sqlite::database
193
sqlite::database::open(const fs::path& file, int open_flags)
194
{
195
PRE_MSG(!file.str().empty(), "Use database::temporary() instead");
196
PRE_MSG(file.str() != ":memory:", "Use database::in_memory() instead");
197
198
int flags = 0;
199
if (open_flags & open_readonly) {
200
flags |= SQLITE_OPEN_READONLY;
201
open_flags &= ~open_readonly;
202
}
203
if (open_flags & open_readwrite) {
204
flags |= SQLITE_OPEN_READWRITE;
205
open_flags &= ~open_readwrite;
206
}
207
if (open_flags & open_create) {
208
flags |= SQLITE_OPEN_CREATE;
209
open_flags &= ~open_create;
210
}
211
PRE(open_flags == 0);
212
213
return database(utils::make_optional(file),
214
impl::safe_open(file.c_str(), flags), true);
215
}
216
217
218
/// Opens an unnamed on-disk SQLite database.
219
///
220
/// \return A file-backed database instance.
221
///
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.
224
sqlite::database
225
sqlite::database::temporary(void)
226
{
227
return database(none, impl::safe_open("", SQLITE_OPEN_READWRITE), true);
228
}
229
230
231
/// Gets the internal sqlite3 object.
232
///
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.
237
void*
238
sqlite::database::raw_database(void)
239
{
240
return _pimpl->db;
241
}
242
243
244
/// Terminates the connection to the database.
245
///
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().
248
///
249
/// \pre close() has not yet been called.
250
void
251
sqlite::database::close(void)
252
{
253
_pimpl->close();
254
}
255
256
257
/// Returns the path to the connected database.
258
///
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.
261
///
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
266
{
267
return _pimpl->db_filename;
268
}
269
270
271
/// Executes an arbitrary SQL string.
272
///
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.
277
///
278
/// \param sql The SQL commands to be executed.
279
///
280
/// \throw api_error If there is any problem while processing the SQL.
281
void
282
sqlite::database::exec(const std::string& sql)
283
{
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");
287
}
288
289
290
/// Opens a new transaction.
291
///
292
/// \return An object representing the state of the transaction.
293
///
294
/// \throw api_error If there is any problem while opening the transaction.
295
sqlite::transaction
296
sqlite::database::begin_transaction(void)
297
{
298
exec("BEGIN TRANSACTION");
299
return transaction(*this);
300
}
301
302
303
/// Prepares a new statement.
304
///
305
/// \param sql The SQL statement to prepare.
306
///
307
/// \return The prepared statement.
308
sqlite::statement
309
sqlite::database::create_statement(const std::string& sql)
310
{
311
LD(F("Creating statement: %s") % sql);
312
sqlite3_stmt* stmt;
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));
318
}
319
320
321
/// Returns the row identifier of the last insert.
322
///
323
/// \return A row identifier.
324
int64_t
325
sqlite::database::last_insert_rowid(void)
326
{
327
return ::sqlite3_last_insert_rowid(_pimpl->db);
328
}
329
330