Skip to content

Commit

Permalink
fix Conds bug on Find and add test for FindAndCount (go-xorm#652)
Browse files Browse the repository at this point in the history
  • Loading branch information
lunny authored Jul 24, 2017
1 parent 1aa1846 commit 8cfde0e
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 10 deletions.
2 changes: 1 addition & 1 deletion session_cond.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (session *Session) NotIn(column string, args ...interface{}) *Session {
return session
}

// Conds returns session query conditions
// Conds returns session query conditions except auto bean conditions
func (session *Session) Conds() builder.Cond {
return session.Statement.cond
}
32 changes: 32 additions & 0 deletions session_cond_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,35 @@ func TestIn(t *testing.T) {
panic(err)
}
}

func TestFindAndCount(t *testing.T) {
assert.NoError(t, prepareEngine())

type FindAndCount struct {
Id int64
Name string
}

assert.NoError(t, testEngine.Sync2(new(FindAndCount)))

_, err := testEngine.Insert([]FindAndCount{
{
Name: "test1",
},
{
Name: "test2",
},
})
assert.NoError(t, err)

var results []FindAndCount
sess := testEngine.Where("name = ?", "test1")
conds := sess.Conds()
err = sess.Find(&results)
assert.NoError(t, err)
assert.EqualValues(t, 1, len(results))

total, err := testEngine.Where(conds).Count(new(FindAndCount))
assert.NoError(t, err)
assert.EqualValues(t, 1, total)
}
3 changes: 2 additions & 1 deletion session_find.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ func (session *Session) Find(rowsSlicePtr interface{}, condiBean ...interface{})
}
}

condSQL, condArgs, err := builder.ToSQL(session.Statement.cond.And(autoCond))
session.Statement.cond = session.Statement.cond.And(autoCond)
condSQL, condArgs, err := builder.ToSQL(session.Statement.cond)
if err != nil {
return err
}
Expand Down
21 changes: 13 additions & 8 deletions statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -890,17 +890,24 @@ func (statement *Statement) buildConds(table *core.Table, bean interface{}, incl
statement.unscoped, statement.mustColumnMap, statement.TableName(), statement.TableAlias, addedTableName)
}

func (statement *Statement) genConds(bean interface{}) (string, []interface{}, error) {
func (statement *Statement) mergeConds(bean interface{}) error {
if !statement.noAutoCondition {
var addedTableName = (len(statement.JoinStr) > 0)
autoCond, err := statement.buildConds(statement.RefTable, bean, true, true, false, true, addedTableName)
if err != nil {
return "", nil, err
return err
}
statement.cond = statement.cond.And(autoCond)
}

if err := statement.processIDParam(); err != nil {
return err
}
return nil
}

func (statement *Statement) genConds(bean interface{}) (string, []interface{}, error) {
if err := statement.mergeConds(bean); err != nil {
return "", nil, err
}

Expand Down Expand Up @@ -940,14 +947,12 @@ func (statement *Statement) genGetSQL(bean interface{}) (string, []interface{},
columnStr = "*"
}

var condSQL string
var condArgs []interface{}
var err error
if isStruct {
condSQL, condArgs, err = statement.genConds(bean)
} else {
condSQL, condArgs, err = builder.ToSQL(statement.cond)
if err := statement.mergeConds(bean); err != nil {
return "", nil, err
}
}
condSQL, condArgs, err := builder.ToSQL(statement.cond)
if err != nil {
return "", nil, err
}
Expand Down

0 comments on commit 8cfde0e

Please sign in to comment.