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(database/gdb): invalid order by statement generated when multiple order inputs #3803

Merged
merged 4 commits into from
Sep 28, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading