Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
bmizerany committed Mar 23, 2012
0 parents commit df1f131
Show file tree
Hide file tree
Showing 8 changed files with 1,333 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
.db
*.test
66 changes: 66 additions & 0 deletions README.md
@@ -0,0 +1,66 @@
# pq - A pure Go postgres driver for Go's database/sql package

## Install

go get github.com/bmizerany/pq

## Use

package main

import (
_ "github.com/bmizerany/pq"
"database/sql"
)

func main() {
db, err := sql.Open("postgres", "user=pqgotest dbname=pqgotest sslmode=verify-full")
// ...
}

**Connection String Parameters**

These are a subset of the libpq connection parameters.
See http://www.postgresql.org/docs/9.0/static/libpq-connect.html

* `dbname` - The name of the database to connect to
* `user` - The user to sign in as
* `password` - The user's password
* `host` - The host to connect to. Values that start with `/` are for unix domain sockets. (default is `localhost`)
* `port` - The port to bind to. (default is `5432`)
* `sslmode` - Whether or not to use SSL (default is `require`, this is not the default for libpq)
Valid values are:
* `disable` - No SSL
* `require` - Always SSL (skip verification)
* `verify-full` - Always SSL (require verification)

See http://tip.golang.org/pkg/database/sql to learn how to use with `pq` through the `database/sql` package.

## Features

* SSL
* Handles bad connections for `database/sql`
* Scan `time.Time` correctly (i.e. `timestamp[tz]`, `time[tz]`, `date`)
* Scan binary blobs correctly (i.e. `bytea`)

## Future / Things you can help with

* Notifications: `LISTEN`/`NOTIFY`
* `hstore` sugar (i.e. handling hstore in `rows.Scan`)

## Thank you (alphabetical)

Some of these contributors are from the original library `bmizerany/pq.go` whose
code still exists in here.

* Blake Gentry (bgentry)
* Brad Fitzpatrick (bradfitz)
* Daniel Farina (fdr)
* Everyone at The Go Team
* Federico Romero (federomero)
* Heroku (heroku)
* Keith Rarick (kr)
* Mike Lewis (mikelikespie)
* Ryan Smith (ryandotsmith)
* Samuel Stauffer (samuel)
* notedit (notedit)
74 changes: 74 additions & 0 deletions buf.go
@@ -0,0 +1,74 @@
package pq

import (
"bytes"
"encoding/binary"
)

type readBuf []byte

func (b *readBuf) int32() (n int) {
n = int(binary.BigEndian.Uint32(*b))
*b = (*b)[4:]
return
}

func (b *readBuf) int16() (n int) {
n = int(binary.BigEndian.Uint16(*b))
*b = (*b)[2:]
return
}

var stringTerm = []byte{0}

func (b *readBuf) string() string {
i := bytes.Index(*b, stringTerm)
if i < 0 {
errorf("invalid message format; expected string terminator")
}
s := (*b)[:i]
*b = (*b)[i+1:]
return string(s)
}

func (b *readBuf) next(n int) (v []byte) {
v = (*b)[:n]
*b = (*b)[n:]
return
}

func (b *readBuf) byte() byte {
return b.next(1)[0]
}

type writeBuf []byte

func newWriteBuf(c byte) *writeBuf {
b := make(writeBuf, 5)
b[0] = c
return &b
}

func (b *writeBuf) int32(n int) {
x := make([]byte, 4)
binary.BigEndian.PutUint32(x, uint32(n))
*b = append(*b, x...)
}

func (b *writeBuf) int16(n int) {
x := make([]byte, 2)
binary.BigEndian.PutUint16(x, uint16(n))
*b = append(*b, x...)
}

func (b *writeBuf) string(s string) {
*b = append(*b, (s + "\000")...)
}

func (b *writeBuf) byte(c byte) {
*b = append(*b, c)
}

func (b *writeBuf) bytes(v []byte) {
*b = append(*b, v...)
}

0 comments on commit df1f131

Please sign in to comment.