Skip to content

Commit b7d3089

Browse files
yahondaclaude
andcommitted
Skip TNS-alias path for service-name and SID syntax databases
When TNS_ADMIN is set, databases starting with `/` or `:` should use the normal service-name/SID URL format instead of the TNS-alias path. Replace allow(ENV).to receive stubs with actual ENV unset/restore. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 1f9d4c4 commit b7d3089

2 files changed

Lines changed: 38 additions & 7 deletions

File tree

lib/plsql/jdbc_connection.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def self.create_raw(params)
5454

5555
def self.jdbc_connection_url(params)
5656
database = params[:database]
57-
if ENV["TNS_ADMIN"] && database && !params[:host] && !params[:url]
57+
if ENV["TNS_ADMIN"] && database && database !~ %r{\A[:/]} && !params[:host] && !params[:url]
5858
"jdbc:oracle:thin:@#{database}"
5959
else
6060
return params[:url] if params[:url]

spec/plsql/connection_spec.rb

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -474,8 +474,13 @@
474474
end
475475

476476
it "should use default host and port when not specified" do
477-
url = PLSQL::JDBCConnection.jdbc_connection_url(database: "/MYSERVICENAME")
478-
expect(url).to eq "jdbc:oracle:thin:@//localhost:1521/MYSERVICENAME"
477+
original_tns_admin = ENV.delete("TNS_ADMIN")
478+
begin
479+
url = PLSQL::JDBCConnection.jdbc_connection_url(database: "/MYSERVICENAME")
480+
expect(url).to eq "jdbc:oracle:thin:@//localhost:1521/MYSERVICENAME"
481+
ensure
482+
ENV["TNS_ADMIN"] = original_tns_admin
483+
end
479484
end
480485

481486
it "should use SID syntax when database starts with colon" do
@@ -489,10 +494,36 @@
489494
end
490495

491496
it "should use TNS alias when TNS_ADMIN is set and no host specified" do
492-
allow(ENV).to receive(:[]).and_call_original
493-
allow(ENV).to receive(:[]).with("TNS_ADMIN").and_return("/path/to/tns")
494-
url = PLSQL::JDBCConnection.jdbc_connection_url(database: "MYALIAS")
495-
expect(url).to eq "jdbc:oracle:thin:@MYALIAS"
497+
original_tns_admin = ENV["TNS_ADMIN"]
498+
ENV["TNS_ADMIN"] = "/path/to/tns"
499+
begin
500+
url = PLSQL::JDBCConnection.jdbc_connection_url(database: "MYALIAS")
501+
expect(url).to eq "jdbc:oracle:thin:@MYALIAS"
502+
ensure
503+
ENV["TNS_ADMIN"] = original_tns_admin
504+
end
505+
end
506+
507+
it "should use service name syntax when TNS_ADMIN is set and database starts with slash" do
508+
original_tns_admin = ENV["TNS_ADMIN"]
509+
ENV["TNS_ADMIN"] = "/path/to/tns"
510+
begin
511+
url = PLSQL::JDBCConnection.jdbc_connection_url(database: "/MYSERVICENAME")
512+
expect(url).to eq "jdbc:oracle:thin:@//localhost:1521/MYSERVICENAME"
513+
ensure
514+
ENV["TNS_ADMIN"] = original_tns_admin
515+
end
516+
end
517+
518+
it "should use SID syntax when TNS_ADMIN is set and database starts with colon" do
519+
original_tns_admin = ENV["TNS_ADMIN"]
520+
ENV["TNS_ADMIN"] = "/path/to/tns"
521+
begin
522+
url = PLSQL::JDBCConnection.jdbc_connection_url(database: ":MYSID")
523+
expect(url).to eq "jdbc:oracle:thin:@localhost:1521:MYSID"
524+
ensure
525+
ENV["TNS_ADMIN"] = original_tns_admin
526+
end
496527
end
497528

498529
it "should use custom URL when provided" do

0 commit comments

Comments
 (0)