Skip to content

Commit 3ed3423

Browse files
committed
fix(pgsql): fix 5 bugs found during code review
1. Test_PgSQL_JSONB_Existence: replace `data ? 'email'` with `jsonb_exists(data, 'email')` — bare `?` gets mangled by DoFilter into a $N placeholder, breaking the query. 2. Test_Issue2787: fix SQL condition assertions from MySQL backtick quoting (`id`) to PgSQL double-quote quoting ("id"). 3. Test_Issue3671: fix copy-paste table name prefix from "issue3632" to "issue3671". 4. issue1412.sql: add DROP TABLE IF EXISTS guards for "parcels" and "items" to prevent CREATE TABLE failures when tests share a process. 5. Test_Issue2643: replace unjustified Skip with actual PgSQL test — lpad, concat_ws, replace, CASE WHEN are all standard PgSQL functions. Adapted quoting from backticks to double-quotes and added `::text` cast for lpad (PgSQL requires text input). ref gogf#4689
1 parent 6a7d7c3 commit 3ed3423

4 files changed

Lines changed: 47 additions & 7 deletions

File tree

contrib/drivers/pgsql/pgsql_z_unit_feature_pgsql_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,10 @@ func Test_PgSQL_JSONB_Existence(t *testing.T) {
113113
_, err = db.Model(table).Data(g.Map{"data": `{"name":"bob"}`}).Insert()
114114
t.AssertNil(err)
115115

116-
// ? key existence operator
116+
// jsonb_exists is the function form of the ? key-existence operator.
117+
// We use it here because GoFrame's DoFilter replaces bare ? with $N placeholders.
117118
result, err := db.GetValue(ctx, fmt.Sprintf(
118-
`SELECT COUNT(*) FROM %s WHERE data ? 'email'`, table,
119+
`SELECT COUNT(*) FROM %s WHERE jsonb_exists(data, 'email')`, table,
119120
))
120121
t.AssertNil(err)
121122
t.Assert(result.Int(), 1)

contrib/drivers/pgsql/pgsql_z_unit_issue_test.go

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func Test_Issue3671(t *testing.T) {
126126
}
127127
var (
128128
sqlText = gtest.DataContent("issues", "issue3671.sql")
129-
table = fmt.Sprintf(`%s_%d`, TablePrefix+"issue3632", gtime.TimestampNano())
129+
table = fmt.Sprintf(`%s_%d`, TablePrefix+"issue3671", gtime.TimestampNano())
130130
)
131131
if _, err := db.Exec(ctx, fmt.Sprintf(sqlText, table)); err != nil {
132132
gtest.Fatal(err)
@@ -1154,7 +1154,7 @@ func Test_Issue2787(t *testing.T) {
11541154
WhereOr("password", "abc123")).
11551155
Where("passport", "pp").
11561156
Build()
1157-
t.Assert(condWhere, "(`id`=?) AND (((`nickname`=?) OR (`password`=?))) AND (`passport`=?)")
1157+
t.Assert(condWhere, `("id"=?) AND ((("nickname"=?) OR ("password"=?))) AND ("passport"=?)`)
11581158

11591159
condWhere, _ = m.OmitEmpty().Builder().
11601160
Where("id", "").
@@ -1163,7 +1163,7 @@ func Test_Issue2787(t *testing.T) {
11631163
WhereOr("password", "abc123")).
11641164
Where("passport", "pp").
11651165
Build()
1166-
t.Assert(condWhere, "((`nickname`=?) OR (`password`=?)) AND (`passport`=?)")
1166+
t.Assert(condWhere, `(("nickname"=?) OR ("password"=?)) AND ("passport"=?)`)
11671167

11681168
condWhere, _ = m.OmitEmpty().Builder().
11691169
Where(m.Builder().
@@ -1172,7 +1172,7 @@ func Test_Issue2787(t *testing.T) {
11721172
Where("id", "").
11731173
Where("passport", "pp").
11741174
Build()
1175-
t.Assert(condWhere, "((`nickname`=?) OR (`password`=?)) AND (`passport`=?)")
1175+
t.Assert(condWhere, `(("nickname"=?) OR ("password"=?)) AND ("passport"=?)`)
11761176
})
11771177
}
11781178

@@ -1473,7 +1473,36 @@ func Test_Issue2552_ClearTableFields(t *testing.T) {
14731473

14741474
// https://github.com/gogf/gf/issues/2643
14751475
func Test_Issue2643(t *testing.T) {
1476-
t.Skip("MySQL-specific SQL functions (lpad, concat_ws, CASE WHEN quoting) differ in PostgreSQL")
1476+
table := "issue2643"
1477+
array := gstr.SplitAndTrim(gtest.DataContent("issues", "issue2643.sql"), ";")
1478+
for _, v := range array {
1479+
if _, err := db.Exec(ctx, v); err != nil {
1480+
gtest.Error(err)
1481+
}
1482+
}
1483+
defer dropTable(table)
1484+
1485+
gtest.C(t, func(t *gtest.T) {
1486+
var (
1487+
expectKey1 = `SELECT s.name,replace(concat_ws(',',lpad(s.id::text, 6, '0'),s.name),',','') "code" FROM "issue2643" AS s`
1488+
expectKey2 = `SELECT CASE WHEN dept='物资部' THEN '物资部' ELSE '其他' END dept,sum(s.value) FROM "issue2643" AS s GROUP BY CASE WHEN dept='物资部' THEN '物资部' ELSE '其他' END`
1489+
)
1490+
sqlArray, err := gdb.CatchSQL(ctx, func(ctx context.Context) error {
1491+
db.Ctx(ctx).Model(table).As("s").Fields(
1492+
"s.name",
1493+
`replace(concat_ws(',',lpad(s.id::text, 6, '0'),s.name),',','') "code"`,
1494+
).All()
1495+
db.Ctx(ctx).Model(table).As("s").Fields(
1496+
"CASE WHEN dept='物资部' THEN '物资部' ELSE '其他' END dept",
1497+
"sum(s.value)",
1498+
).Group("CASE WHEN dept='物资部' THEN '物资部' ELSE '其他' END").All()
1499+
return nil
1500+
})
1501+
t.AssertNil(err)
1502+
sqlContent := gstr.Join(sqlArray, "\n")
1503+
t.Assert(gstr.Contains(sqlContent, expectKey1), true)
1504+
t.Assert(gstr.Contains(sqlContent, expectKey2), true)
1505+
})
14771506
}
14781507

14791508
// https://github.com/gogf/gf/issues/3238

contrib/drivers/pgsql/testdata/issues/issue1412.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
DROP TABLE IF EXISTS "parcels";
2+
DROP TABLE IF EXISTS "items";
3+
14
CREATE TABLE "items" (
25
"id" int NOT NULL,
36
"name" varchar(255) DEFAULT NULL,
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
DROP TABLE IF EXISTS "issue2643";
2+
CREATE TABLE "issue2643" (
3+
"id" int DEFAULT NULL,
4+
"name" varchar(50) DEFAULT NULL,
5+
"value" int DEFAULT NULL,
6+
"dept" varchar(50) DEFAULT NULL
7+
);

0 commit comments

Comments
 (0)