~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/replicator/replicator.cc

  • Committer: Jay Pipes
  • Date: 2008-12-18 15:55:03 UTC
  • mto: This revision was merged to the branch mainline in revision 717.
  • Revision ID: jpipes@serialcoder-20081218155503-u45ygyunrdyyvquq
Fix for Bug#308457.  Gave UTF8 enclosure and escape character on LOAD DATA INFILE and changed the error message to be more descriptive

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2006 MySQL AB
 
2
 
 
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.
 
6
 
 
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.
 
11
 
 
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 */
 
15
 
 
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>
 
23
 
 
24
#include <iostream>
 
25
#include <fstream>
 
26
#include <string>
 
27
using namespace std;
 
28
 
 
29
static bool isEnabled;
 
30
static char *log_directory= NULL;
 
31
int log_file= -1;
 
32
 
 
33
static bool write_to_disk(int file, drizzle::EventList *list)
 
34
{
 
35
  std::string buffer;
 
36
  size_t length;
 
37
  size_t written;
 
38
 
 
39
  list->SerializePartialToString(&buffer);
 
40
 
 
41
  length= buffer.length();
 
42
 
 
43
  cout << "Writing record of " << length << "." << endl;
 
44
 
 
45
  if ((written= write(file, &length, sizeof(uint64_t))) != sizeof(uint64_t))
 
46
  {
 
47
    cerr << "Only wrote " << written << " out of " << length << "." << endl;
 
48
    return true;
 
49
  }
 
50
 
 
51
  if ((written= write(file, buffer.c_str(), length)) != length)
 
52
  {
 
53
    cerr << "Only wrote " << written << " out of " << length << "." << endl;
 
54
    return true;
 
55
  }
 
56
 
 
57
  return false;
 
58
}
 
59
 
 
60
static bool statement(Session *session, const char *query, size_t)
 
61
{
 
62
  using namespace drizzle;
 
63
 
 
64
  drizzle::EventList list;
 
65
 
 
66
  if (isEnabled == false)
 
67
    return false;
 
68
  cerr << "Got into statement" <<endl;
 
69
 
 
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);
 
78
 
 
79
  return write_to_disk(log_file, &list);
 
80
}
 
81
 
 
82
static bool session_init(Session *session)
 
83
{
 
84
  using namespace drizzle;
 
85
 
 
86
  if (isEnabled == false)
 
87
    return false;
 
88
 
 
89
  drizzle::EventList *list= new drizzle::EventList;
 
90
  session->setReplicationData(list);
 
91
 
 
92
  drizzle::Event *record= list->add_event();
 
93
 
 
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");
 
101
 
 
102
  return false;
 
103
}
 
104
 
 
105
static bool row_insert(Session *session, Table *)
 
106
{
 
107
  using namespace drizzle;
 
108
 
 
109
  if (isEnabled == false)
 
110
    return false;
 
111
 
 
112
  drizzle::EventList *list= (drizzle::EventList *)session->getReplicationData();
 
113
  drizzle::Event *record= list->add_event();
 
114
 
 
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);
 
122
 
 
123
  return false;
 
124
}
 
125
 
 
126
static bool row_update(Session *session, Table *, 
 
127
                          const unsigned char *, 
 
128
                          const unsigned char *)
 
129
{
 
130
  using namespace drizzle;
 
131
 
 
132
  if (isEnabled == false)
 
133
    return false;
 
134
 
 
135
  drizzle::EventList *list= (drizzle::EventList *)session->getReplicationData();
 
136
  drizzle::Event *record= list->add_event();
 
137
 
 
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);
 
145
 
 
146
  return false;
 
147
}
 
148
 
 
149
static bool row_delete(Session *session, Table *)
 
150
{
 
151
  using namespace drizzle;
 
152
 
 
153
  if (isEnabled == false)
 
154
    return false;
 
155
 
 
156
  drizzle::EventList *list= (drizzle::EventList *)session->getReplicationData();
 
157
  drizzle::Event *record= list->add_event();
 
158
 
 
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);
 
166
 
 
167
  return false;
 
168
}
 
169
 
 
170
static bool end_transaction(Session *session, bool autocommit, bool commit)
 
171
{
 
172
  bool error;
 
173
  using namespace drizzle;
 
174
 
 
175
  if (isEnabled == false)
 
176
    return false;
 
177
 
 
178
  cerr << "Got into end" <<endl;
 
179
 
 
180
  drizzle::EventList *list= (drizzle::EventList *)session->getReplicationData();
 
181
  drizzle::Event *record= list->add_event();
 
182
 
 
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);
 
189
 
 
190
  if (commit)
 
191
  {
 
192
    if (autocommit)
 
193
      record->set_sql("COMMIT");
 
194
    else
 
195
      record->set_sql("AUTOCOMMIT");
 
196
  }
 
197
  else
 
198
    record->set_sql("ROLLBACK");
 
199
 
 
200
  error= write_to_disk(log_file, list);
 
201
 
 
202
  session->setReplicationData(NULL);
 
203
  delete(list);
 
204
 
 
205
  return error;
 
206
}
 
207
 
 
208
static int init(void *p)
 
209
{
 
210
  replicator_t *repl = (replicator_t *)p;
 
211
 
 
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;
 
218
 
 
219
 
 
220
  if (isEnabled)
 
221
  {
 
222
    using std::string;
 
223
    string logname;
 
224
 
 
225
    logname.append(log_directory ? log_directory : "/tmp");
 
226
    logname.append("/replication_log");
 
227
 
 
228
    if ((log_file= open(logname.c_str(), O_TRUNC|O_CREAT|O_SYNC|O_WRONLY, S_IRWXU)) == -1)
 
229
    {
 
230
      cerr << "Can not open file: " << logname.c_str() << endl;
 
231
      exit(0);
 
232
    }
 
233
  }
 
234
 
 
235
  return 0;
 
236
}
 
237
 
 
238
static int deinit(void *)
 
239
{
 
240
  if (log_file != -1)
 
241
    close(log_file);
 
242
 
 
243
  return 0;
 
244
}
 
245
 
 
246
static DRIZZLE_SYSVAR_BOOL(
 
247
  enabled,
 
248
  isEnabled,
 
249
  PLUGIN_VAR_NOCMDARG,
 
250
  N_("Enable Replicator"),
 
251
  NULL, /* check func */
 
252
  NULL, /* update func */
 
253
  false /* default */);
 
254
 
 
255
static DRIZZLE_SYSVAR_STR(
 
256
  directory,
 
257
  log_directory,
 
258
  PLUGIN_VAR_READONLY,
 
259
  N_("Directory to place replication logs."),
 
260
  NULL, /* check func */
 
261
  NULL, /* update func*/
 
262
  NULL /* default */);
 
263
 
 
264
static struct st_mysql_sys_var* system_variables[]= {
 
265
  DRIZZLE_SYSVAR(directory),
 
266
  DRIZZLE_SYSVAR(enabled),
 
267
  NULL,
 
268
};
 
269
 
 
270
mysql_declare_plugin(replicator)
 
271
{
 
272
  DRIZZLE_REPLICATOR_PLUGIN,
 
273
  "replicator",
 
274
  "0.1",
 
275
  "Brian Aker",
 
276
  "Basic replication module",
 
277
  PLUGIN_LICENSE_GPL,
 
278
  init, /* Plugin Init */
 
279
  deinit, /* Plugin Deinit */
 
280
  NULL,   /* status variables */
 
281
  system_variables,   /* system variables */
 
282
  NULL    /* config options */
 
283
}
 
284
mysql_declare_plugin_end;