Skip to content

Commit

Permalink
Add SelectOrCreate.
Browse files Browse the repository at this point in the history
  • Loading branch information
vmihailenco committed Apr 6, 2016
1 parent aa40f0d commit 5bd620b
Show file tree
Hide file tree
Showing 9 changed files with 181 additions and 32 deletions.
6 changes: 3 additions & 3 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,17 +240,17 @@ func (db *DB) Model(model interface{}) *orm.Query {
return orm.NewQuery(db, model)
}

// Create inserts model into database.
// Create inserts the model into database.
func (db *DB) Create(model interface{}) error {
return orm.Create(db, model)
}

// Update updates model in database.
// Update updates the model in database.
func (db *DB) Update(model interface{}) error {
return orm.Update(db, model)
}

// Delete deletes model in database.
// Delete deletes the model from database.
func (db *DB) Delete(model interface{}) error {
return orm.Delete(db, model)
}
Expand Down
3 changes: 2 additions & 1 deletion db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"gopkg.in/pg.v4"
)

func TestPG(t *testing.T) {
func TestGinkgo(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "pg")
}
Expand Down Expand Up @@ -266,6 +266,7 @@ func createTestSchema(db *pg.DB) error {
`CREATE TABLE book_genres (book_id int, genre_id int, genre__rating int)`,
`CREATE TABLE translations (id serial, book_id int, lang varchar(2))`,
`CREATE TABLE comments (trackable_id int, trackable_type varchar(100), text text)`,
`CREATE UNIQUE INDEX authors_name ON authors (name)`,
}
for _, q := range sql {
_, err := db.Exec(q)
Expand Down
4 changes: 2 additions & 2 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
)

var (
ErrNoRows = internal.Errorf("pg: no rows in result set")
ErrMultiRows = internal.Errorf("pg: multiple rows in result set")
ErrNoRows = internal.ErrNoRows
ErrMultiRows = internal.ErrMultiRows

errSSLNotSupported = internal.Errorf("pg: SSL is not enabled on the server")

Expand Down
57 changes: 38 additions & 19 deletions example_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"gopkg.in/pg.v4"
)

func connectDB() *pg.DB {
func modelDB() *pg.DB {
db := pg.Connect(&pg.Options{
User: "postgres",
})
Expand Down Expand Up @@ -74,7 +74,7 @@ func connectDB() *pg.DB {
}

func ExampleDB_Create() {
db := connectDB()
db := modelDB()

book := Book{
Title: "new book",
Expand All @@ -95,7 +95,7 @@ func ExampleDB_Create() {
}

func ExampleDB_Create_onConflict() {
db := connectDB()
db := modelDB()

book := Book{
Id: 100,
Expand Down Expand Up @@ -123,8 +123,27 @@ func ExampleDB_Create_onConflict() {
// did nothing
}

func ExampleDB_Create_getOrCreate() {
db := modelDB()

author := Author{
Name: "R. Scott Bakker",
}
created, err := db.Model(&author).
Column("id").
Where("name = ?name").
OnConflict("DO NOTHING").
Returning("id").
SelectOrCreate()
if err != nil {
panic(err)
}
fmt.Println(created, author)
// true Author<ID=2 Name="R. Scott Bakker">
}

func ExampleDB_Model_firstRow() {
db := connectDB()
db := modelDB()

var firstBook Book
err := db.Model(&firstBook).First()
Expand All @@ -136,7 +155,7 @@ func ExampleDB_Model_firstRow() {
}

func ExampleDB_Model_lastRow() {
db := connectDB()
db := modelDB()

var lastBook Book
err := db.Model(&lastBook).Last()
Expand All @@ -148,7 +167,7 @@ func ExampleDB_Model_lastRow() {
}

func ExampleDB_Model_selectAllColumns() {
db := connectDB()
db := modelDB()

var book Book
err := db.Model(&book).Column("book.*").First()
Expand All @@ -160,7 +179,7 @@ func ExampleDB_Model_selectAllColumns() {
}

func ExampleDB_Model_selectSomeColumns() {
db := connectDB()
db := modelDB()

var book Book
err := db.Model(&book).
Expand All @@ -175,7 +194,7 @@ func ExampleDB_Model_selectSomeColumns() {
}

func ExampleDB_Model_countRows() {
db := connectDB()
db := modelDB()

count, err := db.Model(Book{}).Count()
if err != nil {
Expand All @@ -201,7 +220,7 @@ func ExampleDB_Model_nullEmptyValue() {
}

func ExampleDB_Model_hasOne() {
db := connectDB()
db := modelDB()

var book Book
err := db.Model(&book).Column("book.*", "Author").First()
Expand All @@ -213,7 +232,7 @@ func ExampleDB_Model_hasOne() {
}

func ExampleDB_Model_hasMany() {
db := connectDB()
db := modelDB()

var author Author
err := db.Model(&author).Column("author.*", "Books").First()
Expand All @@ -225,7 +244,7 @@ func ExampleDB_Model_hasMany() {
}

func ExampleDB_Model_hasManyToMany() {
db := connectDB()
db := modelDB()

var book Book
err := db.Model(&book).Column("book.*", "Genres").First()
Expand All @@ -237,7 +256,7 @@ func ExampleDB_Model_hasManyToMany() {
}

func ExampleDB_Update() {
db := connectDB()
db := modelDB()

err := db.Update(&Book{
Id: 1,
Expand All @@ -258,7 +277,7 @@ func ExampleDB_Update() {
}

func ExampleDB_Update_someColumns() {
db := connectDB()
db := modelDB()

book := Book{
Id: 1,
Expand All @@ -275,7 +294,7 @@ func ExampleDB_Update_someColumns() {
}

func ExampleDB_Update_usingSqlFunction() {
db := connectDB()
db := modelDB()

id := 1
data := map[string]interface{}{
Expand All @@ -295,7 +314,7 @@ func ExampleDB_Update_usingSqlFunction() {
}

func ExampleDB_Update_multipleRows() {
db := connectDB()
db := modelDB()

ids := pg.Ints{1, 2}
data := map[string]interface{}{
Expand All @@ -316,7 +335,7 @@ func ExampleDB_Update_multipleRows() {
}

func ExampleDB_Delete() {
db := connectDB()
db := modelDB()

book := Book{
Title: "title 1",
Expand All @@ -338,7 +357,7 @@ func ExampleDB_Delete() {
}

func ExampleDB_Delete_multipleRows() {
db := connectDB()
db := modelDB()

ids := pg.Ints{1, 2, 3}
res, err := db.Model(Book{}).Where("id IN (?)", ids).Delete()
Expand All @@ -358,7 +377,7 @@ func ExampleDB_Delete_multipleRows() {
}

func ExampleQ() {
db := connectDB()
db := modelDB()

var maxId int
err := db.Model(Book{}).Column(pg.Q("max(id)")).Scan(&maxId)
Expand All @@ -370,7 +389,7 @@ func ExampleQ() {
}

func ExampleF() {
db := connectDB()
db := modelDB()

var book Book
err := db.Model(&book).Where("? = ?", pg.F("id"), 1).Select()
Expand Down
5 changes: 5 additions & 0 deletions internal/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ package internal

import "fmt"

var (
ErrNoRows = Errorf("pg: no rows in result set")
ErrMultiRows = Errorf("pg: multiple rows in result set")
)

type Error struct {
s string
}
Expand Down
18 changes: 18 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import (
"io"
"net"
"strings"
"sync"
"testing"
"time"

_ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq"
. "github.com/onsi/ginkgo"
. "gopkg.in/check.v1"

"gopkg.in/pg.v4"
Expand Down Expand Up @@ -270,3 +272,19 @@ func (cn *badConn) Write([]byte) (int, error) {
}
return 0, badConnError("bad connection")
}

func perform(n int, cbs ...func(int)) {
var wg sync.WaitGroup
for _, cb := range cbs {
for i := 0; i < n; i++ {
wg.Add(1)
go func(cb func(int), i int) {
defer GinkgoRecover()
defer wg.Done()

cb(i)
}(cb, i)
}
}
wg.Wait()
}
7 changes: 6 additions & 1 deletion orm/insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,12 @@ func (ins insertQuery) AppendQuery(b []byte, params []interface{}) ([]byte, erro
b = append(b, ins.onConflict...)
}

b = appendReturning(b, strct, table.PKs)
if len(ins.returning) > 0 {
b = append(b, " RETURNING "...)
b = append(b, ins.returning...)
} else {
b = appendReturning(b, strct, table.PKs)
}

return b, nil
}
Loading

0 comments on commit 5bd620b

Please sign in to comment.