1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2010 Joseph Daly <skinny.moey@gmail.com>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; version 2 of the License.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21
#include <plugin/transaction_log/utilities/transaction_log_connection.h>
26
TransactionLogConnection::TransactionLogConnection(string &host, uint16_t port,
27
string &username, string &password,
28
bool drizzle_protocol) :
30
drizzleProtocol(drizzle_protocol)
37
drizzle= drizzle_create();
41
errorHandler(NULL, DRIZZLE_RETURN_MEMORY, "drizzle_create() failed");
42
throw "drizzle_create() failed";
45
connection= drizzle_con_create(drizzle);
46
if (connection == NULL)
48
errorHandler(NULL, DRIZZLE_RETURN_MEMORY, "drizzle_create() failed");
49
throw "drizzle_con_create() failed";
51
drizzle_con_set_tcp(connection, (char *)host.c_str(), port);
52
drizzle_con_set_auth(connection, (char *)username.c_str(), (char *)password.c_str());
54
drizzle_con_add_options(connection,
55
drizzle_protocol ? DRIZZLE_CON_EXPERIMENTAL : DRIZZLE_CON_MYSQL);
57
drizzle_return_t ret= drizzle_con_connect(connection);
59
if (ret != DRIZZLE_RETURN_OK)
61
errorHandler(NULL, ret, "when trying to connect");
66
void TransactionLogConnection::query(const std::string &str_query,
67
drizzle_result_st *result)
71
if (drizzle_query_str(connection, result, str_query.c_str(), &ret) == NULL ||
72
ret != DRIZZLE_RETURN_OK)
74
if (ret == DRIZZLE_RETURN_ERROR_CODE)
76
cerr << "Error executing query: " <<
77
drizzle_result_error(result) << endl;
78
drizzle_result_free(result);
82
cerr << "Error executing query: " <<
83
drizzle_con_error(connection) << endl;
84
drizzle_result_free(result);
89
if (drizzle_result_buffer(result) != DRIZZLE_RETURN_OK)
91
cerr << "Could not buffer result: " <<
92
drizzle_con_error(connection) << endl;
93
drizzle_result_free(result);
98
void TransactionLogConnection::errorHandler(drizzle_result_st *res,
99
drizzle_return_t ret, const char *when)
103
cerr << "Got error: " << drizzle_con_error(connection) << " " << when << endl;
105
else if (ret == DRIZZLE_RETURN_ERROR_CODE)
107
cerr << "Got error: " << drizzle_result_error(res) << " (" << drizzle_result_error_code(res) << ") " << when << endl;
108
drizzle_result_free(res);
112
cerr << "Got error: " << ret << " " << when << endl;