~drizzle-trunk/drizzle/development

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <config.h>

#include "binlog_encoding.h"
#include "binary_log.h"

#include "ioutil.h"

#include <google/protobuf/io/zero_copy_stream_impl.h>
#include <google/protobuf/io/coded_stream.h>

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <map>

#include <getopt.h>
#include <sys/stat.h>
#include <fcntl.h>

using namespace std;
using namespace google::protobuf::io;

typedef std::map<std::string,std::string> Assign;

void print_usage_and_exit(char *prog) {
  using std::cerr;
  const char *name= strrchr(prog, '/');

  if (name)
    ++name;
  else
    name= "binlog_writer";
  cerr << "Usage: " << name << " <options> <query>\n"
       << "    --output name    Append query to file <name> (default: 'log.bin')\n"
       << "    --set var=val    Set value of user variable for query\n"
       << "    --trans-id <id>  Set transaction id to <id>\n"
       << flush;
  exit(1);
}


void
write_query(CodedOutputStream* out,
            unsigned long trans_id,
            const string& query,
            const Assign& assign)
{
  BinaryLog::Query *message = new BinaryLog::Query;

  {
    BinaryLog::Header *header= message->mutable_header();
    header->set_seqno(time(NULL));
    header->set_server_id(1);
    header->set_trans_id(trans_id);
  }

  message->set_query(query);
  for (Assign::const_iterator ii= assign.begin() ;
       ii != assign.end() ;
       ++ii )
  {
    BinaryLog::Query::Variable *var= message->add_variable();
    var->set_name(ii->first);
    var->set_value(ii->second);
  }

  BinaryLog::Event event(BinaryLog::Event::QUERY, message);
  event.write(out);
}


int main(int argc, char *argv[])
{
  GOOGLE_PROTOBUF_VERIFY_VERSION;

  static struct option options[] = {
    { "set",       1 /* has_arg */, NULL, 0 },
    { "trans-id",  1 /* has_arg */, NULL, 0 },
    { "output",    1 /* has_arg */, NULL, 0 },
    { 0, 0, 0, 0 }
  };

  Assign assign;
  unsigned long trans_id= 0;
  const char* file_name= "log.bin";

  int ch, option_index;
  while ((ch= getopt_long(argc, argv, "", options, &option_index)) != -1) {
    if (ch == '?')
      print_usage_and_exit(argv[0]);

    switch (option_index) {
    case 0:                                     // --set
    {
      // Split the supplied string at the first '='
      char *end= optarg + strlen(optarg);
      char *pos= strchr(optarg, '=');
      if (!pos)
        pos= end;
      const string key(optarg, pos);
      const string value(pos == end ? end : pos+1, end);
      assign[key]= value;
    }

    case 1:                                     // --trans-id
      trans_id= strtoul(optarg, NULL, 0);
      break;

    case 2:                                     // --output
      file_name= optarg;
      break;
    }
  }

  if (optind >= argc)
    print_usage_and_exit(argv[0]);

  filebuf fb;

  fb.open(file_name, ios::app | ios::out);

  ostream os(&fb);

  ZeroCopyOutputStream* raw_output = new OstreamOutputStream(&os);
  CodedOutputStream* coded_output = new CodedOutputStream(raw_output);

  stringstream sout;
  sout << ioutil::join(" ", &argv[optind], &argv[argc]);

  write_query(coded_output, trans_id, sout.str(), assign);

  delete coded_output;
  delete raw_output;
  fb.close();
  exit(0);
}