Skip to content

Commit 228f757

Browse files
committed
[mysql] updated so that we can run with non-official driver (+ against MariaDB)
+ includes preliminary `adapter: mariadb` support - MariaDB driver does not return modified row count on execute - adapt those tests
1 parent c912fd0 commit 228f757

File tree

7 files changed

+95
-43
lines changed

7 files changed

+95
-43
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require 'arjdbc/mysql'

lib/arjdbc/mysql/connection_methods.rb

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ def mysql_connection(config)
2727
config[:url] = url
2828
end
2929

30+
mariadb_driver = ! mysql_driver && driver[0, 12] == 'org.mariadb.' # org.mariadb.jdbc.Driver
31+
3032
properties = ( config[:properties] ||= {} )
3133
if mysql_driver
3234
properties['zeroDateTimeBehavior'] ||= 'convertToNull'
@@ -42,22 +44,48 @@ def mysql_connection(config)
4244
end
4345
end
4446
if config[:sslkey] || sslcert = config[:sslcert] # || config[:use_ssl]
45-
properties['useSSL'] ||= true
46-
properties['requireSSL'] ||= true if mysql_driver
47-
properties['clientCertificateKeyStoreUrl'] ||= begin
48-
java.io.File.new(sslcert).to_url.to_s
49-
end if sslcert && mysql_driver
50-
if sslca = config[:sslca] && mysql_driver
51-
properties['trustCertificateKeyStoreUrl'] ||= begin
52-
java.io.File.new(sslca).to_url.to_s
47+
properties['useSSL'] ||= true # supported by MariaDB as well
48+
if mysql_driver
49+
properties['requireSSL'] ||= true if mysql_driver
50+
properties['clientCertificateKeyStoreUrl'] ||= begin
51+
java.io.File.new(sslcert).to_url.to_s
52+
end if sslcert
53+
if sslca = config[:sslca]
54+
properties['trustCertificateKeyStoreUrl'] ||= begin
55+
java.io.File.new(sslca).to_url.to_s
56+
end
57+
else
58+
properties['verifyServerCertificate'] ||= false if mysql_driver
5359
end
54-
else
55-
properties['verifyServerCertificate'] ||= false if mysql_driver
60+
end
61+
if mariadb_driver
62+
properties['verifyServerCertificate'] ||= false
5663
end
5764
end
65+
if socket = config[:socket]
66+
properties['localSocket'] ||= socket if mariadb_driver
67+
end
5868

5969
jdbc_connection(config)
6070
end
6171
alias_method :jdbcmysql_connection, :mysql_connection
6272
alias_method :mysql2_connection, :mysql_connection
73+
74+
def mariadb_connection(config)
75+
config[:adapter_spec] ||= ::ArJdbc::MySQL
76+
config[:adapter_class] = ActiveRecord::ConnectionAdapters::MysqlAdapter unless config.key?(:adapter_class)
77+
78+
return jndi_connection(config) if jndi_config?(config)
79+
80+
begin
81+
require 'jdbc/mariadb'
82+
::Jdbc::MariaDB.load_driver(:require) if defined?(::Jdbc::MariaDB.load_driver)
83+
rescue LoadError # assuming driver.jar is on the class-path
84+
end
85+
86+
config[:driver] ||= 'org.mariadb.jdbc.Driver'
87+
88+
mysql_connection(config)
89+
end
90+
alias_method :jdbcmariadb_connection, :mariadb_connection
6391
end

rakelib/02-test.rake

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,14 @@ test_task_for :H2, :desc => 'Run tests against H2 database engine'
8585
test_task_for :HSQLDB, :desc => 'Run tests against HyperSQL (Java) database'
8686
test_task_for :MSSQL, :driver => :jtds, :database_name => 'MS-SQL (SQLServer)'
8787
test_task_for :MySQL, :prereqs => 'db:mysql'
88-
test_task_for :PostgreSQL, :prereqs => 'db:postgresql', :driver => 'postgres'
88+
test_task_for :PostgreSQL, :driver => 'postgres', :prereqs => 'db:postgresql'
8989
task :test_postgres => :test_postgresql # alias
9090
test_task_for :SQLite3
9191
task :test_sqlite => :test_sqlite3 # alias
9292
test_task_for :Firebird
9393

94+
test_task_for :MariaDB, :prereqs => 'db:mysql', :files => FileList["test/db/mysql/*_test.rb"]
95+
9496
# ensure driver for these DBs is on your class-path
9597
[ :Oracle, :DB2, :Informix, :CacheDB ].each do |adapter|
9698
test_task_for adapter, :desc => "Run tests against #{adapter} (ensure driver is on class-path)"

test/db/mysql/connection_test.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require 'db/mysql'
1+
require File.expand_path('test_helper', File.dirname(__FILE__))
22

33
class MySQLConnectionTest < Test::Unit::TestCase
44

@@ -9,10 +9,10 @@ def test_mysql_default_in_strict_mode
99
def test_mysql_strict_mode_disabled
1010
run_without_connection do |orig_connection|
1111
ActiveRecord::Base.establish_connection(orig_connection.merge({:strict => false}))
12-
assert_equal [['']], select_rows("SELECT @@SESSION.sql_mode")
12+
assert_equal [['']], select_rows("SELECT @@SESSION.sql_mode") if ! mariadb_driver?
1313
end
1414
end
15-
15+
1616
def test_mysql_set_session_variable
1717
run_without_connection do |orig_connection|
1818
ActiveRecord::Base.establish_connection(orig_connection.deep_merge({:variables => {:default_week_format => 3}}))
@@ -29,14 +29,14 @@ def test_mysql_set_session_variable_to_default
2929
assert_equal global_mode_rows, session_mode_rows
3030
end
3131
end
32-
32+
3333
protected
34-
34+
3535
def select_rows(sql)
3636
result = ActiveRecord::Base.connection.exec_query(sql)
3737
result.respond_to?(:rows) ? result.rows : [ result.first.map { |_,value| value } ]
3838
end
39-
39+
4040
private
4141

4242
def run_without_connection

test/db/mysql/simple_test.rb

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
# To run this script, run the following in a mysql instance:
2-
#
3-
# drop database if exists weblog_development;
4-
# create database weblog_development;
5-
# grant all on weblog_development.* to blog@localhost;
6-
# flush privileges;
1+
require File.expand_path('test_helper', File.dirname(__FILE__))
72

8-
require 'jdbc_common'
9-
require 'db/mysql'
3+
require 'simple'
4+
require 'has_many_through'
5+
require 'row_locking'
106

117
class MysqlSimpleTest < Test::Unit::TestCase
128
include SimpleTestMethods
@@ -16,6 +12,14 @@ class MysqlSimpleTest < Test::Unit::TestCase
1612
include XmlColumnTestMethods
1713
include CustomSelectTestMethods
1814

15+
# @override
16+
def test_execute_update
17+
e = Entry.create! :title => '42'; Entry.create! :title => '43'
18+
count = connection.execute("UPDATE entries SET title = 'updated-title' WHERE id = #{e.id}")
19+
assert_equal 1, count if ! mariadb_driver? && defined? JRUBY_VERSION # nil with mysql2
20+
assert_equal 'updated-title', e.reload.title
21+
end
22+
1923
# MySQL does not support precision beyond seconds :
2024
# DATETIME or TIMESTAMP value can include a trailing fractional seconds part
2125
# in up to microseconds (6 digits) precision. Although this fractional part
@@ -163,7 +167,7 @@ def test_update_sql_public_and_returns_rows_affected
163167
e2 = Entry.create! :title => 'another', :content => 'meee', :rating => 40.2
164168
rows_affected = ActiveRecord::Base.connection.update_sql "UPDATE entries " +
165169
"SET content='updated content' WHERE rating > 10 AND title IS NOT NULL"
166-
assert_equal 2, rows_affected
170+
assert_equal 2, rows_affected if ! mariadb_driver? && defined? JRUBY_VERSION
167171
assert_equal 'updated content', e1.reload.content
168172
assert_equal 'updated content', e2.reload.content
169173
end
@@ -360,16 +364,6 @@ def with_bulk_change_table(table)
360364
end
361365
end
362366

363-
private
364-
365-
def mysql_adapter_class
366-
if defined? ActiveRecord::ConnectionAdapters::Mysql2Adapter
367-
ActiveRecord::ConnectionAdapters::Mysql2Adapter
368-
else
369-
ActiveRecord::ConnectionAdapters::MysqlAdapter
370-
end
371-
end
372-
373367
end
374368

375369
class MysqlHasManyThroughTest < Test::Unit::TestCase

test/db/mysql/test_helper.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
require 'test_helper'
2+
require 'db/mysql'
3+
4+
module MySQLTestHelper
5+
6+
private
7+
8+
def mysql_adapter_class
9+
if defined? ActiveRecord::ConnectionAdapters::Mysql2Adapter
10+
ActiveRecord::ConnectionAdapters::Mysql2Adapter
11+
else
12+
ActiveRecord::ConnectionAdapters::MysqlAdapter
13+
end
14+
end
15+
16+
def mariadb_driver?
17+
jdbc_conn = connection.jdbc_connection(true)
18+
jdbc_conn.java_class.name.start_with?('org.mariadb.jdbc.')
19+
end
20+
21+
end
22+
23+
class Test::Unit::TestCase
24+
include MySQLTestHelper
25+
end

test/db/mysql_config.rb

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@
1212

1313
if driver = ENV['DRIVER']
1414
if driver =~ /maria/i
15-
if driver.index('.').nil?
16-
driver = 'org.mariadb.jdbc.Driver'
17-
end
18-
jars = File.expand_path('../jars', File.dirname(__FILE__))
19-
if jar = Dir.glob("#{jars}/mariadb*.jar").last
20-
load jar
21-
end
15+
driver = 'org.mariadb.jdbc.Driver' if driver.index('.').nil?
16+
$LOAD_PATH << File.expand_path('../../../jdbc-mariadb/lib', __FILE__)
17+
require 'jdbc/mariadb'; Jdbc::MariaDB.load_driver
2218
end
2319
MYSQL_CONFIG[:driver] = driver if driver.index('.')
20+
else
21+
# detect rake test_mariadb when "jdbc-mariadb/lib" is on the load-path :
22+
if $LOAD_PATH.find { |path| path =~ /jdbc\-mariadb\/lib$/ }
23+
MYSQL_CONFIG[:adapter] = 'mariadb'
24+
#MYSQL_CONFIG[:driver] = 'org.mariadb.jdbc.Driver'
25+
end
2426
end

0 commit comments

Comments
 (0)