README
¶
PostgreSQL client for Golang 
Supports:
- Basic types: integers, floats, string, bool, time.Time, and pointers to these types.
- sql.NullBool, sql.NullString, sql.NullInt64 and sql.Float64.
- sql.Scanner and sql/driver.Valuer interfaces.
- Arrays.
- Partially hstore.
- Transactions.
- Prepared statements.
- Notifications:
LISTEN
/NOTIFY
. COPY FROM
andCOPY TO
.- Timeouts. Client sends
CancelRequest
message on timeout. - Connection pool.
- Queries are retried when possible.
- PostgreSQL to Go struct mapping.
API docs: http://godoc.org/gopkg.in/pg.v2. Make sure to check examples: http://godoc.org/gopkg.in/pg.v2#pkg-examples.
Installation
Install:
go get gopkg.in/pg.v2
Changelog
v3
Not ready for production.
v2
- Support for named placeholders:
a := &Article{Id: 1, Name: "Hello world"}
_, err := db.ExecOne(`UPDATE articles SET name = ?name WHERE id = ?id`, a)
- CopyFrom/CopyTo support:
r := strings.NewReader("hello\t5\nworld\t5\nfoo\t3\nbar\t3\n")
res, err := t.db.CopyFrom(r, "COPY test FROM STDIN")
- Simplify collections:
type Articles []*Article
func (articles *Articles) New() interface{} {
a := &Article{}
*articles = append(*articles, a)
return a
}
v1
Initial release.
Example
package pg_test
import (
"fmt"
"gopkg.in/pg.v2"
)
type User struct {
Name string
Emails []string
}
type Users []*User
func (users *Users) New() interface{} {
u := &User{}
*users = append(*users, u)
return u
}
func CreateUser(db *pg.DB, user *User) error {
_, err := db.ExecOne(`INSERT INTO users VALUES (?name, ?emails)`, user)
return err
}
func GetUsers(db *pg.DB) ([]*User, error) {
var users Users
_, err := db.Query(&users, `SELECT * FROM users`)
if err != nil {
return nil, err
}
return users, nil
}
func ExampleDB_Query() {
db := pg.Connect(&pg.Options{
User: "postgres",
})
defer db.Close()
_, err := db.Exec(`CREATE TEMP TABLE users (name text, emails text[])`)
if err != nil {
panic(err)
}
err = CreateUser(db, &User{"admin", []string{"admin1@admin", "admin2@admin"}})
if err != nil {
panic(err)
}
err = CreateUser(db, &User{"root", []string{"root1@root", "root2@root"}})
if err != nil {
panic(err)
}
users, err := GetUsers(db)
if err != nil {
panic(err)
}
fmt.Println(users[0], users[1])
// Output: &{admin [admin1@admin admin2@admin]} &{root [root1@root root2@root]}
}
Documentation
¶
Overview ¶
Package github.com/vmihailenco/pg implements a PostgreSQL client.
Index ¶
- Variables
- func AppendQ(dst []byte, src string, params ...interface{}) ([]byte, error)
- func Decode(dst interface{}, f []byte) error
- func DecodeValue(dst reflect.Value, f []byte) error
- type Appender
- type DB
- func (db *DB) Begin() (*Tx, error)
- func (db *DB) Close() error
- func (db *DB) CopyFrom(r io.Reader, q string, args ...interface{}) (*Result, error)
- func (db *DB) CopyTo(w io.WriteCloser, q string, args ...interface{}) (*Result, error)
- func (db *DB) Exec(q string, args ...interface{}) (res *Result, err error)
- func (db *DB) ExecOne(q string, args ...interface{}) (*Result, error)
- func (db *DB) Listen(channels ...string) (*Listener, error)
- func (db *DB) Prepare(q string) (*Stmt, error)
- func (db *DB) Query(f Factory, q string, args ...interface{}) (res *Result, err error)
- func (db *DB) QueryOne(model interface{}, q string, args ...interface{}) (*Result, error)
- func (db *DB) RunInTransaction(fn func(*Tx) error) error
- type Error
- type F
- type Factory
- type IntegrityError
- type Ints
- type IntsSet
- type Listener
- type Loader
- type Options
- type Q
- type RawAppender
- type RecordReader
- type Result
- type Stmt
- func (stmt *Stmt) Close() error
- func (stmt *Stmt) Exec(args ...interface{}) (res *Result, err error)
- func (stmt *Stmt) ExecOne(args ...interface{}) (*Result, error)
- func (stmt *Stmt) Query(f Factory, args ...interface{}) (res *Result, err error)
- func (stmt *Stmt) QueryOne(model interface{}, args ...interface{}) (*Result, error)
- type Strings
- type Tx
- func (tx *Tx) Commit() error
- func (tx *Tx) Exec(q string, args ...interface{}) (*Result, error)
- func (tx *Tx) ExecOne(q string, args ...interface{}) (*Result, error)
- func (tx *Tx) Prepare(q string) (*Stmt, error)
- func (tx *Tx) Query(f Factory, q string, args ...interface{}) (*Result, error)
- func (tx *Tx) QueryOne(model interface{}, q string, args ...interface{}) (*Result, error)
- func (tx *Tx) Rollback() error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ( ErrSSLNotSupported = errorf("pg: SSL is not enabled on the server") ErrNoRows = errorf("pg: no rows in result set") ErrMultiRows = errorf("pg: multiple rows in result set") )
View Source
var (
Discard = discardLoader{}
)
Functions ¶
Types ¶
type DB ¶
type DB struct {
// contains filtered or unexported fields
}
Thread-safe.
type IntegrityError ¶
type IntegrityError struct {
// contains filtered or unexported fields
}
type Listener ¶
type Listener struct {
// contains filtered or unexported fields
}
Not thread-safe.
Example ¶
Output: mychan "hello world" <nil>
type Options ¶
type Options struct { Network string Host string Port string User string Password string Database string SSL bool // Params specify connection run-time configuration parameters. Params map[string]interface{} PoolSize int DialTimeout time.Duration ReadTimeout time.Duration WriteTimeout time.Duration IdleTimeout time.Duration IdleCheckFrequency time.Duration }
type RawAppender ¶
type RecordReader ¶
type Stmt ¶
type Stmt struct {
// contains filtered or unexported fields
}
Not thread-safe.
Source Files
¶
Click to show internal directories.
Click to hide internal directories.