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/gettext.h>
26
#include <drizzled/replication_services.h>
27
#include <drizzled/algorithm/crc32.h>
28
#include <sys/types.h>
39
#include <drizzled/message/transaction.pb.h>
40
#include <drizzled/message/statement_transform.h>
41
#include <drizzled/util/convert.h>
43
#include <google/protobuf/io/coded_stream.h>
44
#include <google/protobuf/io/zero_copy_stream_impl.h>
47
using namespace google;
48
using namespace drizzled;
50
static const char *replace_with_spaces= "\n\r";
52
static void printStatement(const message::Statement &statement)
54
vector<string> sql_strings;
56
message::transformStatementToSql(statement,
59
true /* already in transaction */);
61
for (vector<string>::iterator sql_string_iter= sql_strings.begin();
62
sql_string_iter != sql_strings.end();
65
string &sql= *sql_string_iter;
68
* Replace \n and \r with spaces so that SQL statements
69
* are always on a single line
72
string::size_type found= sql.find_first_of(replace_with_spaces);
73
while (found != string::npos)
76
found= sql.find_first_of(replace_with_spaces, found);
81
* Embedded NUL characters are a pain in the ass.
84
string::size_type found= sql.find_first_of('\0');
85
while (found != string::npos)
88
sql.insert(found + 1, 1, '0');
89
found= sql.find_first_of('\0', found);
93
cout << sql << ';' << endl;
97
static void printTransaction(const message::Transaction &transaction)
99
const message::TransactionContext trx= transaction.transaction_context();
101
size_t num_statements= transaction.statement_size();
104
cout << "START TRANSACTION;" << endl;
105
for (x= 0; x < num_statements; ++x)
107
const message::Statement &statement= transaction.statement(x);
108
printStatement(statement);
110
cout << "COMMIT;" << endl;
113
int main(int argc, char* argv[])
115
GOOGLE_PROTOBUF_VERIFY_VERSION;
118
if (argc < 2 || argc > 3)
120
fprintf(stderr, _("Usage: %s TRANSACTION_LOG [--checksum] \n"), argv[0]);
124
message::Transaction transaction;
126
file= open(argv[1], O_RDONLY);
129
fprintf(stderr, _("Cannot open file: %s\n"), argv[1]);
133
bool do_checksum= false;
137
string checksum_arg(argv[2]);
138
transform(checksum_arg.begin(), checksum_arg.end(), checksum_arg.begin(), ::tolower);
140
if ("--checksum" == checksum_arg)
144
protobuf::io::ZeroCopyInputStream *raw_input= new protobuf::io::FileInputStream(file);
145
protobuf::io::CodedInputStream *coded_input= new protobuf::io::CodedInputStream(raw_input);
148
char *temp_buffer= NULL;
150
uint32_t previous_length= 0;
151
uint32_t checksum= 0;
153
uint32_t message_type= 0;
155
/* Read in the length of the command */
156
while (result == true &&
157
coded_input->ReadLittleEndian32(&message_type) == true &&
158
coded_input->ReadLittleEndian32(&length) == true)
160
if (message_type != ReplicationServices::TRANSACTION)
162
fprintf(stderr, _("Found a non-transaction message in log. Currently, not supported.\n"));
166
if (length > INT_MAX)
168
fprintf(stderr, _("Attempted to read record bigger than INT_MAX\n"));
175
* First time around...just malloc the length. This block gets rid
176
* of a GCC warning about uninitialized temp_buffer.
178
temp_buffer= (char *) malloc(static_cast<size_t>(length));
180
/* No need to allocate if we have a buffer big enough... */
181
else if (length > previous_length)
183
temp_buffer= (char *) realloc(buffer, static_cast<size_t>(length));
186
if (temp_buffer == NULL)
188
fprintf(stderr, _("Memory allocation failure trying to allocate %" PRIu64 " bytes.\n"),
189
static_cast<uint64_t>(length));
195
/* Read the Command */
196
result= coded_input->ReadRaw(buffer, (int) length);
199
fprintf(stderr, _("Could not read transaction message.\n"));
200
fprintf(stderr, _("GPB ERROR: %s.\n"), strerror(errno));
202
hexdump.reserve(length * 4);
203
bytesToHexdumpFormat(hexdump, reinterpret_cast<const unsigned char *>(buffer), length);
204
fprintf(stderr, _("HEXDUMP:\n\n%s\n"), hexdump.c_str());
208
result= transaction.ParseFromArray(buffer, static_cast<int32_t>(length));
211
fprintf(stderr, _("Unable to parse command. Got error: %s.\n"), transaction.InitializationErrorString().c_str());
215
hexdump.reserve(length * 4);
216
bytesToHexdumpFormat(hexdump, reinterpret_cast<const unsigned char *>(buffer), length);
217
fprintf(stderr, _("HEXDUMP:\n\n%s\n"), hexdump.c_str());
222
/* Print the transaction */
223
printTransaction(transaction);
225
/* Skip 4 byte checksum */
226
coded_input->ReadLittleEndian32(&checksum);
230
if (checksum != drizzled::algorithm::crc32(buffer, static_cast<size_t>(length)))
232
fprintf(stderr, _("Checksum failed. Wanted %" PRIu32 " got %" PRIu32 "\n"), checksum, drizzled::algorithm::crc32(buffer, static_cast<size_t>(length)));
236
previous_length= length;
244
return (result == true ? 0 : 1);