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
24
#include <drizzled/global.h>
25
#include <drizzled/gettext.h>
26
#include <sys/types.h>
33
#include <drizzled/message/transaction.pb.h>
34
#include <drizzled/message/statement_transform.h>
36
#include <google/protobuf/io/coded_stream.h>
37
#include <google/protobuf/io/zero_copy_stream_impl.h>
40
using namespace drizzled;
41
using namespace google;
43
static void printStatement(const message::Statement &statement)
45
cout << "/* Start Timestamp: " << statement.start_timestamp() << " ";
46
cout << " End Timestamp: " << statement.end_timestamp() << " */" << endl;
48
vector<string> sql_strings;
50
message::transformStatementToSql(statement, sql_strings, message::DRIZZLE);
52
vector<string>::iterator sql_string_iter= sql_strings.begin();
53
const std::string newline= "\n";
54
while (sql_string_iter != sql_strings.end())
56
string &sql= *sql_string_iter;
58
* Replace \n with spaces so that SQL statements
59
* are always on a single line
61
while (sql.find(newline) != std::string::npos)
62
sql.replace(sql.find(newline), 1, " ");
64
cout << sql << ';' << endl;
69
static void printTransaction(const message::Transaction &transaction)
71
const message::TransactionContext trx= transaction.transaction_context();
73
cout << "/* SERVER ID: " << trx.server_id() << " TRX ID: " << trx.transaction_id() << " */ " << endl;
75
size_t num_statements= transaction.statement_size();
78
for (x= 0; x < num_statements; ++x)
80
const message::Statement &statement= transaction.statement(x);
81
printStatement(statement);
85
int main(int argc, char* argv[])
87
GOOGLE_PROTOBUF_VERIFY_VERSION;
92
fprintf(stderr, _("Usage: %s TRANSACTION_LOG\n"), argv[0]);
96
message::Transaction transaction;
98
file= open(argv[1], O_RDONLY);
101
fprintf(stderr, _("Cannot open file: %s\n"), argv[1]);
105
protobuf::io::ZeroCopyInputStream *raw_input= new protobuf::io::FileInputStream(file);
106
protobuf::io::CodedInputStream *coded_input= new protobuf::io::CodedInputStream(raw_input);
109
char *temp_buffer= NULL;
111
uint64_t previous_length= 0;
114
/* Read in the length of the command */
115
while (result == true && coded_input->ReadLittleEndian64(&length) == true)
117
if (length > SIZE_MAX)
119
fprintf(stderr, _("Attempted to read record bigger than SIZE_MAX\n"));
126
* First time around...just malloc the length. This block gets rid
127
* of a GCC warning about uninitialized temp_buffer.
129
temp_buffer= (char *) malloc((size_t) length);
131
/* No need to allocate if we have a buffer big enough... */
132
else if (length > previous_length)
134
temp_buffer= (char *) realloc(buffer, (size_t) length);
137
if (temp_buffer == NULL)
139
fprintf(stderr, _("Memory allocation failure trying to allocate %" PRIu64 " bytes.\n"),
140
static_cast<uint64_t>(length));
146
/* Read the Command */
147
result= coded_input->ReadRaw(buffer, (int) length);
150
fprintf(stderr, _("Could not read transaction message.\n"));
151
fprintf(stderr, _("GPB ERROR: %s.\n"), strerror(errno));
152
fprintf(stderr, _("Raw buffer read: %s.\n"), buffer);
156
result= transaction.ParseFromArray(buffer, static_cast<size_t>(length));
159
fprintf(stderr, _("Unable to parse command. Got error: %s.\n"), transaction.InitializationErrorString().c_str());
161
fprintf(stderr, _("BUFFER: %s\n"), buffer);
165
/* Print the transaction */
166
printTransaction(transaction);
168
previous_length= length;
176
return (result == true ? 0 : 1);