1
#include <drizzled/global.h>
3
#include <drizzled/message/binlog_encoding.h>
4
#include <drizzled/message/binary_log.h>
8
#include <google/protobuf/io/zero_copy_stream_impl.h>
9
#include <google/protobuf/io/coded_stream.h>
22
using namespace google;
24
typedef std::map<std::string,std::string> Assign;
26
static void print_usage_and_exit(char *prog) {
28
const char *name= strrchr(prog, '/');
33
name= "binlog_writer";
34
cerr << "Usage: " << name << " <options> <query>\n"
35
<< " --output name Append query to file <name> (default: 'log.bin')\n"
36
<< " --set var=val Set value of user variable for query\n"
37
<< " --trans-id <id> Set transaction id to <id>\n"
44
write_query(protobuf::io::CodedOutputStream* out,
45
unsigned long trans_id,
49
BinaryLog::Query *message = new BinaryLog::Query;
52
BinaryLog::Header *header= message->mutable_header();
53
header->set_seqno(time(NULL));
54
header->set_server_id(1);
55
header->set_trans_id(trans_id);
58
message->set_query(query);
59
for (Assign::const_iterator ii= assign.begin() ;
63
BinaryLog::Query::Variable *var= message->add_variable();
64
var->set_name(ii->first);
65
var->set_val(ii->second);
68
BinaryLog::Event event(BinaryLog::Event::QUERY, message);
73
int main(int argc, char *argv[])
75
GOOGLE_PROTOBUF_VERIFY_VERSION;
77
static struct option options[] = {
78
{ "set", 1 /* has_arg */, NULL, 0 },
79
{ "trans-id", 1 /* has_arg */, NULL, 0 },
80
{ "output", 1 /* has_arg */, NULL, 0 },
85
unsigned long trans_id= 0;
86
const char* file_name= "log.bin";
89
while ((ch= getopt_long(argc, argv, "", options, &option_index)) != -1) {
91
print_usage_and_exit(argv[0]);
93
switch (option_index) {
96
// Split the supplied string at the first '='
97
char *end= optarg + strlen(optarg);
98
char *pos= strchr(optarg, '=');
101
const string key(optarg, pos);
102
const string value(pos == end ? end : pos+1, end);
106
case 1: // --trans-id
107
trans_id= strtoul(optarg, NULL, 0);
117
print_usage_and_exit(argv[0]);
121
fb.open(file_name, ios::app | ios::out);
125
protobuf::io::ZeroCopyOutputStream* raw_output=
126
new protobuf::io::OstreamOutputStream(&os);
127
protobuf::io::CodedOutputStream* coded_output=
128
new protobuf::io::CodedOutputStream(raw_output);
131
sout << ioutil::join(" ", &argv[optind], &argv[argc]);
133
write_query(coded_output, trans_id, sout.str(), assign);