9
#include <drizzled/message/replication_event.pb.h>
9
#include <drizzled/message/transaction.pb.h>
11
11
using namespace std;
12
12
using namespace drizzled::message;
15
Example reader application for master.info data.
18
void printRecord(const ::drizzled::message::EventList *list)
15
* @file Example application for reading change records and transactions
18
void printInsert(const drizzled::message::Command &container, const drizzled::message::InsertRecord &record)
21
cout << "INSERT INTO `" << container.schema() << "`.`" << container.table() << "` (";
23
int32_t num_fields= record.insert_field_size();
26
for (x= 0; x < num_fields; x++)
31
const Table::Field f= record.insert_field(x);
33
cout << "`" << f.name() << "`";
39
* There may be an INSERT VALUES (),() type statement. We know the
40
* number of records is equal to the field_values array size divided
41
* by the number of fields.
43
* So, we do an inner and an outer loop. Outer loop is on the number
44
* of records and the inner loop on the number of fields. In this way,
45
* we know that record.field_values(outer_loop * num_fields) + inner_loop))
46
* always gives us our correct field value.
48
int32_t num_records= (record.insert_value_size() / num_fields);
50
for (x= 0; x < num_records; x++)
56
for (y= 0; y < num_fields; y++)
61
cout << "\"" << record.insert_value((x * num_fields) + y) << "\"";
69
void printDeleteWithPK(const drizzled::message::Command &container, const drizzled::message::DeleteRecord &record)
71
cout << "DELETE FROM `" << container.schema() << "`.`" << container.table() << "`";
73
int32_t num_where_fields= record.where_field_size();
75
* Make sure we catch anywhere we're not aligning the fields with
76
* the field_values arrays...
78
assert(num_where_fields == record.where_value_size());
82
for (x= 0; x < num_where_fields; x++)
85
cout << " AND "; /* Always AND condition with a multi-column PK */
87
const Table::Field f= record.where_field(x);
89
/* Always equality conditions */
90
cout << "`" << f.name() << "` = \"" << record.where_value(x) << "\"";
94
void printUpdateWithPK(const drizzled::message::Command &container, const drizzled::message::UpdateRecord &record)
96
int32_t num_update_fields= record.update_field_size();
99
cout << "UPDATE `" << container.schema() << "`.`" << container.table() << "` SET ";
101
for (x= 0;x < num_update_fields; x++)
103
Table::Field f= record.update_field(x);
108
cout << "`" << f.name() << "` = \"" << record.after_value(x) << "\"";
111
int32_t num_where_fields= record.where_field_size();
113
* Make sure we catch anywhere we're not aligning the fields with
114
* the field_values arrays...
116
assert(num_where_fields == record.where_value_size());
119
for (x= 0;x < num_where_fields; x++)
122
cout << " AND "; /* Always AND condition with a multi-column PK */
124
const Table::Field f= record.where_field(x);
126
/* Always equality conditions */
127
cout << "`" << f.name() << "` = \"" << record.where_value(x) << "\"";
131
void printTransaction(const drizzled::message::Transaction &transaction)
22
for (e_size= 0; e_size < list->event_size(); e_size++)
135
cout << "/* Start Time: " << transaction.start_timestamp() << " */ START TRANSACTION;"<< endl;
137
for (e_size= 0; e_size < transaction.command_size(); e_size++)
24
const Event event= list->event(e_size);
27
cout << endl << "##########################################################################################" << endl << endl;
139
const drizzled::message::Command command= transaction.command(e_size);
141
drizzled::message::TransactionContext trx= command.transaction_context();
143
cout << "/* SID: " << trx.server_id() << " XID: " << trx.transaction_id() << " */ ";
145
switch (command.type())
38
cout << "INSERT INTO " << event.table() << " (";
40
for (x= 0; x < event.field_names_size() ; x++)
45
cout << event.field_names(x);
48
cout << ") VALUES " << endl;
50
for (x= 0; x < event.values_size(); x++)
53
Event_Value values= event.values(x);
59
for (y= 0; y < values.val_size() ; y++)
64
cout << "\"" << values.val(y) << "\"";
75
Event_Value values= event.values(0);
77
cout << "DELETE FROM " << event.table() << " WHERE " << event.primary_key() << " IN (";
79
for (x= 0; x < values.val_size() ; x++)
84
cout << "\"" << values.val(x) << "\"";
94
for (count= 0; count < event.values_size() ; count++)
97
Event_Value values= event.values(count);
99
cout << "UPDATE " << event.table() << " SET ";
101
for (x= 1; x < values.val_size() ; x++)
106
cout << event.field_names(x - 1) << " = \"" << values.val(x) << "\"";
109
cout << " WHERE " << event.primary_key() << " = " << values.val(0) << endl;
115
cout << "COMMIT" << endl;
147
case Command::START_TRANSACTION:
148
cout << "START TRANSACTION;";
150
case Command::COMMIT:
153
case Command::ROLLBACK:
156
case Command::INSERT:
158
printInsert(command, command.insert_record());
161
case Command::DELETE:
163
printDeleteWithPK(command, command.delete_record());
166
case Command::UPDATE:
168
printUpdateWithPK(command, command.update_record());
121
cout << "Original SQL: " << event.sql() << endl;
123
cout << "AUTOCOMMIT: " << event.autocommit() << endl;
124
cout << "Server id: " << event.server_id() << endl;
125
cout << "Query id: " << event.query_id() << endl;
126
cout << "Transaction id: " << event.transaction_id() << endl;
127
cout << "Schema: " << event.schema() << endl;
128
if (event.type() != Event::DDL)
129
cout << "Table Name: " << event.table() << endl;
176
cout << "/* Commit Time: " << transaction.end_timestamp() << " */ COMMIT;" << endl;
133
179
int main(int argc, char* argv[])
163
210
cerr << "Attempted to read record bigger than SIZE_MAX" << endl;
166
214
temp_buffer= (char *)realloc(buffer, (size_t)length);
167
215
if (temp_buffer == NULL)
169
cerr << "Memory allocation failure trying to " << length << "." << endl;
217
cerr << "Memory allocation failure trying to allocate " << length << " bytes." << endl;
220
memset(temp_buffer, 0, length);
172
221
buffer= temp_buffer;
222
size_t read_bytes= 0;
174
/* Read the record */
175
if (read(file, buffer, (size_t)length) != (ssize_t)length)
224
/* Read the transaction */
225
if ((read_bytes= read(file, buffer, (uint64_t)length)) != (uint64_t)length)
177
cerr << "Could not read entire record." << endl;
227
cerr << "Could not read entire transaction. Read " << read_bytes << " bytes instead of " << length << " bytes." << endl;
180
list.ParseFromArray(buffer, (int)length);
230
transaction.ParseFromArray(buffer, (int) length);
182
/* Print the record */
232
/* Print the transaction */
233
printTransaction(transaction);