forked from gobuffalo/pop
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add many_to_many tag association and allow model to use TableName to …
…get table name used in query
- Loading branch information
1 parent
bdc7ae7
commit 3dce10b
Showing
8 changed files
with
155 additions
and
2 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
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,75 @@ | ||
package associations | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"strings" | ||
|
||
"github.com/markbates/inflect" | ||
) | ||
|
||
type manyToManyAssociation struct { | ||
fieldType reflect.Type | ||
fieldValue reflect.Value | ||
model reflect.Value | ||
manyToManyTableName string | ||
fkID string | ||
} | ||
|
||
func init() { | ||
associationBuilders["many_to_many"] = func(p associationParams) (Association, error) { | ||
return &manyToManyAssociation{ | ||
fieldType: p.modelValue.FieldByName(p.field.Name).Type(), | ||
fieldValue: p.modelValue.FieldByName(p.field.Name), | ||
model: p.modelValue, | ||
manyToManyTableName: p.popTags.Find("many_to_many").Value, | ||
fkID: p.popTags.Find("fk_id").Value, | ||
}, nil | ||
} | ||
} | ||
|
||
func (m *manyToManyAssociation) TableName() string { | ||
method := m.fieldValue.MethodByName("TableName") | ||
if method.IsValid() { | ||
out := method.Call([]reflect.Value{}) | ||
return out[0].String() | ||
} | ||
return inflect.Tableize(m.fieldType.Name()) | ||
} | ||
|
||
func (m *manyToManyAssociation) Type() reflect.Kind { | ||
return m.fieldType.Kind() | ||
} | ||
|
||
func (m *manyToManyAssociation) Interface() interface{} { | ||
if m.fieldValue.Kind() == reflect.Ptr { | ||
val := reflect.New(m.fieldType.Elem()) | ||
m.fieldValue.Set(val) | ||
return m.fieldValue.Interface() | ||
} | ||
return m.fieldValue.Addr().Interface() | ||
} | ||
|
||
// SQLConstraint returns the content for a where clause, and the args | ||
// needed to execute it. | ||
func (m *manyToManyAssociation) SQLConstraint() (string, []interface{}) { | ||
modelColumnID := fmt.Sprintf("%s%s", strings.ToLower(m.model.Type().Name()), "_id") | ||
|
||
var columnFieldID string | ||
i := reflect.Indirect(m.fieldValue) | ||
if i.Kind() == reflect.Slice { | ||
t := i.Type().Elem() | ||
columnFieldID = fmt.Sprintf("%s%s", strings.ToLower(t.Name()), "_id") | ||
} else { | ||
columnFieldID = fmt.Sprintf("%s%s", strings.ToLower(i.Type().Name()), "_id") | ||
} | ||
|
||
if m.fkID != "" { | ||
columnFieldID = m.fkID | ||
} | ||
|
||
subQuery := fmt.Sprintf("select %s from %s where %s = ?", columnFieldID, m.manyToManyTableName, modelColumnID) | ||
modelIDValue := m.model.FieldByName("ID").Interface() | ||
|
||
return fmt.Sprintf("id in (%s)", subQuery), []interface{}{modelIDValue} | ||
} |
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