Merge pull request #1499 from zxr615/fix_orderby_null

order by null is not escaped
This commit is contained in:
John Guo
2021-11-26 20:37:27 +08:00
committed by GitHub
2 changed files with 13 additions and 1 deletions

View File

@ -276,6 +276,7 @@ func doQuoteWord(s, charLeft, charRight string) string {
// doQuoteString quotes string with quote chars.
// For example, if quote char is '`':
// "null" => "NULL"
// "user" => "`user`"
// "user u" => "`user` u"
// "user,user_detail" => "`user`,`user_detail`"
@ -289,7 +290,11 @@ func doQuoteString(s, charLeft, charRight string) string {
array2 := gstr.SplitAndTrim(v1, " ")
array3 := gstr.Split(gstr.Trim(array2[0]), ".")
if len(array3) == 1 {
array3[0] = doQuoteWord(array3[0], charLeft, charRight)
if strings.EqualFold(array3[0], "NULL") {
array3[0] = doQuoteWord(array3[0], "", "")
} else {
array3[0] = doQuoteWord(array3[0], charLeft, charRight)
}
} else if len(array3) >= 2 {
array3[0] = doQuoteWord(array3[0], charLeft, charRight)
// Note:

View File

@ -1083,6 +1083,13 @@ func Test_Model_OrderBy(t *testing.T) {
t.Assert(len(result), TableSize)
t.Assert(result[0]["nickname"].String(), fmt.Sprintf("name_%d", TableSize))
})
gtest.C(t, func(t *gtest.T) {
result, err := db.Model(table).Order("NULL").All()
t.AssertNil(err)
t.Assert(len(result), TableSize)
t.Assert(result[0]["nickname"].String(), "name_1")
})
}
func Test_Model_GroupBy(t *testing.T) {