/
libdrizzle_client.cc
191 lines (157 loc) · 4.89 KB
/
libdrizzle_client.cc
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/* BEGIN LICENSE
* Copyright (C) 2011-2013 Percona Ireland Ltd.
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2, as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranties of
* MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
* END LICENSE */
#include <percona_playback/plugin.h>
#include <percona_playback/query_result.h>
#include <percona_playback/gettext.h>
#include <boost/program_options.hpp>
#include <stdio.h>
namespace po= boost::program_options;
#include <percona_playback/db_thread.h>
#include <libdrizzle/drizzle.h>
#include <libdrizzle/drizzle_client.h>
class QueryResult;
class LibDrizzleOptions;
class LibDrizzleDBThread : public DBThread
{
private:
drizzle_st *driz;
drizzle_con_st *client;
LibDrizzleOptions *options;
public:
LibDrizzleDBThread(uint64_t _thread_id, LibDrizzleOptions *opt) :
DBThread(_thread_id, boost::shared_ptr<Queries>(new Queries())),
driz(NULL),
options(opt)
{
}
bool connect();
void disconnect();
void execute_query(const std::string &query, QueryResult *r,
const QueryResult &expected_result);
};
class LibDrizzleOptions
{
public:
LibDrizzleOptions() :
host("127.0.0.1"),
user("root"),
password(""),
schema("test"),
port(3306)
{
}
std::string host;
std::string user;
std::string password;
std::string schema;
unsigned int port;
};
bool LibDrizzleDBThread::connect()
{
driz= drizzle_create(NULL);
assert(driz != NULL);
client= drizzle_con_add_tcp(driz, NULL,
options->host.c_str(),
options->port,
options->user.c_str(),
options->password.c_str(),
options->schema.c_str(), DRIZZLE_CON_MYSQL);
return true;
}
void LibDrizzleDBThread::disconnect()
{
drizzle_free(driz);
}
void LibDrizzleDBThread::execute_query(const std::string &query, QueryResult *r,
const QueryResult &)
{
drizzle_result_st result;
drizzle_return_t ret;
drizzle_query(client, &result, query.c_str(), query.length(), &ret);
if(ret != 0)
{
r->setError(ret);
}
else
{
drizzle_result_buffer(&result);
r->setError(ret);
r->setWarningCount(drizzle_result_warning_count(&result));
r->setRowsSent(drizzle_result_row_count(&result));
}
drizzle_result_free_all(client);
}
class LibDrizzleDBClientPlugin : public percona_playback::DBClientPlugin
{
private:
LibDrizzleOptions options;
public:
LibDrizzleDBClientPlugin(std::string _name) : DBClientPlugin(_name) {};
virtual DBThread* create(uint64_t _thread_id) {
return new LibDrizzleDBThread(_thread_id, &options);
}
virtual boost::program_options::options_description* getProgramOptions() {
static po::options_description libdrizzle_options(_("libdrizzle Client Options"));
libdrizzle_options.add_options()
("libdrizzle-host", po::value<std::string>(), _("Hostname of MySQL/Drizzle server"))
("libdrizzle-username", po::value<std::string>(), _("Username to connect to MySQL/Drizzle"))
("libdrizzle-password", po::value<std::string>(), _("Password for MySQL/Drizzle user"))
("libdrizzle-schema", po::value<std::string>(), _("Schema to connect to"))
("libdrizzle-port", po::value<unsigned int>(), _("port number"))
;
return &libdrizzle_options;
}
virtual int processOptions(boost::program_options::variables_map &vm) {
if (!active &&
(vm.count("libdrizzle-host") ||
vm.count("libdrizzle-username") ||
vm.count("libdrizzle-password") ||
vm.count("libdrizzle-schema") ||
vm.count("libdrizzle-port")))
{
fprintf(stderr,
gettext("libdrizzle_client plugin is not selected, "
"you shouldn't use this plugin-related "
"command line options\n"));
return -1;
}
if (vm.count("libdrizzle-host"))
{
options.host= vm["libdrizzle-host"].as<std::string>();
}
if (vm.count("libdrizzle-username"))
{
options.user= vm["libdrizzle-username"].as<std::string>();
}
if (vm.count("libdrizzle-password"))
{
options.password= vm["libdrizzle-password"].as<std::string>();
}
if (vm.count("libdrizzle-schema"))
{
options.schema= vm["libdrizzle-schema"].as<std::string>();
}
if (vm.count("libdrizzle-port"))
{
options.port= vm["libdrizzle-port"].as<unsigned int>();
}
return 0;
}
};
static void init_plugin(percona_playback::PluginRegistry &r)
{
r.add("libdrizzle", new LibDrizzleDBClientPlugin("libdrizzle"));
}
PERCONA_PLAYBACK_PLUGIN(init_plugin);