Skip to content

Commit 63e8a46

Browse files
authored
Merge pull request #6 from iaddict/patch-1
Load mssql jdbc driver with safety checks
2 parents acdd5ba + aaa4acd commit 63e8a46

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

lib/activerecord-jdbcsqlserver-adapter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
require 'active_record/log_subscriber' # Need to make sure this is loaded before we load Core for monkey patching
66

77
# Load the jar file for the jdbc driver
8-
require 'jdbc/mssql'
8+
require_relative './jdbc_mssql_driver_loader'
99

1010
# Standadard arjdbc functionality
1111
require 'arjdbc/abstract/connection_management'

lib/jdbc_mssql_driver_loader.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module JdbcMssqlDriverLoader
2+
def self.check_and_maybe_load_driver
3+
driver_name = "com.microsoft.sqlserver.jdbc.SQLServerDriver"
4+
if (Java::JavaClass.for_name(driver_name) rescue nil)
5+
driver = Java::ComMicrosoftSqlserverJdbc::SQLServerDriver.new
6+
which = driver
7+
.getClass().getClassLoader().loadClass(driver_name)
8+
.getProtectionDomain().getCodeSource().getLocation().to_s
9+
warn "You already required a mssql jdbc driver (#{which}), skipping gem jdbc-mssql"
10+
11+
major_version = driver.major_version
12+
required_major_version = 8
13+
if major_version < required_major_version
14+
raise "MSSQL jdbc driver version is to old (given major version #{major_version} < required major version #{required_major_version})"
15+
end
16+
else
17+
require "jdbc/mssql"
18+
end
19+
end
20+
21+
check_and_maybe_load_driver
22+
end

0 commit comments

Comments
 (0)