3
#include "binlog_encoding.h"
4
#include "binary_log.h"
8
#include <google/protobuf/io/zero_copy_stream_impl.h>
9
#include <google/protobuf/io/coded_stream.h>
20
using namespace google::protobuf::io;
22
typedef std::map<std::string,std::string> Assign;
24
void print_usage_and_exit(char *prog) {
26
const char *name= strrchr(prog, '/');
31
name= "binlog_writer";
32
cerr << "Usage: " << name << " <options> <query>\n"
33
<< " --output name Append query to file <name> (default: 'log.bin')\n"
34
<< " --set var=val Set value of user variable for query\n"
35
<< " --trans-id <id> Set transaction id to <id>\n"
42
write_query(CodedOutputStream* out,
43
unsigned long trans_id,
44
const std::string& query,
47
BinaryLog::Query *message = new BinaryLog::Query;
50
BinaryLog::Header *header= message->mutable_header();
51
header->set_seqno(time(NULL));
52
header->set_server_id(1);
53
header->set_trans_id(trans_id);
56
message->set_query(query);
57
for (Assign::const_iterator ii= assign.begin() ;
61
BinaryLog::Query::Variable *var= message->add_variable();
62
var->set_name(ii->first);
63
var->set_value(ii->second);
66
BinaryLog::Event event(BinaryLog::Event::QUERY, message);
71
int main(int argc, char *argv[])
74
GOOGLE_PROTOBUF_VERIFY_VERSION;
76
static struct option options[] = {
77
{ "set", 1 /* has_arg */, NULL, 0 },
78
{ "trans-id", 1 /* has_arg */, NULL, 0 },
79
{ "output", 1 /* has_arg */, NULL, 0 },
84
unsigned long trans_id= 0;
85
const char* file_name= "log.bin";
88
while ((ch= getopt_long(argc, argv, "", options, &option_index)) != -1) {
90
print_usage_and_exit(argv[0]);
92
switch (option_index) {
95
// Split the supplied string at the first '='
96
char *end= optarg + strlen(optarg);
97
char *pos= strchr(optarg, '=');
100
const std::string key(optarg, pos);
101
const std::string value(pos == end ? end : pos+1, end);
105
case 1: // --trans-id
106
trans_id= strtoul(optarg, NULL, 0);
116
print_usage_and_exit(argv[0]);
118
int outfd= open(file_name, O_WRONLY | O_CREAT | O_APPEND, 0644);
125
ZeroCopyOutputStream* raw_output = new FileOutputStream(outfd);
126
CodedOutputStream* coded_output = new CodedOutputStream(raw_output);
128
std::stringstream sout;
129
sout << ioutil::join(" ", &argv[optind], &argv[argc]);
131
write_query(coded_output, trans_id, sout.str(), assign);