forked from bskari/mysql-cpp
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMySqlException.cpp
More file actions
Latest commit
59 lines (44 loc) · 1.54 KB
/
Copy pathMySqlException.cpp
File metadata and controls
59 lines (44 loc) · 1.54 KB
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(constMYSQL* const connection)
: message_(getServerErrorMessage(connection))
{
}
MySqlException::MySqlException(const MySqlPreparedStatement& statement)
: message_(getServerErrorMessage(statement.statementHandle_))
{
}
MySqlException::~MySqlException() noexcept {
}
constchar* MySqlException::what() constnoexcept {
return message_.c_str();
}
constchar* MySqlException::getServerErrorMessage(constMYSQL* 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.
constchar* const message = mysql_error(const_cast<MYSQL*>(conn));
if ('\0' != message[0]) { // Error message isn't empty
return message;
}
return"Unknown error";
}
constchar* MySqlException::getServerErrorMessage(
constMYSQL_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.
constchar* const message = mysql_stmt_error(
const_cast<MYSQL_STMT*>(statement));
if ('\0' != message[0]) { // Error message isn't empty
return message;
}
return"Unknown error";
}