1
0
mirror of https://github.com/gogf/gf.git synced 2025-04-05 03:05:05 +08:00

fix(database/gdb): incompatitable statement like Order("id", "dasc") (#3949)

This commit is contained in:
John Guo 2024-11-21 15:08:29 +08:00 committed by GitHub
parent 9c8b21af7b
commit e9ce1bde87
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 63 additions and 12 deletions

View File

@ -1345,3 +1345,35 @@ func Test_Issue3626(t *testing.T) {
t.Assert(count, 1)
})
}
// https://github.com/gogf/gf/issues/3932
func Test_Issue3932(t *testing.T) {
table := createInitTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
one, err := db.Model(table).Order("id", "desc").One()
t.AssertNil(err)
t.Assert(one["id"], 10)
})
gtest.C(t, func(t *gtest.T) {
one, err := db.Model(table).Order("id desc").One()
t.AssertNil(err)
t.Assert(one["id"], 10)
})
gtest.C(t, func(t *gtest.T) {
one, err := db.Model(table).Order("id desc, nickname asc").One()
t.AssertNil(err)
t.Assert(one["id"], 10)
})
gtest.C(t, func(t *gtest.T) {
one, err := db.Model(table).Order("id desc", "nickname asc").One()
t.AssertNil(err)
t.Assert(one["id"], 10)
})
gtest.C(t, func(t *gtest.T) {
one, err := db.Model(table).Order("id desc").Order("nickname asc").One()
t.AssertNil(err)
t.Assert(one["id"], 10)
})
}

View File

@ -1293,7 +1293,7 @@ func Test_Model_OrderBy(t *testing.T) {
})
gtest.C(t, func(t *gtest.T) {
result, err := db.Model(table).Order("NULL").All()
result, err := db.Model(table).Order(gdb.Raw("NULL")).All()
t.AssertNil(err)
t.Assert(len(result), TableSize)
t.Assert(result[0]["nickname"].String(), "name_1")

View File

@ -1431,7 +1431,7 @@ func Test_Model_OrderBy(t *testing.T) {
})
gtest.C(t, func(t *gtest.T) {
result, err := db.Model(table).Order("NULL").All()
result, err := db.Model(table).Order(gdb.Raw("NULL")).All()
t.AssertNil(err)
t.Assert(len(result), TableSize)
t.Assert(result[0]["nickname"].String(), "name_1")

View File

@ -1390,7 +1390,7 @@ func Test_Model_OrderBy(t *testing.T) {
})
gtest.C(t, func(t *gtest.T) {
result, err := db.Model(table).Order("NULL").All()
result, err := db.Model(table).Order(gdb.Raw("NULL")).All()
t.AssertNil(err)
t.Assert(len(result), TableSize)
t.Assert(result[0]["nickname"].String(), "name_1")

View File

@ -15,27 +15,42 @@ import (
// Order sets the "ORDER BY" statement for the model.
//
// Eg:
// Example:
// Order("id desc")
// Order("id", "desc")
// Order("id desc,name asc")
// Order("id desc", "name asc")
// Order("id desc").Order("name asc")
// Order(gdb.Raw("field(id, 3,1,2)")).
func (m *Model) Order(orderBy ...interface{}) *Model {
if len(orderBy) == 0 {
return m
}
model := m.getModel()
if model.orderBy != "" {
model.orderBy += ","
}
var (
core = m.db.GetCore()
model = m.getModel()
)
for _, v := range orderBy {
if model.orderBy != "" {
model.orderBy += ","
}
switch v.(type) {
case Raw, *Raw:
model.orderBy += gconv.String(v)
return model
default:
orderByStr := gconv.String(v)
if gstr.Contains(orderByStr, " ") {
model.orderBy += core.QuoteString(orderByStr)
} else {
if gstr.Equal(orderByStr, "ASC") || gstr.Equal(orderByStr, "DESC") {
model.orderBy = gstr.TrimRight(model.orderBy, ",")
model.orderBy += " " + orderByStr
} else {
model.orderBy += core.QuoteWord(orderByStr)
}
}
}
}
model.orderBy += model.db.GetCore().QuoteString(gstr.JoinAny(orderBy, ", "))
return model
}
@ -67,10 +82,14 @@ func (m *Model) Group(groupBy ...string) *Model {
if len(groupBy) == 0 {
return m
}
model := m.getModel()
var (
core = m.db.GetCore()
model = m.getModel()
)
if model.groupBy != "" {
model.groupBy += ","
}
model.groupBy += model.db.GetCore().QuoteString(strings.Join(groupBy, ","))
model.groupBy += core.QuoteString(strings.Join(groupBy, ","))
return model
}