Skip to content

Commit

Permalink
Added support for BelongsToThrough
Browse files Browse the repository at this point in the history
  • Loading branch information
markbates committed Oct 15, 2015
1 parent 12ab9da commit 5854df9
Show file tree
Hide file tree
Showing 26 changed files with 471 additions and 363 deletions.
23 changes: 14 additions & 9 deletions belongs_to.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
package pop

import (
"fmt"

"github.com/markbates/inflect"
)
import "fmt"

func (c *Connection) BelongsTo(model interface{}) *Query {
return Q(c).BelongsTo(model)
}

func (q *Query) BelongsTo(model interface{}) *Query {
m := &Model{Value: model}
tn := m.TableName()
tn = inflect.Singularize(tn)
args := []interface{}{m.ID()}
q.WhereClauses = append(q.WhereClauses, Clause{fmt.Sprintf("%s_id = ?", tn), args})
q.Where(fmt.Sprintf("%s = ?", m.AssociationName()), m.ID())
return q
}

func (c *Connection) BelongsToThrough(bt, thru interface{}) *Query {
return Q(c).BelongsToThrough(bt, thru)
}

func (q *Query) BelongsToThrough(bt, thru interface{}) *Query {
q.BelongsToThroughClauses = append(q.BelongsToThroughClauses, BelongsToThroughClause{
BelongsTo: &Model{Value: bt},
Through: &Model{Value: thru},
})
return q
}
20 changes: 20 additions & 0 deletions belongs_to_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package pop_test

import (
"fmt"
"os"
"strings"
"testing"

"github.com/markbates/pop"
Expand All @@ -21,3 +24,20 @@ func Test_BelongsTo(t *testing.T) {
r.Equal(cl.Arguments, []interface{}{1})
})
}

func Test_BelongsToThrough(t *testing.T) {
transaction(func(tx *pop.Connection) {
r := require.New(t)

q := tx.BelongsToThrough(&User{ID: 1}, &Friend{})
qs := "SELECT enemies.A FROM enemies AS enemies, good_friends AS good_friends WHERE good_friends.user_id = ? AND enemies.id = good_friends.enemy_id"
if os.Getenv("SODA_DIALECT") == "postgres" {
qs = strings.Replace(qs, "?", "$1", -1)
}

sql, args := q.ToSQL(&pop.Model{Value: &Enemy{}})
r.Equal(qs, sql)
fmt.Printf("args: %s\n", args)
r.Equal([]interface{}{1}, args)
})
}
7 changes: 7 additions & 0 deletions clause.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,10 @@ func (c FromClauses) String() string {
}
return strings.Join(cs, ", ")
}

type BelongsToThroughClause struct {
BelongsTo *Model
Through *Model
}

type BelongsToThroughClauses []BelongsToThroughClause
213 changes: 0 additions & 213 deletions columns.go

This file was deleted.

20 changes: 20 additions & 0 deletions columns/column.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package columns

import "fmt"

type Column struct {
Name string
Writeable bool
Readable bool
SelectSQL string
}

func (c Column) UpdateString() string {
return fmt.Sprintf("%s = :%s", c.Name, c.Name)
}

func (c *Column) SetSelectSQL(s string) {
c.SelectSQL = s
c.Writeable = false
c.Readable = true
}
14 changes: 14 additions & 0 deletions columns/column_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package columns_test

import (
"testing"

"github.com/markbates/pop/columns"
"github.com/stretchr/testify/require"
)

func Test_Column_UpdateString(t *testing.T) {
r := require.New(t)
c := columns.Column{Name: "foo"}
r.Equal(c.UpdateString(), "foo = :foo")
}
Loading

0 comments on commit 5854df9

Please sign in to comment.