From 025e95853526b14aa886dff99120690c684a27ec Mon Sep 17 00:00:00 2001 From: fifsky Date: Fri, 7 Dec 2018 16:04:12 +0800 Subject: [PATCH] complete relation where --- CHANGELOG.md | 7 ++++++- README.md | 16 +++++++++++++--- builder_test.go | 4 ++-- relation_test.go | 24 ++++++++++++++++++++++-- wrapper_test.go | 4 ++-- 5 files changed, 45 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 23abbd6..5a3fce1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index af2afd4..06ebbed 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 ``` @@ -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 ``` @@ -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. diff --git a/builder_test.go b/builder_test.go index 8d2a716..44b6c54 100644 --- a/builder_test.go +++ b/builder_test.go @@ -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() @@ -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() diff --git a/relation_test.go b/relation_test.go index d15755a..e69e343 100644 --- a/relation_test.go +++ b/relation_test.go @@ -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,""," ") @@ -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) diff --git a/wrapper_test.go b/wrapper_test.go index 7ba4932..fda986f 100644 --- a/wrapper_test.go +++ b/wrapper_test.go @@ -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") @@ -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")