-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d1378b4
commit 7107956
Showing
7 changed files
with
186 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
**/.idea/* | ||
cover.out | ||
**db |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package orm | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
|
||
"github.com/golobby/orm/qb" | ||
) | ||
|
||
type Entity struct { | ||
repo *Repository | ||
obj interface{} | ||
} | ||
|
||
func (r *Repository) Entity(obj interface{}) *Entity { | ||
return &Entity{r, obj} | ||
} | ||
|
||
func (e *Entity) HasMany(out interface{}) error { | ||
t := reflect.TypeOf(out) | ||
v := reflect.ValueOf(out) | ||
if t.Kind() == reflect.Ptr { | ||
t = t.Elem() | ||
v = v.Elem() | ||
} | ||
if t.Kind() == reflect.Slice { | ||
t = t.Elem() | ||
} | ||
target := reflect.New(t).Interface() | ||
repo := NewRepository(e.repo.conn, e.repo.dialect, target) | ||
|
||
var q string | ||
var args []interface{} | ||
for _, field := range e.repo.metadata.Fields { | ||
if !field.IsRel { | ||
continue | ||
} | ||
if field.RelationMetadata.Table == repo.metadata.Table { | ||
q, args = qb.NewSelect(). | ||
From(field.RelationMetadata.Table). | ||
Select(field.RelationMetadata.objectMetadata.Columns(true)...). | ||
Where(qb.WhereHelpers.Equal(field.RelationMetadata.LeftColumn, field.RelationMetadata.RightColumn)). | ||
WithArgs(e.repo.getPkValue(e.obj)). | ||
Build() | ||
} | ||
} | ||
if q == "" { | ||
return fmt.Errorf("cannot build the query") | ||
} | ||
return repo.Bind(out, q, args...) | ||
} | ||
func (e *Entity) HasOne(out interface{}) error { | ||
t := reflect.TypeOf(out) | ||
v := reflect.ValueOf(out) | ||
if t.Kind() == reflect.Ptr { | ||
t = t.Elem() | ||
v = v.Elem() | ||
} | ||
target := reflect.New(t).Interface() | ||
repo := NewRepository(e.repo.conn, e.repo.dialect, target) | ||
|
||
var q string | ||
var args []interface{} | ||
for _, field := range e.repo.metadata.Fields { | ||
if !field.IsRel { | ||
continue | ||
} | ||
if field.RelationMetadata.Table == repo.metadata.Table { | ||
q, args = qb.NewSelect(). | ||
From(field.RelationMetadata.Table). | ||
Select(field.RelationMetadata.objectMetadata.Columns(true)...). | ||
Where(qb.WhereHelpers.Equal(field.RelationMetadata.LeftColumn, field.RelationMetadata.RightColumn)). | ||
WithArgs(e.repo.getPkValue(e.obj)). | ||
Build() | ||
} | ||
} | ||
if q == "" { | ||
return fmt.Errorf("cannot build the query") | ||
} | ||
return repo.Bind(out, q, args...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.