|
| 1 | +ojdbc_jars = [] |
| 2 | + |
1 | 3 | begin |
2 | 4 | require "java" |
3 | 5 | require "jruby" |
4 | 6 |
|
5 | | - # ojdbc6.jar or ojdbc5.jar file should be in JRUBY_HOME/lib or should be in ENV['PATH'] or load path |
| 7 | + # Oracle JDBC driver jar should be in JRUBY_HOME/lib or should be in ENV['PATH'] or load path |
6 | 8 |
|
7 | 9 | java_version = java.lang.System.getProperty("java.version") |
8 | | - ojdbc_jars = if java_version =~ /^1.5/ |
9 | | - %w(ojdbc5.jar) |
10 | | - elsif java_version =~ /^1.6/ |
11 | | - %w(ojdbc6.jar) |
12 | | - elsif java_version >= "1.7" |
13 | | - # Oracle 11g client ojdbc6.jar is also compatible with Java 1.7 |
14 | | - # Oracle 12c client provides new ojdbc7.jar |
15 | | - %w(ojdbc7.jar ojdbc6.jar) |
| 10 | + java_major = if java_version =~ /^1\.(\d+)/ |
| 11 | + $1.to_i |
16 | 12 | else |
17 | | - [] |
| 13 | + java_version.to_i |
18 | 14 | end |
19 | 15 |
|
20 | | - if ENV_JAVA["java.class.path"] !~ Regexp.new(ojdbc_jars.join("|")) |
| 16 | + ojdbc_jars << "ojdbc17.jar" if java_major >= 17 |
| 17 | + ojdbc_jars << "ojdbc11.jar" if java_major >= 11 |
| 18 | + ojdbc_jars << "ojdbc8.jar" if java_major >= 8 |
| 19 | + ojdbc_jars << "ojdbc7.jar" if java_major >= 7 |
| 20 | + ojdbc_jars << "ojdbc6.jar" if java_major >= 6 |
| 21 | + ojdbc_jars << "ojdbc5.jar" if java_major == 5 |
| 22 | + |
| 23 | + if ENV_JAVA["java.class.path"] !~ Regexp.union(ojdbc_jars) |
21 | 24 | # On Unix environment variable should be PATH, on Windows it is sometimes Path |
22 | 25 | env_path = (ENV["PATH"] || ENV["Path"] || "").split(File::PATH_SEPARATOR) |
23 | 26 | # Look for JDBC driver at first in lib subdirectory (application specific JDBC file version) |
|
33 | 36 | end |
34 | 37 | end |
35 | 38 |
|
36 | | - java.sql.DriverManager.registerDriver Java::oracle.jdbc.OracleDriver.new |
37 | | - |
38 | 39 | # set tns_admin property from TNS_ADMIN environment variable |
39 | 40 | if !java.lang.System.get_property("oracle.net.tns_admin") && ENV["TNS_ADMIN"] |
40 | 41 | java.lang.System.set_property("oracle.net.tns_admin", ENV["TNS_ADMIN"]) |
41 | 42 | end |
42 | 43 |
|
43 | | -rescue LoadError, NameError |
| 44 | +rescue LoadError |
44 | 45 | # JDBC driver is unavailable. |
45 | 46 | raise LoadError, "ERROR: ruby-plsql could not load Oracle JDBC driver. Please install #{ojdbc_jars.empty? ? "Oracle JDBC" : ojdbc_jars.join(' or ') } library." |
46 | 47 | end |
47 | 48 |
|
48 | 49 | module PLSQL |
49 | 50 | class JDBCConnection < Connection # :nodoc: |
| 51 | + begin |
| 52 | + ORACLE_DRIVER = Java::oracle.jdbc.OracleDriver.new |
| 53 | + java.sql.DriverManager.registerDriver ORACLE_DRIVER |
| 54 | + rescue NameError |
| 55 | + raise LoadError, "ERROR: ruby-plsql could not load Oracle JDBC driver. " \ |
| 56 | + "Please install the appropriate Oracle JDBC driver. " \ |
| 57 | + "See https://www.oracle.com/database/technologies/appdev/jdbc-downloads.html" |
| 58 | + end |
| 59 | + |
50 | 60 | def self.create_raw(params) |
51 | 61 | url = jdbc_connection_url(params) |
52 | | - new(java.sql.DriverManager.getConnection(url, params[:username], params[:password])) |
| 62 | + conn = begin |
| 63 | + java.sql.DriverManager.getConnection(url, params[:username], params[:password]) |
| 64 | + rescue Java::JavaSql::SQLException => e |
| 65 | + raise unless e.message =~ /no suitable driver/i |
| 66 | + # bypass DriverManager to work in cases where ojdbc*.jar |
| 67 | + # is added to the load path at runtime and not on the |
| 68 | + # system classpath |
| 69 | + ORACLE_DRIVER.connect(url, java.util.Properties.new.tap do |props| |
| 70 | + props.setProperty("user", params[:username]) |
| 71 | + props.setProperty("password", params[:password]) |
| 72 | + end) |
| 73 | + end |
| 74 | + conn.setAutoCommit(false) |
| 75 | + new(conn) |
53 | 76 | end |
54 | 77 |
|
55 | 78 | def self.jdbc_connection_url(params) |
|
0 commit comments