forked from bskari/mysql-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMySqlPreparedStatement.cpp
52 lines (43 loc) · 1.4 KB
/
MySqlPreparedStatement.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
#include <cassert>
#include <cstring>
#include <mysql/mysql.h>
#include <string>
#include "MySqlException.hpp"
#include "MySqlPreparedStatement.hpp"
using std::string;
using std::strlen;
MySqlPreparedStatement::MySqlPreparedStatement(
const char* query,
MYSQL* const connection
)
: statementHandle_(mysql_stmt_init(connection))
, parameterCount_()
, fieldCount_()
{
assert(nullptr != connection);
if (nullptr == statementHandle_) {
throw MySqlException("MySQL out of memory");
}
const size_t length = strlen(query);
if (0 != mysql_stmt_prepare(statementHandle_, query, length)) {
string errorMessage(
MySqlException::getServerErrorMessage(statementHandle_));
if (0 != mysql_stmt_free_result(statementHandle_)) {
errorMessage += "; There was an error freeing this statement";
}
if (0 != mysql_stmt_close(statementHandle_)) {
errorMessage += "; There was an error closing this statement";
}
throw MySqlException(errorMessage);
}
parameterCount_ = mysql_stmt_param_count(statementHandle_);
fieldCount_ = mysql_stmt_field_count(statementHandle_);
}
MySqlPreparedStatement::~MySqlPreparedStatement() {
if (0 != mysql_stmt_free_result(statementHandle_)) {
// TODO Log an error
}
if (0 != mysql_stmt_close(statementHandle_)) {
// TODO Log an error
}
}