11
11
#include <drizzled/message/replication.pb.h>
13
#include "drizzled/message/command_transform.h"
13
15
#include "drizzled/korr.h"
15
17
using namespace std;
16
18
using namespace drizzled;
19
* @file Example application for reading change records (Command messages)
23
* This program is used in the serial_event_log test suite to verify
24
* the log written by that plugin.
27
static void printInsert(const message::Command &container,
28
const message::InsertRecord &record)
31
cout << "INSERT INTO `" << container.schema() << "`.`" << container.table() << "` (";
33
assert(record.insert_field_size() > 0);
34
assert(record.insert_value_size() > 0);
35
assert(record.insert_value_size() % record.insert_field_size() == 0);
37
int32_t num_fields= record.insert_field_size();
40
for (x= 0; x < num_fields; x++)
45
const message::Table::Field f= record.insert_field(x);
47
cout << "`" << f.name() << "`";
53
* There may be an INSERT VALUES (),() type statement. We know the
54
* number of records is equal to the field_values array size divided
55
* by the number of fields.
57
* So, we do an inner and an outer loop. Outer loop is on the number
58
* of records and the inner loop on the number of fields. In this way,
59
* we know that record.field_values(outer_loop * num_fields) + inner_loop))
60
* always gives us our correct field value.
62
int32_t num_records= (record.insert_value_size() / num_fields);
64
for (x= 0; x < num_records; x++)
70
for (y= 0; y < num_fields; y++)
75
cout << "\"" << record.insert_value((x * num_fields) + y) << "\"";
83
static void printDeleteWithPK(const message::Command &container,
84
const message::DeleteRecord &record)
86
cout << "DELETE FROM `" << container.schema() << "`.`" << container.table() << "`";
88
assert(record.where_field_size() > 0);
89
assert(record.where_value_size() == record.where_field_size());
91
int32_t num_where_fields= record.where_field_size();
93
* Make sure we catch anywhere we're not aligning the fields with
94
* the field_values arrays...
96
assert(num_where_fields == record.where_value_size());
100
for (x= 0; x < num_where_fields; x++)
103
cout << " AND "; /* Always AND condition with a multi-column PK */
105
const message::Table::Field f= record.where_field(x);
107
/* Always equality conditions */
108
cout << "`" << f.name() << "` = \"" << record.where_value(x) << "\"";
114
static void printUpdateWithPK(const message::Command &container,
115
const message::UpdateRecord &record)
117
int32_t num_update_fields= record.update_field_size();
120
assert(record.update_field_size() > 0);
121
assert(record.where_field_size() > 0);
122
assert(record.where_value_size() == record.where_field_size());
124
cout << "UPDATE `" << container.schema() << "`.`" << container.table() << "` SET ";
126
for (x= 0;x < num_update_fields; x++)
128
message::Table::Field f= record.update_field(x);
133
cout << "`" << f.name() << "` = \"" << record.after_value(x) << "\"";
136
int32_t num_where_fields= record.where_field_size();
138
* Make sure we catch anywhere we're not aligning the fields with
139
* the field_values arrays...
141
assert(num_where_fields == record.where_value_size());
144
for (x= 0;x < num_where_fields; x++)
147
cout << " AND "; /* Always AND condition with a multi-column PK */
149
const message::Table::Field f= record.where_field(x);
151
/* Always equality conditions */
152
cout << "`" << f.name() << "` = \"" << record.where_value(x) << "\"";
157
20
static void printCommand(const message::Command &command)
159
22
cout << "/* Timestamp: " << command.timestamp() << " */"<< endl;
170
switch (command.type())
172
case message::Command::START_TRANSACTION:
173
cout << "START TRANSACTION;";
175
case message::Command::COMMIT:
178
case message::Command::ROLLBACK:
181
case message::Command::INSERT:
183
printInsert(command, command.insert_record());
186
case message::Command::DELETE:
188
printDeleteWithPK(command, command.delete_record());
191
case message::Command::UPDATE:
193
printUpdateWithPK(command, command.update_record());
196
case message::Command::RAW_SQL:
198
std::string sql= command.sql();
199
/* Replace \n with spaces */
200
const std::string newline= "\n";
201
while (sql.find(newline) != std::string::npos)
202
sql.replace(sql.find(newline), 1, " ");
208
cout << "Received an unknown Command type: " << (int32_t) command.type();
35
message::transformCommand2Sql(command, &sql, message::DRIZZLE);
38
* Replace \n with spaces so that SQL statements
39
* are always on a single line
41
const std::string newline= "\n";
42
while (sql.find(newline) != std::string::npos)
43
sql.replace(sql.find(newline), 1, " ");
45
cout << sql << ';' << endl;
213
48
int main(int argc, char* argv[])