1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2009 Sun Microsystems
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; version 2 of the License.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
#include <drizzled/global.h>
22
#include <drizzled/message/binlog_encoding.h>
23
#include <drizzled/message/binary_log.h>
25
#include "drizzled/message/ioutil.h"
27
#include <google/protobuf/io/zero_copy_stream_impl.h>
28
#include <google/protobuf/io/coded_stream.h>
40
using namespace drizzled;
42
using namespace google;
44
typedef std::map<std::string,std::string> Assign;
46
static void print_usage_and_exit(char *prog) {
48
const char *name= strrchr(prog, '/');
53
name= "binlog_writer";
54
cerr << "Usage: " << name << " <options> <query>\n"
55
<< " --output name Append query to file <name> (default: 'log.bin')\n"
56
<< " --set var=val Set value of user variable for query\n"
57
<< " --trans-id <id> Set transaction id to <id>\n"
64
write_query(protobuf::io::CodedOutputStream* out,
65
unsigned long trans_id,
69
BinaryLog::Query *message = new BinaryLog::Query;
72
BinaryLog::Header *header= message->mutable_header();
73
header->set_seqno(static_cast<google::protobuf::uint32>(time(NULL)));
74
header->set_server_id(1);
75
header->set_trans_id(static_cast<google::protobuf::uint32>(trans_id));
78
message->set_query(query);
79
for (Assign::const_iterator ii= assign.begin() ;
83
BinaryLog::Query::Variable *var= message->add_variable();
84
var->set_name(ii->first);
85
var->set_val(ii->second);
88
BinaryLog::Event event(BinaryLog::Event::QUERY, message);
93
int main(int argc, char *argv[])
95
GOOGLE_PROTOBUF_VERIFY_VERSION;
97
static struct option options[] = {
98
{ "set", 1 /* has_arg */, NULL, 0 },
99
{ "trans-id", 1 /* has_arg */, NULL, 0 },
100
{ "output", 1 /* has_arg */, NULL, 0 },
105
unsigned long trans_id= 0;
106
const char* file_name= "log.bin";
108
int ch, option_index;
109
while ((ch= getopt_long(argc, argv, "", options, &option_index)) != -1) {
111
print_usage_and_exit(argv[0]);
113
switch (option_index) {
116
// Split the supplied string at the first '='
117
char *end= optarg + strlen(optarg);
118
char *pos= strchr(optarg, '=');
121
const string key(optarg, pos);
122
const string value(pos == end ? end : pos+1, end);
126
case 1: // --trans-id
127
trans_id= strtoul(optarg, NULL, 0);
137
print_usage_and_exit(argv[0]);
141
fb.open(file_name, ios::app | ios::out);
145
protobuf::io::ZeroCopyOutputStream* raw_output=
146
new protobuf::io::OstreamOutputStream(&os);
147
protobuf::io::CodedOutputStream* coded_output=
148
new protobuf::io::CodedOutputStream(raw_output);
151
sout << message::ioutil::join(" ", &argv[optind], &argv[argc]);
153
write_query(coded_output, trans_id, sout.str(), assign);