Skip to content

Commit

Permalink
Add support for Unix sockets. Fixes #14.
Browse files Browse the repository at this point in the history
  • Loading branch information
vmihailenco committed Jul 11, 2014
1 parent fc615cd commit 47a55b3
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ install:
- ln -s `pwd` $HOME/gopath/src/gopkg.in/pg.v2

before_script:
- psql -c 'CREATE DATABASE test;' -U postgres
- psql -c 'CREATE DATABASE test' -U postgres
12 changes: 6 additions & 6 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ import (
var zeroTime = time.Time{}

func dial(opt *Options) (net.Conn, error) {
return net.DialTimeout(
"tcp", net.JoinHostPort(opt.getHost(), opt.getPort()), opt.getDialTimeout())
return net.DialTimeout(opt.getNetwork(), opt.getAddr(), opt.getDialTimeout())
}

type conn struct {
opt *Options
cn net.Conn
br *bufio.Reader
buf *buffer
opt *Options
cn net.Conn
br *bufio.Reader
buf *buffer

inUse bool
usedAt time.Time

Expand Down
17 changes: 17 additions & 0 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
const defaultBackoff = 100 * time.Millisecond

type Options struct {
Network string
Host string
Port string
User string
Expand All @@ -28,6 +29,13 @@ type Options struct {
IdleCheckFrequency time.Duration
}

func (opt *Options) getNetwork() string {
if opt == nil || opt.Network == "" {
return "tcp"
}
return opt.Network
}

func (opt *Options) getHost() string {
if opt == nil || opt.Host == "" {
return "localhost"
Expand All @@ -42,6 +50,15 @@ func (opt *Options) getPort() string {
return opt.Port
}

func (opt *Options) getAddr() string {
switch opt.getNetwork() {
case "tcp":
return net.JoinHostPort(opt.getHost(), opt.getPort())
default:
return opt.getHost()
}
}

func (opt *Options) getUser() string {
if opt == nil || opt.User == "" {
return ""
Expand Down
13 changes: 13 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ import (
"gopkg.in/pg.v2/pgutil"
)

func TestUnixSocket(t *testing.T) {
db := pg.Connect(&pg.Options{
Network: "unix",
Host: "/var/run/postgresql/.s.PGSQL.5432",
User: "postgres",
Database: "test",
})
_, err := db.Exec("SELECT 1")
if err != nil {
t.Fatal(err)
}
}

func Test(t *testing.T) { TestingT(t) }

var _ = Suite(&DBTest{})
Expand Down

0 comments on commit 47a55b3

Please sign in to comment.