From 2bffb43138795ce9f23ffc0115900de2e70342d6 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Tue, 29 Jul 2014 17:28:10 +0800 Subject: [PATCH] Write stub code for Many2Many --- main.go | 8 ++++++-- many2many.go | 23 +++++++++++++++++++++++ migration_test.go | 4 ++++ relations_test.go | 21 ++++++++++++++++++--- scope_private.go | 6 +++--- structs_test.go | 6 ++++++ 6 files changed, 60 insertions(+), 8 deletions(-) create mode 100644 many2many.go diff --git a/main.go b/main.go index 147ea3c2..bf3e65f8 100644 --- a/main.go +++ b/main.go @@ -338,7 +338,11 @@ func (s *DB) AddUniqueIndex(indexName string, column ...string) *DB { return s } -func (s *DB) RemoveIndex(column string) *DB { - s.clone().NewScope(s.Value).removeIndex(column) +func (s *DB) RemoveIndex(indexName string) *DB { + s.clone().NewScope(s.Value).removeIndex(indexName) + return s +} + +func (s *DB) Many2Many(column string) *DB { return s } diff --git a/many2many.go b/many2many.go new file mode 100644 index 00000000..a10121fd --- /dev/null +++ b/many2many.go @@ -0,0 +1,23 @@ +package gorm + +type many2many struct { +} + +func (*many2many) Find(value interface{}) { +} + +func (*many2many) Append(values interface{}) { +} + +func (*many2many) Delete(value interface{}) { +} + +func (*many2many) Clear(value interface{}) { +} + +func (*many2many) Replace(values interface{}) { +} + +func (*many2many) Count(values interface{}) int { + return 0 +} diff --git a/migration_test.go b/migration_test.go index 08e04b27..68982a78 100644 --- a/migration_test.go +++ b/migration_test.go @@ -51,6 +51,10 @@ func runMigration() { if err := db.AutoMigrate(Role{}).Error; err != nil { panic(fmt.Sprintf("No error should happen when create table, but got %+v", err)) } + + if err := db.AutoMigrate(Language{}).Error; err != nil { + panic(fmt.Sprintf("No error should happen when create table, but got %+v", err)) + } } func TestIndexes(t *testing.T) { diff --git a/relations_test.go b/relations_test.go index 719aac9d..a62f11e3 100644 --- a/relations_test.go +++ b/relations_test.go @@ -127,10 +127,25 @@ func TestRelated(t *testing.T) { } func TestQueryManyToManyWithRelated(t *testing.T) { - // db.Model(&User{}).Related(&[]Language{}, "Languages") - // SELECT `languages`.* FROM `languages` INNER JOIN `user_languages` ON `languages`.`id` = `user_languages`.`language_id` WHERE `user_languages`.`user_id` = 111 - // db.Model(&User{}).Many2Many("Languages").Find(&[]Language{}) + var languages = []Language{{Name: "ZH"}, {Name: "EN"}, {Name: "DE"}} + user := User{Name: "Many2Many", Languages: languages} + db.Debug().Save(&user) + + var newLanguages []Language + db.Model(&user).Related(&newLanguages, "Languages") + if len(newLanguages) != 3 { + t.Errorf("Query many to many relations") + } + + newLanguages = []Language{} + db.Model(&user).Many2Many("Languages").Find(&newLanguages) + if len(newLanguages) != 3 { + t.Errorf("Query many to many relations") + } + // db.Model(&User{}).Many2Many("Languages").Add(&Language{}) // db.Model(&User{}).Many2Many("Languages").Remove(&Language{}) // db.Model(&User{}).Many2Many("Languages").Replace(&[]Language{}) + // db.Model(&User{}).Related(&[]Language{}, "Languages") + // SELECT `languages`.* FROM `languages` INNER JOIN `user_languages` ON `languages`.`id` = `user_languages`.`language_id` WHERE `user_languages`.`user_id` = 111 } diff --git a/scope_private.go b/scope_private.go index da851fb1..1cdcfafb 100644 --- a/scope_private.go +++ b/scope_private.go @@ -300,7 +300,7 @@ func (scope *Scope) updatedAttrsWithValues(values map[string]interface{}, ignore } func (scope *Scope) sqlTagForField(field *Field) (tag string) { - tag, addationalTag, size := parseSqlTag(field.Tag.Get(scope.db.parent.tagIdentifier)) + tag, additionalTag, size := parseSqlTag(field.Tag.Get(scope.db.parent.tagIdentifier)) if tag == "-" { field.IsIgnored = true @@ -330,8 +330,8 @@ func (scope *Scope) sqlTagForField(field *Field) (tag string) { } } - if len(addationalTag) > 0 { - tag = tag + " " + addationalTag + if len(additionalTag) > 0 { + tag = tag + " " + additionalTag } return } diff --git a/structs_test.go b/structs_test.go index 95fd9c18..bd515a6b 100644 --- a/structs_test.go +++ b/structs_test.go @@ -24,6 +24,7 @@ type User struct { ShippingAddressId int64 // Embedded struct's foreign key CreditCard CreditCard Latitude float64 + Languages []Language `gorm:"many2many:user_languages;"` CompanyId int64 Company Role @@ -60,6 +61,11 @@ type Address struct { DeletedAt time.Time } +type Language struct { + Id int + Name string +} + type Product struct { Id int64 Code string