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
|
#include <iostream>
#include <fstream>
#include <string>
#include <uuid/uuid.h>
#include "replication_event.pb.h"
using namespace std;
static uint64_t query_id= 0;
char transaction_id[37];
/*
Example script for reader a Drizzle master replication list.
*/
void write_ddl(drizzle::Event *record, const char *sql)
{
uuid_t uu;
uuid_generate_time(uu);
uuid_unparse(uu, transaction_id);
using namespace drizzle;
record->set_type(Event::DDL);
record->set_autocommit(true);
record->set_server_id("localhost");
record->set_query_id(query_id++);
record->set_transaction_id(transaction_id);
record->set_schema("test");
record->set_sql(sql);
}
void write_insert(drizzle::Event *record, const char *trx)
{
using namespace drizzle;
Event::Value *value;
record->set_type(Event::INSERT);
record->set_autocommit(true);
record->set_server_id("localhost");
record->set_query_id(query_id++);
record->set_transaction_id(trx);
record->set_schema("test");
record->set_table("t1");
record->set_sql("INSERT INTO t1 (a) VALUES (1) (2)");
/* Add Field Names */
record->add_field_names("a");
/* Add values (first row) */
value= record->add_values();
value->add_value("1");
/* Add values (second row) */
value= record->add_values();
value->add_value("2");
}
void write_delete(drizzle::Event *record, const char *trx)
{
using namespace drizzle;
uuid_t uu;
Event::Value *value;
record->set_type(Event::DELETE);
record->set_autocommit(true);
record->set_server_id("localhost");
record->set_query_id(query_id++);
record->set_transaction_id(trx);
record->set_schema("test");
record->set_table("t1");
record->set_sql("DELETE FROM t1 WHERE a IN (1, 2)");
/* Add Field Names */
record->set_primary_key("a");
/* Add values for IN() */
value= record->add_values();
value->add_value("1");
value->add_value("2");
}
void write_update(drizzle::Event *record, const char *trx)
{
using namespace drizzle;
Event::Value *value;
record->set_type(Event::UPDATE);
record->set_autocommit(true);
record->set_server_id("localhost");
record->set_query_id(query_id++);
record->set_transaction_id(trx);
record->set_schema("test");
record->set_table("t1");
record->set_sql("UPDATE t1 SET a=5 WHERE a = 1 ");
record->set_primary_key("a");
/* Add Field Names */
record->add_field_names("a");
/* Add values (first row) */
value= record->add_values();
value->add_value("1"); // The first value is always the primary key comparison value
value->add_value("5");
/* Add values (second row) */
value= record->add_values();
value->add_value("2");
value->add_value("6");
}
int main(int argc, char* argv[])
{
GOOGLE_PROTOBUF_VERIFY_VERSION;
if (argc != 2) {
cerr << "Usage: " << argv[0] << " REPLICATION_EVENT_LOG " << endl;
return -1;
}
drizzle::EventList list;
write_ddl(list.add_event(), "CREATE TABLE A (a int) ENGINE=innodb");
write_insert(list.add_event(), transaction_id);
write_delete(list.add_event(), transaction_id);
write_update(list.add_event(), transaction_id);
fstream output(argv[1], ios::out | ios::trunc | ios::binary);
if (!list.SerializeToOstream(&output))
{
cerr << "Failed to write replication event log." << endl;
return -1;
}
return 0;
}
|