1
/* Copyright (C) 2006 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
16
#include <drizzled/server_includes.h>
17
#include <drizzled/gettext.h>
18
#include <drizzled/session.h>
19
#include <drizzled/error.h>
20
#include <drizzled/plugin_replicator.h>
21
#include <drizzled/serialize/serialize.h>
28
static bool isEnabled;
29
static char *log_directory= NULL;
32
static bool write_to_disk(int file, drizzle::EventList *list)
38
list->SerializePartialToString(&buffer);
40
length= buffer.length();
42
cout << "Writing record of " << length << "." << endl;
44
if ((written= write(file, &length, sizeof(uint64_t))) != sizeof(uint64_t))
46
cerr << "Only wrote " << written << " out of " << length << "." << endl;
50
if ((written= write(file, buffer.c_str(), length)) != length)
52
cerr << "Only wrote " << written << " out of " << length << "." << endl;
59
static bool statement(Session *session, const char *query, size_t)
61
using namespace drizzle;
63
drizzle::EventList list;
65
if (isEnabled == false)
67
cerr << "Got into statement" <<endl;
69
drizzle::Event *record= list.add_event();
70
record->set_type(Event::DDL);
71
record->set_autocommit(true);
72
record->set_server_id("localhost");
73
record->set_query_id(10);
74
record->set_transaction_id("junk");
75
record->set_schema(session->db);
76
record->set_sql(query);
78
return write_to_disk(log_file, &list);
81
static bool session_init(Session *session)
83
using namespace drizzle;
85
if (isEnabled == false)
88
drizzle::EventList *list= new drizzle::EventList;
89
session->setReplicationData(list);
91
drizzle::Event *record= list->add_event();
93
record->set_type(Event::DDL);
94
record->set_autocommit(true);
95
record->set_server_id("localhost");
96
record->set_query_id(10);
97
record->set_transaction_id("junk");
98
record->set_schema(session->db);
99
record->set_sql("BEGIN");
104
static bool row_insert(Session *session, Table *)
106
using namespace drizzle;
108
if (isEnabled == false)
111
drizzle::EventList *list= (drizzle::EventList *)session->getReplicationData();
112
drizzle::Event *record= list->add_event();
114
record->set_type(Event::INSERT);
115
record->set_autocommit(true);
116
record->set_server_id("localhost");
117
record->set_query_id(10);
118
record->set_transaction_id("junk");
119
record->set_schema(session->db);
120
record->set_sql(session->query);
125
static bool row_update(Session *session, Table *,
126
const unsigned char *,
127
const unsigned char *)
129
using namespace drizzle;
131
if (isEnabled == false)
134
drizzle::EventList *list= (drizzle::EventList *)session->getReplicationData();
135
drizzle::Event *record= list->add_event();
137
record->set_type(Event::UPDATE);
138
record->set_autocommit(true);
139
record->set_server_id("localhost");
140
record->set_query_id(10);
141
record->set_transaction_id("junk");
142
record->set_schema(session->db);
143
record->set_sql(session->query);
148
static bool row_delete(Session *session, Table *)
150
using namespace drizzle;
152
if (isEnabled == false)
155
drizzle::EventList *list= (drizzle::EventList *)session->getReplicationData();
156
drizzle::Event *record= list->add_event();
158
record->set_type(Event::DELETE);
159
record->set_autocommit(true);
160
record->set_server_id("localhost");
161
record->set_query_id(10);
162
record->set_transaction_id("junk");
163
record->set_schema(session->db);
164
record->set_sql(session->query);
169
static bool end_transaction(Session *session, bool autocommit, bool commit)
172
using namespace drizzle;
174
if (isEnabled == false)
177
cerr << "Got into end" <<endl;
179
drizzle::EventList *list= (drizzle::EventList *)session->getReplicationData();
180
drizzle::Event *record= list->add_event();
182
record->set_type(Event::DELETE);
183
record->set_autocommit(true);
184
record->set_server_id("localhost");
185
record->set_query_id(10);
186
record->set_transaction_id("junk");
187
record->set_schema(session->db);
192
record->set_sql("COMMIT");
194
record->set_sql("AUTOCOMMIT");
197
record->set_sql("ROLLBACK");
199
error= write_to_disk(log_file, list);
201
session->setReplicationData(NULL);
207
static int init(void *p)
209
replicator_t *repl = (replicator_t *)p;
211
repl->statement= statement;
212
repl->session_init= session_init;
213
repl->row_insert= row_insert;
214
repl->row_delete= row_delete;
215
repl->row_update= row_update;
216
repl->end_transaction= end_transaction;
224
logname.append(log_directory ? log_directory : "/tmp");
225
logname.append("/replication_log");
227
if ((log_file= open(logname.c_str(), O_TRUNC|O_CREAT|O_SYNC|O_WRONLY, S_IRWXU)) == -1)
229
cerr << "Can not open file: " << logname.c_str() << endl;
237
static int deinit(void *)
245
static DRIZZLE_SYSVAR_BOOL(
249
N_("Enable Replicator"),
250
NULL, /* check func */
251
NULL, /* update func */
252
false /* default */);
254
static DRIZZLE_SYSVAR_STR(
258
N_("Directory to place replication logs."),
259
NULL, /* check func */
260
NULL, /* update func*/
263
static struct st_mysql_sys_var* system_variables[]= {
264
DRIZZLE_SYSVAR(directory),
265
DRIZZLE_SYSVAR(enabled),
269
mysql_declare_plugin(replicator)
271
DRIZZLE_REPLICATOR_PLUGIN,
275
"Basic replication module",
277
init, /* Plugin Init */
278
deinit, /* Plugin Deinit */
279
NULL, /* status variables */
280
system_variables, /* system variables */
281
NULL /* config options */
283
mysql_declare_plugin_end;