Skip to content

Commit

Permalink
fix(database/gdb): incompatitable statement in OrderBy("id", "dasc")
Browse files Browse the repository at this point in the history
  • Loading branch information
gqcn committed Nov 20, 2024
1 parent 9085188 commit d04717b
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 10 deletions.
32 changes: 32 additions & 0 deletions contrib/drivers/mysql/mysql_z_unit_issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
2 changes: 1 addition & 1 deletion contrib/drivers/mysql/mysql_z_unit_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
37 changes: 28 additions & 9 deletions database/gdb/gdb_model_order_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

0 comments on commit d04717b

Please sign in to comment.