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
#define DRIZZLE_SERVER 1 /* for session variable max_allowed_packet */
17
#include <drizzled/server_includes.h>
18
#include <drizzled/gettext.h>
19
#include <drizzled/session.h>
20
#include <drizzled/error.h>
21
#include <drizzled/plugin_replicator.h>
22
#include <drizzled/serialize/serialize.h>
29
static bool isEnabled;
30
static char *log_directory= NULL;
33
static bool write_to_disk(int file, drizzle::EventList *list)
39
list->SerializePartialToString(&buffer);
41
length= buffer.length();
43
cout << "Writing record of " << length << "." << endl;
45
if ((written= write(file, &length, sizeof(uint64_t))) != sizeof(uint64_t))
47
cerr << "Only wrote " << written << " out of " << length << "." << endl;
51
if ((written= write(file, buffer.c_str(), length)) != length)
53
cerr << "Only wrote " << written << " out of " << length << "." << endl;
60
static bool statement(Session *session, const char *query, size_t)
62
using namespace drizzle;
64
drizzle::EventList list;
66
if (isEnabled == false)
68
cerr << "Got into statement" <<endl;
70
drizzle::Event *record= list.add_event();
71
record->set_type(Event::DDL);
72
record->set_autocommit(true);
73
record->set_server_id("localhost");
74
record->set_query_id(10);
75
record->set_transaction_id("junk");
76
record->set_schema(session->db);
77
record->set_sql(query);
79
return write_to_disk(log_file, &list);
82
static bool session_init(Session *session)
84
using namespace drizzle;
86
if (isEnabled == false)
89
drizzle::EventList *list= new drizzle::EventList;
90
session->setReplicationData(list);
92
drizzle::Event *record= list->add_event();
94
record->set_type(Event::DDL);
95
record->set_autocommit(true);
96
record->set_server_id("localhost");
97
record->set_query_id(10);
98
record->set_transaction_id("junk");
99
record->set_schema(session->db);
100
record->set_sql("BEGIN");
105
static bool row_insert(Session *session, Table *)
107
using namespace drizzle;
109
if (isEnabled == false)
112
drizzle::EventList *list= (drizzle::EventList *)session->getReplicationData();
113
drizzle::Event *record= list->add_event();
115
record->set_type(Event::INSERT);
116
record->set_autocommit(true);
117
record->set_server_id("localhost");
118
record->set_query_id(10);
119
record->set_transaction_id("junk");
120
record->set_schema(session->db);
121
record->set_sql(session->query);
126
static bool row_update(Session *session, Table *,
127
const unsigned char *,
128
const unsigned char *)
130
using namespace drizzle;
132
if (isEnabled == false)
135
drizzle::EventList *list= (drizzle::EventList *)session->getReplicationData();
136
drizzle::Event *record= list->add_event();
138
record->set_type(Event::UPDATE);
139
record->set_autocommit(true);
140
record->set_server_id("localhost");
141
record->set_query_id(10);
142
record->set_transaction_id("junk");
143
record->set_schema(session->db);
144
record->set_sql(session->query);
149
static bool row_delete(Session *session, Table *)
151
using namespace drizzle;
153
if (isEnabled == false)
156
drizzle::EventList *list= (drizzle::EventList *)session->getReplicationData();
157
drizzle::Event *record= list->add_event();
159
record->set_type(Event::DELETE);
160
record->set_autocommit(true);
161
record->set_server_id("localhost");
162
record->set_query_id(10);
163
record->set_transaction_id("junk");
164
record->set_schema(session->db);
165
record->set_sql(session->query);
170
static bool end_transaction(Session *session, bool autocommit, bool commit)
173
using namespace drizzle;
175
if (isEnabled == false)
178
cerr << "Got into end" <<endl;
180
drizzle::EventList *list= (drizzle::EventList *)session->getReplicationData();
181
drizzle::Event *record= list->add_event();
183
record->set_type(Event::DELETE);
184
record->set_autocommit(true);
185
record->set_server_id("localhost");
186
record->set_query_id(10);
187
record->set_transaction_id("junk");
188
record->set_schema(session->db);
193
record->set_sql("COMMIT");
195
record->set_sql("AUTOCOMMIT");
198
record->set_sql("ROLLBACK");
200
error= write_to_disk(log_file, list);
202
session->setReplicationData(NULL);
208
static int init(void *p)
210
replicator_t *repl = (replicator_t *)p;
212
repl->statement= statement;
213
repl->session_init= session_init;
214
repl->row_insert= row_insert;
215
repl->row_delete= row_delete;
216
repl->row_update= row_update;
217
repl->end_transaction= end_transaction;
225
logname.append(log_directory ? log_directory : "/tmp");
226
logname.append("/replication_log");
228
if ((log_file= open(logname.c_str(), O_TRUNC|O_CREAT|O_SYNC|O_WRONLY, S_IRWXU)) == -1)
230
cerr << "Can not open file: " << logname.c_str() << endl;
238
static int deinit(void *)
246
static DRIZZLE_SYSVAR_BOOL(
250
N_("Enable Replicator"),
251
NULL, /* check func */
252
NULL, /* update func */
253
false /* default */);
255
static DRIZZLE_SYSVAR_STR(
259
N_("Directory to place replication logs."),
260
NULL, /* check func */
261
NULL, /* update func*/
264
static struct st_mysql_sys_var* system_variables[]= {
265
DRIZZLE_SYSVAR(directory),
266
DRIZZLE_SYSVAR(enabled),
270
mysql_declare_plugin(replicator)
272
DRIZZLE_REPLICATOR_PLUGIN,
276
"Basic replication module",
278
init, /* Plugin Init */
279
deinit, /* Plugin Deinit */
280
NULL, /* status variables */
281
system_variables, /* system variables */
282
NULL /* config options */
284
mysql_declare_plugin_end;