diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2021-06-12 20:26:01 +0100 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2021-06-12 20:26:01 +0100 |
commit | 231bc3fc8d1902bb19b6fce39ffd72cce4b4f2c3 (patch) | |
tree | e074ea9bf53400600831215822a848fe27bfac55 /lib/input/mysqlConn.cpp | |
parent | Have verify return its expression, might be useful (diff) | |
download | mygrate-231bc3fc8d1902bb19b6fce39ffd72cce4b4f2c3.tar.bz2 mygrate-231bc3fc8d1902bb19b6fce39ffd72cce4b4f2c3.tar.xz mygrate-231bc3fc8d1902bb19b6fce39ffd72cce4b4f2c3.zip |
Avoid direct use of runtime_error in MySQL stuff
Adds proper exception which extends it and gets the MySQL error message.
Diffstat (limited to 'lib/input/mysqlConn.cpp')
-rw-r--r-- | lib/input/mysqlConn.cpp | 14 |
1 files changed, 8 insertions, 6 deletions
diff --git a/lib/input/mysqlConn.cpp b/lib/input/mysqlConn.cpp index 4899deb..c7705f5 100644 --- a/lib/input/mysqlConn.cpp +++ b/lib/input/mysqlConn.cpp @@ -11,6 +11,8 @@ #include <vector> namespace MyGrate::Input { + MySQLErr::MySQLErr(const std::string & when, MYSQL * c) : std::runtime_error(when + ": " + mysql_error(c)) { } + MySQLConn::MySQLConn( const char * const host, const char * const user, const char * const pass, unsigned short port) : st_mysql {} @@ -18,9 +20,9 @@ namespace MyGrate::Input { mysql_init(this); if (!mysql_real_connect(this, host, user, pass, "", port, nullptr, 0)) { mysql_close(this); - throw std::runtime_error("ConnectionError"); + throw MySQLErr("Connecting", this); } - verify<std::runtime_error>(!mysql_set_character_set(this, "utf8"), "Set character set"); + verify<MySQLErr>(!mysql_set_character_set(this, "utf8"), "Setting char set", this); } MySQLConn::~MySQLConn() @@ -31,18 +33,18 @@ namespace MyGrate::Input { void MySQLConn::query(const char * const q) { - verify<std::runtime_error>(!mysql_query(this, q), q); + verify<MySQLErr>(!mysql_query(this, q), q, this); } void MySQLConn::query(const char * const q, const std::initializer_list<DbValue> & vs) { StmtPtr stmt {mysql_stmt_init(this), &mysql_stmt_close}; - verify<std::runtime_error>(!mysql_stmt_prepare(stmt.get(), q, strlen(q)), q); + verify<MySQLErr>(!mysql_stmt_prepare(stmt.get(), q, strlen(q)), q, this); verify<std::logic_error>(mysql_stmt_param_count(stmt.get()) == vs.size(), "Param count mismatch"); Bindings b {vs}; - verify<std::runtime_error>(!mysql_stmt_bind_param(stmt.get(), b.binds.data()), "Param count mismatch"); - verify<std::runtime_error>(!mysql_stmt_execute(stmt.get()), q); + verify<std::logic_error>(!mysql_stmt_bind_param(stmt.get(), b.binds.data()), "Param count mismatch"); + verify<MySQLErr>(!mysql_stmt_execute(stmt.get()), q, this); } DbPrepStmtPtr |