~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/serialize/binlog_writer.cc

  • Committer: Brian Aker
  • Date: 2008-11-04 15:39:09 UTC
  • mfrom: (575.1.2 devel)
  • Revision ID: brian@tangent.org-20081104153909-c72hn65udxs1ccal
Merge of Monty's work

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <config.h>
 
2
 
1
3
#include "binlog_encoding.h"
2
4
#include "binary_log.h"
3
5
 
7
9
#include <google/protobuf/io/coded_stream.h>
8
10
 
9
11
#include <iostream>
 
12
#include <fstream>
10
13
#include <sstream>
11
14
#include <string>
12
15
#include <map>
15
18
#include <sys/stat.h>
16
19
#include <fcntl.h>
17
20
 
 
21
using namespace std;
18
22
using namespace google::protobuf::io;
19
23
 
20
24
typedef std::map<std::string,std::string> Assign;
31
35
       << "    --output name    Append query to file <name> (default: 'log.bin')\n"
32
36
       << "    --set var=val    Set value of user variable for query\n"
33
37
       << "    --trans-id <id>  Set transaction id to <id>\n"
34
 
       << std::flush;
 
38
       << flush;
35
39
  exit(1);
36
40
}
37
41
 
39
43
void
40
44
write_query(CodedOutputStream* out,
41
45
            unsigned long trans_id,
42
 
            const std::string& query,
 
46
            const string& query,
43
47
            const Assign& assign)
44
48
{
45
49
  BinaryLog::Query *message = new BinaryLog::Query;
68
72
 
69
73
int main(int argc, char *argv[])
70
74
{
71
 
  using std::ios;
72
75
  GOOGLE_PROTOBUF_VERIFY_VERSION;
73
76
 
74
77
  static struct option options[] = {
95
98
      char *pos= strchr(optarg, '=');
96
99
      if (!pos)
97
100
        pos= end;
98
 
      const std::string key(optarg, pos);
99
 
      const std::string value(pos == end ? end : pos+1, end);
 
101
      const string key(optarg, pos);
 
102
      const string value(pos == end ? end : pos+1, end);
100
103
      assign[key]= value;
101
104
    }
102
105
 
113
116
  if (optind >= argc)
114
117
    print_usage_and_exit(argv[0]);
115
118
 
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);
 
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);
124
126
  CodedOutputStream* coded_output = new CodedOutputStream(raw_output);
125
127
 
126
 
  std::stringstream sout;
 
128
  stringstream sout;
127
129
  sout << ioutil::join(" ", &argv[optind], &argv[argc]);
128
130
 
129
131
  write_query(coded_output, trans_id, sout.str(), assign);
130
132
 
131
133
  delete coded_output;
132
134
  delete raw_output;
133
 
  close(outfd);
 
135
  fb.close();
134
136
  exit(0);
135
137
}