Path: blob/main/contrib/kyua/utils/sqlite/exceptions.cpp
48198 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#include "utils/sqlite/exceptions.hpp"2930extern "C" {31#include <sqlite3.h>32}3334#include <string>3536#include "utils/format/macros.hpp"37#include "utils/fs/path.hpp"38#include "utils/optional.ipp"39#include "utils/sqlite/c_gate.hpp"40#include "utils/sqlite/database.hpp"4142namespace fs = utils::fs;43namespace sqlite = utils::sqlite;4445using utils::optional;464748namespace {495051/// Formats the database filename returned by sqlite for user consumption.52///53/// \param db_filename An optional database filename.54///55/// \return A string describing the filename.56static std::string57format_db_filename(const optional< fs::path >& db_filename)58{59if (db_filename)60return db_filename.get().str();61else62return "in-memory or temporary";63}646566} // anonymous namespace676869/// Constructs a new error with a plain-text message.70///71/// \param db_filename_ Database filename as returned by database::db_filename()72/// for error reporting purposes.73/// \param message The plain-text error message.74sqlite::error::error(const optional< fs::path >& db_filename_,75const std::string& message) :76std::runtime_error(F("%s (sqlite db: %s)") % message %77format_db_filename(db_filename_)),78_db_filename(db_filename_)79{80}818283/// Destructor for the error.84sqlite::error::~error(void) throw()85{86}878889/// Returns the path to the database that raised this error.90///91/// \return A database filename as returned by database::db_filename().92const optional< fs::path >&93sqlite::error::db_filename(void) const94{95return _db_filename;96}979899/// Constructs a new error.100///101/// \param db_filename_ Database filename as returned by database::db_filename()102/// for error reporting purposes.103/// \param api_function_ The name of the API function that caused the error.104/// \param message_ The plain-text error message provided by SQLite.105sqlite::api_error::api_error(const optional< fs::path >& db_filename_,106const std::string& api_function_,107const std::string& message_) :108error(db_filename_, F("%s (sqlite op: %s)") % message_ % api_function_),109_api_function(api_function_)110{111}112113114/// Destructor for the error.115sqlite::api_error::~api_error(void) throw()116{117}118119120/// Constructs a new api_error with the message in the SQLite database.121///122/// \param database_ The SQLite database.123/// \param api_function_ The name of the SQLite C API function that caused the124/// error.125///126/// \return A new api_error with the retrieved message.127sqlite::api_error128sqlite::api_error::from_database(database& database_,129const std::string& api_function_)130{131::sqlite3* c_db = database_c_gate(database_).c_database();132return api_error(database_.db_filename(), api_function_,133::sqlite3_errmsg(c_db));134}135136137/// Gets the name of the SQlite C API function that caused this error.138///139/// \return The name of the function.140const std::string&141sqlite::api_error::api_function(void) const142{143return _api_function;144}145146147/// Constructs a new error.148///149/// \param db_filename_ Database filename as returned by database::db_filename()150/// for error reporting purposes.151/// \param name_ The name of the unknown column.152sqlite::invalid_column_error::invalid_column_error(153const optional< fs::path >& db_filename_,154const std::string& name_) :155error(db_filename_, F("Unknown column '%s'") % name_),156_column_name(name_)157{158}159160161/// Destructor for the error.162sqlite::invalid_column_error::~invalid_column_error(void) throw()163{164}165166167/// Gets the name of the column that could not be found.168///169/// \return The name of the column requested by the user.170const std::string&171sqlite::invalid_column_error::column_name(void) const172{173return _column_name;174}175176177