612.2.4
by Monty Taylor
Moved some defines to config.h. Stopped including config.h directly anywhere. |
1 |
#include <drizzled/global.h> |
481.1.9
by Monty Taylor
Added autoconf tests for location of cstdint and cinttypes. Use those in C++ programs now, so that we don't have to define _STDC_LIMIT_MACROS, etc by hand. Stop, in fact, defining those by hand. |
2 |
|
988.1.1
by Jay Pipes
Changes libserialize to libdrizzledmessage per ML discussion. All GPB messages are now in the drizzled::message namespace. |
3 |
#include <drizzled/message/binlog_encoding.h> |
4 |
#include <drizzled/message/binary_log.h> |
|
324.1.1
by Mats Kindahl
Adding specification of a simple protobuf-based binary log format, |
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> |
|
520.9.1
by mordred
More solaris fixes. |
12 |
#include <fstream> |
324.1.1
by Mats Kindahl
Adding specification of a simple protobuf-based binary log format, |
13 |
#include <sstream> |
14 |
#include <string> |
|
15 |
#include <map> |
|
16 |
||
17 |
#include <getopt.h> |
|
18 |
#include <sys/stat.h> |
|
19 |
#include <fcntl.h> |
|
20 |
||
520.9.1
by mordred
More solaris fixes. |
21 |
using namespace std; |
324.1.1
by Mats Kindahl
Adding specification of a simple protobuf-based binary log format, |
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" |
|
520.9.1
by mordred
More solaris fixes. |
38 |
<< flush; |
324.1.1
by Mats Kindahl
Adding specification of a simple protobuf-based binary log format, |
39 |
exit(1); |
40 |
}
|
|
41 |
||
42 |
||
43 |
void
|
|
44 |
write_query(CodedOutputStream* out, |
|
45 |
unsigned long trans_id, |
|
520.9.1
by mordred
More solaris fixes. |
46 |
const string& query, |
324.1.1
by Mats Kindahl
Adding specification of a simple protobuf-based binary log format, |
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); |
|
919.2.3
by Monty Taylor
Changed two proto files so that we can stop skipping warnings on protobuf code. |
65 |
var->set_val(ii->second); |
324.1.1
by Mats Kindahl
Adding specification of a simple protobuf-based binary log format, |
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; |
|
520.9.1
by mordred
More solaris fixes. |
101 |
const string key(optarg, pos); |
102 |
const string value(pos == end ? end : pos+1, end); |
|
324.1.1
by Mats Kindahl
Adding specification of a simple protobuf-based binary log format, |
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 |
||
520.9.1
by mordred
More solaris fixes. |
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); |
|
324.1.1
by Mats Kindahl
Adding specification of a simple protobuf-based binary log format, |
126 |
CodedOutputStream* coded_output = new CodedOutputStream(raw_output); |
127 |
||
520.9.1
by mordred
More solaris fixes. |
128 |
stringstream sout; |
324.1.1
by Mats Kindahl
Adding specification of a simple protobuf-based binary log format, |
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; |
|
520.9.1
by mordred
More solaris fixes. |
135 |
fb.close(); |
779.3.18
by Monty Taylor
Cleaned up warnings up through innodb. |
136 |
return 0; |
324.1.1
by Mats Kindahl
Adding specification of a simple protobuf-based binary log format, |
137 |
}
|