Skip to content

Commit

Permalink
fix(database/gdb): invalid order by statement generated when multiple…
Browse files Browse the repository at this point in the history
… order inputs (#3803)
  • Loading branch information
LanceAdd authored Sep 28, 2024
1 parent f45f648 commit 66ee52c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
20 changes: 20 additions & 0 deletions contrib/drivers/mysql/mysql_z_unit_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"database/sql"
"fmt"
"os"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -4818,3 +4819,22 @@ func Test_Model_Year_Date_Time_DateTime_Timestamp(t *testing.T) {
t.AssertLT(one["timestamp"].GTime().Sub(now).Seconds(), 5)
})
}

func Test_OrderBy_Statement_Generated(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
array := gstr.SplitAndTrim(gtest.DataContent(`fix_gdb_order_by.sql`), ";")
for _, v := range array {
if _, err := db.Exec(ctx, v); err != nil {
gtest.Error(err)
}
}
defer dropTable(`employee`)
sqlArray, _ := gdb.CatchSQL(ctx, func(ctx context.Context) error {
g.DB("default").Ctx(ctx).Model("employee").Order("name asc", "age desc").All()
return nil
})
rawSql := strings.ReplaceAll(sqlArray[len(sqlArray)-1], " ", "")
expectSql := strings.ReplaceAll("SELECT * FROM `employee` ORDER BY `name` asc, `age` desc", " ", "")
t.Assert(rawSql, expectSql)
})
}
9 changes: 9 additions & 0 deletions contrib/drivers/mysql/testdata/fix_gdb_order_by.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CREATE TABLE IF NOT EXISTS `employee`
(
id BIGINT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
age INT NOT NULL
);

INSERT INTO employee(name, age) VALUES ('John', 30);
INSERT INTO employee(name, age) VALUES ('Mary', 28);
2 changes: 1 addition & 1 deletion database/gdb/gdb_model_order_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (m *Model) Order(orderBy ...interface{}) *Model {
return model
}
}
model.orderBy += model.db.GetCore().QuoteString(gstr.JoinAny(orderBy, " "))
model.orderBy += model.db.GetCore().QuoteString(gstr.JoinAny(orderBy, ", "))
return model
}

Expand Down

0 comments on commit 66ee52c

Please sign in to comment.