~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/serialize/replication_event_writer.cc

  • Committer: Elan Ruusamäe
  • Date: 2008-12-02 20:06:31 UTC
  • mfrom: (637 drizzle)
  • mto: (641.3.10 devel)
  • mto: This revision was merged to the branch mainline in revision 649.
  • Revision ID: glen@haarber.alkohol.ee-20081202200631-f6h3vbnjaojvk3uq
- merge from trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <iostream>
 
2
#include <fstream>
 
3
#include <string>
 
4
#include <uuid/uuid.h>
 
5
 
 
6
#include "replication_event.pb.h"
 
7
using namespace std;
 
8
 
 
9
static uint64_t query_id= 0;
 
10
char transaction_id[37];
 
11
 
 
12
/* 
 
13
  Example script for reader a Drizzle master replication list.
 
14
*/
 
15
 
 
16
void write_ddl(drizzle::Event *record, const char *sql)
 
17
{
 
18
  uuid_t uu;
 
19
 
 
20
  uuid_generate_time(uu);
 
21
  uuid_unparse(uu, transaction_id);
 
22
 
 
23
  using namespace drizzle;
 
24
  record->set_type(Event::DDL);
 
25
  record->set_autocommit(true);
 
26
  record->set_server_id("localhost");
 
27
  record->set_query_id(query_id++);
 
28
  record->set_transaction_id(transaction_id);
 
29
  record->set_schema("test");
 
30
  record->set_sql(sql);
 
31
}
 
32
 
 
33
void write_insert(drizzle::Event *record, const char *trx)
 
34
{
 
35
  using namespace drizzle;
 
36
  Event::Value *value;
 
37
 
 
38
  record->set_type(Event::INSERT);
 
39
  record->set_autocommit(true);
 
40
  record->set_server_id("localhost");
 
41
  record->set_query_id(query_id++);
 
42
  record->set_transaction_id(trx);
 
43
  record->set_schema("test");
 
44
  record->set_table("t1");
 
45
  record->set_sql("INSERT INTO t1 (a) VALUES (1) (2)");
 
46
 
 
47
  /* Add Field Names */
 
48
  record->add_field_names("a");
 
49
 
 
50
  /* Add values (first row) */
 
51
  value= record->add_values();
 
52
  value->add_value("1");
 
53
 
 
54
  /* Add values (second row) */
 
55
  value= record->add_values();
 
56
  value->add_value("2");
 
57
}
 
58
 
 
59
void write_delete(drizzle::Event *record, const char *trx)
 
60
{
 
61
  using namespace drizzle;
 
62
  uuid_t uu;
 
63
  Event::Value *value;
 
64
 
 
65
  record->set_type(Event::DELETE);
 
66
  record->set_autocommit(true);
 
67
  record->set_server_id("localhost");
 
68
  record->set_query_id(query_id++);
 
69
  record->set_transaction_id(trx);
 
70
  record->set_schema("test");
 
71
  record->set_table("t1");
 
72
  record->set_sql("DELETE FROM t1 WHERE a IN (1, 2)");
 
73
 
 
74
  /* Add Field Names */
 
75
  record->set_primary_key("a");
 
76
 
 
77
  /* Add values for IN() */
 
78
  value= record->add_values();
 
79
  value->add_value("1");
 
80
  value->add_value("2");
 
81
}
 
82
 
 
83
void write_update(drizzle::Event *record, const char *trx)
 
84
{
 
85
  using namespace drizzle;
 
86
  Event::Value *value;
 
87
 
 
88
  record->set_type(Event::UPDATE);
 
89
  record->set_autocommit(true);
 
90
  record->set_server_id("localhost");
 
91
  record->set_query_id(query_id++);
 
92
  record->set_transaction_id(trx);
 
93
  record->set_schema("test");
 
94
  record->set_table("t1");
 
95
  record->set_sql("UPDATE t1 SET a=5 WHERE a = 1 ");
 
96
  record->set_primary_key("a");
 
97
 
 
98
  /* Add Field Names */
 
99
  record->add_field_names("a");
 
100
 
 
101
  /* Add values (first row) */
 
102
  value= record->add_values();
 
103
  value->add_value("1"); // The first value is always the primary key comparison value
 
104
  value->add_value("5");
 
105
 
 
106
  /* Add values (second row) */
 
107
  value= record->add_values();
 
108
  value->add_value("2");
 
109
  value->add_value("6");
 
110
}
 
111
 
 
112
int main(int argc, char* argv[]) 
 
113
{
 
114
  GOOGLE_PROTOBUF_VERIFY_VERSION;
 
115
 
 
116
  if (argc != 2) {
 
117
    cerr << "Usage:  " << argv[0] << " REPLICATION_EVENT_LOG " << endl;
 
118
    return -1;
 
119
  }
 
120
 
 
121
  drizzle::EventList list;
 
122
 
 
123
  write_ddl(list.add_event(), "CREATE TABLE A (a int) ENGINE=innodb");
 
124
  write_insert(list.add_event(), transaction_id);
 
125
  write_delete(list.add_event(), transaction_id);
 
126
  write_update(list.add_event(), transaction_id);
 
127
 
 
128
  fstream output(argv[1], ios::out | ios::trunc | ios::binary);
 
129
  if (!list.SerializeToOstream(&output)) 
 
130
  {
 
131
    cerr << "Failed to write replication event log." << endl;
 
132
    return -1;
 
133
  }
 
134
 
 
135
  return 0;
 
136
}