y-ken / fluent-plugin-mysql-replicator

Fluentd input plugin to track insert/update/delete event from MySQL databases.
http://rubygems.org/gems/fluent-plugin-mysql-replicator
Other
56 stars 24 forks source link

fix syntax error when query result has a value starts with 'select' #25

Closed uchisako closed 7 years ago

uchisako commented 7 years ago

I got warning when query result has a value starts with select.
ex. selector2001

2017-09-13 09:51:10 +0900 [warn]: mysql_replicator: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'selector2001' at line 1
2017-09-13 10:06:10 +0900 [warn]: mysql_replicator: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'selector2001' at line 1

And It's happened by find nested query.

row.select {|k, v| v.to_s.strip.match(/^SELECT/i) }.each do |k, v|

I added a white space condition with SELECT.

y-ken commented 7 years ago

Thank you! does it work multi line query such as this?

SELECT
  foo
FROM
  bar
uchisako commented 7 years ago

I think it is OK.

Here is a simple test code.

rows = []
rows[0] = "SELECT00"
rows[1] = "selector2001"
rows[2] = "SELECT
 foo
FROM
 bar"
rows[3] = "SELECT   foo FROM bar"
rows[4] = "select\r\n foo\r\nfrom\r\n bar"
rows[5] = "select\n foo\nfrom\n bar"

rows.each do |row|
  if row.match(/^SELECT(\s+)/i)
    print "*** select detect ***\n"
    print "`#{row}`\n\n"
  end
end

and results

*** select detect ***
`SELECT
 foo
FROM
 bar`

*** select detect ***
`SELECT   foo FROM bar`

*** select detect ***
`select
 foo
from
 bar`

*** select detect ***
`select
 foo
from
 bar`
y-ken commented 7 years ago

Thank you for testing!