Drizzled Public API Documentation

transaction_log_connection.cc

00001 /* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2010 Joseph Daly <skinny.moey@gmail.com>
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; version 2 of the License.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018  */
00019 
00020 #include <config.h>
00021 #include "transaction_log_connection.h"
00022 #include <iostream>
00023 
00024 using namespace std;
00025 using namespace drizzled;
00026 
00027 TransactionLogConnection::TransactionLogConnection(string &host, uint16_t port,
00028                                                    string &username, string &password,
00029                                                    bool drizzle_protocol)
00030   :
00031     hostName(host),
00032     drizzleProtocol(drizzle_protocol)
00033 {
00034   drizzle_return_t ret;
00035 
00036   if (host.empty())
00037     host= "localhost";
00038 
00039   drizzle_create(&drizzle);
00040   drizzle_con_create(&drizzle, &connection);
00041   drizzle_con_set_tcp(&connection, (char *)host.c_str(), port);
00042   drizzle_con_set_auth(&connection, (char *)username.c_str(),
00043     (char *)password.c_str());
00044   drizzle_con_add_options(&connection,
00045     drizzle_protocol ? DRIZZLE_CON_EXPERIMENTAL : DRIZZLE_CON_MYSQL);
00046   ret= drizzle_con_connect(&connection);
00047   if (ret != DRIZZLE_RETURN_OK)
00048   {
00049     errorHandler(NULL, ret, "when trying to connect");
00050     throw 1;
00051   }
00052 }
00053 
00054 void TransactionLogConnection::query(const std::string &str_query,
00055                                      drizzle_result_st *result)
00056 {
00057   drizzle_return_t ret;
00058   if (drizzle_query_str(&connection, result, str_query.c_str(), &ret) == NULL ||
00059       ret != DRIZZLE_RETURN_OK)
00060   {
00061     if (ret == DRIZZLE_RETURN_ERROR_CODE)
00062     {
00063       cerr << "Error executing query: " <<
00064         drizzle_result_error(result) << endl;
00065       drizzle_result_free(result);
00066     }
00067     else
00068     {
00069       cerr << "Error executing query: " <<
00070         drizzle_con_error(&connection) << endl;
00071       drizzle_result_free(result);
00072     }
00073     return;
00074   }
00075 
00076   if (drizzle_result_buffer(result) != DRIZZLE_RETURN_OK)
00077   {
00078     cerr << "Could not buffer result: " <<
00079         drizzle_con_error(&connection) << endl;
00080     drizzle_result_free(result);
00081     return;
00082   }
00083   return;
00084 }
00085 
00086 void TransactionLogConnection::errorHandler(drizzle_result_st *res,
00087   drizzle_return_t ret, const char *when)
00088 {
00089   if (res == NULL)
00090   {
00091     cerr << "Got error: " << drizzle_con_error(&connection) << " "
00092       << when << endl;
00093   }
00094   else if (ret == DRIZZLE_RETURN_ERROR_CODE)
00095   {
00096     cerr << "Got error: " << drizzle_result_error(res)
00097       << " (" << drizzle_result_error_code(res) << ") " << when << endl;
00098     drizzle_result_free(res);
00099   }
00100   else
00101   {
00102     cerr << "Got error: " << ret << " " << when << endl;
00103   }
00104 
00105   return;
00106 }