Path: blob/main/contrib/kyua/utils/sqlite/database.hpp
48199 views
// Copyright 2011 The Kyua Authors.1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions are5// met:6//7// * Redistributions of source code must retain the above copyright8// notice, this list of conditions and the following disclaimer.9// * Redistributions in binary form must reproduce the above copyright10// notice, this list of conditions and the following disclaimer in the11// documentation and/or other materials provided with the distribution.12// * Neither the name of Google Inc. nor the names of its contributors13// may be used to endorse or promote products derived from this software14// without specific prior written permission.15//16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2728/// \file utils/sqlite/database.hpp29/// Wrapper classes and utilities for the SQLite database state.30///31/// This module contains thin RAII wrappers around the SQLite 3 structures32/// representing the database, and lightweight.3334#if !defined(UTILS_SQLITE_DATABASE_HPP)35#define UTILS_SQLITE_DATABASE_HPP3637#include "utils/sqlite/database_fwd.hpp"3839extern "C" {40#include <stdint.h>41}4243#include <cstddef>44#include <memory>4546#include "utils/fs/path_fwd.hpp"47#include "utils/optional_fwd.hpp"48#include "utils/sqlite/c_gate_fwd.hpp"49#include "utils/sqlite/statement_fwd.hpp"50#include "utils/sqlite/transaction_fwd.hpp"5152namespace utils {53namespace sqlite {545556/// Constant for the database::open flags: open in read-only mode.57static const int open_readonly = 1 << 0;58/// Constant for the database::open flags: open in read-write mode.59static const int open_readwrite = 1 << 1;60/// Constant for the database::open flags: create on open.61static const int open_create = 1 << 2;626364/// A RAII model for the SQLite 3 database.65///66/// This class holds the database of the SQLite 3 interface during its existence67/// and provides wrappers around several SQLite 3 library functions that operate68/// on such database.69///70/// These wrapper functions differ from the C versions in that they use the71/// implicit database hold by the class, they use C++ types where appropriate72/// and they use exceptions to report errors.73///74/// The wrappers intend to be as lightweight as possible but, in some75/// situations, they are pretty complex because of the workarounds needed to76/// make the SQLite 3 more C++ friendly. We prefer a clean C++ interface over77/// optimal efficiency, so this is OK.78class database {79struct impl;8081/// Pointer to the shared internal implementation.82std::shared_ptr< impl > _pimpl;8384friend class database_c_gate;85database(const utils::optional< utils::fs::path >&, void*, const bool);86void* raw_database(void);8788public:89~database(void);9091static database in_memory(void);92static database open(const fs::path&, int);93static database temporary(void);94void close(void);9596const utils::optional< utils::fs::path >& db_filename(void) const;9798void exec(const std::string&);99100transaction begin_transaction(void);101statement create_statement(const std::string&);102103int64_t last_insert_rowid(void);104};105106107} // namespace sqlite108} // namespace utils109110#endif // !defined(UTILS_SQLITE_DATABASE_HPP)111112113