From 3a7e2b9a7bde4682f2273022e9d2ddecd7e5e3b8 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 29 Dec 2015 02:42:43 +0000 Subject: Reshuffle and add new exceptions --- libsqlitepp/sqlite-command.cpp | 28 ++++++++++++++-------------- libsqlitepp/sqlite-connection.cpp | 28 +++++++++++++++------------- libsqlitepp/sqlite-connection.h | 5 +++++ libsqlitepp/sqlite-error.cpp | 25 +++++-------------------- libsqlitepp/sqlite-error.h | 16 +++++++--------- libsqlitepp/sqlite-modifycommand.cpp | 4 ++-- libsqlitepp/sqlite-selectcommand.cpp | 4 ++-- libsqlitepp/unittests/testsqlite.cpp | 6 +++--- 8 files changed, 53 insertions(+), 63 deletions(-) (limited to 'libsqlitepp') diff --git a/libsqlitepp/sqlite-command.cpp b/libsqlitepp/sqlite-command.cpp index 48563fa..3d8ac07 100644 --- a/libsqlitepp/sqlite-command.cpp +++ b/libsqlitepp/sqlite-command.cpp @@ -8,7 +8,7 @@ SQLite::Command::Command(const Connection * conn, const std::string & sql) : c(conn) { if (sqlite3_prepare_v2(conn->db, sql.c_str(), sql.length(), &stmt, NULL) != SQLITE_OK) { - throw Error(sqlite3_errmsg(conn->db)); + throw Error(conn->db); } } @@ -21,85 +21,85 @@ void SQLite::Command::bindParamI(unsigned int n, int v) { if (sqlite3_bind_int(stmt, n + 1, v) != SQLITE_OK) { - throw Error(sqlite3_errmsg(c->db)); + throw Error(c->db); } } void SQLite::Command::bindParamI(unsigned int n, long int v) { if (sqlite3_bind_int64(stmt, n + 1, v) != SQLITE_OK) { - throw Error(sqlite3_errmsg(c->db)); + throw Error(c->db); } } void SQLite::Command::bindParamI(unsigned int n, long long int v) { if (sqlite3_bind_int64(stmt, n + 1, v) != SQLITE_OK) { - throw Error(sqlite3_errmsg(c->db)); + throw Error(c->db); } } void SQLite::Command::bindParamI(unsigned int n, unsigned int v) { if (sqlite3_bind_int64(stmt, n + 1, v) != SQLITE_OK) { - throw Error(sqlite3_errmsg(c->db)); + throw Error(c->db); } } void SQLite::Command::bindParamI(unsigned int n, long unsigned int v) { if (sqlite3_bind_int64(stmt, n + 1, v) != SQLITE_OK) { - throw Error(sqlite3_errmsg(c->db)); + throw Error(c->db); } } void SQLite::Command::bindParamI(unsigned int n, long long unsigned int v) { if (sqlite3_bind_int64(stmt, n + 1, v) != SQLITE_OK) { - throw Error(sqlite3_errmsg(c->db)); + throw Error(c->db); } } void SQLite::Command::bindParamF(unsigned int n, double v) { if (sqlite3_bind_double(stmt, n + 1, v) != SQLITE_OK) { - throw Error(sqlite3_errmsg(c->db)); + throw Error(c->db); } } void SQLite::Command::bindParamF(unsigned int n, float v) { if (sqlite3_bind_double(stmt, n + 1, v) != SQLITE_OK) { - throw Error(sqlite3_errmsg(c->db)); + throw Error(c->db); } } void SQLite::Command::bindParamS(unsigned int n, const Glib::ustring & s) { if (sqlite3_bind_text(stmt, n + 1, s.c_str(), s.length(), SQLITE_STATIC) != SQLITE_OK) { - throw Error(sqlite3_errmsg(c->db)); + throw Error(c->db); } } void SQLite::Command::bindParamB(unsigned int, bool) { - throw Error("Not supported"); + throw DB::ParameterTypeNotSupported(); } void SQLite::Command::bindParamT(unsigned int, const boost::posix_time::time_duration &) { - throw Error("Not supported"); + throw DB::ParameterTypeNotSupported(); } void SQLite::Command::bindParamT(unsigned int, const boost::posix_time::ptime &) { - throw Error("Not supported"); + throw DB::ParameterTypeNotSupported(); } void SQLite::Command::bindNull(unsigned int n) { if (sqlite3_bind_null(stmt, n + 1) != SQLITE_OK) { - throw Error(sqlite3_errmsg(c->db)); + throw Error(c->db); } } diff --git a/libsqlitepp/sqlite-connection.cpp b/libsqlitepp/sqlite-connection.cpp index 5eead63..df7de68 100644 --- a/libsqlitepp/sqlite-connection.cpp +++ b/libsqlitepp/sqlite-connection.cpp @@ -3,17 +3,19 @@ #include "sqlite-selectcommand.h" #include "sqlite-modifycommand.h" +SQLite::ConnectionError::ConnectionError(sqlite3 * db) : + SQLite::Error(db) +{ +} + SQLite::Connection::Connection(const std::string & str) : txDepth(0), rolledback(false) { if (sqlite3_open(str.c_str(), &db) != SQLITE_OK) { - if (db) { - std::string err(sqlite3_errmsg(db)); - sqlite3_close(db); - throw Error(err.c_str()); - } - throw Error("Unknown error opening database"); + ConnectionError err(db); + sqlite3_close(db); + throw err; } } @@ -27,7 +29,7 @@ SQLite::Connection::finish() const { if (txDepth != 0) { rollbackTx(); - throw Error("Transaction still open"); + throw DB::TransactionStillOpen(); } } @@ -36,7 +38,7 @@ SQLite::Connection::beginTx() const { if (txDepth == 0) { if (sqlite3_exec(db, "BEGIN TRANSACTION", NULL, NULL, NULL) != SQLITE_OK) { - throw Error(sqlite3_errmsg(db)); + throw Error(db); } rolledback = false; } @@ -51,7 +53,7 @@ SQLite::Connection::commitTx() const } if (--txDepth == 0) { if (sqlite3_exec(db, "COMMIT TRANSACTION", NULL, NULL, NULL) != SQLITE_OK) { - throw Error(sqlite3_errmsg(db)); + throw Error(db); } } return txDepth; @@ -62,7 +64,7 @@ SQLite::Connection::rollbackTx() const { if (--txDepth == 0) { if (sqlite3_exec(db, "ROLLBACK TRANSACTION", NULL, NULL, NULL) != SQLITE_OK) { - throw Error(sqlite3_errmsg(db)); + throw Error(db); } } else { @@ -111,19 +113,19 @@ SQLite::Connection::newModifyCommand(const std::string & sql) const void SQLite::Connection::beginBulkUpload(const char *, const char *) const { - throw Error("Not implemented"); + throw DB::BulkUploadNotSupported(); } void SQLite::Connection::endBulkUpload(const char *) const { - throw Error("Not implemented"); + throw DB::BulkUploadNotSupported(); } size_t SQLite::Connection::bulkUploadData(const char *, size_t) const { - throw Error("Not implemented"); + throw DB::BulkUploadNotSupported(); } int64_t diff --git a/libsqlitepp/sqlite-connection.h b/libsqlitepp/sqlite-connection.h index 9398ee8..e7f2956 100644 --- a/libsqlitepp/sqlite-connection.h +++ b/libsqlitepp/sqlite-connection.h @@ -6,6 +6,11 @@ #include namespace SQLite { + class ConnectionError : public virtual Error, public virtual DB::ConnectionError { + public: + ConnectionError(sqlite3 *); + }; + class Connection : public DB::Connection { public: Connection(const std::string & info); diff --git a/libsqlitepp/sqlite-error.cpp b/libsqlitepp/sqlite-error.cpp index a0758ef..03ffac4 100644 --- a/libsqlitepp/sqlite-error.cpp +++ b/libsqlitepp/sqlite-error.cpp @@ -1,29 +1,14 @@ #include "sqlite-error.h" #include -SQLite::Error::Error() : - msg(NULL) +SQLite::Error::Error(sqlite3 * db) : + msg(sqlite3_errmsg(db)) { } -SQLite::Error::Error(const SQLite::Error & e) : - msg(e.msg ? strdup(e.msg) : NULL) +std::string +SQLite::Error::message() const throw() { -} - -SQLite::Error::Error(const char * e) : - msg(e ? strdup(e) : NULL) -{ -} - -SQLite::Error::~Error() throw() -{ - free(msg); -} - -const char * -SQLite::Error::what() const throw() -{ - return msg ? msg : "No message"; + return msg; } diff --git a/libsqlitepp/sqlite-error.h b/libsqlitepp/sqlite-error.h index 3f8d32e..a42d2e4 100644 --- a/libsqlitepp/sqlite-error.h +++ b/libsqlitepp/sqlite-error.h @@ -2,21 +2,19 @@ #define SQLITE_ERROR_H #include +#include +#include +#include namespace SQLite { - class Error : public DB::Error { + class Error : public AdHoc::Exception { public: - Error(); - Error(const Error &); - Error(const char *); - ~Error() throw(); + Error(sqlite3 *); - const char * what() const throw(); + std::string message() const throw() override; private: - char * msg; - }; - class ConnectionError : public Error, public virtual DB::ConnectionError { + std::string msg; }; } diff --git a/libsqlitepp/sqlite-modifycommand.cpp b/libsqlitepp/sqlite-modifycommand.cpp index cb5d761..d5f44cc 100644 --- a/libsqlitepp/sqlite-modifycommand.cpp +++ b/libsqlitepp/sqlite-modifycommand.cpp @@ -19,12 +19,12 @@ SQLite::ModifyCommand::execute(bool anc) { if (sqlite3_step(stmt) != SQLITE_DONE) { sqlite3_reset(stmt); - throw Error(sqlite3_errmsg(c->db)); + throw Error(c->db); } unsigned int rows = sqlite3_changes(c->db); sqlite3_reset(stmt); if (rows == 0 && !anc) { - throw Error("No rows affected"); + throw DB::NoRowsAffected(); } return rows; } diff --git a/libsqlitepp/sqlite-selectcommand.cpp b/libsqlitepp/sqlite-selectcommand.cpp index 9974647..ca72a9f 100644 --- a/libsqlitepp/sqlite-selectcommand.cpp +++ b/libsqlitepp/sqlite-selectcommand.cpp @@ -37,7 +37,7 @@ namespace SQLite { h.null(); return; case SQLITE_BLOB: - throw std::runtime_error("Blobs not supported"); + throw DB::ColumnTypeNotSupported(); } } @@ -73,7 +73,7 @@ SQLite::SelectCommand::fetch() case SQLITE_DONE: return false; default: - throw Error(sqlite3_errmsg(c->db)); + throw Error(c->db); } } diff --git a/libsqlitepp/unittests/testsqlite.cpp b/libsqlitepp/unittests/testsqlite.cpp index 9cd8de9..1e4aa90 100644 --- a/libsqlitepp/unittests/testsqlite.cpp +++ b/libsqlitepp/unittests/testsqlite.cpp @@ -2,9 +2,9 @@ #include #include -#include -#include -#include +#include +#include +#include #include #include #include -- cgit v1.2.3