Skip to content

Commit

Permalink
WIP: ManyToMant
Browse files Browse the repository at this point in the history
  • Loading branch information
amirrezaask committed Jan 31, 2022
1 parent c04bca0 commit 0a6ee50
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 17 deletions.
5 changes: 5 additions & 0 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import (
"fmt"
)

type keyValue struct {
Key string
Value interface{}
}

type PlaceholderGenerator func(n int) []string
type placeHolderGenerators struct {
Postgres PlaceholderGenerator
Expand Down
6 changes: 0 additions & 6 deletions kv.go

This file was deleted.

17 changes: 14 additions & 3 deletions relations.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,8 @@ var ManyToManyConfigurators = &_ManyToManyConfigurators{
},
}

var tableer = reflect.TypeOf((Table)(nil))

func (e *entity) ManyToMany(out interface{}, configs ...ManyToManyConfigurator) error {
c := &ManyToManyConfig{}
for _, config := range configs {
Expand All @@ -252,15 +254,24 @@ func (e *entity) ManyToMany(out interface{}, configs ...ManyToManyConfigurator)
return fmt.Errorf("no way to infer many to many intermediate table yet.")
}
if c.IntermediateLocalColumn == "" {

table := e.repo.metadata.Table
table = pluralize.NewClient().Singular(table)
c.IntermediateLocalColumn = table + "_id"
}
t := reflect.TypeOf(out)
v := reflect.New(t).Interface()

if t.Kind() == reflect.Ptr {
t = t.Elem()
}
if c.IntermediateForeignColumn == "" {

table := tableName(v)
c.IntermediateForeignColumn = pluralize.NewClient().Singular(table) + "_id"
}
if c.ForeignTable == "" {

c.IntermediateForeignColumn = tableName(v)
}

sub, _ := newSelect().From(c.IntermediateTable).Where(c.IntermediateLocalColumn, "=", fmt.Sprint(e.repo.getPkValue(e.obj))).Build()
q, args := newSelect().
From(c.ForeignTable).
Expand Down
14 changes: 6 additions & 8 deletions repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ import (
)

type Repository struct {
dialect *dialect
conn *sql.DB
metadata *objectMetadata
eagerLoad bool
dialect *dialect
conn *sql.DB
metadata *objectMetadata
}

func (r *Repository) Schema() *objectMetadata {
Expand All @@ -20,10 +19,9 @@ func (r *Repository) Schema() *objectMetadata {
func NewRepository(conn *sql.DB, dialect *dialect, makeRepositoryFor interface{}) *Repository {
md := objectMetadataFrom(makeRepositoryFor, dialect)
s := &Repository{
conn: conn,
metadata: md,
dialect: dialect,
eagerLoad: true,
conn: conn,
metadata: md,
dialect: dialect,
}
return s
}
Expand Down

0 comments on commit 0a6ee50

Please sign in to comment.