~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/serialize/binlog_writer.cc

  • Committer: Daniel Nichter
  • Date: 2011-10-23 16:01:37 UTC
  • mto: This revision was merged to the branch mainline in revision 2448.
  • Revision ID: daniel@percona.com-20111023160137-7ac3blgz8z4tf8za
Add Administration Getting Started and Logging.  Capitalize SQL clause keywords.

Show diffs side-by-side

added added

removed removed

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