Skip to content

Commit

Permalink
updates, fix belongsTo
Browse files Browse the repository at this point in the history
  • Loading branch information
amirrezaask committed Jan 31, 2022
1 parent 936bc9a commit 3027fd7
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 34 deletions.
24 changes: 13 additions & 11 deletions examples/blog/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,31 @@ import (
"fmt"
"github.com/golobby/orm"
_ "github.com/mattn/go-sqlite3"
"os"
)

type Post struct {
ID int64
Comments []*Comment
Content string
ID int64
Comments []*Comment
Content string
Categories []*Category
}

type Comment struct {
ID int64
PostID int64
Post Post
ID int64
PostID int64
Post Post
Content string
}

type Category struct {
ID int64
ID int64
Title string
Posts []*Post
}

func main() {
_ = os.Remove("blog.db")
dbGolobby, err := sql.Open("sqlite3", "blog.db")
if err != nil {
panic(err)
Expand All @@ -52,7 +54,7 @@ func main() {
}
err = postRepository.Entity(firstPost).Save()
if err != nil {
panic(err)
panic(err)
}
fmt.Println("Post primary key is ", firstPost.ID)
firstComment := &Comment{
Expand All @@ -71,13 +73,13 @@ func main() {
var newPost Post
err = commentRepository.Entity(firstComment).BelongsTo(&newPost)
if err != nil {
panic(err)
panic(err)
}
fmt.Printf("loaded comment %d post %+v", firstComment.PostID, firstPost)

//err = postRepository.Entity(firstPost).ManyToMany(&firstPost.Categories)
//err = postRepository.Entity(firstPost).ManyToMany(&firstPost.Categories, orm.ManyToManyConfigurators.)
//if err != nil {
// panic(err)
//}
//fmt.Printf("loaded post %d categories %+v", firstPost.ID, firstPost.Categories)
}
}
65 changes: 42 additions & 23 deletions relations.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/gertd/go-pluralize"
"reflect"
)

type HasManyConfig struct {
PropertyTable string
PropertyForeignKey string
Expand All @@ -15,6 +16,7 @@ type _HasManyDefaultConfigurators struct {
PropertyTable func(name string) HasManyConfigurator
PropertyForeignKey func(name string) HasManyConfigurator
}

var HasManyConfigurators = &_HasManyDefaultConfigurators{
PropertyTable: func(name string) HasManyConfigurator {
return func(config *HasManyConfig) {
Expand All @@ -27,7 +29,8 @@ var HasManyConfigurators = &_HasManyDefaultConfigurators{
}
},
}
func (e *entity) HasMany(out interface{}, configs...HasManyConfigurator) error {

func (e *entity) HasMany(out interface{}, configs ...HasManyConfigurator) error {
c := &HasManyConfig{}
for _, config := range configs {
config(c)
Expand All @@ -37,7 +40,7 @@ func (e *entity) HasMany(out interface{}, configs...HasManyConfigurator) error {
c.PropertyTable = tableName(out)
}
if c.PropertyForeignKey == "" {
c.PropertyForeignKey = pluralize.NewClient().Singular(e.repo.metadata.Table)+"_id"
c.PropertyForeignKey = pluralize.NewClient().Singular(e.repo.metadata.Table) + "_id"
}
t := reflect.TypeOf(out)
v := reflect.ValueOf(out)
Expand Down Expand Up @@ -68,6 +71,7 @@ func (e *entity) HasMany(out interface{}, configs...HasManyConfigurator) error {
}
return repo.BindContext(context.Background(), out, q, args...)
}

type HasOneConfig struct {
PropertyTable string
PropertyForeignKey string
Expand All @@ -77,6 +81,7 @@ type _HasOneDefaultConfigurators struct {
PropertyTable func(name string) HasOneConfigurator
PropertyForeignKey func(name string) HasOneConfigurator
}

var HasOneConfigurators = &_HasOneDefaultConfigurators{
PropertyTable: func(name string) HasOneConfigurator {
return func(config *HasOneConfig) {
Expand All @@ -100,7 +105,7 @@ func (e *entity) HasOne(out interface{}, configs ...HasOneConfigurator) error {
c.PropertyTable = tableName(out)
}
if c.PropertyForeignKey == "" {
c.PropertyForeignKey = pluralize.NewClient().Singular(e.repo.metadata.Table)+"_id"
c.PropertyForeignKey = pluralize.NewClient().Singular(e.repo.metadata.Table) + "_id"
}
t := reflect.TypeOf(out)
v := reflect.ValueOf(out)
Expand Down Expand Up @@ -131,15 +136,19 @@ func (e *entity) HasOne(out interface{}, configs ...HasOneConfigurator) error {
}
return repo.BindContext(context.Background(), out, q, args...)
}

type BelongsToConfig struct {
OwnerTable string
LocalForeignKey string
OwnerTable string
LocalForeignKey string
ForeignColumnName string
}
type BelongsToConfigurator func(config *BelongsToConfig)
type _BelongsToConfigurator struct {
OwnerTable func(name string) BelongsToConfigurator
LocalKey func(name string) BelongsToConfigurator
OwnerTable func(name string) BelongsToConfigurator
LocalKey func(name string) BelongsToConfigurator
ForeignColumnName func(name string) BelongsToConfigurator
}

var BelongsToConfigurators = &_BelongsToConfigurator{
OwnerTable: func(name string) BelongsToConfigurator {
return func(config *BelongsToConfig) {
Expand All @@ -151,7 +160,13 @@ var BelongsToConfigurators = &_BelongsToConfigurator{
config.LocalForeignKey = name
}
},
ForeignColumnName: func(name string) BelongsToConfigurator {
return func(config *BelongsToConfig) {
config.ForeignColumnName = name
}
},
}

func (e *entity) BelongsTo(out interface{}, configs ...BelongsToConfigurator) error {
c := &BelongsToConfig{}
for _, config := range configs {
Expand All @@ -160,8 +175,11 @@ func (e *entity) BelongsTo(out interface{}, configs ...BelongsToConfigurator) er
if c.OwnerTable == "" {
c.OwnerTable = tableName(out)
}
if c.LocalForeignKey == ""{
c.LocalForeignKey = pluralize.NewClient().Singular(c.OwnerTable)+"_id"
if c.LocalForeignKey == "" {
c.LocalForeignKey = pluralize.NewClient().Singular(c.OwnerTable) + "_id"
}
if c.ForeignColumnName == "" {
c.ForeignColumnName = "id"
}
t := reflect.TypeOf(out)
v := reflect.ValueOf(out)
Expand All @@ -171,7 +189,7 @@ func (e *entity) BelongsTo(out interface{}, configs ...BelongsToConfigurator) er
}
ph := e.repo.dialect.PlaceholderChar
if e.repo.dialect.IncludeIndexInPlaceholder {
ph = ph+"1"
ph = ph + "1"
}
target := reflect.New(t).Interface()
repo := NewRepository(e.repo.conn, e.repo.dialect, target)
Expand All @@ -183,30 +201,30 @@ func (e *entity) BelongsTo(out interface{}, configs ...BelongsToConfigurator) er
}
}

ownerID:=e.repo.valuesOf(e.obj, true)[ownerIDidx]
ownerID := e.repo.valuesOf(e.obj, true)[ownerIDidx]

q, args := newSelect().
From(c.OwnerTable).
Where(WhereHelpers.Equal(c.LocalForeignKey, ph)).
Where(WhereHelpers.Equal(c.ForeignColumnName, ph)).
WithArgs(ownerID).Build()

return repo.BindContext(context.Background(), out, q, args...)
}

type ManyToManyConfig struct {
IntermediateTable string
IntermediateLocalColumn string
IntermediateTable string
IntermediateLocalColumn string
IntermediateForeignColumn string
ForeignTable string
ForeignLookupColumn string

ForeignTable string
ForeignLookupColumn string
}
type ManyToManyConfigurator func(config *ManyToManyConfig)
type _ManyToManyConfigurators struct {
IntermediateTable func(name string) ManyToManyConfigurator
IntermediateLocalColumn func(name string) ManyToManyConfigurator
IntermediateForeignColumn func(name string) ManyToManyConfigurator

IntermediateTable func(name string) ManyToManyConfigurator
IntermediateLocalColumn func(name string) ManyToManyConfigurator
IntermediateForeignColumn func(name string) ManyToManyConfigurator
}

var ManyToManyConfigurators = &_ManyToManyConfigurators{
IntermediateTable: func(name string) ManyToManyConfigurator {
return func(config *ManyToManyConfig) {
Expand All @@ -224,7 +242,8 @@ var ManyToManyConfigurators = &_ManyToManyConfigurators{
}
},
}
func (e *entity) ManyToMany(out interface{}, configs ...ManyToManyConfigurator ) error {

func (e *entity) ManyToMany(out interface{}, configs ...ManyToManyConfigurator) error {
c := &ManyToManyConfig{}
for _, config := range configs {
config(c)
Expand All @@ -251,4 +270,4 @@ func (e *entity) ManyToMany(out interface{}, configs ...ManyToManyConfigurator )
return NewRepository(e.repo.conn, e.repo.dialect, out).
BindContext(context.Background(), out, q, args...)

}
}

0 comments on commit 3027fd7

Please sign in to comment.