pokemon-go-trade / vendor / github.com / go-pg / pg /
Simon Lindgren authored on 12 Feb 2021
..
internal first commit 3 years ago
orm first commit 3 years ago
types first commit 3 years ago
urlvalues first commit 3 years ago
.travis.yml first commit 3 years ago
CHANGELOG.md first commit 3 years ago
LICENSE first commit 3 years ago
Makefile first commit 3 years ago
README.md first commit 3 years ago
base.go first commit 3 years ago
bench_test.go first commit 3 years ago
conv_test.go first commit 3 years ago
db.go first commit 3 years ago
db_test.go first commit 3 years ago
doc.go first commit 3 years ago
error.go first commit 3 years ago
example_array_test.go first commit 3 years ago
example_composite_test.go first commit 3 years ago
example_hstore_test.go first commit 3 years ago
example_many2many_self_test.go first commit 3 years ago
example_many2many_test.go first commit 3 years ago
example_model_test.go first commit 3 years ago
example_placeholders_test.go first commit 3 years ago
example_soft_delete_test.go first commit 3 years ago
example_test.go first commit 3 years ago
exampledb_model_test.go first commit 3 years ago
exampledb_query_test.go first commit 3 years ago
export_test.go first commit 3 years ago
hook.go first commit 3 years ago
hook_test.go first commit 3 years ago
listener.go first commit 3 years ago
listener_test.go first commit 3 years ago
loader_test.go first commit 3 years ago
main_test.go first commit 3 years ago
messages.go first commit 3 years ago
options.go first commit 3 years ago
options_test.go first commit 3 years ago
pg.go first commit 3 years ago
pool_test.go first commit 3 years ago
race_test.go first commit 3 years ago
result.go first commit 3 years ago
stmt.go first commit 3 years ago
tx.go first commit 3 years ago
tx_test.go first commit 3 years ago
README.md

PostgreSQL client and ORM for Golang

Build Status GoDoc

Features:

Get Started

go get -u github.com/go-pg/pg

Look & Feel

package pg_test

import (
    "fmt"

    "github.com/go-pg/pg"
    "github.com/go-pg/pg/orm"
)

type User struct {
    Id     int64
    Name   string
    Emails []string
}

func (u User) String() string {
    return fmt.Sprintf("User<%d %s %v>", u.Id, u.Name, u.Emails)
}

type Story struct {
    Id       int64
    Title    string
    AuthorId int64
    Author   *User
}

func (s Story) String() string {
    return fmt.Sprintf("Story<%d %s %s>", s.Id, s.Title, s.Author)
}

func ExampleDB_Model() {
    db := pg.Connect(&pg.Options{
        User: "postgres",
    })
    defer db.Close()

    err := createSchema(db)
    if err != nil {
        panic(err)
    }

    user1 := &User{
        Name:   "admin",
        Emails: []string{"admin1@admin", "admin2@admin"},
    }
    err = db.Insert(user1)
    if err != nil {
        panic(err)
    }

    err = db.Insert(&User{
        Name:   "root",
        Emails: []string{"root1@root", "root2@root"},
    })
    if err != nil {
        panic(err)
    }

    story1 := &Story{
        Title:    "Cool story",
        AuthorId: user1.Id,
    }
    err = db.Insert(story1)
    if err != nil {
        panic(err)
    }

    // Select user by primary key.
    user := &User{Id: user1.Id}
    err = db.Select(user)
    if err != nil {
        panic(err)
    }

    // Select all users.
    var users []User
    err = db.Model(&users).Select()
    if err != nil {
        panic(err)
    }

    // Select story and associated author in one query.
    story := new(Story)
    err = db.Model(story).
        Relation("Author").
        Where("story.id = ?", story1.Id).
        Select()
    if err != nil {
        panic(err)
    }

    fmt.Println(user)
    fmt.Println(users)
    fmt.Println(story)
    // Output: User<1 admin [admin1@admin admin2@admin]>
    // [User<1 admin [admin1@admin admin2@admin]> User<2 root [root1@root root2@root]>]
    // Story<1 Cool story User<1 admin [admin1@admin admin2@admin]>>
}

func createSchema(db *pg.DB) error {
    for _, model := range []interface{}{(*User)(nil), (*Story)(nil)} {
        err := db.CreateTable(model, &orm.CreateTableOptions{
            Temp: true,
        })
        if err != nil {
            return err
        }
    }
    return nil
}

See also