Skip to content

Commit 9b08c20

Browse files
committed
Use URI for transport
1 parent 9169977 commit 9b08c20

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

spec/connection_options_spec.cr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ private def from_uri(uri)
55
end
66

77
private def tcp(host, port)
8-
MySql::Connection::TCPSocketTransport.new(host: host, port: port)
8+
URI.new("tcp", host, port)
99
end
1010

1111
private def socket(path)
12-
MySql::Connection::UnixSocketTransport.new(path: Path.new(path))
12+
URI.new("unix", nil, nil, path)
1313
end
1414

1515
describe Connection::Options do

src/mysql/connection.cr

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
require "socket"
22

33
class MySql::Connection < DB::Connection
4-
record UnixSocketTransport, path : Path
5-
record TCPSocketTransport, host : String, port : Int32
6-
74
record Options,
8-
transport : UnixSocketTransport | TCPSocketTransport,
5+
transport : URI,
96
username : String?,
107
password : String?,
118
initial_catalog : String?,
@@ -16,16 +13,15 @@ class MySql::Connection < DB::Connection
1613

1714
if (host = uri.hostname) && !host.blank?
1815
port = uri.port || 3306
19-
transport = TCPSocketTransport.new(host: host, port: port)
16+
transport = URI.new("tcp", host, port)
2017

2118
# for tcp socket we support the first component to be the database
2219
# but the query string takes presedence because it's more explicit
2320
if initial_catalog.nil? && (path = uri.path) && path.size > 1
2421
initial_catalog = path[1..-1]
2522
end
2623
else
27-
# uri.path has a final / we want to drop
28-
transport = UnixSocketTransport.new(path: Path.new(uri.path.chomp('/')))
24+
transport = URI.new("unix", nil, nil, uri.path)
2925
end
3026

3127
username = uri.user
@@ -47,12 +43,16 @@ class MySql::Connection < DB::Connection
4743
begin
4844
charset_id = Collations.id_for_collation(mysql_options.charset).to_u8
4945

46+
transport = mysql_options.transport
5047
@socket =
51-
case transport = mysql_options.transport
52-
in TCPSocketTransport
53-
TCPSocket.new(transport.host, transport.port)
54-
in UnixSocketTransport
55-
UNIXSocket.new(transport.path.to_s)
48+
case transport.scheme
49+
when "tcp"
50+
host = transport.host || raise "Missing host in transport #{transport}"
51+
TCPSocket.new(host, transport.port)
52+
when "unix"
53+
UNIXSocket.new(transport.path)
54+
else
55+
raise "Transport not supported #{transport}"
5656
end
5757

5858
handshake = read_packet(Protocol::HandshakeV10)

0 commit comments

Comments
 (0)