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 #include "utils/sqlite/exceptions.hpp" 30 31 extern "C" { 32 #include <sqlite3.h> 33 } 34 35 #include "utils/format/macros.hpp" 36 #include "utils/sqlite/c_gate.hpp" 37 38 namespace sqlite = utils::sqlite; 39 40 41 /// Constructs a new error with a plain-text message. 42 /// 43 /// \param message The plain-text error message. 44 sqlite::error::error(const std::string& message) : 45 std::runtime_error(message) 46 { 47 } 48 49 50 /// Destructor for the error. 51 sqlite::error::~error(void) throw() 52 { 53 } 54 55 56 /// Constructs a new error. 57 /// 58 /// \param api_function_ The name of the API function that caused the error. 59 /// \param message_ The plain-text error message provided by SQLite. 60 sqlite::api_error::api_error(const std::string& api_function_, 61 const std::string& message_) : 62 error(message_), 63 _api_function(api_function_) 64 { 65 } 66 67 68 /// Destructor for the error. 69 sqlite::api_error::~api_error(void) throw() 70 { 71 } 72 73 74 /// Constructs a new api_error with the message in the SQLite database. 75 /// 76 /// \param database_ The SQLite database. 77 /// \param api_function_ The name of the SQLite C API function that caused the 78 /// error. 79 /// 80 /// \return A new api_error with the retrieved message. 81 sqlite::api_error 82 sqlite::api_error::from_database(database& database_, 83 const std::string& api_function_) 84 { 85 ::sqlite3* c_db = database_c_gate(database_).c_database(); 86 return api_error(api_function_, ::sqlite3_errmsg(c_db)); 87 } 88 89 90 /// Gets the name of the SQlite C API function that caused this error. 91 /// 92 /// \return The name of the function. 93 const std::string& 94 sqlite::api_error::api_function(void) const 95 { 96 return _api_function; 97 } 98 99 100 /// Constructs a new error. 101 /// 102 /// \param name_ The name of the unknown column. 103 sqlite::invalid_column_error::invalid_column_error(const std::string& name_) : 104 error(F("Unknown column '%s'") % name_), 105 _column_name(name_) 106 { 107 } 108 109 110 /// Destructor for the error. 111 sqlite::invalid_column_error::~invalid_column_error(void) throw() 112 { 113 } 114 115 116 /// Gets the name of the column that could not be found. 117 /// 118 /// \return The name of the column requested by the user. 119 const std::string& 120 sqlite::invalid_column_error::column_name(void) const 121 { 122 return _column_name; 123 } 124