ruby-binlog 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -22,6 +22,9 @@ gem install ruby-binlog
22
22
  case event
23
23
  when Binlog::QueryEvent
24
24
  puts event.query
25
+ when Binlog::RowEvent
26
+ puts event.event_type
27
+ p event.rows
25
28
  else
26
29
  puts "(#{event.event_type})"
27
30
  end
data/ext/Makefile CHANGED
@@ -9,32 +9,32 @@ hdrdir = $(topdir)
9
9
  VPATH = $(srcdir):$(topdir):$(hdrdir)
10
10
  prefix = $(DESTDIR)/usr
11
11
  exec_prefix = $(DESTDIR)/usr
12
- vendorarchdir = $(libdir)/ruby/$(ruby_version)/$(sitearch)
13
- vendordir = $(DESTDIR)/usr/lib/ruby
14
- sitearchdir = $(libdir)/ruby/site_ruby/$(ruby_version)/$(sitearch)
12
+ mandir = $(DESTDIR)/usr/share/man
15
13
  localedir = $(datarootdir)/locale
16
- dvidir = $(docdir)
14
+ vendordir = $(DESTDIR)/usr/lib/ruby
17
15
  datadir = $(DESTDIR)/usr/share
18
- rubylibdir = $(vendordir)/$(ruby_version)
19
- bindir = $(DESTDIR)/usr/bin
20
- sharedstatedir = $(DESTDIR)/var/lib
16
+ sitearchdir = $(libdir)/ruby/site_ruby/$(ruby_version)/$(sitearch)
21
17
  libexecdir = $(DESTDIR)/usr/libexec
22
- vendorlibdir = $(vendordir)/$(ruby_version)
23
- oldincludedir = $(DESTDIR)/usr/include
24
- sitedir = $(DESTDIR)/usr/lib/ruby/site_ruby
25
- mandir = $(DESTDIR)/usr/share/man
26
- datarootdir = $(prefix)/share
27
- htmldir = $(docdir)
28
- pdfdir = $(docdir)
29
- localstatedir = $(DESTDIR)/var
30
- includedir = $(DESTDIR)/usr/include
31
- sitelibdir = $(sitedir)/$(ruby_version)
32
- sysconfdir = $(DESTDIR)/etc
33
18
  psdir = $(docdir)
19
+ sysconfdir = $(DESTDIR)/etc
20
+ bindir = $(DESTDIR)/usr/bin
21
+ includedir = $(DESTDIR)/usr/include
22
+ vendorarchdir = $(libdir)/ruby/$(ruby_version)/$(sitearch)
23
+ sitedir = $(DESTDIR)/usr/lib/ruby/site_ruby
34
24
  libdir = $(DESTDIR)/usr/lib64
25
+ sitelibdir = $(sitedir)/$(ruby_version)
26
+ rubylibdir = $(vendordir)/$(ruby_version)
27
+ localstatedir = $(DESTDIR)/var
28
+ infodir = $(DESTDIR)/usr/share/info
35
29
  docdir = $(datarootdir)/doc/$(PACKAGE)
30
+ datarootdir = $(prefix)/share
36
31
  archdir = $(libdir)/ruby/$(ruby_version)/$(sitearch)
37
- infodir = $(DESTDIR)/usr/share/info
32
+ oldincludedir = $(DESTDIR)/usr/include
33
+ sharedstatedir = $(DESTDIR)/var/lib
34
+ htmldir = $(docdir)
35
+ dvidir = $(docdir)
36
+ vendorlibdir = $(vendordir)/$(ruby_version)
37
+ pdfdir = $(docdir)
38
38
  sbindir = $(DESTDIR)/usr/sbin
39
39
 
40
40
  CC = gcc
data/ext/binlog.so CHANGED
Binary file
data/ext/ruby_binlog.cpp CHANGED
@@ -16,19 +16,29 @@ namespace binlog {
16
16
 
17
17
  struct Client {
18
18
  Binary_log *m_binlog;
19
+ VALUE m_table_map;
19
20
 
20
21
  static void free(Client *p) {
21
22
  if (p->m_binlog) {
22
23
  delete p->m_binlog;
23
24
  p->m_binlog = 0;
25
+ p->m_table_map = 0;
24
26
  }
25
27
 
26
28
  delete p;
27
29
  }
28
30
 
31
+ static void mark(Client *p) {
32
+ if (p->m_table_map) {
33
+ rb_gc_mark(p->m_table_map);
34
+ }
35
+ }
36
+
29
37
  static VALUE alloc(VALUE klass) {
30
38
  Client *p = new Client();
31
- return Data_Wrap_Struct(klass, 0, &free, p);
39
+ p->m_binlog = 0;
40
+ p->m_table_map = 0;
41
+ return Data_Wrap_Struct(klass, &mark, &free, p);
32
42
  }
33
43
 
34
44
  static VALUE initialize(VALUE self, VALUE uri) {
@@ -92,17 +102,18 @@ struct Client {
92
102
  case TABLE_MAP_EVENT:
93
103
  retval = rb_funcall(rb_cBinlogTableMapEvent, rb_intern("new"), 0);
94
104
  TableMapEvent::set_event(retval, event);
105
+ p->m_table_map = retval;
95
106
  break;
96
107
 
97
108
  // XXX: Is it right?
98
- case PRE_GA_WRITE_ROWS_EVENT:
99
- case PRE_GA_UPDATE_ROWS_EVENT:
100
- case PRE_GA_DELETE_ROWS_EVENT:
109
+ //case PRE_GA_WRITE_ROWS_EVENT:
110
+ //case PRE_GA_UPDATE_ROWS_EVENT:
111
+ //case PRE_GA_DELETE_ROWS_EVENT:
101
112
  case WRITE_ROWS_EVENT:
102
113
  case UPDATE_ROWS_EVENT:
103
114
  case DELETE_ROWS_EVENT:
104
115
  retval = rb_funcall(rb_cBinlogRowEvent, rb_intern("new"), 0);
105
- RowEvent::set_event(retval, event);
116
+ RowEvent::set_event(retval, event, p->m_table_map);
106
117
  break;
107
118
 
108
119
  case INTVAR_EVENT:
data/ext/ruby_binlog.o CHANGED
Binary file
@@ -98,19 +98,30 @@ struct TableMapEvent : public Event {
98
98
 
99
99
  struct RowEvent : public Event {
100
100
  mysql::Row_event *m_event;
101
+ VALUE m_table_map;
101
102
 
102
103
  static void free(RowEvent *p);
104
+ static void mark(RowEvent *p);
103
105
  static VALUE alloc(VALUE klass);
104
- static void set_event(VALUE self, mysql::Binary_log_event *event);
106
+ static void set_event(VALUE self, mysql::Binary_log_event *event, VALUE table_map);
105
107
  static void init();
106
108
 
107
109
  static VALUE get_table_id(VALUE self);
110
+ static VALUE get_db_name(VALUE self);
111
+ static VALUE get_table_name(VALUE self);
108
112
  static VALUE get_flags(VALUE self);
109
113
  static VALUE get_columns_len(VALUE self);
110
114
  static VALUE get_null_bits_len(VALUE self);
111
115
  static VALUE get_columns_before_image(VALUE self);
112
116
  static VALUE get_used_columns(VALUE self);
113
117
  static VALUE get_row(VALUE self);
118
+ static VALUE get_rows(VALUE self);
119
+
120
+ private:
121
+ static void proc0(mysql::Row_of_fields &fields, VALUE rb_fields);
122
+ static VALUE proc_insert(mysql::Row_of_fields &fields);
123
+ static VALUE proc_update(mysql::Row_of_fields &old_fields, mysql::Row_of_fields &new_fields);
124
+ static VALUE proc_delete(mysql::Row_of_fields &fields);
114
125
  };
115
126
 
116
127
  struct IntVarEvent : public Event {
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
@@ -9,27 +9,36 @@ void RowEvent::free(RowEvent *p) {
9
9
  if (p->m_event) {
10
10
  delete p->m_event;
11
11
  p->m_event = 0;
12
+ p->m_table_map = 0;
12
13
  p->m_event_header = 0;
13
14
  }
14
15
 
15
16
  delete p;
16
17
  }
17
18
 
19
+ void RowEvent::mark(RowEvent *p) {
20
+ if (p->m_table_map) {
21
+ rb_gc_mark(p->m_table_map);
22
+ }
23
+ }
24
+
18
25
  VALUE RowEvent::alloc(VALUE klass) {
19
26
  RowEvent *p;
20
27
 
21
28
  p = new RowEvent();
22
29
  p->m_event = 0;
30
+ p->m_table_map = 0;
23
31
  p->m_event_header = 0;
24
32
 
25
- return Data_Wrap_Struct(klass, 0, &free, p);
33
+ return Data_Wrap_Struct(klass, &mark, &free, p);
26
34
  }
27
35
 
28
- void RowEvent::set_event(VALUE self, mysql::Binary_log_event *event) {
36
+ void RowEvent::set_event(VALUE self, mysql::Binary_log_event *event, VALUE table_map) {
29
37
  RowEvent *p;
30
38
 
31
39
  Data_Get_Struct(self, RowEvent, p);
32
40
  p->m_event = static_cast<Row_event*>(event);
41
+ p->m_table_map = table_map;
33
42
  p->m_event_header = event->header();
34
43
  }
35
44
 
@@ -39,13 +48,16 @@ void RowEvent::init() {
39
48
 
40
49
  Event::init(rb_cBinlogRowEvent);
41
50
 
42
- rb_define_method(rb_cBinlogRowEvent, "table_id", __F(&get_table_id), 0);
43
- rb_define_method(rb_cBinlogRowEvent, "flags", __F(&get_flags), 0);
44
- rb_define_method(rb_cBinlogRowEvent, "columns_len", __F(&get_columns_len), 0);
45
- rb_define_method(rb_cBinlogRowEvent, "null_bits_len", __F(&get_null_bits_len), 0);
46
- rb_define_method(rb_cBinlogRowEvent, "columns_before_image", __F(&get_columns_before_image), 0);
47
- rb_define_method(rb_cBinlogRowEvent, "used_columns", __F(&get_used_columns), 0);
48
- rb_define_method(rb_cBinlogRowEvent, "row", __F(&get_row), 0);
51
+ rb_define_method(rb_cBinlogRowEvent, "table_id", __F(&get_table_id), 0);
52
+ rb_define_method(rb_cBinlogRowEvent, "db_name", __F(&get_db_name), 0);
53
+ rb_define_method(rb_cBinlogRowEvent, "table_name", __F(&get_table_name), 0);
54
+ rb_define_method(rb_cBinlogRowEvent, "flags", __F(&get_flags), 0);
55
+ rb_define_method(rb_cBinlogRowEvent, "columns_len", __F(&get_columns_len), 0);
56
+ rb_define_method(rb_cBinlogRowEvent, "null_bits_len", __F(&get_null_bits_len), 0);
57
+ rb_define_method(rb_cBinlogRowEvent, "raw_columns_before_image", __F(&get_columns_before_image), 0);
58
+ rb_define_method(rb_cBinlogRowEvent, "raw_used_columns", __F(&get_used_columns), 0);
59
+ rb_define_method(rb_cBinlogRowEvent, "raw_row", __F(&get_row), 0);
60
+ rb_define_method(rb_cBinlogRowEvent, "rows", __F(&get_rows), 0);
49
61
  }
50
62
 
51
63
  VALUE RowEvent::get_table_id(VALUE self) {
@@ -54,6 +66,32 @@ VALUE RowEvent::get_table_id(VALUE self) {
54
66
  return ULL2NUM(p->m_event->table_id);
55
67
  }
56
68
 
69
+ VALUE RowEvent::get_db_name(VALUE self) {
70
+ RowEvent *p;
71
+ Data_Get_Struct(self, RowEvent, p);
72
+
73
+ if (p->m_table_map) {
74
+ TableMapEvent *tme;
75
+ Data_Get_Struct(p->m_table_map, TableMapEvent, tme);
76
+ return rb_str_new2(tme->m_event->db_name.c_str());
77
+ } else {
78
+ return Qnil;
79
+ }
80
+ }
81
+
82
+ VALUE RowEvent::get_table_name(VALUE self) {
83
+ RowEvent *p;
84
+ Data_Get_Struct(self, RowEvent, p);
85
+
86
+ if (p->m_table_map) {
87
+ TableMapEvent *tme;
88
+ Data_Get_Struct(p->m_table_map, TableMapEvent, tme);
89
+ return rb_str_new2(tme->m_event->table_name.c_str());
90
+ } else {
91
+ return Qnil;
92
+ }
93
+ }
94
+
57
95
  VALUE RowEvent::get_flags(VALUE self) {
58
96
  RowEvent *p;
59
97
  Data_Get_Struct(self, RowEvent, p);
@@ -114,5 +152,91 @@ VALUE RowEvent::get_row(VALUE self) {
114
152
  return retval;
115
153
  }
116
154
 
155
+ VALUE RowEvent::get_rows(VALUE self) {
156
+ RowEvent *p;
157
+ TableMapEvent *tme;
158
+ VALUE retval = rb_ary_new();
159
+
160
+ Data_Get_Struct(self, RowEvent, p);
161
+
162
+ if (!p->m_table_map) {
163
+ return retval;
164
+ }
165
+
166
+ Data_Get_Struct(p->m_table_map , TableMapEvent, tme);
167
+
168
+ mysql::Row_event_set rows(p->m_event, tme->m_event);
169
+ mysql::Row_event_set::iterator itor = rows.begin();
170
+
171
+ do {
172
+ VALUE rb_row = Qnil;
173
+ mysql::Row_of_fields fields = *itor;
174
+ Log_event_type event_type = p->m_event->get_event_type();
175
+
176
+ if (event_type == mysql::WRITE_ROWS_EVENT) {
177
+ rb_row = proc_insert(fields);
178
+ } else if (event_type == mysql::UPDATE_ROWS_EVENT) {
179
+ itor++;
180
+ mysql::Row_of_fields fields2 = *itor;
181
+ rb_row = proc_update(fields, fields2);
182
+ } else if (event_type == mysql::DELETE_ROWS_EVENT) {
183
+ rb_row = proc_delete(fields);
184
+ }
185
+
186
+ /*
187
+ if (event->get_event_type() == mysql::UPDATE_ROWS_EVENT)
188
+ {
189
+ ++it;
190
+ mysql::Row_of_fields fields2= *it;
191
+ table_update(os.str(),fields,fields2);
192
+ }
193
+ if (event->get_event_type() == mysql::DELETE_ROWS_EVENT)
194
+ table_delete(os.str(),fields);
195
+ */
196
+
197
+ rb_ary_push(retval, rb_row);
198
+ } while (++itor != rows.end());
199
+
200
+ return retval;
201
+ }
202
+
203
+ void RowEvent::proc0(mysql::Row_of_fields &fields, VALUE rb_fields) {
204
+ mysql::Converter converter;
205
+ mysql::Row_of_fields::iterator itor = fields.begin();
206
+
207
+ do {
208
+ std::string str;
209
+ converter.to(str, *itor);
210
+ rb_ary_push(rb_fields, rb_str_new2(str.c_str()));
211
+ } while(++itor != fields.end());
212
+ }
213
+
214
+ VALUE RowEvent::proc_insert(mysql::Row_of_fields &fields) {
215
+ VALUE rb_new_fields = rb_ary_new();
216
+ proc0(fields, rb_new_fields);
217
+ return rb_new_fields;
218
+ }
219
+
220
+ VALUE RowEvent::proc_update(mysql::Row_of_fields &old_fields, mysql::Row_of_fields &new_fields) {
221
+ VALUE rb_row, rb_old_fields, rb_new_fields;
222
+
223
+ rb_row = rb_ary_new();
224
+ rb_old_fields = rb_ary_new();
225
+ rb_new_fields = rb_ary_new();
226
+
227
+ proc0(old_fields, rb_old_fields);
228
+ proc0(new_fields, rb_new_fields);
229
+ rb_ary_push(rb_row, rb_old_fields);
230
+ rb_ary_push(rb_row, rb_new_fields);
231
+
232
+ return rb_row;
233
+ }
234
+
235
+ VALUE RowEvent::proc_delete(mysql::Row_of_fields &fields) {
236
+ VALUE rb_old_fields = rb_ary_new();
237
+ proc0(fields, rb_old_fields);
238
+ return rb_old_fields;
239
+ }
240
+
117
241
  } // namespace binlog
118
242
  } // namespace ruby
Binary file
Binary file
Binary file
Binary file
Binary file
data/ext/test.rb ADDED
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env ruby
2
+ require "binlog"
3
+
4
+ client = Binlog::Client.new("mysql://repl:repl@hastur")
5
+ client.connect
6
+ client.position = 4
7
+
8
+ while event = client.wait_for_next_event
9
+ case event
10
+ when Binlog::QueryEvent
11
+ puts event.query
12
+ when Binlog::RowEvent
13
+ puts event.event_type
14
+ p event.rows
15
+ else
16
+ puts "(#{event.event_type})"
17
+ end
18
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-binlog
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 0
10
- version: 0.1.0
9
+ - 1
10
+ version: 0.1.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - winebarrel
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-08-25 00:00:00 Z
18
+ date: 2012-08-26 00:00:00 Z
19
19
  dependencies: []
20
20
 
21
21
  description: Ruby binding for MySQL Binary log API.
@@ -57,6 +57,7 @@ files:
57
57
  - ext/binlog.so
58
58
  - ext/ruby_binlog_incident_event.cpp
59
59
  - ext/ruby_binlog_table_map_event.o
60
+ - ext/test.rb
60
61
  - README
61
62
  homepage: https://bitbucket.org/winebarrel/ruby-binlog
62
63
  licenses: []