1
#include "binlog_encoding.h"
2
#include "binary_log.h"
6
#include <google/protobuf/io/zero_copy_stream_impl.h>
7
#include <google/protobuf/io/coded_stream.h>
18
using namespace google::protobuf::io;
20
typedef std::map<std::string,std::string> Assign;
22
void print_usage_and_exit(char *prog) {
24
const char *name= strrchr(prog, '/');
29
name= "binlog_writer";
30
cerr << "Usage: " << name << " <options> <query>\n"
31
<< " --output name Append query to file <name> (default: 'log.bin')\n"
32
<< " --set var=val Set value of user variable for query\n"
33
<< " --trans-id <id> Set transaction id to <id>\n"
40
write_query(CodedOutputStream* out,
41
unsigned long trans_id,
42
const std::string& query,
45
BinaryLog::Query *message = new BinaryLog::Query;
48
BinaryLog::Header *header= message->mutable_header();
49
header->set_seqno(time(NULL));
50
header->set_server_id(1);
51
header->set_trans_id(trans_id);
54
message->set_query(query);
55
for (Assign::const_iterator ii= assign.begin() ;
59
BinaryLog::Query::Variable *var= message->add_variable();
60
var->set_name(ii->first);
61
var->set_value(ii->second);
64
BinaryLog::Event event(BinaryLog::Event::QUERY, message);
69
int main(int argc, char *argv[])
72
GOOGLE_PROTOBUF_VERIFY_VERSION;
74
static struct option options[] = {
75
{ "set", 1 /* has_arg */, NULL, 0 },
76
{ "trans-id", 1 /* has_arg */, NULL, 0 },
77
{ "output", 1 /* has_arg */, NULL, 0 },
82
unsigned long trans_id= 0;
83
const char* file_name= "log.bin";
86
while ((ch= getopt_long(argc, argv, "", options, &option_index)) != -1) {
88
print_usage_and_exit(argv[0]);
90
switch (option_index) {
93
// Split the supplied string at the first '='
94
char *end= optarg + strlen(optarg);
95
char *pos= strchr(optarg, '=');
98
const std::string key(optarg, pos);
99
const std::string value(pos == end ? end : pos+1, end);
103
case 1: // --trans-id
104
trans_id= strtoul(optarg, NULL, 0);
114
print_usage_and_exit(argv[0]);
116
int outfd= open(file_name, O_WRONLY | O_CREAT | O_APPEND, 0644);
123
ZeroCopyOutputStream* raw_output = new FileOutputStream(outfd);
124
CodedOutputStream* coded_output = new CodedOutputStream(raw_output);
126
std::stringstream sout;
127
sout << ioutil::join(" ", &argv[optind], &argv[argc]);
129
write_query(coded_output, trans_id, sout.str(), assign);