forked from bskari/mysql-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMySqlException.cpp
59 lines (44 loc) · 1.54 KB
/
MySqlException.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "MySqlException.hpp"
#include "MySqlPreparedStatement.hpp"
#include <mysql/mysql.h>
#include <string>
using std::string;
MySqlException::MySqlException(const string& message)
: message_(message)
{
}
MySqlException::MySqlException(const MYSQL* const connection)
: message_(getServerErrorMessage(connection))
{
}
MySqlException::MySqlException(const MySqlPreparedStatement& statement)
: message_(getServerErrorMessage(statement.statementHandle_))
{
}
MySqlException::~MySqlException() noexcept {
}
const char* MySqlException::what() const noexcept {
return message_.c_str();
}
const char* MySqlException::getServerErrorMessage(const MYSQL* const conn) {
// This error should be unique per connection, so it should be thread safe
// The MySQL C interface is backward compatible with C89, so it doesn't
// recognize const. It *should* be const though, so just work around it.
const char* const message = mysql_error(const_cast<MYSQL*>(conn));
if ('\0' != message[0]) { // Error message isn't empty
return message;
}
return "Unknown error";
}
const char* MySqlException::getServerErrorMessage(
const MYSQL_STMT* const statement
) {
// The MySQL C interface is backward compatible with C89, so it doesn't
// recognize const. It *should* be const though, so just work around it.
const char* const message = mysql_stmt_error(
const_cast<MYSQL_STMT*>(statement));
if ('\0' != message[0]) { // Error message isn't empty
return message;
}
return "Unknown error";
}