Skip to content

Commit

Permalink
Merge Options.Host and Options.Port into Options.Addr.
Browse files Browse the repository at this point in the history
  • Loading branch information
vmihailenco committed Mar 8, 2016
1 parent 9c29a0b commit 54da9a4
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 52 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## v4

- `Options.Host` and `Options.Port` merged into `Options.Addr`.
- `LoadInto` renamed to `Scan`, `ColumnLoader` renamed to `ColumnScanner`, LoadColumn renamed to ScanColumn, `NewRecord() interface{}` changed to `NewModel() ColumnScanner`, `AppendQuery(dst []byte) []byte` changed to `AppendValue(dst []byte, quote bool) ([]byte, error)`.
- Structs, maps and slices are marshalled to JSON by default.
- Added support for scanning slices, .e.g. scanning `[]int`.
Expand Down
8 changes: 8 additions & 0 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package pg

import (
"container/list"
"crypto/md5"
"crypto/tls"
"encoding/binary"
"encoding/hex"
"fmt"
"io"
"net"
Expand Down Expand Up @@ -340,3 +342,9 @@ func (cn *conn) FlushWrite() error {
}
return err
}

func md5s(s string) string {
h := md5.New()
h.Write([]byte(s))
return hex.EncodeToString(h.Sum(nil))
}
28 changes: 7 additions & 21 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ type Options struct {
// The network type, either tcp or unix.
// Default is tcp.
Network string
Host string
Port string
Addr string
User string
Password string
Database string
Expand Down Expand Up @@ -63,27 +62,14 @@ func (opt *Options) getNetwork() string {
return opt.Network
}

func (opt *Options) getHost() string {
if opt == nil || opt.Host == "" {
return "localhost"
}
return opt.Host
}

func (opt *Options) getPort() string {
if opt == nil || opt.Port == "" {
return "5432"
}
return opt.Port
}

func (opt *Options) getAddr() string {
switch opt.getNetwork() {
case "tcp":
return net.JoinHostPort(opt.getHost(), opt.getPort())
default:
return opt.getHost()
if opt.Addr != "" {
return opt.Addr
}
if opt.getNetwork() == "unix" {
return "/var/run/postgresql/.s.PGSQL.5432"
}
return "localhost:5432"
}

func (opt *Options) getUser() string {
Expand Down
14 changes: 13 additions & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package pg_test
import (
"bytes"
"database/sql/driver"
"io"
"strings"
"testing"
"time"
Expand All @@ -17,7 +18,7 @@ import (
func TestUnixSocket(t *testing.T) {
db := pg.Connect(&pg.Options{
Network: "unix",
Host: "/var/run/postgresql/.s.PGSQL.5432",
Addr: "/var/run/postgresql/.s.PGSQL.5432",
User: "postgres",
})
defer db.Close()
Expand Down Expand Up @@ -221,3 +222,14 @@ func (t *DBTest) TestCopyTo(c *C) {
c.Assert(err, IsNil)
c.Assert(res.Affected(), Equals, 1000000)
}

//------------------------------------------------------------------------------

// NopWriteCloser is a WriteCloser which does nothing in Close.
type NopWriteCloser struct {
io.Writer
}

func (NopWriteCloser) Close() error {
return nil
}
4 changes: 0 additions & 4 deletions orm/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,6 @@ func (f *Formatter) Append(dst []byte, src string) ([]byte, error) {
}

func (f *Formatter) AppendBytes(dst []byte, src []byte) ([]byte, error) {
if f.params == nil {
return append(dst, src...), nil
}

p := parser.New(src)

for p.Valid() {
Expand Down
12 changes: 0 additions & 12 deletions util.go

This file was deleted.

14 changes: 0 additions & 14 deletions util_test.go

This file was deleted.

0 comments on commit 54da9a4

Please sign in to comment.