fluent-plugin-mysql 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/fluent-plugin-mysql.gemspec +3 -3
- data/lib/fluent/plugin/out_mysql.rb +8 -42
- data/test/plugin/test_out_mysql.rb +0 -11
- metadata +4 -4
data/.gitignore
CHANGED
data/fluent-plugin-mysql.gemspec
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
2
|
Gem::Specification.new do |gem|
|
3
3
|
gem.name = "fluent-plugin-mysql"
|
4
|
-
gem.version = "0.0.
|
4
|
+
gem.version = "0.0.2"
|
5
5
|
gem.authors = ["TAGOMORI Satoshi"]
|
6
6
|
gem.email = ["tagomoris@gmail.com"]
|
7
7
|
gem.description = %q{fluent plugin to insert mysql as json(single column) or insert statement}
|
@@ -14,7 +14,7 @@ Gem::Specification.new do |gem|
|
|
14
14
|
gem.require_paths = ["lib"]
|
15
15
|
|
16
16
|
gem.add_development_dependency "fluentd"
|
17
|
-
gem.add_development_dependency "mysql2"
|
17
|
+
gem.add_development_dependency "mysql2-cs-bind"
|
18
18
|
gem.add_runtime_dependency "fluentd"
|
19
|
-
gem.add_runtime_dependency "mysql2"
|
19
|
+
gem.add_runtime_dependency "mysql2-cs-bind"
|
20
20
|
end
|
@@ -18,7 +18,7 @@ class Fluent::MysqlOutput < Fluent::BufferedOutput
|
|
18
18
|
|
19
19
|
def initialize
|
20
20
|
super
|
21
|
-
require 'mysql2'
|
21
|
+
require 'mysql2-cs-bind'
|
22
22
|
end
|
23
23
|
|
24
24
|
def configure(conf)
|
@@ -44,11 +44,10 @@ class Fluent::MysqlOutput < Fluent::BufferedOutput
|
|
44
44
|
|
45
45
|
if @sql
|
46
46
|
begin
|
47
|
-
# using nil to pass call of @handler.escape (@handler is set in #start)
|
48
47
|
if @format == 'json'
|
49
|
-
pseudo_bind(@sql, [nil])
|
48
|
+
Mysql2::Client.pseudo_bind(@sql, [nil])
|
50
49
|
else
|
51
|
-
pseudo_bind(@sql, @key_names.map{|n| nil})
|
50
|
+
Mysql2::Client.pseudo_bind(@sql, @key_names.map{|n| nil})
|
52
51
|
end
|
53
52
|
rescue ArgumentError => e
|
54
53
|
raise Fluent::ConfigError, "mismatch between sql placeholders and key_names"
|
@@ -68,47 +67,10 @@ class Fluent::MysqlOutput < Fluent::BufferedOutput
|
|
68
67
|
|
69
68
|
def start
|
70
69
|
super
|
71
|
-
@handler ||= Mysql2::Client.new({:host => @host, :port => @port,
|
72
|
-
:username => @username, :password => @password,
|
73
|
-
:database => @database})
|
74
70
|
end
|
75
71
|
|
76
72
|
def shutdown
|
77
73
|
super
|
78
|
-
@handler.close
|
79
|
-
end
|
80
|
-
|
81
|
-
def pseudo_bind(sql, values)
|
82
|
-
sql = sql.dup
|
83
|
-
|
84
|
-
placeholders = []
|
85
|
-
search_pos = 0
|
86
|
-
while pos = sql.index('?', search_pos)
|
87
|
-
placeholders.push(pos)
|
88
|
-
search_pos = pos + 1
|
89
|
-
end
|
90
|
-
raise ArgumentError, "mismatch between placeholders number and values arguments" if placeholders.length != values.length
|
91
|
-
|
92
|
-
while pos = placeholders.pop()
|
93
|
-
rawvalue = values.pop()
|
94
|
-
if rawvalue.nil?
|
95
|
-
sql[pos] = 'NULL'
|
96
|
-
elsif rawvalue.is_a?(Time)
|
97
|
-
val = rawvalue.strftime('%Y-%m-%d %H:%M:%S')
|
98
|
-
sql[pos] = "'" + val + "'"
|
99
|
-
else
|
100
|
-
val = @handler.escape(rawvalue.to_s)
|
101
|
-
sql[pos] = "'" + val + "'"
|
102
|
-
end
|
103
|
-
end
|
104
|
-
sql
|
105
|
-
end
|
106
|
-
|
107
|
-
def query(sql, *values)
|
108
|
-
values = values.flatten
|
109
|
-
# pseudo prepared statements
|
110
|
-
return @handler.query(sql) if values.length < 1
|
111
|
-
@handler.query(self.pseudo_bind(sql, values))
|
112
74
|
end
|
113
75
|
|
114
76
|
def format(tag, time, record)
|
@@ -116,8 +78,12 @@ class Fluent::MysqlOutput < Fluent::BufferedOutput
|
|
116
78
|
end
|
117
79
|
|
118
80
|
def write(chunk)
|
81
|
+
handler = Mysql2::Client.new({:host => @host, :port => @port,
|
82
|
+
:username => @username, :password => @password,
|
83
|
+
:database => @database})
|
119
84
|
chunk.msgpack_each { |tag, time, data|
|
120
|
-
|
85
|
+
handler.xquery(@sql, data)
|
121
86
|
}
|
87
|
+
handler.close
|
122
88
|
end
|
123
89
|
end
|
@@ -66,17 +66,6 @@ sql INSERT INTO baz (col1,col2,col3,col4) VALUES (?,?,?,?)
|
|
66
66
|
}
|
67
67
|
end
|
68
68
|
|
69
|
-
def test_pseudo_bind
|
70
|
-
d = create_driver
|
71
|
-
sql = 'INSERT INTO baz SET col1=?'
|
72
|
-
# assert_equal "INSERT INTO baz SET col1='HOGE'", d.instance.pseudo_bind(sql, ['HOGE'])
|
73
|
-
assert_equal "INSERT INTO baz SET col1=NULL", d.instance.pseudo_bind(sql, [nil])
|
74
|
-
assert_equal "INSERT INTO baz SET col1='2012-04-16 17:38:00'", d.instance.pseudo_bind(sql, [Time.local(2012, 4, 16, 17, 38, 0)])
|
75
|
-
end
|
76
|
-
|
77
|
-
def test_query
|
78
|
-
end
|
79
|
-
|
80
69
|
def test_format
|
81
70
|
d = create_driver
|
82
71
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fluent-plugin-mysql
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-04-
|
12
|
+
date: 2012-04-20 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: fluentd
|
@@ -28,7 +28,7 @@ dependencies:
|
|
28
28
|
- !ruby/object:Gem::Version
|
29
29
|
version: '0'
|
30
30
|
- !ruby/object:Gem::Dependency
|
31
|
-
name: mysql2
|
31
|
+
name: mysql2-cs-bind
|
32
32
|
requirement: !ruby/object:Gem::Requirement
|
33
33
|
none: false
|
34
34
|
requirements:
|
@@ -60,7 +60,7 @@ dependencies:
|
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: '0'
|
62
62
|
- !ruby/object:Gem::Dependency
|
63
|
-
name: mysql2
|
63
|
+
name: mysql2-cs-bind
|
64
64
|
requirement: !ruby/object:Gem::Requirement
|
65
65
|
none: false
|
66
66
|
requirements:
|