diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2015-12-29 02:42:43 +0000 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2015-12-29 06:00:07 +0000 |
commit | 3a7e2b9a7bde4682f2273022e9d2ddecd7e5e3b8 (patch) | |
tree | d5dc58209bd8fa28047c54a11c09bc64505d5354 | |
parent | Remove rebind (diff) | |
download | libdbpp-sqlite-3a7e2b9a7bde4682f2273022e9d2ddecd7e5e3b8.tar.bz2 libdbpp-sqlite-3a7e2b9a7bde4682f2273022e9d2ddecd7e5e3b8.tar.xz libdbpp-sqlite-3a7e2b9a7bde4682f2273022e9d2ddecd7e5e3b8.zip |
Reshuffle and add new exceptions
-rw-r--r-- | libsqlitepp/sqlite-command.cpp | 28 | ||||
-rw-r--r-- | libsqlitepp/sqlite-connection.cpp | 28 | ||||
-rw-r--r-- | libsqlitepp/sqlite-connection.h | 5 | ||||
-rw-r--r-- | libsqlitepp/sqlite-error.cpp | 25 | ||||
-rw-r--r-- | libsqlitepp/sqlite-error.h | 16 | ||||
-rw-r--r-- | libsqlitepp/sqlite-modifycommand.cpp | 4 | ||||
-rw-r--r-- | libsqlitepp/sqlite-selectcommand.cpp | 4 | ||||
-rw-r--r-- | libsqlitepp/unittests/testsqlite.cpp | 6 |
8 files changed, 53 insertions, 63 deletions
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 <sqlite3.h> 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 <string.h> -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 <error.h> +#include <exception.h> +#include <sqlite3.h> +#include <visibility.h> namespace SQLite { - class Error : public DB::Error { + class Error : public AdHoc::Exception<DB::Error> { 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 <boost/test/unit_test.hpp> #include <definedDirs.h> -#include <dbpp/modifycommand.h> -#include <dbpp/selectcommand.h> -#include <dbpp/column.h> +#include <modifycommand.h> +#include <selectcommand.h> +#include <column.h> #include <sqlite-mock.h> #include <testCore.h> #include <boost/date_time/posix_time/posix_time.hpp> |