Skip to content

Commit

Permalink
complete relation where
Browse files Browse the repository at this point in the history
  • Loading branch information
fifsky committed Dec 7, 2018
1 parent d0dc70b commit 025e958
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 10 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
## [v2.0.0 (2018-12-06)](https://github.com/ilibs/gosql/compare/v1.0.10...v2.0.0)
## [v1.1.1 (2018-12-07)](https://github.com/ilibs/gosql/compare/v1.1.0...v1.1.1)

### Added
- Added Relation where

## [v1.1.0 (2018-12-06)](https://github.com/ilibs/gosql/compare/v1.0.10...v1.1.0)

### Added
- Added Relation
Expand Down
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ err := gosql.Select(&user, "select * from users where id in(?)",[]int{1,2,3})
## Relation
gosql used the golang structure to express the relationships between tables,You only need to use the `relation` Tag to specify the associated field, see example
```go
type UserMoment struct {
type MomentList struct {
models.Moments
User *models.Users `json:"user" db:"-" relation:"user_id,id"` //one-to-one
Photos []*models.Photos `json:"photos" db:"-" relation:"id,moment_id"` //one-to-many
Expand All @@ -303,7 +303,7 @@ type UserMoment struct {

Get single result
```go
moment := &UserMoment{}
moment := &MomentList{}
err := Model(moment).Where("status = 1 and id = ?",14).Get()
//output User and Photos and you get the result
```
Expand All @@ -328,7 +328,7 @@ SQL:

Get list result, many-to-many
```go
var moments = make([]*UserMoment, 0)
var moments = make([]*MomentList, 0)
err := Model(&moments).Where("status = 1").Limit(10).All()
//You get the total result for *UserMoment slice
```
Expand All @@ -350,6 +350,16 @@ SQL:
Time: 0.00087s
```


Relation Where:
```go
moment := &MomentList{}
err := Relation("User" , func(b *Builder) {
//this is builder instance,
b.Where("gender = 0")
}).Get(moment , "select * from moments")
```

## Hooks
Hooks are functions that are called before or after creation/querying/updating/deletion.

Expand Down
4 changes: 2 additions & 2 deletions builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ func TestBuilder_NullString(t *testing.T) {
}

func TestBuilder_Relation1(t *testing.T) {
moment := &UserMoment{}
moment := &MomentList{}
err := Model(moment).Relation("User" , func(b *Builder) {
b.Where("gender = 1")
}).Where("status = 1 and id = ?",14).Get()
Expand All @@ -563,7 +563,7 @@ func TestBuilder_Relation1(t *testing.T) {
}

func TestBuilder_Relation2(t *testing.T) {
var moments = make([]*UserMoment, 0)
var moments = make([]*MomentList, 0)
err := Model(&moments).Relation("User" , func(b *Builder) {
b.Where("gender = 0")
}).Where("status = 1").Limit(10).All()
Expand Down
24 changes: 22 additions & 2 deletions relation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,34 @@ import (
"github.com/ilibs/gosql/example/models"
)


type UserMoment struct {
models.Users
Moments []*models.Moments `json:"moments" db:"-" relation:"id,user_id"`
}

func TestRelationOne2(t *testing.T) {
moment := &UserMoment{}
err := Model(moment).Relation("Moments", func(b *Builder) {
b.Limit(2)
}).Where("id = ?",5).Get()

b , _ :=json.MarshalIndent(moment,""," ")
fmt.Println(string(b), err)

if err != nil {
t.Fatal(err)
}
}

type MomentList struct {
models.Moments
User *models.Users `json:"user" db:"-" relation:"user_id,id"`
Photos []*models.Photos `json:"photos" db:"-" relation:"id,moment_id"`
}

func TestRelationOne(t *testing.T) {
moment := &UserMoment{}
moment := &MomentList{}
err := Model(moment).Where("status = 1 and id = ?",14).Get()

b , _ :=json.MarshalIndent(moment,""," ")
Expand All @@ -35,7 +55,7 @@ func TestRelationOne(t *testing.T) {
}

func TestRelationAll(t *testing.T) {
var moments = make([]*UserMoment, 0)
var moments = make([]*MomentList, 0)
err := Model(&moments).Where("status = 1").Limit(10).All()
if err != nil {
t.Fatal(err)
Expand Down
4 changes: 2 additions & 2 deletions wrapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ func TestTxx(t *testing.T) {
}

func TestWrapper_Relation(t *testing.T) {
moment := &UserMoment{}
moment := &MomentList{}
err := Relation("User" , func(b *Builder) {
b.Where("gender = 0")
}).Get(moment , "select * from moments")
Expand All @@ -367,7 +367,7 @@ func TestWrapper_Relation(t *testing.T) {


func TestWrapper_Relation2(t *testing.T) {
var moments = make([]*UserMoment, 0)
var moments = make([]*MomentList, 0)
err := Relation("User" , func(b *Builder) {
b.Where("gender = 1")
}).Select(&moments , "select * from moments")
Expand Down

0 comments on commit 025e958

Please sign in to comment.