~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-03 00:14:39 UTC
  • Revision ID: brian@tangent.org-20080703001439-pit0mcl0wk8elxlq
Cleanup of sql-common and mysqldump

Show diffs side-by-side

added added

removed removed

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