11require " socket"
22
33class 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