Here is one option: instead of placing the connection verification logic in the DAO
class, move it into a separate class that encapsulates the MYSQL pointer. So something like:
class DAO {
public:
...
private:
MyMYSQLHandleAccessWrapper m_conn;
};
class MyMYSQLHandleAccessWrapper {
public:
...
MYSQL* GetHandle() {
// TODO: place verification logic here
return m_conn;
};
...
private:
...
MYSQL* m_conn;
};
Now, the logic in DAO
that would have directly accessed m_conn
will access m_conn.GetHandle()
, which can implement the connection logic. So, e.g. instead of QueryXXX
looking like:
DAO::QueryXXX(...) {
check_connection(..);
do_stuff_with_mysql(m_conn);
}
You would have:
DAO::QueryXXX(...) {
MYSQL* mysql = m_conn.GetHandle();
do_stuff_with_mysql(mysql);
}
It's not that different, but it forces the implementer of functions like QueryXXX
to invoke the connection logic by requiring a call to GetHandle
before being able to do anything with the underlying MYSQL API.