Skip to content

Commit 7bb1432

Browse files
Pere Urbon-Bayesjordansissel
authored andcommitted
Transform all time object from RubyTime to LogStash::Timestamp when reading from a DB
Fixes #83
1 parent 0755f0b commit 7bb1432

File tree

2 files changed

+52
-8
lines changed

2 files changed

+52
-8
lines changed

lib/logstash/plugin_mixins/jdbc.rb

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,12 @@ def execute_statement(statement, parameters)
143143
if @jdbc_paging_enabled
144144
query.each_page(@jdbc_page_size) do |paged_dataset|
145145
paged_dataset.each do |row|
146-
#Stringify row keys
147-
yield Hash[row.map { |k, v| [k.to_s, v] }]
146+
yield extract_values_from(row)
148147
end
149148
end
150149
else
151150
query.each do |row|
152-
#Stringify row keys
153-
yield Hash[row.map { |k, v| [k.to_s, v] }]
151+
yield extract_values_from(row)
154152
end
155153
end
156154
success = true
@@ -173,4 +171,20 @@ def symbolized_params(parameters)
173171
hash
174172
end
175173
end
174+
175+
private
176+
#Stringify row keys and decorate values when necessary
177+
def extract_values_from(row)
178+
Hash[row.map { |k, v| [k.to_s, decorate_value(v)] }]
179+
end
180+
181+
private
182+
def decorate_value(value)
183+
if value.is_a?(Time)
184+
# transform it to LogStash::Timestamp as required by LS
185+
LogStash::Timestamp.new(value)
186+
else
187+
value # no-op
188+
end
189+
end
176190
end

spec/inputs/jdbc_spec.rb

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
Jdbc::Derby.load_driver
2020
db.create_table :test_table do
2121
DateTime :created_at
22-
Integer :num
22+
Integer :num
23+
DateTime :custom_time
2324
end
2425
end
2526

@@ -96,7 +97,7 @@
9697
let(:settings) do
9798
{
9899
"statement" => "SELECT :num_param as num_param FROM SYSIBM.SYSDUMMY1",
99-
"parameters" => {"num_param" => 10}
100+
"parameters" => { "num_param" => 10}
100101
}
101102
end
102103

@@ -159,7 +160,7 @@
159160

160161
it "should fetch all rows" do
161162
num_rows.times do
162-
db[:test_table].insert(:num => 1, :created_at => Time.now.utc)
163+
db[:test_table].insert(:num => 1, :custom_time => Time.now.utc, :created_at => Time.now.utc)
163164
end
164165

165166
plugin.run(queue)
@@ -169,6 +170,35 @@
169170

170171
end
171172

173+
context "when fetching time data" do
174+
175+
let(:settings) do
176+
{
177+
"statement" => "SELECT * from test_table",
178+
}
179+
end
180+
181+
let(:num_rows) { 10 }
182+
183+
before do
184+
num_rows.times do
185+
db[:test_table].insert(:num => 1, :custom_time => Time.now.utc, :created_at => Time.now.utc)
186+
end
187+
188+
plugin.register
189+
end
190+
191+
after do
192+
plugin.stop
193+
end
194+
195+
it "should convert it to LogStash::Timestamp " do
196+
plugin.run(queue)
197+
event = queue.pop
198+
expect(event["custom_time"]).to be_a(LogStash::Timestamp)
199+
end
200+
end
201+
172202
context "when iteratively running plugin#run" do
173203
let(:settings) do
174204
{"statement" => "SELECT num, created_at FROM test_table WHERE created_at > :sql_last_start"}
@@ -289,7 +319,7 @@
289319

290320
before do
291321
num_rows.times do
292-
db[:test_table].insert(:num => 1, :created_at => Time.now.utc)
322+
db[:test_table].insert(:num => 1, :custom_time => Time.now.utc, :created_at => Time.now.utc)
293323
end
294324

295325
plugin.register

0 commit comments

Comments
 (0)