Skip to content

Commit

Permalink
Add unix domain socket support and update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
timestretch committed Jul 19, 2020
1 parent abffe3a commit cdfefd4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,12 @@ Then use the example above changing the `DB.open` line to
```crystal
DB.open "mysql://test:yourpassword@localhost/test" do |db|
```

## Unix Domain Socket

If you specify a socket path, it will override the hostname in the URI.

```crystal
DB.open "mysql://root@localhost/test?socket=/tmp/mysql.sock" do |db|
end
```
15 changes: 12 additions & 3 deletions src/mysql/connection.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class MySql::Connection < DB::Connection

def initialize(context : DB::ConnectionContext)
super(context)
@socket = uninitialized TCPSocket
@socket = uninitialized TCPSocket | UNIXSocket

begin
host = context.uri.hostname || raise "no host provided"
Expand All @@ -23,8 +23,17 @@ class MySql::Connection < DB::Connection
else
initial_catalog = nil
end

@socket = TCPSocket.new(host, port)

if !context.uri.query.nil?
params = HTTP::Params.parse(context.uri.query.to_s)
end

if !params.nil? && params["socket"]
@socket = UNIXSocket.new(params["socket"])
else
@socket = TCPSocket.new(host, port)
end

handshake = read_packet(Protocol::HandshakeV10)

write_packet(1) do |packet|
Expand Down

0 comments on commit cdfefd4

Please sign in to comment.