From 231bc3fc8d1902bb19b6fce39ffd72cce4b4f2c3 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 12 Jun 2021 20:26:01 +0100 Subject: Avoid direct use of runtime_error in MySQL stuff Adds proper exception which extends it and gets the MySQL error message. --- lib/input/mysqlConn.cpp | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'lib/input/mysqlConn.cpp') 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 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(!mysql_set_character_set(this, "utf8"), "Set character set"); + verify(!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(!mysql_query(this, q), q); + verify(!mysql_query(this, q), q, this); } void MySQLConn::query(const char * const q, const std::initializer_list & vs) { StmtPtr stmt {mysql_stmt_init(this), &mysql_stmt_close}; - verify(!mysql_stmt_prepare(stmt.get(), q, strlen(q)), q); + verify(!mysql_stmt_prepare(stmt.get(), q, strlen(q)), q, this); verify(mysql_stmt_param_count(stmt.get()) == vs.size(), "Param count mismatch"); Bindings b {vs}; - verify(!mysql_stmt_bind_param(stmt.get(), b.binds.data()), "Param count mismatch"); - verify(!mysql_stmt_execute(stmt.get()), q); + verify(!mysql_stmt_bind_param(stmt.get(), b.binds.data()), "Param count mismatch"); + verify(!mysql_stmt_execute(stmt.get()), q, this); } DbPrepStmtPtr -- cgit v1.2.3