1
#include <drizzled/server_includes.h>
2
#include <drizzled/gettext.h>
11
#include <drizzled/message/replication.pb.h>
13
#include "drizzled/message/command_transform.h"
15
#include "drizzled/korr.h"
18
using namespace drizzled;
20
static void printCommand(const message::Command &command)
22
cout << "/* Timestamp: " << command.timestamp() << " */"<< endl;
24
message::TransactionContext trx= command.transaction_context();
26
cout << "/* SERVER ID: " << trx.server_id() << " TRX ID: " << trx.transaction_id();
28
if (command.has_session_id())
29
cout << " SESSION ID: " << command.session_id();
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;
48
int main(int argc, char* argv[])
50
GOOGLE_PROTOBUF_VERIFY_VERSION;
55
fprintf(stderr, _("Usage: %s COMMAND_LOG\n"), argv[0]);
59
message::Command command;
61
file= open(argv[1], O_RDONLY);
64
fprintf(stderr, _("Cannot open file: %s\n"), argv[1]);
68
char *temp_buffer= NULL;
69
uint64_t previous_length= 0;
70
ssize_t read_bytes= 0;
74
/* We use korr.h macros when writing and must do the same when reading... */
75
unsigned char coded_length[8];
76
unsigned char coded_checksum[4];
78
/* Read in the length of the command */
79
while ((read_bytes= read(file, coded_length, sizeof(uint64_t))) != 0)
83
fprintf(stderr, _("Failed to read initial length header\n"));
86
length= uint8korr(coded_length);
88
if (length > SIZE_MAX)
90
fprintf(stderr, _("Attempted to read record bigger than SIZE_MAX\n"));
97
* First time around...just malloc the length. This block gets rid
98
* of a GCC warning about uninitialized temp_buffer.
100
temp_buffer= (char *) malloc((size_t) length);
102
/* No need to allocate if we have a buffer big enough... */
103
else if (length > previous_length)
105
temp_buffer= (char *) realloc(buffer, (size_t) length);
108
if (temp_buffer == NULL)
110
fprintf(stderr, _("Memory allocation failure trying to allocate %" PRIu64 " bytes.\n"), length);
116
/* Read the Command */
117
read_bytes= read(file, buffer, (size_t) length);
118
if ((read_bytes != (ssize_t) length))
120
fprintf(stderr, _("Could not read entire transaction. Read %" PRIu64 " bytes instead of %" PRIu64 " bytes.\n"), (uint64_t) read_bytes, (uint64_t) length);
124
if (! command.ParseFromArray(buffer, (int) length))
126
fprintf(stderr, _("Unable to parse command. Got error: %s.\n"), command.InitializationErrorString().c_str());
128
fprintf(stderr, _("BUFFER: %s\n"), buffer);
132
/* Read the checksum */
133
read_bytes= read(file, coded_checksum, sizeof(uint32_t));
134
if ((read_bytes != (ssize_t) sizeof(uint32_t)))
136
fprintf(stderr, _("Could not read entire checksum. Read %" PRIu64 " bytes instead of 4 bytes.\n"), (uint64_t) read_bytes);
139
checksum= uint4korr(coded_checksum);
143
/* @TODO checksumming.. */
146
/* Print the command */
147
printCommand(command);
149
/* Reset our length check */
150
previous_length= length;
151
memset(coded_length, 0, sizeof(coded_length));