Skip to content

Commit

Permalink
Fix the bug that ScanAndCount and AllAndCount report errors in sqlser…
Browse files Browse the repository at this point in the history
…ver (#3155)
  • Loading branch information
wlqe committed Nov 16, 2023
1 parent 9694a68 commit 85acd3d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
37 changes: 37 additions & 0 deletions contrib/drivers/mssql/mssql_z_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2548,3 +2548,40 @@ func Test_Model_WherePrefixLike(t *testing.T) {
t.Assert(r[0]["ID"], "3")
})
}

func Test_Model_AllAndCount(t *testing.T) {
table := createInitTable()
defer dropTable(table)

gtest.C(t, func(t *gtest.T) {
result, total, err := db.Model(table).Order("id").Limit(0, 3).AllAndCount(false)
t.Assert(err, nil)

t.Assert(len(result), 3)
t.Assert(total, TableSize)
})
}

func Test_Model_ScanAndCount(t *testing.T) {
table := createInitTable()
defer dropTable(table)

gtest.C(t, func(t *gtest.T) {
type User struct {
Id int
Passport string
Password string
NickName string
CreateTime gtime.Time
}

users := make([]User, 0)
total := 0

err := db.Model(table).Order("id").Limit(0, 3).ScanAndCount(&users, &total, false)
t.Assert(err, nil)

t.Assert(len(users), 3)
t.Assert(total, TableSize)
})
}
6 changes: 4 additions & 2 deletions database/gdb/gdb_model_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -768,8 +768,10 @@ func (m *Model) formatCondition(
}
}
// ORDER BY.
if m.orderBy != "" {
conditionExtra += " ORDER BY " + m.orderBy
if !isCountStatement { // The count statement of sqlserver cannot contain the order by statement
if m.orderBy != "" {
conditionExtra += " ORDER BY " + m.orderBy
}
}
// LIMIT.
if !isCountStatement {
Expand Down

0 comments on commit 85acd3d

Please sign in to comment.