~drizzle-trunk/drizzle/development

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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include <drizzled/global.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <iostream>
#include <fstream>
#include <string>
#include <unistd.h>
#include <drizzled/serialize/replication_event.pb.h>

using namespace std;
using namespace drizzle;

/*
  Example reader application for master.info data.
*/

void printRecord(const ::drizzle::EventList *list)
{
  int32_t e_size;

  for (e_size= 0; e_size < list->event_size(); e_size++)
  {
    const Event event= list->event(e_size);

    if (e_size != 0)
      cout << endl << "##########################################################################################" << endl << endl;

    switch (event.type())
    {
    case Event::DDL:
      cout << "DDL ";
      break;
    case Event::INSERT:
      {
        int32_t x;

        cout << "INSERT INTO " << event.table() << " (";

        for (x= 0; x < event.field_names_size() ; x++)
        {
          if (x != 0)
            cout << ", ";

          cout << event.field_names(x);
        }

        cout << ") VALUES " << endl;

        for (x= 0; x < event.values_size(); x++)
        {
          int32_t y;
          Event_Value values= event.values(x);

          if (x != 0)
            cout << ", ";

          cout << "(";
          for (y= 0; y < values.value_size() ; y++)
          {
            if (y != 0)
              cout << ", ";

            cout << "\"" << values.value(y) << "\"";
          }
          cout << ")";
        }

        cout << ";" << endl;
        break;
      }
    case Event::DELETE:
      {
        int32_t x;
        Event_Value values= event.values(0);

        cout << "DELETE FROM " << event.table() << " WHERE " << event.primary_key() << " IN (";

        for (x= 0; x < values.value_size() ; x++)
        {
          if (x != 0)
            cout << ", ";

          cout << "\"" << values.value(x) << "\"";
        }

        cout << ")" << endl;
        break;
      }
    case Event::UPDATE:
      {
        int32_t count;

        for (count= 0; count < event.values_size() ; count++)
        {
          int32_t x;
          Event_Value values= event.values(count);

          cout << "UPDATE "  << event.table() << " SET ";

          for (x= 1; x < values.value_size() ; x++)
          {
            if (x != 1)
              cout << ", ";

            cout << event.field_names(x - 1) << " = \"" << values.value(x) << "\"";
          }

          cout << " WHERE " << event.primary_key() << " = " << values.value(0) << endl;
        }

        break;
      }
    case Event::COMMIT:
      cout << "COMMIT" << endl;
      break;
    }
    cout << endl;

    if (event.has_sql())
      cout << "Original SQL: " << event.sql() << endl;

    cout << "AUTOCOMMIT: " << event.autocommit() << endl;
    cout << "Server id: " << event.server_id() << endl;
    cout << "Query id: " << event.query_id() << endl;
    cout << "Transaction id: " << event.transaction_id() << endl;
    cout << "Schema: " << event.schema() << endl;
    if (event.type() != Event::DDL)
      cout << "Table Name: " << event.table() << endl;
  }
}

int main(int argc, char* argv[])
{
  GOOGLE_PROTOBUF_VERIFY_VERSION;
  int file;

  if (argc != 2)
  {
    cerr << "Usage:  " << argv[0] << " replication event log " << endl;
    return -1;
  }

  EventList list;

  if ((file= open(argv[1], O_RDONLY)) == -1)
  {
    cerr << "Can not open file: " << argv[0] << endl;
  }

  while (1)
  {
    uint64_t length;
    char *buffer= NULL;
    char *temp_buffer;

    /* Read the size */
    if (read(file, &length, sizeof(uint64_t)) != sizeof(uint64_t))
      break;

    if (length > SIZE_MAX)
    {
      cerr << "Attempted to read record bigger than SIZE_MAX" << endl;
      exit(1);
    }
    temp_buffer= (char *)realloc(buffer, (size_t)length);
    if (temp_buffer == NULL)
    {
      cerr << "Memory allocation failure trying to " << length << "."  << endl;
      exit(1);
    }
    buffer= temp_buffer;

    /* Read the record */
    if (read(file, buffer, (size_t)length) != (ssize_t)length)
    {
      cerr << "Could not read entire record." << endl;
      exit(1);
    }
    list.ParseFromArray(buffer, (int)length);

    /* Print the record */
    printRecord(&list);
  }


  return 0;
}