Skip to content

Commit

Permalink
Merge pull request #6 from iaddict/patch-1
Browse files Browse the repository at this point in the history
Load mssql jdbc driver with safety checks
  • Loading branch information
rdubya authored Dec 11, 2020
2 parents acdd5ba + aaa4acd commit 63e8a46
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/activerecord-jdbcsqlserver-adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
require 'active_record/log_subscriber' # Need to make sure this is loaded before we load Core for monkey patching

# Load the jar file for the jdbc driver
require 'jdbc/mssql'
require_relative './jdbc_mssql_driver_loader'

# Standadard arjdbc functionality
require 'arjdbc/abstract/connection_management'
Expand Down
22 changes: 22 additions & 0 deletions lib/jdbc_mssql_driver_loader.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module JdbcMssqlDriverLoader
def self.check_and_maybe_load_driver
driver_name = "com.microsoft.sqlserver.jdbc.SQLServerDriver"
if (Java::JavaClass.for_name(driver_name) rescue nil)
driver = Java::ComMicrosoftSqlserverJdbc::SQLServerDriver.new
which = driver
.getClass().getClassLoader().loadClass(driver_name)
.getProtectionDomain().getCodeSource().getLocation().to_s
warn "You already required a mssql jdbc driver (#{which}), skipping gem jdbc-mssql"

major_version = driver.major_version
required_major_version = 8
if major_version < required_major_version
raise "MSSQL jdbc driver version is to old (given major version #{major_version} < required major version #{required_major_version})"
end
else
require "jdbc/mssql"
end
end

check_and_maybe_load_driver
end

0 comments on commit 63e8a46

Please sign in to comment.