~fallenpegasus/drizzle/work-foo

« back to all changes in this revision

Viewing changes to plugin/transaction_log/utilities/transaction_log_connection.cc

  • Committer: Continuous Integration
  • Date: 2012-03-04 12:46:01 UTC
  • mfrom: (2516.2.1 workspace)
  • Revision ID: ci@drizzle.org-20120304124601-nqj8znhwmhhbsbha
Merge staging tree to trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 
 *
4
 
 *  Copyright (C) 2010 Joseph Daly <skinny.moey@gmail.com>
5
 
 *
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.
9
 
 *
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.
14
 
 *
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
18
 
 */
19
 
 
20
 
#include <config.h>
21
 
#include <plugin/transaction_log/utilities/transaction_log_connection.h>
22
 
#include <iostream>
23
 
 
24
 
using namespace std;
25
 
 
26
 
TransactionLogConnection::TransactionLogConnection(string &host, uint16_t port,
27
 
                                                   string &username, string &password,
28
 
                                                   bool drizzle_protocol) :
29
 
  hostName(host),
30
 
  drizzleProtocol(drizzle_protocol)
31
 
{
32
 
  if (host.empty())
33
 
  {
34
 
    host= "localhost";
35
 
  }
36
 
 
37
 
  drizzle= drizzle_create();
38
 
 
39
 
  if (drizzle == NULL)
40
 
  {
41
 
    errorHandler(NULL, DRIZZLE_RETURN_MEMORY, "drizzle_create() failed");
42
 
    throw "drizzle_create() failed";
43
 
  }
44
 
 
45
 
  connection= drizzle_con_create(drizzle);
46
 
  if (connection == NULL)
47
 
  {
48
 
    errorHandler(NULL, DRIZZLE_RETURN_MEMORY, "drizzle_create() failed");
49
 
    throw "drizzle_con_create() failed";
50
 
  }
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());
53
 
 
54
 
  drizzle_con_add_options(connection,
55
 
                          drizzle_protocol ? DRIZZLE_CON_EXPERIMENTAL : DRIZZLE_CON_MYSQL);
56
 
 
57
 
  drizzle_return_t ret= drizzle_con_connect(connection);
58
 
 
59
 
  if (ret != DRIZZLE_RETURN_OK)
60
 
  {
61
 
    errorHandler(NULL, ret, "when trying to connect");
62
 
    throw 1;
63
 
  }
64
 
}
65
 
 
66
 
void TransactionLogConnection::query(const std::string &str_query,
67
 
                                     drizzle_result_st *result)
68
 
{
69
 
  drizzle_return_t ret;
70
 
 
71
 
  if (drizzle_query_str(connection, result, str_query.c_str(), &ret) == NULL ||
72
 
      ret != DRIZZLE_RETURN_OK)
73
 
  {
74
 
    if (ret == DRIZZLE_RETURN_ERROR_CODE)
75
 
    {
76
 
      cerr << "Error executing query: " <<
77
 
        drizzle_result_error(result) << endl;
78
 
      drizzle_result_free(result);
79
 
    }
80
 
    else
81
 
    {
82
 
      cerr << "Error executing query: " <<
83
 
        drizzle_con_error(connection) << endl;
84
 
      drizzle_result_free(result);
85
 
    }
86
 
    return;
87
 
  }
88
 
 
89
 
  if (drizzle_result_buffer(result) != DRIZZLE_RETURN_OK)
90
 
  {
91
 
    cerr << "Could not buffer result: " <<
92
 
        drizzle_con_error(connection) << endl;
93
 
    drizzle_result_free(result);
94
 
    return;
95
 
  }
96
 
}
97
 
 
98
 
void TransactionLogConnection::errorHandler(drizzle_result_st *res,
99
 
                                            drizzle_return_t ret, const char *when)
100
 
{
101
 
  if (res == NULL)
102
 
  {
103
 
    cerr << "Got error: " << drizzle_con_error(connection) << " " << when << endl;
104
 
  }
105
 
  else if (ret == DRIZZLE_RETURN_ERROR_CODE)
106
 
  {
107
 
    cerr << "Got error: " << drizzle_result_error(res) << " (" << drizzle_result_error_code(res) << ") " << when << endl;
108
 
    drizzle_result_free(res);
109
 
  }
110
 
  else
111
 
  {
112
 
    cerr << "Got error: " << ret << " " << when << endl;
113
 
  }
114
 
}