~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/serialize/replication_event_reader.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 "replication_event.pb.h"
 
5
using namespace std;
 
6
 
 
7
/* 
 
8
  Example reader application for master.info data.
 
9
*/
 
10
 
 
11
void printRecord(const drizzle::EventList *list) 
 
12
{
 
13
  using namespace drizzle;
 
14
  uint32_t x;
 
15
 
 
16
  for (x= 0; x < list->event_size(); x++) 
 
17
  {
 
18
    const drizzle::Event event= list->event(x);
 
19
 
 
20
    if (x != 0)
 
21
      cout << endl << "##########################################################################################" << endl << endl;
 
22
 
 
23
    switch (event.type())
 
24
    {
 
25
    case Event::DDL:
 
26
      cout << "DDL ";
 
27
      break;
 
28
    case Event::INSERT:
 
29
      {
 
30
        uint32_t x;
 
31
 
 
32
        cout << "INSERT INTO " << event.table() << " (";
 
33
 
 
34
        for (x= 0; x < event.field_names_size() ; x++)
 
35
        {
 
36
          if (x != 0)
 
37
            cout << ", ";
 
38
 
 
39
          cout << event.field_names(x);
 
40
        }
 
41
 
 
42
        cout << ") VALUES " << endl;
 
43
 
 
44
        for (x= 0; x < event.values_size(); x++)
 
45
        {
 
46
          uint32_t y;
 
47
          Event_Value values= event.values(x);
 
48
 
 
49
          if (x != 0)
 
50
            cout << ", ";
 
51
 
 
52
          cout << "(";
 
53
          for (y= 0; y < values.value_size() ; y++)
 
54
          {
 
55
            if (y != 0)
 
56
              cout << ", ";
 
57
 
 
58
            cout << "\"" << values.value(y) << "\"";
 
59
          }
 
60
          cout << ")";
 
61
        }
 
62
 
 
63
        cout << ";" << endl;
 
64
        break;
 
65
      }
 
66
    case Event::DELETE:
 
67
      {
 
68
        uint32_t x;
 
69
        Event_Value values= event.values(0);
 
70
 
 
71
        cout << "DELETE FROM " << event.table() << " WHERE " << event.primary_key() << " IN (";
 
72
 
 
73
        for (x= 0; x < values.value_size() ; x++)
 
74
        {
 
75
          if (x != 0)
 
76
            cout << ", ";
 
77
 
 
78
          cout << "\"" << values.value(x) << "\"";
 
79
        }
 
80
 
 
81
        cout << ")" << endl;
 
82
        break;
 
83
      }
 
84
    case Event::UPDATE:
 
85
      {
 
86
        uint32_t count;
 
87
 
 
88
        for (count= 0; count < event.values_size() ; count++)
 
89
        {
 
90
          uint32_t x;
 
91
          Event_Value values= event.values(count);
 
92
 
 
93
          cout << "UPDATE "  << event.table() << " SET ";
 
94
 
 
95
          for (x= 1; x < values.value_size() ; x++)
 
96
          {
 
97
            if (x != 1)
 
98
              cout << ", ";
 
99
 
 
100
            cout << event.field_names(x - 1) << " = \"" << values.value(x) << "\"";
 
101
          }
 
102
 
 
103
          cout << " WHERE " << event.primary_key() << " = " << values.value(0) << endl;
 
104
        }
 
105
 
 
106
        break;
 
107
      }
 
108
    case Event::COMMIT:
 
109
      cout << "COMMIT" << endl;
 
110
      break;
 
111
    }
 
112
    cout << endl;
 
113
 
 
114
    if (event.has_sql())
 
115
      cout << "Original SQL: " << event.sql() << endl;
 
116
 
 
117
    cout << "AUTOCOMMIT: " << event.autocommit() << endl;
 
118
    cout << "Server id: " << event.server_id() << endl;
 
119
    cout << "Query id: " << event.query_id() << endl;
 
120
    cout << "Transaction id: " << event.transaction_id() << endl;
 
121
    cout << "Schema: " << event.schema() << endl;
 
122
    if (event.type() != Event::DDL)
 
123
      cout << "Table Name: " << event.table() << endl;
 
124
  }
 
125
}
 
126
 
 
127
int main(int argc, char* argv[]) 
 
128
{
 
129
  GOOGLE_PROTOBUF_VERIFY_VERSION;
 
130
 
 
131
  if (argc != 2) 
 
132
  {
 
133
    cerr << "Usage:  " << argv[0] << " replication event log " << endl;
 
134
    return -1;
 
135
  }
 
136
 
 
137
  drizzle::EventList list;
 
138
 
 
139
  {
 
140
    // Read the existing master.info file
 
141
    fstream input(argv[1], ios::in | ios::binary);
 
142
    if (!list.ParseFromIstream(&input)) 
 
143
    {
 
144
      cerr << "Failed to parse master.info." << endl;
 
145
      return -1;
 
146
    }
 
147
  }
 
148
 
 
149
  printRecord(&list);
 
150
 
 
151
  return 0;
 
152
}