1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2009 Sun Microsystems
8
* Jay Pipes <joinfu@sun.com>
10
* This program is free software; you can redistribute it and/or modify
11
* it under the terms of the GNU General Public License as published by
12
* the Free Software Foundation; version 2 of the License.
14
* This program is distributed in the hope that it will be useful,
15
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
* GNU General Public License for more details.
19
* You should have received a copy of the GNU General Public License
20
* along with this program; if not, write to the Free Software
21
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
#include <drizzled/definitions.h>
26
#include <drizzled/gettext.h>
27
#include <drizzled/replication_services.h>
28
#include <drizzled/algorithm/crc32.h>
29
#include <sys/types.h>
40
#include <drizzled/message/transaction.pb.h>
41
#include <drizzled/message/statement_transform.h>
42
#include <drizzled/util/convert.h>
44
#include <google/protobuf/io/coded_stream.h>
45
#include <google/protobuf/io/zero_copy_stream_impl.h>
48
using namespace google;
49
using namespace drizzled;
51
static const char *replace_with_spaces= "\n\r";
53
static void printStatement(const message::Statement &statement)
55
vector<string> sql_strings;
57
message::transformStatementToSql(statement,
60
true /* already in transaction */);
62
for (vector<string>::iterator sql_string_iter= sql_strings.begin();
63
sql_string_iter != sql_strings.end();
66
string &sql= *sql_string_iter;
69
* Replace \n and \r with spaces so that SQL statements
70
* are always on a single line
73
string::size_type found= sql.find_first_of(replace_with_spaces);
74
while (found != string::npos)
77
found= sql.find_first_of(replace_with_spaces, found);
82
* Embedded NUL characters are a pain in the ass.
85
string::size_type found= sql.find_first_of('\0');
86
while (found != string::npos)
89
sql.insert(found + 1, 1, '0');
90
found= sql.find_first_of('\0', found);
94
cout << sql << ';' << endl;
98
static void printTransaction(const message::Transaction &transaction)
100
const message::TransactionContext trx= transaction.transaction_context();
102
size_t num_statements= transaction.statement_size();
105
cout << "START TRANSACTION;" << endl;
106
for (x= 0; x < num_statements; ++x)
108
const message::Statement &statement= transaction.statement(x);
109
printStatement(statement);
111
cout << "COMMIT;" << endl;
114
int main(int argc, char* argv[])
116
GOOGLE_PROTOBUF_VERIFY_VERSION;
119
if (argc < 2 || argc > 3)
121
fprintf(stderr, _("Usage: %s TRANSACTION_LOG [--checksum] \n"), argv[0]);
125
message::Transaction transaction;
127
file= open(argv[1], O_RDONLY);
130
fprintf(stderr, _("Cannot open file: %s\n"), argv[1]);
134
bool do_checksum= false;
138
string checksum_arg(argv[2]);
139
transform(checksum_arg.begin(), checksum_arg.end(), checksum_arg.begin(), ::tolower);
141
if ("--checksum" == checksum_arg)
145
protobuf::io::ZeroCopyInputStream *raw_input= new protobuf::io::FileInputStream(file);
146
protobuf::io::CodedInputStream *coded_input= new protobuf::io::CodedInputStream(raw_input);
149
char *temp_buffer= NULL;
151
uint32_t previous_length= 0;
152
uint32_t checksum= 0;
154
uint32_t message_type= 0;
156
/* Read in the length of the command */
157
while (result == true &&
158
coded_input->ReadLittleEndian32(&message_type) == true &&
159
coded_input->ReadLittleEndian32(&length) == true)
161
if (message_type != ReplicationServices::TRANSACTION)
163
fprintf(stderr, _("Found a non-transaction message in log. Currently, not supported.\n"));
167
if (length > INT_MAX)
169
fprintf(stderr, _("Attempted to read record bigger than INT_MAX\n"));
176
* First time around...just malloc the length. This block gets rid
177
* of a GCC warning about uninitialized temp_buffer.
179
temp_buffer= (char *) malloc(static_cast<size_t>(length));
181
/* No need to allocate if we have a buffer big enough... */
182
else if (length > previous_length)
184
temp_buffer= (char *) realloc(buffer, static_cast<size_t>(length));
187
if (temp_buffer == NULL)
189
fprintf(stderr, _("Memory allocation failure trying to allocate %" PRIu64 " bytes.\n"),
190
static_cast<uint64_t>(length));
196
/* Read the Command */
197
result= coded_input->ReadRaw(buffer, (int) length);
200
char errmsg[STRERROR_MAX];
201
strerror_r(errno, errmsg, sizeof(errmsg));
202
fprintf(stderr, _("Could not read transaction message.\n"));
203
fprintf(stderr, _("GPB ERROR: %s.\n"), errmsg);
205
hexdump.reserve(length * 4);
206
bytesToHexdumpFormat(hexdump, reinterpret_cast<const unsigned char *>(buffer), length);
207
fprintf(stderr, _("HEXDUMP:\n\n%s\n"), hexdump.c_str());
211
result= transaction.ParseFromArray(buffer, static_cast<int32_t>(length));
214
fprintf(stderr, _("Unable to parse command. Got error: %s.\n"), transaction.InitializationErrorString().c_str());
218
hexdump.reserve(length * 4);
219
bytesToHexdumpFormat(hexdump, reinterpret_cast<const unsigned char *>(buffer), length);
220
fprintf(stderr, _("HEXDUMP:\n\n%s\n"), hexdump.c_str());
225
/* Print the transaction */
226
printTransaction(transaction);
228
/* Skip 4 byte checksum */
229
coded_input->ReadLittleEndian32(&checksum);
233
if (checksum != drizzled::algorithm::crc32(buffer, static_cast<size_t>(length)))
235
fprintf(stderr, _("Checksum failed. Wanted %" PRIu32 " got %" PRIu32 "\n"), checksum, drizzled::algorithm::crc32(buffer, static_cast<size_t>(length)));
239
previous_length= length;
247
return (result == true ? 0 : 1);