Skip to content

Commit 702719b

Browse files
committed
fix(pgsql,gaussdb): translate LockShared() syntax in DoFilter
LockShared() generates "LOCK IN SHARE MODE" which is MySQL-only syntax. Add DoFilter translation to "FOR SHARE" for PgSQL and GaussDB drivers, consistent with existing dialect translations (LIMIT, INSERT IGNORE). Tests now use LockShared() directly instead of Lock("FOR SHARE") workaround.
1 parent 4d14f06 commit 702719b

3 files changed

Lines changed: 17 additions & 8 deletions

File tree

contrib/drivers/gaussdb/gaussdb_do_filter.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ func (d *Driver) DoFilter(
5656
newSql = "INSERT" + newSql[len(gdb.InsertOperationIgnore):]
5757
}
5858

59+
// Translate MySQL shared lock syntax to GaussDB (PostgreSQL-compatible) syntax.
60+
// LockShared() generates "LOCK IN SHARE MODE" which is MySQL-only;
61+
// GaussDB uses "FOR SHARE" for the same semantics.
62+
newSql = gstr.Replace(newSql, "LOCK IN SHARE MODE", "FOR SHARE")
63+
5964
newArgs = args
6065

6166
return d.Core.DoFilter(ctx, link, newSql, newArgs)

contrib/drivers/pgsql/pgsql_do_filter.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ func (d *Driver) DoFilter(
5252
newSql = "INSERT" + newSql[len(gdb.InsertOperationIgnore):] + " ON CONFLICT DO NOTHING"
5353
}
5454

55+
// Translate MySQL shared lock syntax to PostgreSQL syntax.
56+
// LockShared() generates "LOCK IN SHARE MODE" which is MySQL-only;
57+
// PostgreSQL uses "FOR SHARE" for the same semantics.
58+
newSql = gstr.Replace(newSql, "LOCK IN SHARE MODE", "FOR SHARE")
59+
5560
newArgs = args
5661

5762
return d.Core.DoFilter(ctx, link, newSql, newArgs)

contrib/drivers/pgsql/pgsql_z_unit_feature_lock_test.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func Test_Model_Lock(t *testing.T) {
2828
t.Assert(one["id"], 1)
2929

3030
// Test Lock with FOR SHARE (PgSQL equivalent of MySQL's LOCK IN SHARE MODE)
31-
one, err = db.Model(table).Lock("FOR SHARE").Where("id", 3).One()
31+
one, err = db.Model(table).Lock(gdb.LockForShare).Where("id", 3).One()
3232
t.AssertNil(err)
3333
t.Assert(one["id"], 3)
3434

@@ -61,20 +61,19 @@ func Test_Model_LockUpdate(t *testing.T) {
6161
}
6262

6363
// Test_Model_LockShared tests the LockShared convenience method.
64-
// PgSQL: LockShared() generates "LOCK IN SHARE MODE" which is MySQL-only.
65-
// Use Lock("FOR SHARE") instead for PgSQL equivalent.
64+
// PgSQL DoFilter translates "LOCK IN SHARE MODE" to "FOR SHARE" automatically.
6665
func Test_Model_LockShared(t *testing.T) {
6766
table := createInitTable()
6867
defer dropTable(table)
6968

7069
gtest.C(t, func(t *gtest.T) {
71-
// Test Lock("FOR SHARE") basic usage (PgSQL equivalent of LockShared)
72-
one, err := db.Model(table).Lock("FOR SHARE").Where("id", 1).One()
70+
// Test LockShared basic usage
71+
one, err := db.Model(table).LockShared().Where("id", 1).One()
7372
t.AssertNil(err)
7473
t.Assert(one["id"], 1)
7574

76-
// Test Lock("FOR SHARE") with All()
77-
all, err := db.Model(table).Lock("FOR SHARE").Where("id<=?", 5).Order("id").All()
75+
// Test LockShared with All()
76+
all, err := db.Model(table).LockShared().Where("id<=?", 5).Order("id").All()
7877
t.AssertNil(err)
7978
t.Assert(len(all), 5)
8079
t.Assert(all[0]["id"], 1)
@@ -186,7 +185,7 @@ func Test_Model_Lock_ChainedMethods(t *testing.T) {
186185
t.Assert(one["passport"], "user_1")
187186

188187
// Lock with Order and Limit
189-
all, err := db.Model(table).Lock("FOR SHARE").Where("id>?", 5).Order("id desc").Limit(3).All()
188+
all, err := db.Model(table).LockShared().Where("id>?", 5).Order("id desc").Limit(3).All()
190189
t.AssertNil(err)
191190
t.Assert(len(all), 3)
192191
t.Assert(all[0]["id"], 10)

0 commit comments

Comments
 (0)