10
#include <drizzled/serialize/replication_event.pb.h>
13
static uint64_t query_id= 0;
14
char transaction_id[37];
17
Example script for reader a Drizzle master replication list.
20
void write_ddl(drizzle::Event *record, const char *sql)
24
uuid_generate_time(uu);
25
uuid_unparse(uu, transaction_id);
27
using namespace drizzle;
28
record->set_type(Event::DDL);
29
record->set_autocommit(true);
30
record->set_server_id("localhost");
31
record->set_query_id(query_id++);
32
record->set_transaction_id(transaction_id);
33
record->set_schema("test");
37
void write_insert(drizzle::Event *record, const char *trx)
39
using namespace drizzle;
42
record->set_type(Event::INSERT);
43
record->set_autocommit(true);
44
record->set_server_id("localhost");
45
record->set_query_id(query_id++);
46
record->set_transaction_id(trx);
47
record->set_schema("test");
48
record->set_table("t1");
49
record->set_sql("INSERT INTO t1 (a) VALUES (1) (2)");
52
record->add_field_names("a");
54
/* Add values (first row) */
55
value= record->add_values();
56
value->add_value("1");
58
/* Add values (second row) */
59
value= record->add_values();
60
value->add_value("2");
63
void write_delete(drizzle::Event *record, const char *trx)
65
using namespace drizzle;
69
record->set_type(Event::DELETE);
70
record->set_autocommit(true);
71
record->set_server_id("localhost");
72
record->set_query_id(query_id++);
73
record->set_transaction_id(trx);
74
record->set_schema("test");
75
record->set_table("t1");
76
record->set_sql("DELETE FROM t1 WHERE a IN (1, 2)");
79
record->set_primary_key("a");
81
/* Add values for IN() */
82
value= record->add_values();
83
value->add_value("1");
84
value->add_value("2");
87
void write_update(drizzle::Event *record, const char *trx)
89
using namespace drizzle;
92
record->set_type(Event::UPDATE);
93
record->set_autocommit(true);
94
record->set_server_id("localhost");
95
record->set_query_id(query_id++);
96
record->set_transaction_id(trx);
97
record->set_schema("test");
98
record->set_table("t1");
99
record->set_sql("UPDATE t1 SET a=5 WHERE a = 1 ");
100
record->set_primary_key("a");
102
/* Add Field Names */
103
record->add_field_names("a");
105
/* Add values (first row) */
106
value= record->add_values();
107
value->add_value("1"); // The first value is always the primary key comparison value
108
value->add_value("5");
110
/* Add values (second row) */
111
value= record->add_values();
112
value->add_value("2");
113
value->add_value("6");
116
void write_to_disk(int file, drizzle::EventList *list)
122
list->SerializePartialToString(&buffer);
124
length= buffer.length();
126
cout << "Writing record of " << length << "." << endl;
128
if ((written= write(file, &length, sizeof(uint64_t))) != sizeof(uint64_t))
130
cerr << "Only wrote " << written << " out of " << length << "." << endl;
134
if ((written= write(file, buffer.c_str(), length)) != length)
136
cerr << "Only wrote " << written << " out of " << length << "." << endl;
142
int main(int argc, char* argv[])
144
GOOGLE_PROTOBUF_VERIFY_VERSION;
149
cerr << "Usage: " << argv[0] << " REPLICATION_EVENT_LOG " << endl;
153
if ((file= open(argv[1], O_APPEND|O_CREAT|O_SYNC|O_WRONLY, S_IRWXU)) == -1)
155
cerr << "Can not open file: " << argv[0] << endl;
159
drizzle::EventList list;
161
/* Write first set of records */
162
write_ddl(list.add_event(), "CREATE TABLE A (a int) ENGINE=innodb");
163
write_insert(list.add_event(), transaction_id);
165
write_to_disk(file, &list);
167
/* Write Second set of records */
168
write_ddl(list.add_event(), "CREATE TABLE A (a int) ENGINE=innodb");
169
write_delete(list.add_event(), transaction_id);
170
write_update(list.add_event(), transaction_id);
172
write_to_disk(file, &list);