diff options
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 |