Skip to content

Commit 1f9d4c4

Browse files
yahondaclaude
andcommitted
Fix SID syntax to use correct URL format without //
SID connections should use jdbc:oracle:thin:@host:port:SID (without double slash), while service name connections use the thin-style @//host:port/service_name format. Also return early when a custom URL is provided. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 3f1636a commit 1f9d4c4

3 files changed

Lines changed: 14 additions & 6 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ plsql.connect! username: "hr", password: "hr", host: "localhost", database: "MYS
132132
If you need to connect using the legacy SID syntax (for Oracle databases older than 12c), prefix the database name with a colon:
133133

134134
```ruby
135-
# Connects using SID syntax: jdbc:oracle:thin:@//localhost:1521:MYSID
135+
# Connects using SID syntax: jdbc:oracle:thin:@localhost:1521:MYSID
136136
plsql.connect! username: "hr", password: "hr", host: "localhost", database: ":MYSID"
137137
```
138138

lib/plsql/jdbc_connection.rb

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,19 @@ def self.jdbc_connection_url(params)
5757
if ENV["TNS_ADMIN"] && database && !params[:host] && !params[:url]
5858
"jdbc:oracle:thin:@#{database}"
5959
else
60-
unless database =~ /^(:|\/)/
61-
# assume database is a service name if no colon or slash are supplied
62-
database = "/#{database}"
60+
return params[:url] if params[:url]
61+
62+
host = params[:host] || "localhost"
63+
port = params[:port] || 1521
64+
65+
if database =~ /^:/
66+
# SID syntax: jdbc:oracle:thin:@host:port:SID
67+
"jdbc:oracle:thin:@#{host}:#{port}#{database}"
68+
else
69+
# service name syntax: jdbc:oracle:thin:@//host:port/service_name
70+
database = "/#{database}" unless database =~ /^\//
71+
"jdbc:oracle:thin:@//#{host}:#{port}#{database}"
6372
end
64-
params[:url] || "jdbc:oracle:thin:@//#{params[:host] || 'localhost'}:#{params[:port] || 1521}#{database}"
6573
end
6674
end
6775

spec/plsql/connection_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@
480480

481481
it "should use SID syntax when database starts with colon" do
482482
url = PLSQL::JDBCConnection.jdbc_connection_url(host: "myhost", port: 1521, database: ":MYSID")
483-
expect(url).to eq "jdbc:oracle:thin:@//myhost:1521:MYSID"
483+
expect(url).to eq "jdbc:oracle:thin:@myhost:1521:MYSID"
484484
end
485485

486486
it "should use service name syntax when database starts with slash" do

0 commit comments

Comments
 (0)