Fix the bug that ScanAndCount and AllAndCount report errors in sqlserver (#3155)

This commit is contained in:
wlqe 2023-11-16 20:15:48 +08:00 committed by GitHub
parent 9694a68211
commit 85acd3d31d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 2 deletions

View File

@ -2548,3 +2548,40 @@ func Test_Model_WherePrefixLike(t *testing.T) {
t.Assert(r[0]["ID"], "3") 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)
})
}

View File

@ -768,8 +768,10 @@ func (m *Model) formatCondition(
} }
} }
// ORDER BY. // ORDER BY.
if m.orderBy != "" { if !isCountStatement { // The count statement of sqlserver cannot contain the order by statement
conditionExtra += " ORDER BY " + m.orderBy if m.orderBy != "" {
conditionExtra += " ORDER BY " + m.orderBy
}
} }
// LIMIT. // LIMIT.
if !isCountStatement { if !isCountStatement {