Skip to content

Commit

Permalink
fix(database/gdb): gdb.Counter not work in OnDuplicate
Browse files Browse the repository at this point in the history
  • Loading branch information
cyjaysong committed Dec 24, 2024
1 parent 594979c commit 31ab5b0
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 0 deletions.
22 changes: 22 additions & 0 deletions contrib/drivers/mysql/mysql_z_unit_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2812,6 +2812,28 @@ func Test_Model_OnDuplicate(t *testing.T) {
})
}

func Test_Model_OnDuplicateWithCounter(t *testing.T) {
table := createInitTable()
defer dropTable(table)

gtest.C(t, func(t *gtest.T) {
data := g.Map{
"id": 1,
"passport": "pp1",
"password": "pw1",
"nickname": "n1",
"create_time": "2016-06-06",
}
_, err := db.Model(table).OnConflict("id").OnDuplicate(g.Map{
"id": gdb.Counter{Field: "id", Value: 999999},
}).Data(data).Save()
t.AssertNil(err)
one, err := db.Model(table).WherePri(1).One()
t.AssertNil(err)
t.AssertNil(one)
})
}

func Test_Model_OnDuplicateEx(t *testing.T) {
table := createInitTable()
defer dropTable(table)
Expand Down
24 changes: 24 additions & 0 deletions contrib/drivers/pgsql/pgsql_format_upsert.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,30 @@ func (d *Driver) FormatUpsert(columns []string, list gdb.List, option gdb.DoInse
d.Core.QuoteWord(k),
v,
)
case gdb.Counter:
operator, columnVal := "+", v.(gdb.Counter).Value
if columnVal < 0 {
operator, columnVal = "-", -columnVal
}
onDuplicateStr += fmt.Sprintf(
"%s=EXCLUDED.%s%s%s",
d.QuoteWord(k),
d.QuoteWord(v.(gdb.Counter).Field),
operator,
gconv.String(columnVal),
)
case *gdb.Counter:
operator, columnVal := "+", v.(*gdb.Counter).Value
if columnVal < 0 {
operator, columnVal = "-", -columnVal
}
onDuplicateStr += fmt.Sprintf(
"%s=EXCLUDED.%s%s%s",
d.QuoteWord(k),
d.QuoteWord(v.(*gdb.Counter).Field),
operator,
gconv.String(columnVal),
)
default:
onDuplicateStr += fmt.Sprintf(
"%s=EXCLUDED.%s",
Expand Down
22 changes: 22 additions & 0 deletions contrib/drivers/pgsql/pgsql_z_unit_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,28 @@ func Test_Model_OnDuplicate(t *testing.T) {
})
}

func Test_Model_OnDuplicateWithCounter(t *testing.T) {
table := createInitTable()
defer dropTable(table)

gtest.C(t, func(t *gtest.T) {
data := g.Map{
"id": 1,
"passport": "pp1",
"password": "pw1",
"nickname": "n1",
"create_time": "2016-06-06",
}
_, err := db.Model(table).OnConflict("id").OnDuplicate(g.Map{
"id": gdb.Counter{Field: "id", Value: 999999},
}).Data(data).Save()
t.AssertNil(err)
one, err := db.Model(table).WherePri(1).One()
t.AssertNil(err)
t.AssertNil(one)
})
}

func Test_Model_OnDuplicateEx(t *testing.T) {
table := createInitTable()
defer dropTable(table)
Expand Down
25 changes: 25 additions & 0 deletions contrib/drivers/sqlite/sqlite_format_upsert.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,31 @@ func (d *Driver) FormatUpsert(columns []string, list gdb.List, option gdb.DoInse
d.Core.QuoteWord(k),
v,
)

case gdb.Counter:
operator, columnVal := "+", v.(gdb.Counter).Value
if columnVal < 0 {
operator, columnVal = "-", -columnVal
}
onDuplicateStr += fmt.Sprintf(
"%s=EXCLUDED.%s%s%s",
d.QuoteWord(k),
d.QuoteWord(v.(gdb.Counter).Field),
operator,
gconv.String(columnVal),
)
case *gdb.Counter:
operator, columnVal := "+", v.(*gdb.Counter).Value
if columnVal < 0 {
operator, columnVal = "-", -columnVal
}
onDuplicateStr += fmt.Sprintf(
"%s=EXCLUDED.%s%s%s",
d.QuoteWord(k),
d.QuoteWord(v.(*gdb.Counter).Field),
operator,
gconv.String(columnVal),
)
default:
onDuplicateStr += fmt.Sprintf(
"%s=EXCLUDED.%s",
Expand Down
22 changes: 22 additions & 0 deletions contrib/drivers/sqlite/sqlite_z_unit_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4324,3 +4324,25 @@ func Test_OrderRandom(t *testing.T) {
t.Assert(len(result), TableSize)
})
}

func Test_Model_OnDuplicateWithCounter(t *testing.T) {
table := createInitTable()
defer dropTable(table)

gtest.C(t, func(t *gtest.T) {
data := g.Map{
"id": 1,
"passport": "pp1",
"password": "pw1",
"nickname": "n1",
"create_time": "2016-06-06",
}
_, err := db.Model(table).OnConflict("id").OnDuplicate(g.Map{
"id": gdb.Counter{Field: "id", Value: 999999},
}).Data(data).Save()
t.AssertNil(err)
one, err := db.Model(table).WherePri(1).One()
t.AssertNil(err)
t.AssertNil(one)
})
}
24 changes: 24 additions & 0 deletions database/gdb/gdb_core_underlying.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,30 @@ func (c *Core) FormatUpsert(columns []string, list List, option DoInsertOption)
c.QuoteWord(k),
v,
)
case Counter:
operator, columnVal := "+", v.(Counter).Value
if columnVal < 0 {
operator, columnVal = "-", -columnVal
}
onDuplicateStr += fmt.Sprintf(
"%s=%s%s%s",
c.QuoteWord(k),
c.QuoteWord(v.(Counter).Field),
operator,
gconv.String(columnVal),
)
case *Counter:
operator, columnVal := "+", v.(*Counter).Value
if columnVal < 0 {
operator, columnVal = "-", -columnVal
}
onDuplicateStr += fmt.Sprintf(
"%s=%s%s%s",
c.QuoteWord(k),
c.QuoteWord(v.(*Counter).Field),
operator,
gconv.String(columnVal),
)
default:
onDuplicateStr += fmt.Sprintf(
"%s=VALUES(%s)",
Expand Down

0 comments on commit 31ab5b0

Please sign in to comment.