From 43eb5d969dbacf684237d0dc577a6496c245f78a Mon Sep 17 00:00:00 2001 From: Karl Southern Date: Thu, 7 Jul 2016 11:00:33 +0100 Subject: [PATCH] Multiple types in statement now supported --- lib/logstash/outputs/jdbc.rb | 17 +++++++++-------- spec/jdbc_spec_helper.rb | 6 +++++- spec/outputs/jdbc_derby_spec.rb | 4 ++-- spec/outputs/jdbc_mysql_spec.rb | 2 +- spec/outputs/jdbc_sqlite_spec.rb | 2 +- 5 files changed, 18 insertions(+), 13 deletions(-) diff --git a/lib/logstash/outputs/jdbc.rb b/lib/logstash/outputs/jdbc.rb index 472e5da..7dd2a56 100644 --- a/lib/logstash/outputs/jdbc.rb +++ b/lib/logstash/outputs/jdbc.rb @@ -246,14 +246,15 @@ class LogStash::Outputs::Jdbc < LogStash::Outputs::Base def add_statement_event_params(statement, event) @statement[1..-1].each_with_index do |i, idx| - value = event.get(i) - - value = if value.nil? and i.to_s =~ /%{/ - event.sprintf(i) - else - value - end - + if i.is_a? String + value = event.get(i) + if value.nil? and i =~ /%\{/ + value = event.sprintf(i) + end + else + value = i + end + case value when Time # See LogStash::Timestamp, below, for the why behind strftime. diff --git a/spec/jdbc_spec_helper.rb b/spec/jdbc_spec_helper.rb index 32da9e4..04e8d98 100644 --- a/spec/jdbc_spec_helper.rb +++ b/spec/jdbc_spec_helper.rb @@ -31,7 +31,11 @@ RSpec.shared_context 'when outputting messages' do end let(:jdbc_create_table) do - "CREATE table #{jdbc_test_table} (created_at datetime not null, message varchar(512) not null)" + "CREATE table #{jdbc_test_table} (created_at datetime not null, message varchar(512) not null, message_sprintf varchar(512) not null, static_int int not null, static_bit bit not null)" + end + + let(:jdbc_statement) do + ["insert into #{jdbc_test_table} (created_at, message, message_sprintf, static_int, static_bit) values(?, ?, ?, ?, ?)", '@timestamp', 'message', 'sprintf-%{message}', 1, true] end let(:systemd_database_service) do diff --git a/spec/outputs/jdbc_derby_spec.rb b/spec/outputs/jdbc_derby_spec.rb index 039b5ab..b06980b 100644 --- a/spec/outputs/jdbc_derby_spec.rb +++ b/spec/outputs/jdbc_derby_spec.rb @@ -10,7 +10,7 @@ describe 'logstash-output-jdbc: derby', if: ENV['JDBC_DERBY_JAR'] do end let(:jdbc_create_table) do - "CREATE table #{jdbc_test_table} (created_at timestamp not null, message varchar(512) not null)" + "CREATE table #{jdbc_test_table} (created_at timestamp not null, message varchar(512) not null, message_sprintf varchar(512) not null, static_int int not null, static_bit boolean not null)" end let(:jdbc_settings) do @@ -18,7 +18,7 @@ describe 'logstash-output-jdbc: derby', if: ENV['JDBC_DERBY_JAR'] do 'driver_class' => 'org.apache.derby.jdbc.EmbeddedDriver', 'connection_string' => 'jdbc:derby:memory:testdb;create=true', 'driver_jar_path' => ENV[jdbc_jar_env], - 'statement' => ['insert into logstash_output_jdbc_test (created_at, message) values(?, ?)', '@timestamp', 'message'], + 'statement' => jdbc_statement, 'max_flush_exceptions' => 1 } end diff --git a/spec/outputs/jdbc_mysql_spec.rb b/spec/outputs/jdbc_mysql_spec.rb index 70aef24..623ba3f 100644 --- a/spec/outputs/jdbc_mysql_spec.rb +++ b/spec/outputs/jdbc_mysql_spec.rb @@ -18,7 +18,7 @@ describe 'logstash-output-jdbc: mysql', if: ENV['JDBC_MYSQL_JAR'] do 'driver_class' => 'com.mysql.jdbc.Driver', 'connection_string' => 'jdbc:mysql://localhost/logstash_output_jdbc_test?user=root', 'driver_jar_path' => ENV[jdbc_jar_env], - 'statement' => ["insert into #{jdbc_test_table} (created_at, message) values(?, ?)", '@timestamp', 'message'], + 'statement' => jdbc_statement, 'max_flush_exceptions' => 1 } end diff --git a/spec/outputs/jdbc_sqlite_spec.rb b/spec/outputs/jdbc_sqlite_spec.rb index 8b8aa55..81147f4 100644 --- a/spec/outputs/jdbc_sqlite_spec.rb +++ b/spec/outputs/jdbc_sqlite_spec.rb @@ -20,7 +20,7 @@ describe 'logstash-output-jdbc: sqlite', if: ENV['JDBC_SQLITE_JAR'] do 'driver_class' => 'org.sqlite.JDBC', 'connection_string' => "jdbc:sqlite:#{JDBC_SQLITE_FILE}", 'driver_jar_path' => ENV[jdbc_jar_env], - 'statement' => ["insert into #{jdbc_test_table} (created_at, message) values(?, ?)", '@timestamp', 'message'], + 'statement' => jdbc_statement, 'max_flush_exceptions' => 1 } end