~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/serialize/binlog_writer.cc

  • Committer: Brian Aker
  • Date: 2008-07-13 21:20:24 UTC
  • Revision ID: brian@tangent.org-20080713212024-o6263c1vha7yxdeu
More bool removal. More cow bell!

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#include "config.h"
2
 
 
3
 
#include "binlog_encoding.h"
4
 
#include "binary_log.h"
5
 
 
6
 
#include "ioutil.h"
7
 
 
8
 
#include <google/protobuf/io/zero_copy_stream_impl.h>
9
 
#include <google/protobuf/io/coded_stream.h>
10
 
 
11
 
#include <iostream>
12
 
#include <sstream>
13
 
#include <string>
14
 
#include <map>
15
 
 
16
 
#include <getopt.h>
17
 
#include <sys/stat.h>
18
 
#include <fcntl.h>
19
 
 
20
 
using namespace google::protobuf::io;
21
 
 
22
 
typedef std::map<std::string,std::string> Assign;
23
 
 
24
 
void print_usage_and_exit(char *prog) {
25
 
  using std::cerr;
26
 
  const char *name= strrchr(prog, '/');
27
 
 
28
 
  if (name)
29
 
    ++name;
30
 
  else
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"
36
 
       << std::flush;
37
 
  exit(1);
38
 
}
39
 
 
40
 
 
41
 
void
42
 
write_query(CodedOutputStream* out,
43
 
            unsigned long trans_id,
44
 
            const std::string& query,
45
 
            const Assign& assign)
46
 
{
47
 
  BinaryLog::Query *message = new BinaryLog::Query;
48
 
 
49
 
  {
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);
54
 
  }
55
 
 
56
 
  message->set_query(query);
57
 
  for (Assign::const_iterator ii= assign.begin() ;
58
 
       ii != assign.end() ;
59
 
       ++ii )
60
 
  {
61
 
    BinaryLog::Query::Variable *var= message->add_variable();
62
 
    var->set_name(ii->first);
63
 
    var->set_value(ii->second);
64
 
  }
65
 
 
66
 
  BinaryLog::Event event(BinaryLog::Event::QUERY, message);
67
 
  event.write(out);
68
 
}
69
 
 
70
 
 
71
 
int main(int argc, char *argv[])
72
 
{
73
 
  using std::ios;
74
 
  GOOGLE_PROTOBUF_VERIFY_VERSION;
75
 
 
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 },
80
 
    { 0, 0, 0, 0 }
81
 
  };
82
 
 
83
 
  Assign assign;
84
 
  unsigned long trans_id= 0;
85
 
  const char* file_name= "log.bin";
86
 
 
87
 
  int ch, option_index;
88
 
  while ((ch= getopt_long(argc, argv, "", options, &option_index)) != -1) {
89
 
    if (ch == '?')
90
 
      print_usage_and_exit(argv[0]);
91
 
 
92
 
    switch (option_index) {
93
 
    case 0:                                     // --set
94
 
    {
95
 
      // Split the supplied string at the first '='
96
 
      char *end= optarg + strlen(optarg);
97
 
      char *pos= strchr(optarg, '=');
98
 
      if (!pos)
99
 
        pos= end;
100
 
      const std::string key(optarg, pos);
101
 
      const std::string value(pos == end ? end : pos+1, end);
102
 
      assign[key]= value;
103
 
    }
104
 
 
105
 
    case 1:                                     // --trans-id
106
 
      trans_id= strtoul(optarg, NULL, 0);
107
 
      break;
108
 
 
109
 
    case 2:                                     // --output
110
 
      file_name= optarg;
111
 
      break;
112
 
    }
113
 
  }
114
 
 
115
 
  if (optind >= argc)
116
 
    print_usage_and_exit(argv[0]);
117
 
 
118
 
  int outfd= open(file_name, O_WRONLY | O_CREAT | O_APPEND, 0644);
119
 
  if (outfd < 0)
120
 
  {
121
 
    perror("open");
122
 
    exit(1);
123
 
  }
124
 
 
125
 
  ZeroCopyOutputStream* raw_output = new FileOutputStream(outfd);
126
 
  CodedOutputStream* coded_output = new CodedOutputStream(raw_output);
127
 
 
128
 
  std::stringstream sout;
129
 
  sout << ioutil::join(" ", &argv[optind], &argv[argc]);
130
 
 
131
 
  write_query(coded_output, trans_id, sout.str(), assign);
132
 
 
133
 
  delete coded_output;
134
 
  delete raw_output;
135
 
  close(outfd);
136
 
  exit(0);
137
 
}