00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <config.h>
00021 #include <plugin/transaction_log/utilities/transaction_log_connection.h>
00022 #include <iostream>
00023
00024 using namespace std;
00025
00026 TransactionLogConnection::TransactionLogConnection(string &host, uint16_t port,
00027 string &username, string &password,
00028 bool drizzle_protocol) :
00029 hostName(host),
00030 drizzleProtocol(drizzle_protocol)
00031 {
00032 if (host.empty())
00033 {
00034 host= "localhost";
00035 }
00036
00037 drizzle= drizzle_create();
00038
00039 if (drizzle == NULL)
00040 {
00041 errorHandler(NULL, DRIZZLE_RETURN_MEMORY, "drizzle_create() failed");
00042 throw "drizzle_create() failed";
00043 }
00044
00045 connection= drizzle_con_create(drizzle);
00046 if (connection == NULL)
00047 {
00048 errorHandler(NULL, DRIZZLE_RETURN_MEMORY, "drizzle_create() failed");
00049 throw "drizzle_con_create() failed";
00050 }
00051 drizzle_con_set_tcp(connection, (char *)host.c_str(), port);
00052 drizzle_con_set_auth(connection, (char *)username.c_str(), (char *)password.c_str());
00053
00054 drizzle_con_add_options(connection,
00055 drizzle_protocol ? DRIZZLE_CON_EXPERIMENTAL : DRIZZLE_CON_MYSQL);
00056
00057 drizzle_return_t ret= drizzle_con_connect(connection);
00058
00059 if (ret != DRIZZLE_RETURN_OK)
00060 {
00061 errorHandler(NULL, ret, "when trying to connect");
00062 throw 1;
00063 }
00064 }
00065
00066 void TransactionLogConnection::query(const std::string &str_query,
00067 drizzle_result_st *result)
00068 {
00069 drizzle_return_t ret;
00070
00071 if (drizzle_query_str(connection, result, str_query.c_str(), &ret) == NULL ||
00072 ret != DRIZZLE_RETURN_OK)
00073 {
00074 if (ret == DRIZZLE_RETURN_ERROR_CODE)
00075 {
00076 cerr << "Error executing query: " <<
00077 drizzle_result_error(result) << endl;
00078 drizzle_result_free(result);
00079 }
00080 else
00081 {
00082 cerr << "Error executing query: " <<
00083 drizzle_con_error(connection) << endl;
00084 drizzle_result_free(result);
00085 }
00086 return;
00087 }
00088
00089 if (drizzle_result_buffer(result) != DRIZZLE_RETURN_OK)
00090 {
00091 cerr << "Could not buffer result: " <<
00092 drizzle_con_error(connection) << endl;
00093 drizzle_result_free(result);
00094 return;
00095 }
00096 }
00097
00098 void TransactionLogConnection::errorHandler(drizzle_result_st *res,
00099 drizzle_return_t ret, const char *when)
00100 {
00101 if (res == NULL)
00102 {
00103 cerr << "Got error: " << drizzle_con_error(connection) << " " << when << endl;
00104 }
00105 else if (ret == DRIZZLE_RETURN_ERROR_CODE)
00106 {
00107 cerr << "Got error: " << drizzle_result_error(res) << " (" << drizzle_result_error_code(res) << ") " << when << endl;
00108 drizzle_result_free(res);
00109 }
00110 else
00111 {
00112 cerr << "Got error: " << ret << " " << when << endl;
00113 }
00114 }