Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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